DATACMNS-867 - First draft.
This commit is contained in:
22
src/test/java/org/springframework/data/mapping/MappingMetadataTests.java
Normal file → Executable file
22
src/test/java/org/springframework/data/mapping/MappingMetadataTests.java
Normal file → Executable file
@@ -15,9 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -42,25 +40,17 @@ public class MappingMetadataTests {
|
||||
@Test
|
||||
public void testPojoWithId() {
|
||||
|
||||
ctx.setInitialEntitySet(Collections.singleton(PersonWithId.class));
|
||||
ctx.initialize();
|
||||
|
||||
PersistentEntity<?, SamplePersistentProperty> person = ctx.getPersistentEntity(PersonWithId.class);
|
||||
assertNotNull(person.getIdProperty());
|
||||
assertEquals(String.class, person.getIdProperty().getType());
|
||||
|
||||
assertThat(person.getIdProperty()).hasValueSatisfying(it -> assertThat(it.getType()).isEqualTo(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssociations() {
|
||||
|
||||
ctx.setInitialEntitySet(Collections.singleton(PersonWithChildren.class));
|
||||
ctx.initialize();
|
||||
|
||||
PersistentEntity<?, SamplePersistentProperty> person = ctx.getPersistentEntity(PersonWithChildren.class);
|
||||
person.doWithAssociations(new AssociationHandler<SamplePersistentProperty>() {
|
||||
public void doWithAssociation(Association<SamplePersistentProperty> association) {
|
||||
assertEquals(Child.class, association.getInverse().getComponentType());
|
||||
}
|
||||
});
|
||||
|
||||
person.doWithAssociations((AssociationHandler<SamplePersistentProperty>) association -> assertThat(
|
||||
association.getInverse().getComponentType()).isEqualTo(Child.class));
|
||||
}
|
||||
}
|
||||
|
||||
50
src/test/java/org/springframework/data/mapping/ParameterUnitTests.java
Normal file → Executable file
50
src/test/java/org/springframework/data/mapping/ParameterUnitTests.java
Normal file → Executable file
@@ -15,10 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -36,10 +36,8 @@ import org.springframework.data.util.TypeInformation;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ParameterUnitTests<P extends PersistentProperty<P>> {
|
||||
|
||||
@Mock
|
||||
PersistentEntity<Object, P> entity;
|
||||
@Mock
|
||||
PersistentEntity<String, P> stringEntity;
|
||||
@Mock PersistentEntity<Object, P> entity;
|
||||
@Mock PersistentEntity<String, P> stringEntity;
|
||||
|
||||
TypeInformation<Object> type = ClassTypeInformation.from(Object.class);
|
||||
Annotation[] annotations = new Annotation[0];
|
||||
@@ -47,50 +45,50 @@ public class ParameterUnitTests<P extends PersistentProperty<P>> {
|
||||
@Test
|
||||
public void twoParametersWithIdenticalSetupEqual() {
|
||||
|
||||
Parameter<Object, P> left = new Parameter<Object, P>("name", type, annotations, entity);
|
||||
Parameter<Object, P> right = new Parameter<Object, P>("name", type, annotations, entity);
|
||||
Parameter<Object, P> left = new Parameter<Object, P>(Optional.of("name"), type, annotations, Optional.of(entity));
|
||||
Parameter<Object, P> right = new Parameter<Object, P>(Optional.of("name"), type, annotations, Optional.of(entity));
|
||||
|
||||
assertThat(left, is(right));
|
||||
assertThat(left.hashCode(), is(right.hashCode()));
|
||||
assertThat(left).isEqualTo(right);
|
||||
assertThat(left.hashCode()).isEqualTo(right.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twoParametersWithIdenticalSetupAndNullNameEqual() {
|
||||
|
||||
Parameter<Object, P> left = new Parameter<Object, P>(null, type, annotations, entity);
|
||||
Parameter<Object, P> right = new Parameter<Object, P>(null, type, annotations, entity);
|
||||
Parameter<Object, P> left = new Parameter<Object, P>(Optional.empty(), type, annotations, Optional.of(entity));
|
||||
Parameter<Object, P> right = new Parameter<Object, P>(Optional.empty(), type, annotations, Optional.of(entity));
|
||||
|
||||
assertThat(left, is(right));
|
||||
assertThat(left.hashCode(), is(right.hashCode()));
|
||||
assertThat(left).isEqualTo(right);
|
||||
assertThat(left.hashCode()).isEqualTo(right.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twoParametersWithIdenticalAndNullEntitySetupEqual() {
|
||||
|
||||
Parameter<Object, P> left = new Parameter<Object, P>("name", type, annotations, null);
|
||||
Parameter<Object, P> right = new Parameter<Object, P>("name", type, annotations, null);
|
||||
Parameter<Object, P> left = new Parameter<Object, P>(Optional.of("name"), type, annotations, Optional.empty());
|
||||
Parameter<Object, P> right = new Parameter<Object, P>(Optional.of("name"), type, annotations, Optional.empty());
|
||||
|
||||
assertThat(left, is(right));
|
||||
assertThat(left.hashCode(), is(right.hashCode()));
|
||||
assertThat(left).isEqualTo(right);
|
||||
assertThat(left.hashCode()).isEqualTo(right.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twoParametersWithDifferentNameAreNotEqual() {
|
||||
|
||||
Parameter<Object, P> left = new Parameter<Object, P>("first", type, annotations, entity);
|
||||
Parameter<Object, P> right = new Parameter<Object, P>("second", type, annotations, entity);
|
||||
Parameter<Object, P> left = new Parameter<Object, P>(Optional.of("first"), type, annotations, Optional.of(entity));
|
||||
Parameter<Object, P> right = new Parameter<Object, P>(Optional.of("second"), type, annotations,
|
||||
Optional.of(entity));
|
||||
|
||||
assertThat(left, is(not(right)));
|
||||
assertThat(left).isNotEqualTo(right);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void twoParametersWithDifferenTypeAreNotEqual() {
|
||||
|
||||
Parameter left = new Parameter<Object, P>("name", type, annotations, entity);
|
||||
Parameter right = new Parameter<String, P>("name", ClassTypeInformation.from(String.class), annotations,
|
||||
stringEntity);
|
||||
Parameter<Object, P> left = new Parameter<Object, P>(Optional.of("name"), type, annotations, Optional.of(entity));
|
||||
Parameter<String, P> right = new Parameter<String, P>(Optional.of("name"), ClassTypeInformation.from(String.class),
|
||||
annotations, Optional.of(stringEntity));
|
||||
|
||||
assertThat(left, is(not(right)));
|
||||
assertThat(left).isNotEqualTo(right);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Some test methods that define expected behaviour for {@link PersistentEntity} interface. Implementation test classes
|
||||
@@ -27,6 +26,6 @@ import static org.junit.Assert.*;
|
||||
public abstract class PersistentEntitySpec {
|
||||
|
||||
public static void assertInvariants(PersistentEntity<?, ?> entity) {
|
||||
assertThat(entity.getName(), is(notNullValue()));
|
||||
assertThat(entity.getName()).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
87
src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java
Normal file → Executable file
87
src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@@ -38,52 +37,60 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
|
||||
@Test
|
||||
public void findsNoArgConstructorForClassWithoutExplicitConstructor() {
|
||||
|
||||
PreferredConstructorDiscoverer<EntityWithoutConstructor, P> discoverer = new PreferredConstructorDiscoverer<EntityWithoutConstructor, P>(
|
||||
PreferredConstructorDiscoverer<EntityWithoutConstructor, P> discoverer = new PreferredConstructorDiscoverer<>(
|
||||
EntityWithoutConstructor.class);
|
||||
PreferredConstructor<EntityWithoutConstructor, P> constructor = discoverer.getConstructor();
|
||||
|
||||
assertThat(constructor, is(notNullValue()));
|
||||
assertThat(constructor.isNoArgConstructor(), is(true));
|
||||
assertThat(constructor.isExplicitlyAnnotated(), is(false));
|
||||
assertThat(discoverer.getConstructor()).hasValueSatisfying(constructor -> {
|
||||
|
||||
assertThat(constructor).isNotNull();
|
||||
assertThat(constructor.isNoArgConstructor()).isTrue();
|
||||
assertThat(constructor.isExplicitlyAnnotated()).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsNoArgConstructorForClassWithMultipleConstructorsAndNoArgOne() {
|
||||
|
||||
PreferredConstructorDiscoverer<ClassWithEmptyConstructor, P> discoverer = new PreferredConstructorDiscoverer<ClassWithEmptyConstructor, P>(
|
||||
PreferredConstructorDiscoverer<ClassWithEmptyConstructor, P> discoverer = new PreferredConstructorDiscoverer<>(
|
||||
ClassWithEmptyConstructor.class);
|
||||
PreferredConstructor<ClassWithEmptyConstructor, P> constructor = discoverer.getConstructor();
|
||||
|
||||
assertThat(constructor, is(notNullValue()));
|
||||
assertThat(constructor.isNoArgConstructor(), is(true));
|
||||
assertThat(constructor.isExplicitlyAnnotated(), is(false));
|
||||
assertThat(discoverer.getConstructor()).hasValueSatisfying(constructor -> {
|
||||
|
||||
assertThat(constructor).isNotNull();
|
||||
assertThat(constructor.isNoArgConstructor()).isTrue();
|
||||
assertThat(constructor.isExplicitlyAnnotated()).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotThrowExceptionForMultipleConstructorsAndNoNoArgConstructorWithoutAnnotation() {
|
||||
|
||||
PreferredConstructorDiscoverer<ClassWithMultipleConstructorsWithoutEmptyOne, P> discoverer = new PreferredConstructorDiscoverer<ClassWithMultipleConstructorsWithoutEmptyOne, P>(
|
||||
PreferredConstructorDiscoverer<ClassWithMultipleConstructorsWithoutEmptyOne, P> discoverer = new PreferredConstructorDiscoverer<>(
|
||||
ClassWithMultipleConstructorsWithoutEmptyOne.class);
|
||||
assertThat(discoverer.getConstructor(), is(nullValue()));
|
||||
|
||||
assertThat(discoverer.getConstructor()).isNotPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesConstructorWithAnnotationOverEveryOther() {
|
||||
|
||||
PreferredConstructorDiscoverer<ClassWithMultipleConstructorsAndAnnotation, P> discoverer = new PreferredConstructorDiscoverer<ClassWithMultipleConstructorsAndAnnotation, P>(
|
||||
PreferredConstructorDiscoverer<ClassWithMultipleConstructorsAndAnnotation, P> discoverer = new PreferredConstructorDiscoverer<>(
|
||||
ClassWithMultipleConstructorsAndAnnotation.class);
|
||||
PreferredConstructor<ClassWithMultipleConstructorsAndAnnotation, P> constructor = discoverer.getConstructor();
|
||||
|
||||
assertThat(constructor, is(notNullValue()));
|
||||
assertThat(constructor.isNoArgConstructor(), is(false));
|
||||
assertThat(constructor.isExplicitlyAnnotated(), is(true));
|
||||
assertThat(discoverer.getConstructor()).hasValueSatisfying(constructor -> {
|
||||
|
||||
assertThat(constructor.hasParameters(), is(true));
|
||||
Iterator<Parameter<Object, P>> parameters = constructor.getParameters().iterator();
|
||||
assertThat(constructor).isNotNull();
|
||||
assertThat(constructor.isNoArgConstructor()).isFalse();
|
||||
assertThat(constructor.isExplicitlyAnnotated()).isTrue();
|
||||
|
||||
Parameter<?, P> parameter = parameters.next();
|
||||
assertThat(parameter.getType().getType(), typeCompatibleWith(Long.class));
|
||||
assertThat(parameters.hasNext(), is(false));
|
||||
assertThat(constructor.hasParameters()).isTrue();
|
||||
|
||||
Iterator<Parameter<Object, P>> parameters = constructor.getParameters().iterator();
|
||||
|
||||
Parameter<?, P> parameter = parameters.next();
|
||||
assertThat(parameter.getType().getType()).isEqualTo(Long.class);
|
||||
assertThat(parameters.hasNext()).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-134
|
||||
@@ -91,10 +98,12 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
|
||||
|
||||
PersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(ClassTypeInformation.from(Inner.class));
|
||||
PreferredConstructorDiscoverer<Inner, P> discoverer = new PreferredConstructorDiscoverer<Inner, P>(entity);
|
||||
PreferredConstructor<Inner, P> constructor = discoverer.getConstructor();
|
||||
|
||||
Parameter<?, P> parameter = constructor.getParameters().iterator().next();
|
||||
assertThat(constructor.isEnclosingClassParameter(parameter), is(true));
|
||||
assertThat(discoverer.getConstructor()).hasValueSatisfying(constructor -> {
|
||||
|
||||
Parameter<?, P> parameter = constructor.getParameters().iterator().next();
|
||||
assertThat(constructor.isEnclosingClassParameter(parameter)).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
static class EntityWithoutConstructor {
|
||||
@@ -103,39 +112,31 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
|
||||
|
||||
static class ClassWithEmptyConstructor {
|
||||
|
||||
public ClassWithEmptyConstructor() {
|
||||
}
|
||||
public ClassWithEmptyConstructor() {}
|
||||
}
|
||||
|
||||
static class ClassWithMultipleConstructorsAndEmptyOne {
|
||||
|
||||
public ClassWithMultipleConstructorsAndEmptyOne(String value) {
|
||||
}
|
||||
public ClassWithMultipleConstructorsAndEmptyOne(String value) {}
|
||||
|
||||
public ClassWithMultipleConstructorsAndEmptyOne() {
|
||||
}
|
||||
public ClassWithMultipleConstructorsAndEmptyOne() {}
|
||||
}
|
||||
|
||||
static class ClassWithMultipleConstructorsWithoutEmptyOne {
|
||||
|
||||
public ClassWithMultipleConstructorsWithoutEmptyOne(String value) {
|
||||
}
|
||||
public ClassWithMultipleConstructorsWithoutEmptyOne(String value) {}
|
||||
|
||||
public ClassWithMultipleConstructorsWithoutEmptyOne(Long value) {
|
||||
}
|
||||
public ClassWithMultipleConstructorsWithoutEmptyOne(Long value) {}
|
||||
}
|
||||
|
||||
static class ClassWithMultipleConstructorsAndAnnotation {
|
||||
|
||||
public ClassWithMultipleConstructorsAndAnnotation() {
|
||||
}
|
||||
public ClassWithMultipleConstructorsAndAnnotation() {}
|
||||
|
||||
public ClassWithMultipleConstructorsAndAnnotation(String value) {
|
||||
}
|
||||
public ClassWithMultipleConstructorsAndAnnotation(String value) {}
|
||||
|
||||
@PersistenceConstructor
|
||||
public ClassWithMultipleConstructorsAndAnnotation(Long value) {
|
||||
}
|
||||
public ClassWithMultipleConstructorsAndAnnotation(Long value) {}
|
||||
}
|
||||
|
||||
static class Outer {
|
||||
|
||||
169
src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java
Normal file → Executable file
169
src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.mapping.PropertyPath.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
@@ -40,78 +39,78 @@ public class PropertyPathUnitTests {
|
||||
@Rule public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void parsesSimplePropertyCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userName", Foo.class);
|
||||
assertThat(reference.hasNext(), is(false));
|
||||
assertThat(reference.toDotPath(), is("userName"));
|
||||
assertThat(reference.getOwningType(), is((TypeInformation) ClassTypeInformation.from(Foo.class)));
|
||||
|
||||
assertThat(reference.hasNext()).isFalse();
|
||||
assertThat(reference.toDotPath()).isEqualTo("userName");
|
||||
assertThat(reference.getOwningType()).isEqualTo(ClassTypeInformation.from(Foo.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsesPathPropertyCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userName", Bar.class);
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
assertThat(reference.toDotPath(), is("user.name"));
|
||||
assertThat(reference.hasNext()).isTrue();
|
||||
assertThat(reference.next()).isEqualTo(new PropertyPath("name", FooBar.class));
|
||||
assertThat(reference.toDotPath()).isEqualTo("user.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefersLongerMatches() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userName", Sample.class);
|
||||
assertThat(reference.hasNext(), is(false));
|
||||
assertThat(reference.toDotPath(), is("userName"));
|
||||
assertThat(reference.hasNext()).isFalse();
|
||||
assertThat(reference.toDotPath()).isEqualTo("userName");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testname() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userName", Sample2.class);
|
||||
assertThat(reference.getSegment(), is("user"));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
assertThat(reference.getSegment()).isEqualTo("user");
|
||||
assertThat(reference.hasNext()).isTrue();
|
||||
assertThat(reference.next()).isEqualTo(new PropertyPath("name", FooBar.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefersExplicitPaths() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("user_name", Sample.class);
|
||||
assertThat(reference.getSegment(), is("user"));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
assertThat(reference.getSegment()).isEqualTo("user");
|
||||
assertThat(reference.hasNext()).isTrue();
|
||||
assertThat(reference.next()).isEqualTo(new PropertyPath("name", FooBar.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesGenericsCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("usersName", Bar.class);
|
||||
assertThat(reference.getSegment(), is("users"));
|
||||
assertThat(reference.isCollection(), is(true));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
assertThat(reference.getSegment()).isEqualTo("users");
|
||||
assertThat(reference.isCollection()).isTrue();
|
||||
assertThat(reference.hasNext()).isTrue();
|
||||
assertThat(reference.next()).isEqualTo(new PropertyPath("name", FooBar.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesMapCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userMapName", Bar.class);
|
||||
assertThat(reference.getSegment(), is("userMap"));
|
||||
assertThat(reference.isCollection(), is(false));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
assertThat(reference.getSegment()).isEqualTo("userMap");
|
||||
assertThat(reference.isCollection()).isFalse();
|
||||
assertThat(reference.hasNext()).isTrue();
|
||||
assertThat(reference.next()).isEqualTo(new PropertyPath("name", FooBar.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesArrayCorrectly() throws Exception {
|
||||
|
||||
PropertyPath reference = PropertyPath.from("userArrayName", Bar.class);
|
||||
assertThat(reference.getSegment(), is("userArray"));
|
||||
assertThat(reference.isCollection(), is(true));
|
||||
assertThat(reference.hasNext(), is(true));
|
||||
assertThat(reference.next(), is(new PropertyPath("name", FooBar.class)));
|
||||
assertThat(reference.getSegment()).isEqualTo("userArray");
|
||||
assertThat(reference.isCollection()).isTrue();
|
||||
assertThat(reference.hasNext()).isTrue();
|
||||
assertThat(reference.next()).isEqualTo(new PropertyPath("name", FooBar.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -121,21 +120,18 @@ public class PropertyPathUnitTests {
|
||||
PropertyPath.from("usersMame", Bar.class);
|
||||
fail("Expected PropertyReferenceException!");
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getPropertyName(), is("mame"));
|
||||
assertThat(e.getBaseProperty(), is(PropertyPath.from("users", Bar.class)));
|
||||
assertThat(e.getPropertyName()).isEqualTo("mame");
|
||||
assertThat(e.getBaseProperty()).isEqualTo(PropertyPath.from("users", Bar.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesInvalidMapValueTypeProperly() {
|
||||
|
||||
try {
|
||||
PropertyPath.from("userMapMame", Bar.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getPropertyName(), is("mame"));
|
||||
assertThat(e.getBaseProperty(), is(PropertyPath.from("userMap", Bar.class)));
|
||||
}
|
||||
assertThatExceptionOfType(PropertyReferenceException.class)//
|
||||
.isThrownBy(() -> PropertyPath.from("userMapMame", Bar.class))//
|
||||
.matches(e -> e.getPropertyName().equals("mame"))//
|
||||
.matches(e -> e.getBaseProperty().equals(PropertyPath.from("userMap", Bar.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -143,19 +139,19 @@ public class PropertyPathUnitTests {
|
||||
|
||||
PropertyPath from = PropertyPath.from("barUserName", Sample.class);
|
||||
|
||||
assertThat(from, is(notNullValue()));
|
||||
assertThat(from.getLeafProperty(), is(PropertyPath.from("name", FooBar.class)));
|
||||
assertThat(from).isNotNull();
|
||||
assertThat(from.getLeafProperty()).isEqualTo(PropertyPath.from("name", FooBar.class));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-45
|
||||
public void handlesEmptyUnderscoresCorrectly() {
|
||||
|
||||
PropertyPath propertyPath = PropertyPath.from("_foo", Sample2.class);
|
||||
assertThat(propertyPath.getSegment(), is("_foo"));
|
||||
assertThat(propertyPath.getType(), is(typeCompatibleWith(Foo.class)));
|
||||
assertThat(propertyPath.getSegment()).isEqualTo("_foo");
|
||||
assertThat(propertyPath.getType()).isEqualTo(Foo.class);
|
||||
|
||||
propertyPath = PropertyPath.from("_foo__email", Sample2.class);
|
||||
assertThat(propertyPath.toDotPath(), is("_foo._email"));
|
||||
assertThat(propertyPath.toDotPath()).isEqualTo("_foo._email");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -163,9 +159,9 @@ public class PropertyPathUnitTests {
|
||||
|
||||
PropertyPath propertyPath = PropertyPath.from("bar.userMap.name", Sample.class);
|
||||
|
||||
assertThat(propertyPath, is(notNullValue()));
|
||||
assertThat(propertyPath.getSegment(), is("bar"));
|
||||
assertThat(propertyPath.getLeafProperty(), is(PropertyPath.from("name", FooBar.class)));
|
||||
assertThat(propertyPath).isNotNull();
|
||||
assertThat(propertyPath.getSegment()).isEqualTo("bar");
|
||||
assertThat(propertyPath.getLeafProperty()).isEqualTo(PropertyPath.from("name", FooBar.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -174,9 +170,9 @@ public class PropertyPathUnitTests {
|
||||
PropertyPath propertyPath = PropertyPath.from("userName", Foo.class);
|
||||
|
||||
Iterator<PropertyPath> iterator = propertyPath.iterator();
|
||||
assertThat(iterator.hasNext(), is(true));
|
||||
assertThat(iterator.next(), is(propertyPath));
|
||||
assertThat(iterator.hasNext(), is(false));
|
||||
assertThat(iterator.hasNext()).isTrue();
|
||||
assertThat(iterator.next()).isEqualTo(propertyPath);
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -185,41 +181,35 @@ public class PropertyPathUnitTests {
|
||||
PropertyPath propertyPath = PropertyPath.from("user.name", Bar.class);
|
||||
|
||||
Iterator<PropertyPath> iterator = propertyPath.iterator();
|
||||
assertThat(iterator.hasNext(), is(true));
|
||||
assertThat(iterator.next(), is(propertyPath));
|
||||
assertThat(iterator.hasNext(), is(true));
|
||||
assertThat(iterator.next(), is(propertyPath.next()));
|
||||
assertThat(iterator.hasNext(), is(false));
|
||||
assertThat(iterator.hasNext()).isTrue();
|
||||
assertThat(iterator.next()).isEqualTo(propertyPath);
|
||||
assertThat(iterator.hasNext()).isTrue();
|
||||
assertThat(iterator.next()).isEqualTo(propertyPath.next());
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-139
|
||||
public void rejectsInvalidPropertyWithLeadingUnderscore() {
|
||||
try {
|
||||
PropertyPath.from("_id", Foo.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getMessage(), containsString("property _id"));
|
||||
}
|
||||
|
||||
assertThatExceptionOfType(PropertyReferenceException.class)//
|
||||
.isThrownBy(() -> PropertyPath.from("_id", Foo.class))//
|
||||
.withMessageContaining("property _id");
|
||||
}
|
||||
|
||||
@Test // DATACMNS-139
|
||||
public void rejectsNestedInvalidPropertyWithLeadingUnderscore() {
|
||||
try {
|
||||
PropertyPath.from("_foo_id", Sample2.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getMessage(), containsString("property id"));
|
||||
}
|
||||
|
||||
assertThatExceptionOfType(PropertyReferenceException.class)//
|
||||
.isThrownBy(() -> PropertyPath.from("_foo_id", Sample2.class))//
|
||||
.withMessageContaining("property id");
|
||||
}
|
||||
|
||||
@Test // DATACMNS-139
|
||||
public void rejectsNestedInvalidPropertyExplictlySplitWithLeadingUnderscore() {
|
||||
try {
|
||||
PropertyPath.from("_foo__id", Sample2.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getMessage(), containsString("property _id"));
|
||||
}
|
||||
|
||||
assertThatExceptionOfType(PropertyReferenceException.class)//
|
||||
.isThrownBy(() -> PropertyPath.from("_foo__id", Sample2.class))//
|
||||
.withMessageContaining("property _id");
|
||||
}
|
||||
|
||||
@Test(expected = PropertyReferenceException.class) // DATACMNS 158
|
||||
@@ -230,12 +220,9 @@ public class PropertyPathUnitTests {
|
||||
@Test
|
||||
public void rejectsInvalidProperty() {
|
||||
|
||||
try {
|
||||
PropertyPath.from("bar", Foo.class);
|
||||
fail();
|
||||
} catch (PropertyReferenceException e) {
|
||||
assertThat(e.getBaseProperty(), is(nullValue()));
|
||||
}
|
||||
assertThatExceptionOfType(PropertyReferenceException.class)//
|
||||
.isThrownBy(() -> PropertyPath.from("_foo_id", Sample2.class))//
|
||||
.matches(e -> e.getBaseProperty().getSegment().equals("_foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -246,12 +233,12 @@ public class PropertyPathUnitTests {
|
||||
|
||||
PropertyPath shortPath = PropertyPath.from("user", Bar.class);
|
||||
|
||||
assertThat(left, is(right));
|
||||
assertThat(right, is(left));
|
||||
assertThat(left, is(not(shortPath)));
|
||||
assertThat(shortPath, is(not(left)));
|
||||
assertThat(left).isEqualTo(right);
|
||||
assertThat(right).isEqualTo(left);
|
||||
assertThat(left).isNotEqualTo(shortPath);
|
||||
assertThat(shortPath).isNotEqualTo(left);
|
||||
|
||||
assertThat(left, is(not(new Object())));
|
||||
assertThat(left).isNotEqualTo(new Object());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -262,8 +249,8 @@ public class PropertyPathUnitTests {
|
||||
|
||||
PropertyPath shortPath = PropertyPath.from("user", Bar.class);
|
||||
|
||||
assertThat(left.hashCode(), is(right.hashCode()));
|
||||
assertThat(left.hashCode(), is(not(shortPath.hashCode())));
|
||||
assertThat(left.hashCode()).isEqualTo(right.hashCode());
|
||||
assertThat(left.hashCode()).isNotEqualTo(shortPath.hashCode());
|
||||
}
|
||||
|
||||
@Test // DATACMNS-257
|
||||
@@ -271,8 +258,8 @@ public class PropertyPathUnitTests {
|
||||
|
||||
PropertyPath path = PropertyPath.from("UUID", Foo.class);
|
||||
|
||||
assertThat(path, is(notNullValue()));
|
||||
assertThat(path.getSegment(), is("UUID"));
|
||||
assertThat(path).isNotNull();
|
||||
assertThat(path.getSegment()).isEqualTo("UUID");
|
||||
}
|
||||
|
||||
@Test // DATACMNS-257
|
||||
@@ -280,10 +267,10 @@ public class PropertyPathUnitTests {
|
||||
|
||||
PropertyPath path = PropertyPath.from("_fooUUID", Sample2.class);
|
||||
|
||||
assertThat(path, is(notNullValue()));
|
||||
assertThat(path.getSegment(), is("_foo"));
|
||||
assertThat(path.hasNext(), is(true));
|
||||
assertThat(path.next().getSegment(), is("UUID"));
|
||||
assertThat(path).isNotNull();
|
||||
assertThat(path.getSegment()).isEqualTo("_foo");
|
||||
assertThat(path.hasNext()).isTrue();
|
||||
assertThat(path.next().getSegment()).isEqualTo("UUID");
|
||||
}
|
||||
|
||||
@Test // DATACMNS-381
|
||||
|
||||
6
src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java
Normal file → Executable file
6
src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -74,8 +73,7 @@ public class PropertyReferenceExceptionUnitTests {
|
||||
|
||||
Collection<String> matches = exception.getPropertyMatches();
|
||||
|
||||
assertThat(matches, hasSize(1));
|
||||
assertThat(matches, hasItem("name"));
|
||||
assertThat(matches).containsExactly("name");
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
27
src/test/java/org/springframework/data/mapping/SimpleTypeHolderUnitTests.java
Normal file → Executable file
27
src/test/java/org/springframework/data/mapping/SimpleTypeHolderUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collections;
|
||||
@@ -54,7 +53,7 @@ public class SimpleTypeHolderUnitTests {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
|
||||
assertThat(holder.isSimpleType(String.class), is(true));
|
||||
assertThat(holder.isSimpleType(String.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -62,7 +61,7 @@ public class SimpleTypeHolderUnitTests {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(new HashSet<Class<?>>(), false);
|
||||
|
||||
assertThat(holder.isSimpleType(UUID.class), is(false));
|
||||
assertThat(holder.isSimpleType(UUID.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -70,8 +69,8 @@ public class SimpleTypeHolderUnitTests {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolder.class), true);
|
||||
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolder.class), is(true));
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class), is(false));
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolder.class)).isTrue();
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,30 +79,30 @@ public class SimpleTypeHolderUnitTests {
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolder.class), true);
|
||||
SimpleTypeHolder second = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolderUnitTests.class), holder);
|
||||
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolder.class), is(true));
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class), is(false));
|
||||
assertThat(second.isSimpleType(SimpleTypeHolder.class), is(true));
|
||||
assertThat(second.isSimpleType(SimpleTypeHolderUnitTests.class), is(true));
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolder.class)).isTrue();
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class)).isFalse();
|
||||
assertThat(second.isSimpleType(SimpleTypeHolder.class)).isTrue();
|
||||
assertThat(second.isSimpleType(SimpleTypeHolderUnitTests.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersObjectToBeSimpleType() {
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
assertThat(holder.isSimpleType(Object.class), is(true));
|
||||
assertThat(holder.isSimpleType(Object.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersSimpleEnumAsSimple() {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
assertThat(holder.isSimpleType(SimpleEnum.FOO.getClass()), is(true));
|
||||
assertThat(holder.isSimpleType(SimpleEnum.FOO.getClass())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersComplexEnumAsSimple() {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
assertThat(holder.isSimpleType(ComplexEnum.FOO.getClass()), is(true));
|
||||
assertThat(holder.isSimpleType(ComplexEnum.FOO.getClass())).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1006
|
||||
@@ -111,7 +110,7 @@ public class SimpleTypeHolderUnitTests {
|
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder();
|
||||
|
||||
assertThat(holder.isSimpleType(Type.class), is(true));
|
||||
assertThat(holder.isSimpleType(Type.class)).isTrue();
|
||||
}
|
||||
|
||||
enum SimpleEnum {
|
||||
|
||||
16
src/test/java/org/springframework/data/mapping/context/AbstractMappingContextIntegrationTests.java
Normal file → Executable file
16
src/test/java/org/springframework/data/mapping/context/AbstractMappingContextIntegrationTests.java
Normal file → Executable file
@@ -15,13 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
@@ -47,7 +47,7 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
|
||||
context.setInitialEntitySet(Collections.singleton(Person.class));
|
||||
context.initialize();
|
||||
|
||||
assertThat(context.getManagedTypes(), hasItem(ClassTypeInformation.from(Person.class)));
|
||||
assertThat(context.getManagedTypes()).contains(ClassTypeInformation.from(Person.class));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-457
|
||||
@@ -57,7 +57,7 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
|
||||
context.setInitialEntitySet(Collections.singleton(Person.class));
|
||||
context.initialize();
|
||||
|
||||
assertThat(context.hasPersistentEntityFor(Person.class), is(true));
|
||||
assertThat(context.hasPersistentEntityFor(Person.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-243
|
||||
@@ -66,7 +66,7 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(InterfaceOnly.class);
|
||||
|
||||
assertThat(entity.getIdProperty(), is(notNullValue()));
|
||||
assertThat(entity.getIdProperty()).isNotNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-65
|
||||
@@ -116,13 +116,13 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
protected T createPersistentProperty(final Field field, final PropertyDescriptor descriptor,
|
||||
final BasicPersistentEntity<Object, T> owner, final SimpleTypeHolder simpleTypeHolder) {
|
||||
protected T createPersistentProperty(Optional<Field> field, PropertyDescriptor descriptor,
|
||||
final BasicPersistentEntity<Object, T> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
PersistentProperty prop = mock(PersistentProperty.class);
|
||||
|
||||
when(prop.getTypeInformation()).thenReturn(owner.getTypeInformation());
|
||||
when(prop.getName()).thenReturn(field == null ? descriptor.getName() : field.getName());
|
||||
when(prop.getName()).thenReturn(field.map(Field::getName).orElse(descriptor.getName()));
|
||||
when(prop.getPersistentEntityType()).thenReturn(Collections.EMPTY_SET);
|
||||
|
||||
try {
|
||||
|
||||
59
src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java
Normal file → Executable file
59
src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.context;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import groovy.lang.MetaClass;
|
||||
@@ -26,7 +25,6 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
@@ -40,6 +38,7 @@ import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Unit test for {@link AbstractMappingContext}.
|
||||
@@ -62,7 +61,7 @@ public class AbstractMappingContextUnitTests {
|
||||
public void doesNotTryToLookupPersistentEntityForLeafProperty() {
|
||||
PersistentPropertyPath<SamplePersistentProperty> path = context
|
||||
.getPersistentPropertyPath(PropertyPath.from("name", Person.class));
|
||||
assertThat(path, is(notNullValue()));
|
||||
assertThat(path).isNotNull();
|
||||
}
|
||||
|
||||
@Test(expected = MappingException.class) // DATACMNS-92
|
||||
@@ -112,7 +111,7 @@ public class AbstractMappingContextUnitTests {
|
||||
public void returnsNullPersistentEntityForSimpleTypes() {
|
||||
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
assertThat(context.getPersistentEntity(String.class), is(nullValue()));
|
||||
assertThat(context.getPersistentEntity(String.class)).isNull();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) // DATACMNS-214
|
||||
@@ -132,7 +131,7 @@ public class AbstractMappingContextUnitTests {
|
||||
mappingContext.initialize();
|
||||
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Sample.class);
|
||||
assertThat(entity.getPersistentProperty("metaClass"), is(nullValue()));
|
||||
assertThat(entity.getPersistentProperty("metaClass")).isNotPresent();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-332
|
||||
@@ -140,7 +139,10 @@ public class AbstractMappingContextUnitTests {
|
||||
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Extension.class);
|
||||
assertThat(entity.getPersistentProperty("foo").isIdProperty(), is(true));
|
||||
|
||||
assertThat(entity.getPersistentProperty("foo")).hasValueSatisfying(it -> {
|
||||
assertThat(it.isIdProperty()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-345
|
||||
@@ -149,11 +151,15 @@ public class AbstractMappingContextUnitTests {
|
||||
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Sample.class);
|
||||
SamplePersistentProperty property = entity.getPersistentProperty("persons");
|
||||
PersistentEntity<Object, SamplePersistentProperty> propertyEntity = mappingContext.getPersistentEntity(property);
|
||||
|
||||
assertThat(propertyEntity, is(notNullValue()));
|
||||
assertThat(propertyEntity.getType(), is(equalTo((Class) Person.class)));
|
||||
assertThat(entity.getPersistentProperty("persons")).hasValueSatisfying(it -> {
|
||||
|
||||
PersistentEntity<Object, SamplePersistentProperty> propertyEntity = mappingContext.getPersistentEntity(it);
|
||||
|
||||
assertThat(propertyEntity).isNotNull();
|
||||
assertThat(propertyEntity.getType()).isEqualTo(Person.class);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test // DATACMNS-380
|
||||
@@ -162,9 +168,9 @@ public class AbstractMappingContextUnitTests {
|
||||
PersistentPropertyPath<SamplePersistentProperty> path = context.getPersistentPropertyPath("persons.name",
|
||||
Sample.class);
|
||||
|
||||
assertThat(path.getLength(), is(2));
|
||||
assertThat(path.getBaseProperty().getName(), is("persons"));
|
||||
assertThat(path.getLeafProperty().getName(), is("name"));
|
||||
assertThat(path.getLength()).isEqualTo(2);
|
||||
assertThat(path.getBaseProperty().getName()).isEqualTo("persons");
|
||||
assertThat(path.getLeafProperty().getName()).isEqualTo("name");
|
||||
}
|
||||
|
||||
@Test(expected = MappingException.class) // DATACMNS-380
|
||||
@@ -191,7 +197,7 @@ public class AbstractMappingContextUnitTests {
|
||||
public void shouldReturnNullForSimpleTypesIfInStrictIsEnabled() {
|
||||
|
||||
context.setStrict(true);
|
||||
assertThat(context.getPersistentEntity(Integer.class), is(nullValue()));
|
||||
assertThat(context.getPersistentEntity(Integer.class)).isNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-462
|
||||
@@ -210,27 +216,18 @@ public class AbstractMappingContextUnitTests {
|
||||
|
||||
@Test // DATACMNS-695
|
||||
public void persistentPropertyPathTraversesGenericTypesCorrectly() {
|
||||
assertThat(context.getPersistentPropertyPath("field.wrapped.field", Outer.class),
|
||||
is(Matchers.<SamplePersistentProperty> iterableWithSize(3)));
|
||||
assertThat(context.getPersistentPropertyPath("field.wrapped.field", Outer.class)).hasSize(3);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-727
|
||||
public void exposesContextForFailingPropertyPathLookup() {
|
||||
|
||||
try {
|
||||
|
||||
context.getPersistentPropertyPath("persons.firstname", Sample.class);
|
||||
fail("Expected InvalidPersistentPropertyPath!");
|
||||
|
||||
} catch (InvalidPersistentPropertyPath o_O) {
|
||||
|
||||
assertThat(o_O.getMessage(), not(isEmptyOrNullString()));
|
||||
assertThat(o_O.getResolvedPath(), is("persons"));
|
||||
assertThat(o_O.getUnresolvableSegment(), is("firstname"));
|
||||
|
||||
// Make sure, the resolvable part can be obtained
|
||||
assertThat(context.getPersistentPropertyPath(o_O), is(notNullValue()));
|
||||
}
|
||||
assertThatExceptionOfType(InvalidPersistentPropertyPath.class)//
|
||||
.isThrownBy(() -> context.getPersistentPropertyPath("persons.firstname", Sample.class))//
|
||||
.matches(e -> StringUtils.hasText(e.getMessage()))//
|
||||
.matches(e -> e.getResolvedPath().equals("persons"))//
|
||||
.matches(e -> e.getUnresolvableSegment().equals("firstname"))//
|
||||
.matches(e -> context.getPersistentPropertyPath(e) != null);
|
||||
}
|
||||
|
||||
private static void assertHasEntityFor(Class<?> type, SampleMappingContext context, boolean expected) {
|
||||
|
||||
64
src/test/java/org/springframework/data/mapping/context/DefaultPersistenPropertyPathUnitTests.java
Normal file → Executable file
64
src/test/java/org/springframework/data/mapping/context/DefaultPersistenPropertyPathUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -46,15 +45,14 @@ public class DefaultPersistenPropertyPathUnitTests<T extends PersistentProperty<
|
||||
PersistentPropertyPath<T> twoLegs;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
oneLeg = new DefaultPersistentPropertyPath<T>(Arrays.asList(first));
|
||||
twoLegs = new DefaultPersistentPropertyPath<T>(Arrays.asList(first, second));
|
||||
oneLeg = new DefaultPersistentPropertyPath<>(Arrays.asList(first));
|
||||
twoLegs = new DefaultPersistentPropertyPath<>(Arrays.asList(first, second));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullProperties() {
|
||||
new DefaultPersistentPropertyPath<T>(null);
|
||||
new DefaultPersistentPropertyPath<>(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -63,89 +61,69 @@ public class DefaultPersistenPropertyPathUnitTests<T extends PersistentProperty<
|
||||
when(first.getName()).thenReturn("foo");
|
||||
when(second.getName()).thenReturn("bar");
|
||||
|
||||
assertThat(twoLegs.toDotPath(), is("foo.bar"));
|
||||
assertThat(twoLegs.toDotPath()).isEqualTo("foo.bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void usesConverterToCreatePropertyPath() {
|
||||
|
||||
when(converter.convert((T) any())).thenReturn("foo");
|
||||
when(converter.convert(any())).thenReturn("foo");
|
||||
|
||||
assertThat(twoLegs.toDotPath(converter), is("foo.foo"));
|
||||
assertThat(twoLegs.toDotPath(converter)).isEqualTo("foo.foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsCorrectLeafProperty() {
|
||||
|
||||
assertThat(twoLegs.getLeafProperty(), is(second));
|
||||
assertThat(oneLeg.getLeafProperty(), is(first));
|
||||
assertThat(twoLegs.getLeafProperty()).isEqualTo(second);
|
||||
assertThat(oneLeg.getLeafProperty()).isEqualTo(first);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsCorrectBaseProperty() {
|
||||
|
||||
assertThat(twoLegs.getBaseProperty(), is(first));
|
||||
assertThat(oneLeg.getBaseProperty(), is(first));
|
||||
assertThat(twoLegs.getBaseProperty()).isEqualTo(first);
|
||||
assertThat(oneLeg.getBaseProperty()).isEqualTo(first);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsBasePathCorrectly() {
|
||||
|
||||
assertThat(oneLeg.isBasePathOf(twoLegs), is(true));
|
||||
assertThat(twoLegs.isBasePathOf(oneLeg), is(false));
|
||||
assertThat(oneLeg.isBasePathOf(twoLegs)).isTrue();
|
||||
assertThat(twoLegs.isBasePathOf(oneLeg)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void calculatesExtensionCorrectly() {
|
||||
|
||||
PersistentPropertyPath<T> extension = twoLegs.getExtensionForBaseOf(oneLeg);
|
||||
assertThat(extension, is((PersistentPropertyPath<T>) new DefaultPersistentPropertyPath<T>(Arrays.asList(second))));
|
||||
|
||||
assertThat(extension).isEqualTo(new DefaultPersistentPropertyPath<>(Arrays.asList(second)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsTheCorrectParentPath() {
|
||||
assertThat(twoLegs.getParentPath(), is(oneLeg));
|
||||
assertThat(twoLegs.getParentPath()).isEqualTo(oneLeg);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsItselfAsParentPathIfSizeOne() {
|
||||
assertThat(oneLeg.getParentPath(), is(oneLeg));
|
||||
assertThat(oneLeg.getParentPath()).isEqualTo(oneLeg);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathReturnsCorrectSize() {
|
||||
assertThat(oneLeg.getLength(), is(1));
|
||||
assertThat(twoLegs.getLength(), is(2));
|
||||
assertThat(oneLeg.getLength()).isEqualTo(1);
|
||||
assertThat(twoLegs.getLength()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-444
|
||||
public void skipsMappedPropertyNameIfConverterReturnsNull() {
|
||||
|
||||
String result = twoLegs.toDotPath(new Converter<T, String>() {
|
||||
|
||||
@Override
|
||||
public String convert(T source) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(result, is(nullValue()));
|
||||
assertThat(twoLegs.toDotPath(source -> null)).isNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-444
|
||||
public void skipsMappedPropertyNameIfConverterReturnsEmptyStrings() {
|
||||
|
||||
String result = twoLegs.toDotPath(new Converter<T, String>() {
|
||||
|
||||
@Override
|
||||
public String convert(T source) {
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(result, is(nullValue()));
|
||||
assertThat(twoLegs.toDotPath(source -> "")).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
13
src/test/java/org/springframework/data/mapping/context/MappingContextEventUnitTests.java
Normal file → Executable file
13
src/test/java/org/springframework/data/mapping/context/MappingContextEventUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -24,8 +23,6 @@ import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mapping.context.MappingContextEvent;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MappingContextEvent}.
|
||||
@@ -45,21 +42,21 @@ public class MappingContextEventUnitTests<E extends PersistentEntity<?, P>, P ex
|
||||
public void returnsPersistentEntityHandedToTheEvent() {
|
||||
|
||||
MappingContextEvent<E, P> event = new MappingContextEvent<E, P>(mappingContext, entity);
|
||||
assertThat(event.getPersistentEntity(), is(entity));
|
||||
assertThat(event.getPersistentEntity()).isEqualTo(entity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesMappingContextAsEventSource() {
|
||||
|
||||
MappingContextEvent<E, P> event = new MappingContextEvent<E, P>(mappingContext, entity);
|
||||
assertThat(event.getSource(), is((Object) mappingContext));
|
||||
assertThat(event.getSource()).isEqualTo(mappingContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsEmittingMappingContextCorrectly() {
|
||||
|
||||
MappingContextEvent<E, P> event = new MappingContextEvent<E, P>(mappingContext, entity);
|
||||
assertThat(event.wasEmittedBy(mappingContext), is(true));
|
||||
assertThat(event.wasEmittedBy(otherMappingContext), is(false));
|
||||
assertThat(event.wasEmittedBy(mappingContext)).isTrue();
|
||||
assertThat(event.wasEmittedBy(otherMappingContext)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
48
src/test/java/org/springframework/data/mapping/context/MappingContextIsNewStrategyFactoryUnitTests.java
Normal file → Executable file
48
src/test/java/org/springframework/data/mapping/context/MappingContextIsNewStrategyFactoryUnitTests.java
Normal file → Executable file
@@ -15,12 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -45,7 +45,7 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
|
||||
public void setUp() {
|
||||
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
context.setInitialEntitySet(new HashSet<Class<?>>(Arrays.<Class<?>> asList(Entity.class, VersionedEntity.class)));
|
||||
context.setInitialEntitySet(new HashSet<>(Arrays.asList(Entity.class, VersionedEntity.class)));
|
||||
context.afterPropertiesSet();
|
||||
|
||||
factory = new MappingContextIsNewStrategyFactory(new PersistentEntities(Collections.singleton(context)));
|
||||
@@ -55,48 +55,48 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
|
||||
public void returnsPropertyIsNullOrZeroIsNewStrategyForVersionedEntity() {
|
||||
|
||||
IsNewStrategy strategy = factory.getIsNewStrategy(VersionedEntity.class);
|
||||
assertThat(strategy, is(instanceOf(PropertyIsNullOrZeroNumberIsNewStrategy.class)));
|
||||
assertThat(strategy).isInstanceOf(PropertyIsNullOrZeroNumberIsNewStrategy.class);
|
||||
|
||||
VersionedEntity entity = new VersionedEntity();
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
Optional<VersionedEntity> entity = Optional.of(new VersionedEntity());
|
||||
assertThat(strategy.isNew(entity)).isTrue();
|
||||
|
||||
entity.id = 1L;
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
entity.get().id = 1L;
|
||||
assertThat(strategy.isNew(entity)).isTrue();
|
||||
|
||||
entity.version = 0L;
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
entity.get().version = 0L;
|
||||
assertThat(strategy.isNew(entity)).isTrue();
|
||||
|
||||
entity.version = 1L;
|
||||
assertThat(strategy.isNew(entity), is(false));
|
||||
entity.get().version = 1L;
|
||||
assertThat(strategy.isNew(entity)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsPropertyIsNullOrZeroIsNewStrategyForPrimitiveVersionedEntity() {
|
||||
|
||||
IsNewStrategy strategy = factory.getIsNewStrategy(VersionedEntity.class);
|
||||
assertThat(strategy, is(instanceOf(PropertyIsNullOrZeroNumberIsNewStrategy.class)));
|
||||
assertThat(strategy).isInstanceOf(PropertyIsNullOrZeroNumberIsNewStrategy.class);
|
||||
|
||||
VersionedEntity entity = new VersionedEntity();
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
Optional<VersionedEntity> entity = Optional.of(new VersionedEntity());
|
||||
assertThat(strategy.isNew(entity)).isTrue();
|
||||
|
||||
entity.id = 1L;
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
entity.get().id = 1L;
|
||||
assertThat(strategy.isNew(entity)).isTrue();
|
||||
|
||||
entity.version = 1L;
|
||||
assertThat(strategy.isNew(entity), is(false));
|
||||
entity.get().version = 1L;
|
||||
assertThat(strategy.isNew(entity)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsPropertyIsNullIsNewStrategyForEntity() {
|
||||
|
||||
IsNewStrategy strategy = factory.getIsNewStrategy(Entity.class);
|
||||
assertThat(strategy, is(instanceOf(PropertyIsNullIsNewStrategy.class)));
|
||||
assertThat(strategy).isInstanceOf(PropertyIsNullIsNewStrategy.class);
|
||||
|
||||
Entity entity = new Entity();
|
||||
assertThat(strategy.isNew(entity), is(true));
|
||||
Optional<Entity> entity = Optional.of(new Entity());
|
||||
assertThat(strategy.isNew(entity)).isTrue();
|
||||
|
||||
entity.id = 1L;
|
||||
assertThat(strategy.isNew(entity), is(false));
|
||||
entity.get().id = 1L;
|
||||
assertThat(strategy.isNew(entity)).isFalse();
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
|
||||
11
src/test/java/org/springframework/data/mapping/context/PersistentEntitiesUnitTests.java
Normal file → Executable file
11
src/test/java/org/springframework/data/mapping/context/PersistentEntitiesUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -68,10 +67,10 @@ public class PersistentEntitiesUnitTests {
|
||||
|
||||
PersistentEntities entities = new PersistentEntities(Arrays.asList(context));
|
||||
|
||||
assertThat(entities.getPersistentEntity(Sample.class), is(notNullValue()));
|
||||
assertThat(entities.getPersistentEntity(Object.class), is(nullValue()));
|
||||
assertThat(entities.getManagedTypes(), hasItem(ClassTypeInformation.from(Sample.class)));
|
||||
assertThat(entities, hasItem(entities.getPersistentEntity(Sample.class)));
|
||||
assertThat(entities.getPersistentEntity(Sample.class)).isNotNull();
|
||||
assertThat(entities.getPersistentEntity(Object.class)).isNull();
|
||||
assertThat(entities.getManagedTypes()).contains(ClassTypeInformation.from(Sample.class));
|
||||
assertThat(entities).contains(entities.getPersistentEntity(Sample.class));
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
27
src/test/java/org/springframework/data/mapping/context/PropertyMatchUnitTests.java
Normal file → Executable file
27
src/test/java/org/springframework/data/mapping/context/PropertyMatchUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.context;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mapping.context.AbstractMappingContext.PersistentPropertyFilter.PropertyMatch;
|
||||
@@ -38,36 +37,36 @@ public class PropertyMatchUnitTests {
|
||||
public void matchesFieldByConcreteNameAndType() throws Exception {
|
||||
|
||||
PropertyMatch match = new PropertyMatch("name", "java.lang.String");
|
||||
assertThat(match.matches("this$0", Object.class), is(false));
|
||||
assertThat(match.matches("this$1", Object.class), is(false));
|
||||
assertThat(match.matches("name", String.class), is(true));
|
||||
assertThat(match.matches("this$0", Object.class)).isFalse();
|
||||
assertThat(match.matches("this$1", Object.class)).isFalse();
|
||||
assertThat(match.matches("name", String.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesFieldByNamePattern() throws Exception {
|
||||
|
||||
PropertyMatch match = new PropertyMatch("this\\$.*", "java.lang.Object");
|
||||
assertThat(match.matches("this$0", Object.class), is(true));
|
||||
assertThat(match.matches("this$1", Object.class), is(true));
|
||||
assertThat(match.matches("name", String.class), is(false));
|
||||
assertThat(match.matches("this$0", Object.class)).isTrue();
|
||||
assertThat(match.matches("this$1", Object.class)).isTrue();
|
||||
assertThat(match.matches("name", String.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesFieldByNameOnly() throws Exception {
|
||||
|
||||
PropertyMatch match = new PropertyMatch("this\\$.*", null);
|
||||
assertThat(match.matches("this$0", Object.class), is(true));
|
||||
assertThat(match.matches("this$1", Object.class), is(true));
|
||||
assertThat(match.matches("name", String.class), is(false));
|
||||
assertThat(match.matches("this$0", Object.class)).isTrue();
|
||||
assertThat(match.matches("this$1", Object.class)).isTrue();
|
||||
assertThat(match.matches("name", String.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesFieldByTypeNameOnly() throws Exception {
|
||||
|
||||
PropertyMatch match = new PropertyMatch(null, "java.lang.Object");
|
||||
assertThat(match.matches("this$0", Object.class), is(true));
|
||||
assertThat(match.matches("this$1", Object.class), is(true));
|
||||
assertThat(match.matches("name", String.class), is(false));
|
||||
assertThat(match.matches("this$0", Object.class)).isTrue();
|
||||
assertThat(match.matches("this$1", Object.class)).isTrue();
|
||||
assertThat(match.matches("name", String.class)).isFalse();
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
@@ -2,13 +2,14 @@ package org.springframework.data.mapping.context;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
public class SampleMappingContext extends
|
||||
AbstractMappingContext<BasicPersistentEntity<Object, SamplePersistentProperty>, SamplePersistentProperty> {
|
||||
public class SampleMappingContext
|
||||
extends AbstractMappingContext<BasicPersistentEntity<Object, SamplePersistentProperty>, SamplePersistentProperty> {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -18,8 +19,8 @@ public class SampleMappingContext extends
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SamplePersistentProperty createPersistentProperty(final Field field, final PropertyDescriptor descriptor,
|
||||
final BasicPersistentEntity<Object, SamplePersistentProperty> owner, final SimpleTypeHolder simpleTypeHolder) {
|
||||
protected SamplePersistentProperty createPersistentProperty(Optional<Field> field, PropertyDescriptor descriptor,
|
||||
BasicPersistentEntity<Object, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
return new SamplePersistentProperty(field, descriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.mapping.context;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.mapping.Association;
|
||||
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
||||
@@ -25,7 +26,7 @@ import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
|
||||
public class SamplePersistentProperty extends AnnotationBasedPersistentProperty<SamplePersistentProperty> {
|
||||
|
||||
public SamplePersistentProperty(Field field, PropertyDescriptor propertyDescriptor,
|
||||
public SamplePersistentProperty(Optional<Field> field, PropertyDescriptor propertyDescriptor,
|
||||
BasicPersistentEntity<?, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
super(field, propertyDescriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
|
||||
118
src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java
Normal file → Executable file
118
src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java
Normal file → Executable file
@@ -15,17 +15,17 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
@@ -63,7 +63,7 @@ public class AbstractPersistentPropertyUnitTests {
|
||||
|
||||
Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet");
|
||||
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
|
||||
property.getComponentType();
|
||||
}
|
||||
|
||||
@@ -72,24 +72,24 @@ public class AbstractPersistentPropertyUnitTests {
|
||||
|
||||
Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet");
|
||||
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
|
||||
assertThat(property.getPersistentEntityType().iterator().hasNext(), is(false));
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
|
||||
assertThat(property.getPersistentEntityType().iterator().hasNext()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-132
|
||||
public void isEntityWorksForUntypedMaps() throws Exception {
|
||||
|
||||
Field field = ReflectionUtils.findField(TestClassComplex.class, "map");
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
|
||||
assertThat(property.isEntity(), is(false));
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
|
||||
assertThat(property.isEntity()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-132
|
||||
public void isEntityWorksForUntypedCollection() throws Exception {
|
||||
|
||||
Field field = ReflectionUtils.findField(TestClassComplex.class, "collection");
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
|
||||
assertThat(property.isEntity(), is(false));
|
||||
SamplePersistentProperty property = new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
|
||||
assertThat(property.isEntity()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-121
|
||||
@@ -98,11 +98,12 @@ public class AbstractPersistentPropertyUnitTests {
|
||||
Field first = ReflectionUtils.findField(FirstConcrete.class, "genericField");
|
||||
Field second = ReflectionUtils.findField(SecondConcrete.class, "genericField");
|
||||
|
||||
SamplePersistentProperty firstProperty = new SamplePersistentProperty(first, null, entity, typeHolder);
|
||||
SamplePersistentProperty secondProperty = new SamplePersistentProperty(second, null, entity, typeHolder);
|
||||
SamplePersistentProperty firstProperty = new SamplePersistentProperty(Optional.of(first), null, entity, typeHolder);
|
||||
SamplePersistentProperty secondProperty = new SamplePersistentProperty(Optional.of(second), null, entity,
|
||||
typeHolder);
|
||||
|
||||
assertThat(firstProperty, is(secondProperty));
|
||||
assertThat(firstProperty.hashCode(), is(secondProperty.hashCode()));
|
||||
assertThat(firstProperty).isEqualTo(secondProperty);
|
||||
assertThat(firstProperty.hashCode()).isEqualTo(secondProperty.hashCode());
|
||||
}
|
||||
|
||||
@Test // DATACMNS-180
|
||||
@@ -110,115 +111,116 @@ public class AbstractPersistentPropertyUnitTests {
|
||||
|
||||
Field transientField = ReflectionUtils.findField(TestClassComplex.class, "transientField");
|
||||
|
||||
PersistentProperty<?> property = new SamplePersistentProperty(transientField, null, entity, typeHolder);
|
||||
assertThat(property.isTransient(), is(false));
|
||||
PersistentProperty<?> property = new SamplePersistentProperty(Optional.of(transientField), null, entity,
|
||||
typeHolder);
|
||||
assertThat(property.isTransient()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-206
|
||||
public void findsSimpleGettersAndASetters() {
|
||||
|
||||
Field field = ReflectionUtils.findField(AccessorTestClass.class, "id");
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
|
||||
AccessorTestClass.class, "id"), entity, typeHolder);
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field),
|
||||
getPropertyDescriptor(AccessorTestClass.class, "id"), entity, typeHolder);
|
||||
|
||||
assertThat(property.getGetter(), is(notNullValue()));
|
||||
assertThat(property.getSetter(), is(notNullValue()));
|
||||
assertThat(property.getGetter()).isNotNull();
|
||||
assertThat(property.getSetter()).isNotNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-206
|
||||
public void doesNotUseInvalidGettersAndASetters() {
|
||||
|
||||
Field field = ReflectionUtils.findField(AccessorTestClass.class, "anotherId");
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
|
||||
AccessorTestClass.class, "anotherId"), entity, typeHolder);
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field),
|
||||
getPropertyDescriptor(AccessorTestClass.class, "anotherId"), entity, typeHolder);
|
||||
|
||||
assertThat(property.getGetter(), is(nullValue()));
|
||||
assertThat(property.getSetter(), is(nullValue()));
|
||||
assertThat(property.getGetter()).isNull();
|
||||
assertThat(property.getSetter()).isNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-206
|
||||
public void usesCustomGetter() {
|
||||
|
||||
Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetAnotherId");
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
|
||||
AccessorTestClass.class, "yetAnotherId"), entity, typeHolder);
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field),
|
||||
getPropertyDescriptor(AccessorTestClass.class, "yetAnotherId"), entity, typeHolder);
|
||||
|
||||
assertThat(property.getGetter(), is(notNullValue()));
|
||||
assertThat(property.getSetter(), is(nullValue()));
|
||||
assertThat(property.getGetter()).isNotNull();
|
||||
assertThat(property.getSetter()).isNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-206
|
||||
public void usesCustomSetter() {
|
||||
|
||||
Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetYetAnotherId");
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
|
||||
AccessorTestClass.class, "yetYetAnotherId"), entity, typeHolder);
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field),
|
||||
getPropertyDescriptor(AccessorTestClass.class, "yetYetAnotherId"), entity, typeHolder);
|
||||
|
||||
assertThat(property.getGetter(), is(nullValue()));
|
||||
assertThat(property.getSetter(), is(notNullValue()));
|
||||
assertThat(property.getGetter()).isNull();
|
||||
assertThat(property.getSetter()).isNotNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-206
|
||||
public void returnsNullGetterAndSetterIfNoPropertyDescriptorGiven() {
|
||||
|
||||
Field field = ReflectionUtils.findField(AccessorTestClass.class, "id");
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, null, entity,
|
||||
typeHolder);
|
||||
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field), null,
|
||||
entity, typeHolder);
|
||||
|
||||
assertThat(property.getGetter(), is(nullValue()));
|
||||
assertThat(property.getSetter(), is(nullValue()));
|
||||
assertThat(property.getGetter()).isNull();
|
||||
assertThat(property.getSetter()).isNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-337
|
||||
public void resolvesActualType() {
|
||||
|
||||
SamplePersistentProperty property = getProperty(Sample.class, "person");
|
||||
assertThat(property.getActualType(), is((Object) Person.class));
|
||||
assertThat(property.getActualType()).isEqualTo(Person.class);
|
||||
|
||||
property = getProperty(Sample.class, "persons");
|
||||
assertThat(property.getActualType(), is((Object) Person.class));
|
||||
assertThat(property.getActualType()).isEqualTo(Person.class);
|
||||
|
||||
property = getProperty(Sample.class, "personArray");
|
||||
assertThat(property.getActualType(), is((Object) Person.class));
|
||||
assertThat(property.getActualType()).isEqualTo(Person.class);
|
||||
|
||||
property = getProperty(Sample.class, "personMap");
|
||||
assertThat(property.getActualType(), is((Object) Person.class));
|
||||
assertThat(property.getActualType()).isEqualTo(Person.class);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-462
|
||||
public void considersCollectionPropertyEntitiesIfComponentTypeIsEntity() {
|
||||
|
||||
SamplePersistentProperty property = getProperty(Sample.class, "persons");
|
||||
assertThat(property.isEntity(), is(true));
|
||||
assertThat(property.isEntity()).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-462
|
||||
public void considersMapPropertyEntitiesIfValueTypeIsEntity() {
|
||||
|
||||
SamplePersistentProperty property = getProperty(Sample.class, "personMap");
|
||||
assertThat(property.isEntity(), is(true));
|
||||
assertThat(property.isEntity()).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-462
|
||||
public void considersArrayPropertyEntitiesIfComponentTypeIsEntity() {
|
||||
|
||||
SamplePersistentProperty property = getProperty(Sample.class, "personArray");
|
||||
assertThat(property.isEntity(), is(true));
|
||||
assertThat(property.isEntity()).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-462
|
||||
public void considersCollectionPropertySimpleIfComponentTypeIsSimple() {
|
||||
|
||||
SamplePersistentProperty property = getProperty(Sample.class, "strings");
|
||||
assertThat(property.isEntity(), is(false));
|
||||
assertThat(property.isEntity()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-562
|
||||
public void doesNotConsiderPropertyWithTreeMapMapValueAnEntity() {
|
||||
|
||||
SamplePersistentProperty property = getProperty(TreeMapWrapper.class, "map");
|
||||
assertThat(property.getPersistentEntityType(), is(emptyIterable()));
|
||||
assertThat(property.isEntity(), is(false));
|
||||
assertThat(property.getPersistentEntityType()).isEmpty();
|
||||
assertThat(property.isEntity()).isFalse();
|
||||
}
|
||||
|
||||
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
|
||||
@@ -227,22 +229,16 @@ public class AbstractPersistentPropertyUnitTests {
|
||||
ClassTypeInformation.from(type));
|
||||
|
||||
Field field = ReflectionUtils.findField(type, name);
|
||||
return new SamplePersistentProperty(field, null, entity, typeHolder);
|
||||
return new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
|
||||
}
|
||||
|
||||
private static PropertyDescriptor getPropertyDescriptor(Class<?> type, String propertyName) {
|
||||
|
||||
try {
|
||||
|
||||
BeanInfo info = Introspector.getBeanInfo(type);
|
||||
|
||||
for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
|
||||
if (descriptor.getName().equals(propertyName)) {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return Arrays.stream(Introspector.getBeanInfo(type).getPropertyDescriptors())//
|
||||
.filter(it -> it.getName().equals(propertyName))//
|
||||
.findFirst().orElse(null);
|
||||
|
||||
} catch (IntrospectionException e) {
|
||||
return null;
|
||||
@@ -315,7 +311,7 @@ public class AbstractPersistentPropertyUnitTests {
|
||||
|
||||
class SamplePersistentProperty extends AbstractPersistentProperty<SamplePersistentProperty> {
|
||||
|
||||
public SamplePersistentProperty(Field field, PropertyDescriptor propertyDescriptor,
|
||||
public SamplePersistentProperty(Optional<Field> field, PropertyDescriptor propertyDescriptor,
|
||||
PersistentEntity<?, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||
super(field, propertyDescriptor, owner, simpleTypeHolder);
|
||||
}
|
||||
@@ -339,8 +335,8 @@ public class AbstractPersistentPropertyUnitTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <A extends Annotation> A findAnnotation(Class<A> annotationType) {
|
||||
return null;
|
||||
public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -349,8 +345,8 @@ public class AbstractPersistentPropertyUnitTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <A extends Annotation> A findPropertyOrOwnerAnnotation(Class<A> annotationType) {
|
||||
return null;
|
||||
public <A extends Annotation> Optional<A> findPropertyOrOwnerAnnotation(Class<A> annotationType) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
87
src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java
Normal file → Executable file
87
src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java
Normal file → Executable file
@@ -16,14 +16,14 @@
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -83,17 +83,18 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
|
||||
@Test // DATACMNS-282
|
||||
public void populatesAnnotationCacheWithDirectAnnotationsOnCreation() {
|
||||
|
||||
SamplePersistentProperty property = entity.getPersistentProperty("meta");
|
||||
assertThat(entity.getPersistentProperty("meta")).hasValueSatisfying(property -> {
|
||||
|
||||
// Assert direct annotations are cached on construction
|
||||
Map<Class<? extends Annotation>, Annotation> cache = getAnnotationCache(property);
|
||||
assertThat(cache.containsKey(MyAnnotationAsMeta.class), is(true));
|
||||
assertThat(cache.containsKey(MyAnnotation.class), is(false));
|
||||
// Assert direct annotations are cached on construction
|
||||
Map<Class<? extends Annotation>, Annotation> cache = getAnnotationCache(property);
|
||||
assertThat(cache.containsKey(MyAnnotationAsMeta.class)).isTrue();
|
||||
assertThat(cache.containsKey(MyAnnotation.class)).isFalse();
|
||||
|
||||
// Assert meta annotation is found and cached
|
||||
MyAnnotation annotation = property.findAnnotation(MyAnnotation.class);
|
||||
assertThat(annotation, is(notNullValue()));
|
||||
assertThat(cache.containsKey(MyAnnotation.class), is(true));
|
||||
// Assert meta annotation is found and cached
|
||||
assertThat(property.findAnnotation(MyAnnotation.class)).hasValueSatisfying(annotation -> {
|
||||
assertThat(cache.containsKey(MyAnnotation.class)).isTrue();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-282
|
||||
@@ -103,28 +104,28 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
|
||||
context.getPersistentEntity(InvalidSample.class);
|
||||
fail("Expected MappingException!");
|
||||
} catch (MappingException o_O) {
|
||||
assertThat(context.hasPersistentEntityFor(InvalidSample.class), is(false));
|
||||
assertThat(context.hasPersistentEntityFor(InvalidSample.class)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@Test // DATACMNS-243
|
||||
public void defaultsToFieldAccess() {
|
||||
assertThat(getProperty(FieldAccess.class, "name").usePropertyAccess(), is(false));
|
||||
assertThat(getProperty(FieldAccess.class, "name").usePropertyAccess()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-243
|
||||
public void usesAccessTypeDeclaredOnTypeAsDefault() {
|
||||
assertThat(getProperty(PropertyAccess.class, "firstname").usePropertyAccess(), is(true));
|
||||
assertThat(getProperty(PropertyAccess.class, "firstname").usePropertyAccess()).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-243
|
||||
public void propertyAnnotationOverridesTypeConfiguration() {
|
||||
assertThat(getProperty(PropertyAccess.class, "lastname").usePropertyAccess(), is(false));
|
||||
assertThat(getProperty(PropertyAccess.class, "lastname").usePropertyAccess()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-243
|
||||
public void fieldAnnotationOverridesTypeConfiguration() {
|
||||
assertThat(getProperty(PropertyAccess.class, "emailAddress").usePropertyAccess(), is(false));
|
||||
assertThat(getProperty(PropertyAccess.class, "emailAddress").usePropertyAccess()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-243
|
||||
@@ -134,22 +135,22 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
|
||||
|
||||
@Test // DATACMNS-534
|
||||
public void treatsNoAnnotationCorrectly() {
|
||||
assertThat(getProperty(ClassWithReadOnlyProperties.class, "noAnnotations").isWritable(), is(true));
|
||||
assertThat(getProperty(ClassWithReadOnlyProperties.class, "noAnnotations").isWritable()).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-534
|
||||
public void treatsTransientAsNotExisting() {
|
||||
assertThat(getProperty(ClassWithReadOnlyProperties.class, "transientProperty"), nullValue());
|
||||
assertThat(getProperty(ClassWithReadOnlyProperties.class, "transientProperty")).isNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-534
|
||||
public void treatsReadOnlyAsNonWritable() {
|
||||
assertThat(getProperty(ClassWithReadOnlyProperties.class, "readOnlyProperty").isWritable(), is(false));
|
||||
assertThat(getProperty(ClassWithReadOnlyProperties.class, "readOnlyProperty").isWritable()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-534
|
||||
public void considersPropertyWithReadOnlyMetaAnnotationReadOnly() {
|
||||
assertThat(getProperty(ClassWithReadOnlyProperties.class, "customReadOnlyProperty").isWritable(), is(false));
|
||||
assertThat(getProperty(ClassWithReadOnlyProperties.class, "customReadOnlyProperty").isWritable()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-556
|
||||
@@ -163,37 +164,39 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
|
||||
|
||||
SamplePersistentProperty property = getProperty(Sample.class, "getterWithoutField");
|
||||
|
||||
assertThat(property.findAnnotation(MyAnnotation.class), is(nullValue()));
|
||||
assertThat(property.findAnnotation(MyAnnotation.class)).isNotPresent();
|
||||
|
||||
Map<Class<?>, ?> field = (Map<Class<?>, ?>) ReflectionTestUtils.getField(property, "annotationCache");
|
||||
|
||||
assertThat(field.containsKey(MyAnnotation.class), is(true));
|
||||
assertThat(field.get(MyAnnotation.class), is(nullValue()));
|
||||
assertThat(field.containsKey(MyAnnotation.class)).isTrue();
|
||||
assertThat(field.get(MyAnnotation.class)).isEqualTo(Optional.empty());
|
||||
}
|
||||
|
||||
@Test // DATACMNS-825
|
||||
public void composedAnnotationWithAliasForGetCachedCorrectly() {
|
||||
|
||||
SamplePersistentProperty property = entity.getPersistentProperty("metaAliased");
|
||||
assertThat(entity.getPersistentProperty("metaAliased")).hasValueSatisfying(property -> {
|
||||
|
||||
// Assert direct annotations are cached on construction
|
||||
Map<Class<? extends Annotation>, Annotation> cache = getAnnotationCache(property);
|
||||
assertThat(cache.containsKey(MyComposedAnnotationUsingAliasFor.class), is(true));
|
||||
assertThat(cache.containsKey(MyAnnotation.class), is(false));
|
||||
// Assert direct annotations are cached on construction
|
||||
Map<Class<? extends Annotation>, Annotation> cache = getAnnotationCache(property);
|
||||
assertThat(cache.containsKey(MyComposedAnnotationUsingAliasFor.class)).isTrue();
|
||||
assertThat(cache.containsKey(MyAnnotation.class)).isFalse();
|
||||
|
||||
// Assert meta annotation is found and cached
|
||||
MyAnnotation annotation = property.findAnnotation(MyAnnotation.class);
|
||||
assertThat(annotation, is(notNullValue()));
|
||||
assertThat(cache.containsKey(MyAnnotation.class), is(true));
|
||||
// Assert meta annotation is found and cached
|
||||
assertThat(property.findAnnotation(MyAnnotation.class)).hasValueSatisfying(it -> {
|
||||
assertThat(cache.containsKey(MyAnnotation.class)).isTrue();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-825
|
||||
public void composedAnnotationWithAliasShouldHaveSynthesizedAttributeValues() {
|
||||
|
||||
SamplePersistentProperty property = entity.getPersistentProperty("metaAliased");
|
||||
|
||||
MyAnnotation annotation = property.findAnnotation(MyAnnotation.class);
|
||||
assertThat(AnnotationUtils.getValue(annotation), is((Object) "spring"));
|
||||
assertThat(entity.getPersistentProperty("metaAliased")).hasValueSatisfying(property -> {
|
||||
assertThat(property.findAnnotation(MyAnnotation.class)).hasValueSatisfying(annotation -> {
|
||||
assertThat(AnnotationUtils.getValue(annotation)).isEqualTo("spring");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -202,15 +205,17 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
|
||||
}
|
||||
|
||||
private <A extends Annotation> A assertAnnotationPresent(Class<A> annotationType,
|
||||
AnnotationBasedPersistentProperty<?> property) {
|
||||
Optional<? extends AnnotationBasedPersistentProperty<?>> property) {
|
||||
|
||||
A annotation = property.findAnnotation(annotationType);
|
||||
assertThat(annotation, is(notNullValue()));
|
||||
return annotation;
|
||||
Optional<A> annotation = property.flatMap(it -> it.findAnnotation(annotationType));
|
||||
|
||||
assertThat(annotation).isPresent();
|
||||
|
||||
return annotation.get();
|
||||
}
|
||||
|
||||
private SamplePersistentProperty getProperty(Class<?> type, String name) {
|
||||
return context.getPersistentEntity(type).getPersistentProperty(name);
|
||||
return context.getPersistentEntity(type).getPersistentProperty(name).orElse(null);
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
52
src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java
Normal file → Executable file
52
src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java
Normal file → Executable file
@@ -15,8 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assume.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -25,6 +26,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.Assert;
|
||||
@@ -84,14 +86,14 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
public void returnsNullForTypeAliasIfNoneConfigured() {
|
||||
|
||||
PersistentEntity<Entity, T> entity = createEntity(Entity.class);
|
||||
assertThat(entity.getTypeAlias(), is(nullValue()));
|
||||
assertThat(entity.getTypeAlias()).isNotPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsTypeAliasIfAnnotated() {
|
||||
|
||||
PersistentEntity<AliasedEntity, T> entity = createEntity(AliasedEntity.class);
|
||||
assertThat(entity.getTypeAlias(), is((Object) "foo"));
|
||||
assertThat(entity.getTypeAlias()).isEqualTo("foo");
|
||||
}
|
||||
|
||||
@Test // DATACMNS-50
|
||||
@@ -120,22 +122,22 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
|
||||
List<T> properties = (List<T>) ReflectionTestUtils.getField(entity, "properties");
|
||||
|
||||
assertThat(properties.size(), is(3));
|
||||
assertThat(properties).hasSize(3);
|
||||
Iterator<T> iterator = properties.iterator();
|
||||
assertThat(iterator.next(), is(entity.getPersistentProperty("firstName")));
|
||||
assertThat(iterator.next(), is(entity.getPersistentProperty("lastName")));
|
||||
assertThat(iterator.next(), is(entity.getPersistentProperty("ssn")));
|
||||
assertThat(iterator.next()).isEqualTo(entity.getPersistentProperty("firstName"));
|
||||
assertThat(iterator.next()).isEqualTo(entity.getPersistentProperty("lastName"));
|
||||
assertThat(iterator.next()).isEqualTo(entity.getPersistentProperty("ssn"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-186
|
||||
public void addingAndIdPropertySetsIdPropertyInternally() {
|
||||
|
||||
MutablePersistentEntity<Person, T> entity = createEntity(Person.class);
|
||||
assertThat(entity.getIdProperty(), is(nullValue()));
|
||||
assertThat(entity.getIdProperty()).isNull();
|
||||
|
||||
when(property.isIdProperty()).thenReturn(true);
|
||||
entity.addPersistentProperty(property);
|
||||
assertThat(entity.getIdProperty(), is(property));
|
||||
assertThat(entity.getIdProperty()).isEqualTo(property);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-186
|
||||
@@ -157,15 +159,19 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(Entity.class);
|
||||
|
||||
PersistentProperty<?> property = entity.getPersistentProperty(LastModifiedBy.class);
|
||||
assertThat(property, is(notNullValue()));
|
||||
assertThat(property.getName(), is("field"));
|
||||
Optional<SamplePersistentProperty> property = entity.getPersistentProperty(LastModifiedBy.class);
|
||||
|
||||
assertThat(property).hasValueSatisfying(it -> {
|
||||
assertThat(it.getName()).isEqualTo("field");
|
||||
});
|
||||
|
||||
property = entity.getPersistentProperty(CreatedBy.class);
|
||||
assertThat(property, is(notNullValue()));
|
||||
assertThat(property.getName(), is("property"));
|
||||
|
||||
assertThat(entity.getPersistentProperty(CreatedDate.class), is(nullValue()));
|
||||
assertThat(property).hasValueSatisfying(it -> {
|
||||
assertThat(it.getName()).isEqualTo("property");
|
||||
});
|
||||
|
||||
assertThat(entity.getPersistentProperty(CreatedDate.class)).isNotPresent();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-596
|
||||
@@ -179,8 +185,8 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
Entity value = new Entity();
|
||||
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
|
||||
|
||||
assertThat(accessor, is(instanceOf(BeanWrapper.class)));
|
||||
assertThat(accessor.getBean(), is((Object) value));
|
||||
assertThat(accessor).isEqualTo(instanceOf(BeanWrapper.class));
|
||||
assertThat(accessor.getBean()).isEqualTo(value);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-809
|
||||
@@ -194,9 +200,9 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
Entity value = new Entity();
|
||||
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
|
||||
|
||||
assertThat(accessor, is(not(instanceOf(BeanWrapper.class))));
|
||||
assertThat(accessor.getClass().getName(), containsString("_Accessor_"));
|
||||
assertThat(accessor.getBean(), is((Object) value));
|
||||
assertThat(accessor).isNotEqualTo(instanceOf(BeanWrapper.class));
|
||||
assertThat(accessor.getClass().getName()).contains("_Accessor_");
|
||||
assertThat(accessor.getBean()).isEqualTo(value);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) // DATACMNS-596
|
||||
@@ -223,7 +229,7 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(Entity.class);
|
||||
|
||||
assertThat(entity.getPropertyAccessor(new Subtype()), is(notNullValue()));
|
||||
assertThat(entity.getPropertyAccessor(new Subtype())).isNotNull();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-825
|
||||
@@ -231,7 +237,7 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
|
||||
PersistentEntity<AliasEntityUsingComposedAnnotation, T> entity = createEntity(
|
||||
AliasEntityUsingComposedAnnotation.class);
|
||||
assertThat(entity.getTypeAlias(), is((Object) "bar"));
|
||||
assertThat(entity.getTypeAlias()).isEqualTo("bar");
|
||||
}
|
||||
|
||||
@Test // DATACMNS-866
|
||||
@@ -265,7 +271,7 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
}
|
||||
|
||||
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type, Comparator<T> comparator) {
|
||||
return new BasicPersistentEntity<S, T>(ClassTypeInformation.from(type), comparator);
|
||||
return new BasicPersistentEntity<S, T>(ClassTypeInformation.from(type), Optional.ofNullable(comparator));
|
||||
}
|
||||
|
||||
@TypeAlias("foo")
|
||||
|
||||
5
src/test/java/org/springframework/data/mapping/model/CamelCaseAbbreviatingFieldNamingStrategyUnitTests.java
Normal file → Executable file
5
src/test/java/org/springframework/data/mapping/model/CamelCaseAbbreviatingFieldNamingStrategyUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -48,6 +47,6 @@ public class CamelCaseAbbreviatingFieldNamingStrategyUnitTests {
|
||||
private void assertFieldNameForPropertyName(String propertyName, String fieldName) {
|
||||
|
||||
when(property.getName()).thenReturn(propertyName);
|
||||
assertThat(strategy.getFieldName(property), is(fieldName));
|
||||
assertThat(strategy.getFieldName(property)).isEqualTo(fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
29
src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryDatatypeTests.java
Normal file → Executable file
29
src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryDatatypeTests.java
Normal file → Executable file
@@ -15,10 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -26,6 +23,7 @@ import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -53,21 +51,20 @@ public class ClassGeneratingPropertyAccessorFactoryDatatypeTests {
|
||||
|
||||
private final Object bean;
|
||||
private final String propertyName;
|
||||
private final Object value;
|
||||
private final Optional<Object> value;
|
||||
|
||||
public ClassGeneratingPropertyAccessorFactoryDatatypeTests(Object bean, String propertyName, Object value,
|
||||
String displayName) {
|
||||
|
||||
this.bean = bean;
|
||||
this.propertyName = propertyName;
|
||||
this.value = value;
|
||||
this.value = Optional.of(value);
|
||||
}
|
||||
|
||||
@Parameters(name = "{3}")
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<Object[]> parameters() throws Exception {
|
||||
|
||||
List<Object[]> parameters = new ArrayList<Object[]>();
|
||||
List<Object[]> parameters = new ArrayList<>();
|
||||
List<Class<?>> types = Arrays.asList(FieldAccess.class, PropertyAccess.class, PrivateFinalFieldAccess.class, PrivateFinalPropertyAccess.class);
|
||||
|
||||
parameters.addAll(parameters(types, "primitiveInteger", Integer.valueOf(1)));
|
||||
@@ -126,11 +123,13 @@ public class ClassGeneratingPropertyAccessorFactoryDatatypeTests {
|
||||
@Test // DATACMNS-809
|
||||
public void shouldSetAndGetProperty() throws Exception {
|
||||
|
||||
PersistentProperty<?> property = getProperty(bean, propertyName);
|
||||
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
|
||||
assertThat(getProperty(bean, propertyName)).hasValueSatisfying(property -> {
|
||||
|
||||
persistentPropertyAccessor.setProperty(property, value);
|
||||
assertThat(persistentPropertyAccessor.getProperty(property), is(equalTo((Object) value)));
|
||||
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
|
||||
|
||||
persistentPropertyAccessor.setProperty(property, value);
|
||||
assertThat(persistentPropertyAccessor.getProperty(property)).isEqualTo(value);
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-809
|
||||
@@ -139,15 +138,15 @@ public class ClassGeneratingPropertyAccessorFactoryDatatypeTests {
|
||||
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = mappingContext
|
||||
.getPersistentEntity(bean.getClass());
|
||||
|
||||
assertThat(ReflectionTestUtils.getField(persistentEntity, "propertyAccessorFactory"),
|
||||
is(instanceOf(ClassGeneratingPropertyAccessorFactory.class)));
|
||||
assertThat(ReflectionTestUtils.getField(persistentEntity, "propertyAccessorFactory"))
|
||||
.isInstanceOf(ClassGeneratingPropertyAccessorFactory.class);
|
||||
}
|
||||
|
||||
private PersistentPropertyAccessor getPersistentPropertyAccessor(Object bean) {
|
||||
return factory.getPropertyAccessor(mappingContext.getPersistentEntity(bean.getClass()), bean);
|
||||
}
|
||||
|
||||
private PersistentProperty<?> getProperty(Object bean, String name) {
|
||||
private Optional<? extends PersistentProperty<?>> getProperty(Object bean, String name) {
|
||||
|
||||
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = mappingContext
|
||||
.getPersistentEntity(bean.getClass());
|
||||
|
||||
7
src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryEntityTypeTests.java
Normal file → Executable file
7
src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryEntityTypeTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -43,7 +42,7 @@ public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
|
||||
|
||||
Algorithm quickSort = new QuickSort();
|
||||
|
||||
assertThat(getEntityInformation(Algorithm.class).getId(quickSort), is((Object) quickSort.getName()));
|
||||
assertThat(getEntityInformation(Algorithm.class).getId(quickSort)).isEqualTo(quickSort.getName());
|
||||
}
|
||||
|
||||
@Test // DATACMNS-853
|
||||
@@ -51,7 +50,7 @@ public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
|
||||
|
||||
Person jonDoe = new Person("JonDoe");
|
||||
|
||||
assertThat(getEntityInformation(Person.class).getId(jonDoe), is((Object) jonDoe.name));
|
||||
assertThat(getEntityInformation(Person.class).getId(jonDoe)).isEqualTo(jonDoe.name);
|
||||
}
|
||||
|
||||
private EntityInformation<Object, ?> getEntityInformation(Class<?> type) {
|
||||
|
||||
41
src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryTests.java
Normal file → Executable file
41
src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryTests.java
Normal file → Executable file
@@ -16,15 +16,13 @@
|
||||
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -98,11 +96,13 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
|
||||
@Test // DATACMNS-809
|
||||
public void shouldSetAndGetProperty() throws Exception {
|
||||
|
||||
PersistentProperty<?> property = getProperty(bean, propertyName);
|
||||
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
|
||||
assertThat(getProperty(bean, propertyName)).hasValueSatisfying(property -> {
|
||||
|
||||
persistentPropertyAccessor.setProperty(property, "value");
|
||||
assertThat(persistentPropertyAccessor.getProperty(property), is(equalTo((Object) "value")));
|
||||
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
|
||||
|
||||
persistentPropertyAccessor.setProperty(property, Optional.of("value"));
|
||||
assertThat(persistentPropertyAccessor.getProperty(property)).isEqualTo("value");
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-809
|
||||
@@ -112,9 +112,9 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
|
||||
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
|
||||
|
||||
Constructor<?>[] declaredConstructors = persistentPropertyAccessor.getClass().getDeclaredConstructors();
|
||||
assertThat(declaredConstructors.length, is(1));
|
||||
assertThat(declaredConstructors[0].getParameterTypes().length, is(1));
|
||||
assertThat(declaredConstructors[0].getParameterTypes()[0], is(equalTo((Class) expectedConstructorType)));
|
||||
assertThat(declaredConstructors.length).isEqualTo(1);
|
||||
assertThat(declaredConstructors[0].getParameterTypes().length).isEqualTo(1);
|
||||
assertThat(declaredConstructors[0].getParameterTypes()[0]).isEqualTo(expectedConstructorType);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) // DATACMNS-809
|
||||
@@ -125,19 +125,20 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
|
||||
@Test(expected = UnsupportedOperationException.class) // DATACMNS-809
|
||||
public void getPropertyShouldFailOnUnhandledProperty() {
|
||||
|
||||
PersistentProperty<?> property = getProperty(new Dummy(), "dummy");
|
||||
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
|
||||
assertThat(getProperty(new Dummy(), "dummy")).hasValueSatisfying(property -> {
|
||||
|
||||
persistentPropertyAccessor.getProperty(property);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class)//
|
||||
.isThrownBy(() -> getPersistentPropertyAccessor(bean).getProperty(property));
|
||||
});
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class) // DATACMNS-809
|
||||
public void setPropertyShouldFailOnUnhandledProperty() {
|
||||
|
||||
PersistentProperty<?> property = getProperty(new Dummy(), "dummy");
|
||||
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
|
||||
assertThat(getProperty(new Dummy(), "dummy")).hasValueSatisfying(property -> {
|
||||
getPersistentPropertyAccessor(bean).setProperty(property, Optional.empty());
|
||||
});
|
||||
|
||||
persistentPropertyAccessor.setProperty(property, null);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-809
|
||||
@@ -146,15 +147,15 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
|
||||
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = mappingContext
|
||||
.getPersistentEntity(bean.getClass());
|
||||
|
||||
assertThat(ReflectionTestUtils.getField(persistentEntity, "propertyAccessorFactory"),
|
||||
is(instanceOf(ClassGeneratingPropertyAccessorFactory.class)));
|
||||
assertThat(ReflectionTestUtils.getField(persistentEntity, "propertyAccessorFactory"))
|
||||
.isInstanceOf(ClassGeneratingPropertyAccessorFactory.class);
|
||||
}
|
||||
|
||||
private PersistentPropertyAccessor getPersistentPropertyAccessor(Object bean) {
|
||||
return factory.getPropertyAccessor(mappingContext.getPersistentEntity(bean.getClass()), bean);
|
||||
}
|
||||
|
||||
private PersistentProperty<?> getProperty(Object bean, String name) {
|
||||
private Optional<? extends PersistentProperty<?>> getProperty(Object bean, String name) {
|
||||
|
||||
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = mappingContext
|
||||
.getPersistentEntity(bean.getClass());
|
||||
|
||||
45
src/test/java/org/springframework/data/mapping/model/ConvertingPropertyAccessorUnitTests.java
Normal file → Executable file
45
src/test/java/org/springframework/data/mapping/model/ConvertingPropertyAccessorUnitTests.java
Normal file → Executable file
@@ -15,10 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.mapping.PersistentPropertyAccessor;
|
||||
@@ -49,7 +50,7 @@ public class ConvertingPropertyAccessorUnitTests {
|
||||
public void returnsBeanFromDelegate() {
|
||||
|
||||
Object entity = new Entity();
|
||||
assertThat(getAccessor(entity, CONVERSION_SERVICE).getBean(), is(entity));
|
||||
assertThat(getAccessor(entity, CONVERSION_SERVICE).getBean()).isEqualTo(entity);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-596
|
||||
@@ -58,19 +59,20 @@ public class ConvertingPropertyAccessorUnitTests {
|
||||
Entity entity = new Entity();
|
||||
entity.id = 1L;
|
||||
|
||||
ConvertingPropertyAccessor accessor = getAccessor(entity, CONVERSION_SERVICE);
|
||||
|
||||
assertThat(accessor.getProperty(getIdProperty(), String.class), is("1"));
|
||||
assertThat(getIdProperty()).hasValueSatisfying(it -> {
|
||||
assertThat(getAccessor(entity, CONVERSION_SERVICE).getProperty(it, String.class)).hasValue("1");
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-596
|
||||
public void doesNotInvokeConversionForNullValues() {
|
||||
|
||||
ConversionService conversionService = mock(ConversionService.class);
|
||||
ConvertingPropertyAccessor accessor = getAccessor(new Entity(), conversionService);
|
||||
|
||||
assertThat(accessor.getProperty(getIdProperty(), Number.class), is(nullValue()));
|
||||
verify(conversionService, times(0)).convert(1L, Number.class);
|
||||
assertThat(getIdProperty()).hasValueSatisfying(it -> {
|
||||
assertThat(getAccessor(new Entity(), conversionService).getProperty(it, Number.class)).isNotPresent();
|
||||
verify(conversionService, times(0)).convert(1L, Number.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-596
|
||||
@@ -80,30 +82,31 @@ public class ConvertingPropertyAccessorUnitTests {
|
||||
entity.id = 1L;
|
||||
|
||||
ConversionService conversionService = mock(ConversionService.class);
|
||||
ConvertingPropertyAccessor accessor = getAccessor(entity, conversionService);
|
||||
|
||||
assertThat(accessor.getProperty(getIdProperty(), Number.class), is((Number) 1L));
|
||||
verify(conversionService, times(0)).convert(1L, Number.class);
|
||||
assertThat(getIdProperty()).hasValueSatisfying(it -> {
|
||||
assertThat(getAccessor(entity, conversionService).getProperty(it, Number.class)).hasValue(1L);
|
||||
verify(conversionService, times(0)).convert(1L, Number.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-596
|
||||
public void convertsValueOnSetIfTypesDontMatch() {
|
||||
|
||||
Entity entity = new Entity();
|
||||
ConvertingPropertyAccessor accessor = getAccessor(entity, CONVERSION_SERVICE);
|
||||
|
||||
accessor.setProperty(getIdProperty(), "1");
|
||||
|
||||
assertThat(entity.id, is(1L));
|
||||
assertThat(getIdProperty()).hasValueSatisfying(property -> {
|
||||
getAccessor(entity, CONVERSION_SERVICE).setProperty(property, Optional.of("1"));
|
||||
assertThat(entity.id).isEqualTo(1L);
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-596
|
||||
public void doesNotInvokeConversionIfTypeAlreadyMatchesOnSet() {
|
||||
|
||||
ConvertingPropertyAccessor accessor = getAccessor(new Entity(), mock(ConversionService.class));
|
||||
|
||||
accessor.setProperty(getIdProperty(), 1L);
|
||||
verify(mock(ConversionService.class), times(0)).convert(1L, Long.class);
|
||||
assertThat(getIdProperty()).hasValueSatisfying(it -> {
|
||||
getAccessor(new Entity(), mock(ConversionService.class)).setProperty(it, Optional.of(1L));
|
||||
verify(mock(ConversionService.class), times(0)).convert(1L, Long.class);
|
||||
});
|
||||
}
|
||||
|
||||
private static ConvertingPropertyAccessor getAccessor(Object entity, ConversionService conversionService) {
|
||||
@@ -112,7 +115,7 @@ public class ConvertingPropertyAccessorUnitTests {
|
||||
return new ConvertingPropertyAccessor(wrapper, conversionService);
|
||||
}
|
||||
|
||||
private static SamplePersistentProperty getIdProperty() {
|
||||
private static Optional<SamplePersistentProperty> getIdProperty() {
|
||||
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
BasicPersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Entity.class);
|
||||
|
||||
5
src/test/java/org/springframework/data/mapping/model/IdPropertyIdentifierAccessorUnitTests.java
Normal file → Executable file
5
src/test/java/org/springframework/data/mapping/model/IdPropertyIdentifierAccessorUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
@@ -51,7 +50,7 @@ public class IdPropertyIdentifierAccessorUnitTests {
|
||||
IdentifierAccessor accessor = new IdPropertyIdentifierAccessor(
|
||||
mappingContext.getPersistentEntity(SampleWithId.class), sample);
|
||||
|
||||
assertThat(accessor.getIdentifier(), is((Object) sample.id));
|
||||
assertThat(accessor.getIdentifier()).isEqualTo(sample.id);
|
||||
}
|
||||
|
||||
static class Sample {}
|
||||
|
||||
60
src/test/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProviderUnitTests.java
Normal file → Executable file
60
src/test/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProviderUnitTests.java
Normal file → Executable file
@@ -15,20 +15,19 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.PreferredConstructor;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.mapping.model.PersistentEntityParameterValueProviderUnitTests.Outer.Inner;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
@@ -41,13 +40,8 @@ import org.springframework.data.util.ClassTypeInformation;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PersistentEntityParameterValueProviderUnitTests<P extends PersistentProperty<P>> {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Mock
|
||||
PropertyValueProvider<P> propertyValueProvider;
|
||||
@Mock
|
||||
P property;
|
||||
@Mock PropertyValueProvider<P> propertyValueProvider;
|
||||
@Mock P property;
|
||||
|
||||
@Test // DATACMNS-134
|
||||
public void usesParentObjectAsImplicitFirstConstructorArgument() {
|
||||
@@ -55,35 +49,41 @@ public class PersistentEntityParameterValueProviderUnitTests<P extends Persisten
|
||||
Object outer = new Outer();
|
||||
|
||||
PersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(ClassTypeInformation.from(Inner.class)) {
|
||||
|
||||
@Override
|
||||
public P getPersistentProperty(String name) {
|
||||
return property;
|
||||
public Optional<P> getPersistentProperty(String name) {
|
||||
return Optional.ofNullable(property);
|
||||
}
|
||||
};
|
||||
PreferredConstructor<Inner, P> constructor = entity.getPersistenceConstructor();
|
||||
Iterator<Parameter<Object, P>> iterator = constructor.getParameters().iterator();
|
||||
|
||||
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<P>(entity, propertyValueProvider,
|
||||
outer);
|
||||
assertThat(provider.getParameterValue(iterator.next()), is(outer));
|
||||
assertThat(provider.getParameterValue(iterator.next()), is(nullValue()));
|
||||
assertThat(iterator.hasNext(), is(false));
|
||||
doReturn(Optional.empty()).when(propertyValueProvider).getPropertyValue(any());
|
||||
|
||||
assertThat(entity.getPersistenceConstructor()).hasValueSatisfying(constructor -> {
|
||||
|
||||
Iterator<Parameter<Object, P>> iterator = constructor.getParameters().iterator();
|
||||
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<>(entity, propertyValueProvider,
|
||||
Optional.of(outer));
|
||||
|
||||
assertThat(provider.getParameterValue(iterator.next())).hasValue(outer);
|
||||
assertThat(provider.getParameterValue(iterator.next())).isNotPresent();
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsPropertyIfNameDoesNotMatch() {
|
||||
|
||||
PersistentEntity<Entity, P> entity = new BasicPersistentEntity<Entity, P>(ClassTypeInformation.from(Entity.class));
|
||||
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<P>(entity, propertyValueProvider,
|
||||
property);
|
||||
PersistentEntity<Entity, P> entity = new BasicPersistentEntity<>(ClassTypeInformation.from(Entity.class));
|
||||
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<>(entity, propertyValueProvider,
|
||||
Optional.of(property));
|
||||
|
||||
PreferredConstructor<Entity, P> constructor = entity.getPersistenceConstructor();
|
||||
assertThat(entity.getPersistenceConstructor()).hasValueSatisfying(constructor -> {
|
||||
|
||||
exception.expect(MappingException.class);
|
||||
exception.expectMessage("bar");
|
||||
exception.expectMessage(Entity.class.getName());
|
||||
|
||||
provider.getParameterValue(constructor.getParameters().iterator().next());
|
||||
assertThatExceptionOfType(MappingException.class)//
|
||||
.isThrownBy(() -> provider.getParameterValue(constructor.getParameters().iterator().next()))//
|
||||
.withMessageContaining("bar")//
|
||||
.withMessageContaining(Entity.class.getName());
|
||||
});
|
||||
}
|
||||
|
||||
static class Outer {
|
||||
|
||||
5
src/test/java/org/springframework/data/mapping/model/SnakeCaseFieldNamingStrategyUnitTests.java
Normal file → Executable file
5
src/test/java/org/springframework/data/mapping/model/SnakeCaseFieldNamingStrategyUnitTests.java
Normal file → Executable file
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -51,6 +50,6 @@ public class SnakeCaseFieldNamingStrategyUnitTests {
|
||||
private void assertFieldNameForPropertyName(String propertyName, String fieldName) {
|
||||
|
||||
when(property.getName()).thenReturn(propertyName);
|
||||
assertThat(strategy.getFieldName(property), is(fieldName));
|
||||
assertThat(strategy.getFieldName(property)).isEqualTo(fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
19
src/test/java/org/springframework/data/mapping/model/SpelExpressionParameterProviderUnitTests.java
Normal file → Executable file
19
src/test/java/org/springframework/data/mapping/model/SpelExpressionParameterProviderUnitTests.java
Normal file → Executable file
@@ -15,10 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -35,12 +36,9 @@ import org.springframework.data.mapping.model.AbstractPersistentPropertyUnitTest
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SpelExpressionParameterProviderUnitTests {
|
||||
|
||||
@Mock
|
||||
SpELExpressionEvaluator evaluator;
|
||||
@Mock
|
||||
ParameterValueProvider<SamplePersistentProperty> delegate;
|
||||
@Mock
|
||||
ConversionService conversionService;
|
||||
@Mock SpELExpressionEvaluator evaluator;
|
||||
@Mock ParameterValueProvider<SamplePersistentProperty> delegate;
|
||||
@Mock ConversionService conversionService;
|
||||
|
||||
SpELExpressionParameterValueProvider<SamplePersistentProperty> provider;
|
||||
|
||||
@@ -54,6 +52,7 @@ public class SpelExpressionParameterProviderUnitTests {
|
||||
|
||||
parameter = mock(Parameter.class);
|
||||
when(parameter.hasSpelExpression()).thenReturn(true);
|
||||
when(parameter.getSpelExpression()).thenReturn(Optional.empty());
|
||||
when(parameter.getRawType()).thenReturn(Object.class);
|
||||
}
|
||||
|
||||
@@ -72,7 +71,7 @@ public class SpelExpressionParameterProviderUnitTests {
|
||||
@Test
|
||||
public void evaluatesSpELExpression() {
|
||||
|
||||
when(parameter.getSpelExpression()).thenReturn("expression");
|
||||
when(parameter.getSpelExpression()).thenReturn(Optional.of("expression"));
|
||||
|
||||
provider.getParameterValue(parameter);
|
||||
verify(delegate, times(0)).getParameterValue(parameter);
|
||||
@@ -114,7 +113,7 @@ public class SpelExpressionParameterProviderUnitTests {
|
||||
when(evaluator.evaluate(Mockito.anyString())).thenReturn("value");
|
||||
|
||||
Object result = provider.getParameterValue(parameter);
|
||||
assertThat(result, is((Object) "FOO"));
|
||||
assertThat(result).isEqualTo("FOO");
|
||||
verify(delegate, times(0)).getParameterValue(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.springframework.data.annotation.AccessType.Type;
|
||||
*/
|
||||
public class TypeInOtherPackage {
|
||||
|
||||
private String privateField;
|
||||
@SuppressWarnings("unused") private String privateField;
|
||||
String packageDefaultField;
|
||||
protected String protectedField;
|
||||
public String publicField;
|
||||
@@ -38,10 +38,12 @@ public class TypeInOtherPackage {
|
||||
|
||||
@AccessType(Type.PROPERTY) private String publicProperty;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private String getPrivateProperty() {
|
||||
return privateProperty;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void setPrivateProperty(String privateProperty) {
|
||||
this.privateProperty = privateProperty;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user