You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2021/07/26 06:41:52 UTC
[isis] 01/02: ISIS-2820: improves jdo and jpa subtype entity rules
This is an automated email from the ASF dual-hosted git repository.
danhaywood pushed a commit to branch ISIS-2820
in repository https://gitbox.apache.org/repos/asf/isis.git
commit 235bd315ffd9edda8a266dc34956fd1077406dd3
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Mon Jul 26 07:41:26 2021 +0100
ISIS-2820: improves jdo and jpa subtype entity rules
---
.../applib/classrules/ArchitectureJdoRules.java | 29 +++++++++++++++++++---
.../applib/entity/jdo/dom/JdoEntity.java | 1 -
.../applib/entity/jdo/dom/JdoEntity2.java | 4 +--
.../applib/entity/jdo/dom/JdoEntitySubtype.java | 20 +++++++++++++++
4 files changed, 47 insertions(+), 7 deletions(-)
diff --git a/testing/archtestsupport/applib/src/main/java/org/apache/isis/testing/archtestsupport/applib/classrules/ArchitectureJdoRules.java b/testing/archtestsupport/applib/src/main/java/org/apache/isis/testing/archtestsupport/applib/classrules/ArchitectureJdoRules.java
index 5d0bf73..4f43a4e 100644
--- a/testing/archtestsupport/applib/src/main/java/org/apache/isis/testing/archtestsupport/applib/classrules/ArchitectureJdoRules.java
+++ b/testing/archtestsupport/applib/src/main/java/org/apache/isis/testing/archtestsupport/applib/classrules/ArchitectureJdoRules.java
@@ -8,16 +8,15 @@ import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaAnnotation;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaEnumConstant;
-import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.lang.ArchRule;
import org.apache.isis.applib.annotation.DomainObject;
+import static com.tngtech.archunit.base.DescribedPredicate.not;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields;
import lombok.experimental.UtilityClass;
import lombok.val;
-import static org.apache.isis.testing.archtestsupport.applib.classrules.CommonPredicates.haveNoArgProtectedConstructor;
/**
* A library of architecture tests to ensure coding conventions are followed for classes annotated with
@@ -43,10 +42,16 @@ public class ArchitectureJdoRules {
* This rule requires that classes annotated with the JDO {@link javax.jdo.annotations.PersistenceCapable} annotation
* must also be annotated with the JDO {@link javax.jdo.annotations.Version} annotation (in support of optimistic
* locking checks).
+ *
+ * <p>
+ * The rule does <i>not</i> apply to any entities that are subtype entities where there
+ * is a supertype entity.
+ * </p>
*/
public static ArchRule every_jdo_PersistenceCapable_must_be_annotated_with_Version() {
return classes()
.that().areAnnotatedWith(javax.jdo.annotations.PersistenceCapable.class)
+ .and(not(areSubtypeEntities()))
.should().beAnnotatedWith(javax.jdo.annotations.Version.class);
}
@@ -82,8 +87,8 @@ public class ArchitectureJdoRules {
/**
* This rule requires that classes annotated with the JDO {@link javax.jdo.annotations.PersistenceCapable} annotation
- * must also be annotated with the JDO {@link javax.jdo.annotations.Uniques} or {@link javax.jdo.annotations.Unique}
- * constraints.
+ * and is not a subtype entity, must also be annotated with the JDO {@link javax.jdo.annotations.Uniques} or
+ * {@link javax.jdo.annotations.Unique} constraints.
*
* <p>
* This is so that entities will have an alternative business key in addition to the system-defined surrogate
@@ -93,6 +98,7 @@ public class ArchitectureJdoRules {
public static ArchRule every_jdo_PersistenceCapable_must_be_annotated_as_Uniques_or_Unique() {
return classes()
.that().areAnnotatedWith(javax.jdo.annotations.PersistenceCapable.class)
+ .and(not(areSubtypeEntities()))
.should().beAnnotatedWith(javax.jdo.annotations.Uniques.class)
.orShould().beAnnotatedWith(javax.jdo.annotations.Unique.class);
}
@@ -187,6 +193,21 @@ public class ArchitectureJdoRules {
};
}
+ static DescribedPredicate<? super JavaClass> areSubtypeEntities() {
+ return new DescribedPredicate<JavaClass>("are subtype entities ") {
+ @Override public boolean apply(final JavaClass input) {
+ val superclassIfAny = input.getSuperclass();
+ if(!superclassIfAny.isPresent()) {
+ return false;
+ }
+ val superType = superclassIfAny.get();
+ val superClass = superType.toErasure();
+ val persistenceCapableIfAny = superClass
+ .tryGetAnnotationOfType(PersistenceCapable.class);
+ return persistenceCapableIfAny.isPresent();
+ }
+ };
+ }
}
diff --git a/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntity.java b/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntity.java
index 2705271..b8b24ad 100644
--- a/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntity.java
+++ b/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntity.java
@@ -25,7 +25,6 @@ import lombok.RequiredArgsConstructor;
@Version
@Uniques(@Unique(name = "name", members = {"name"}))
@DomainObject(nature = Nature.ENTITY)
-@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@XmlJavaTypeAdapter(PersistentEntityAdapter.class)
public class JdoEntity implements Comparable<JdoEntity> {
diff --git a/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntity2.java b/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntity2.java
index a740743..24dd747 100644
--- a/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntity2.java
+++ b/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntity2.java
@@ -16,9 +16,9 @@ import org.apache.isis.applib.jaxb.PersistentEntityAdapter;
@Version
@DomainObject(nature = Nature.ENTITY)
@XmlJavaTypeAdapter(PersistentEntityAdapter.class)
-public abstract class JdoEntity2 implements Comparable<JdoEntity2> {
+public abstract class JdoEntity2<X extends JdoEntity2<X>> implements Comparable<X> {
- private final String name;
+ protected final String name;
// abstract classes do not need to have no-arg constructor
public JdoEntity2(final String name) {
diff --git a/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntitySubtype.java b/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntitySubtype.java
new file mode 100644
index 0000000..51cddab
--- /dev/null
+++ b/testing/archtestsupport/applib/src/test/java/org/apache/isis/testing/archtestsupport/applib/entity/jdo/dom/JdoEntitySubtype.java
@@ -0,0 +1,20 @@
+package org.apache.isis.testing.archtestsupport.applib.entity.jdo.dom;
+
+import javax.jdo.annotations.PersistenceCapable;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+
+import org.apache.isis.applib.annotation.DomainObject;
+import org.apache.isis.applib.annotation.Nature;
+import org.apache.isis.applib.jaxb.PersistentEntityAdapter;
+
+@PersistenceCapable(schema = "jdo")
+@DomainObject(nature = Nature.ENTITY)
+@XmlJavaTypeAdapter(PersistentEntityAdapter.class)
+public class JdoEntitySubtype extends JdoEntity2<JdoEntitySubtype> {
+
+ public JdoEntitySubtype(final String name) {
+ super(name);
+ }
+
+}
+