Support for jMolecules' Association type.

We no recognize properties of type org.jmolecules.ddd.types.Association as associations in our PersistentProperty model.

Also, we now register JMolecules Converter implementations for Association and Identifier in CustomConversions so that they can persisted like their embedded primitive value out of the box.

Fixes #2315.
Original pull request: #2316.
This commit is contained in:
Oliver Drotbohm
2021-02-26 11:30:52 +01:00
committed by Mark Paluch
parent 3f1609587e
commit 6b0292cc66
9 changed files with 146 additions and 9 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.convert;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.text.DateFormat;
@@ -28,6 +29,8 @@ import java.util.Locale;
import java.util.Map;
import java.util.function.Predicate;
import org.jmolecules.ddd.types.Association;
import org.jmolecules.ddd.types.Identifier;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.ProxyFactory;
@@ -272,6 +275,24 @@ class CustomConversionsUnitTests {
verify(registry).addConverter(any(LocalDateTimeToDateConverter.class));
}
@Test // GH-2315
void addsAssociationConvertersByDefault() {
CustomConversions conversions = new CustomConversions(StoreConversions.NONE, Collections.emptyList());
assertThat(conversions.hasCustomWriteTarget(Association.class)).isTrue();
assertThat(conversions.hasCustomReadTarget(Object.class, Association.class)).isTrue();
}
@Test // GH-2315
void addsIdentifierConvertersByDefault() {
CustomConversions conversions = new CustomConversions(StoreConversions.NONE, Collections.emptyList());
assertThat(conversions.hasCustomWriteTarget(Identifier.class)).isTrue();
assertThat(conversions.hasCustomReadTarget(String.class, Identifier.class)).isTrue();
}
private static Class<?> createProxyTypeFor(Class<?> type) {
ProxyFactory factory = new ProxyFactory();

View File

@@ -224,6 +224,14 @@ public class AbstractPersistentPropertyUnitTests {
assertThat(property.getGetter()).isNotNull();
}
@Test // GH-2315
void detectsJMoleculesAssociation() {
SamplePersistentProperty property = getProperty(JMolecules.class, "association");
assertThat(property.isAssociation()).isTrue();
}
private <T> BasicPersistentEntity<T, SamplePersistentProperty> getEntity(Class<T> type) {
return new BasicPersistentEntity<>(ClassTypeInformation.from(type));
}
@@ -344,11 +352,6 @@ public class AbstractPersistentPropertyUnitTests {
return false;
}
@Override
public boolean isAssociation() {
return false;
}
@Override
protected Association<SamplePersistentProperty> createAssociation() {
return null;
@@ -387,4 +390,8 @@ public class AbstractPersistentPropertyUnitTests {
class TreeMapWrapper {
TreeMap<String, TreeMap<String, String>> map;
}
class JMolecules {
org.jmolecules.ddd.types.Association association;
}
}

View File

@@ -26,6 +26,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import org.jmolecules.ddd.types.Association;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.AliasFor;
@@ -293,6 +294,11 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
.withMessageContaining(NoField.class.getName());
}
@Test // GH-2315
void detectesJMoleculesAssociation() {
assertThat(getProperty(JMolecules.class, "association").isAssociation()).isTrue();
}
@SuppressWarnings("unchecked")
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
@@ -414,8 +420,7 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { FIELD, METHOD, ANNOTATION_TYPE })
@Id
public @interface MyId {
}
public @interface MyId {}
static class FieldAccess {
String name;
@@ -477,4 +482,8 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
String getFirstname();
}
static class JMolecules {
Association association;
}
}