Support for association target type detection for jMolecules.

We now detect the component type of jMolecules' Association as association target type in AbstractPersistentProperty. AnnotationBasedPersistentProperty keeps the lookup of the explicit @Reference around but falls back to the Association analysis for that.

@Reference is now only considered by AnnotationBasedPersistentProperty. It was previously also (erroneously) considered by AbstractPersistentProperty.

It was also changed to return `null` in case no association target type could be looked up instead of returning the sole property's type in case that is an entity. This was done to satisfy the behavior documented on the interface but also to avoid the call to isEntity() during the calculation which might use association information in turn and thus lead to a stack overflow. Reworded the contract of PersistentProperty.getAssociationTargetType() to emphasize the symmetry with ….isAssociation() in implementation classes.

Closes: #2344.
This commit is contained in:
Oliver Drotbohm
2021-03-31 07:51:02 +02:00
parent 85c0ce1450
commit 23ebf3164f
5 changed files with 82 additions and 33 deletions

View File

@@ -32,6 +32,8 @@ import java.util.Optional;
import java.util.TreeMap;
import java.util.TreeSet;
import org.jmolecules.ddd.types.AggregateRoot;
import org.jmolecules.ddd.types.Identifier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.mapping.Association;
@@ -230,6 +232,7 @@ public class AbstractPersistentPropertyUnitTests {
SamplePersistentProperty property = getProperty(JMolecules.class, "association");
assertThat(property.isAssociation()).isTrue();
assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class);
}
private <T> BasicPersistentEntity<T, SamplePersistentProperty> getEntity(Class<T> type) {
@@ -371,11 +374,6 @@ public class AbstractPersistentPropertyUnitTests {
public <A extends Annotation> A findPropertyOrOwnerAnnotation(Class<A> annotationType) {
return null;
}
@Override
public Class<?> getAssociationTargetType() {
return null;
}
}
static class Sample {
@@ -392,6 +390,10 @@ public class AbstractPersistentPropertyUnitTests {
}
class JMolecules {
org.jmolecules.ddd.types.Association association;
org.jmolecules.ddd.types.Association<JMoleculesAggregate, Identifier> association;
}
interface JMoleculesAggregate extends AggregateRoot<JMoleculesAggregate, Identifier> {
}
}

View File

@@ -24,11 +24,17 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import org.assertj.core.api.ClassAssert;
import org.jmolecules.ddd.types.AggregateRoot;
import org.jmolecules.ddd.types.Association;
import org.jmolecules.ddd.types.Identifier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.annotation.AccessType;
@@ -242,12 +248,21 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
assertThatThrownBy(() -> property.getRequiredAnnotation(Transient.class)).isInstanceOf(IllegalStateException.class);
}
@Test // DATACMNS-1318
public void detectsUltimateAssociationTargetClass() {
@TestFactory // DATACMNS-1318
public Stream<DynamicTest> detectsUltimateAssociationTargetClass() {
Stream.of("toSample", "toSample2", "sample", "withoutAnnotation").forEach(it -> {
assertThat(getProperty(WithReferences.class, it).getAssociationTargetType()).isEqualTo(Sample.class);
});
Function<String, ClassAssert> verifier = it -> assertThat(
getProperty(WithReferences.class, it).getAssociationTargetType());
Stream<DynamicTest> positives = DynamicTest.stream(Stream.of("toSample", "toSample2", "sample"), //
it -> String.format("Property %s resolves to %s.", it, Sample.class.getSimpleName()), //
it -> verifier.apply(it).isEqualTo(Sample.class));
Stream<DynamicTest> negatives = DynamicTest.stream(Stream.of("withoutAnnotation"), //
it -> String.format("Property %s resolves to null.", it), //
it -> verifier.apply(it).isNull());
return Stream.concat(positives, negatives);
}
@Test // DATACMNS-1359
@@ -295,8 +310,12 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
}
@Test // GH-2315
void detectesJMoleculesAssociation() {
assertThat(getProperty(JMolecules.class, "association").isAssociation()).isTrue();
void detectsJMoleculesAssociation() {
SamplePersistentProperty property = getProperty(JMolecules.class, "association");
assertThat(property.isAssociation()).isTrue();
assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class);
}
@SuppressWarnings("unchecked")
@@ -484,6 +503,8 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
}
static class JMolecules {
Association association;
Association<JMoleculesAggregate, Identifier> association;
}
interface JMoleculesAggregate extends AggregateRoot<JMoleculesAggregate, Identifier> {}
}