DATACMNS-867 - Second draft.

This commit is contained in:
Oliver Gierke
2016-06-21 16:52:28 +02:00
parent 57ed50a730
commit d4811e29d9
222 changed files with 2297 additions and 2138 deletions

View File

@@ -40,7 +40,7 @@ public class MappingMetadataTests {
@Test
public void testPojoWithId() {
PersistentEntity<?, SamplePersistentProperty> person = ctx.getPersistentEntity(PersonWithId.class);
PersistentEntity<?, SamplePersistentProperty> person = ctx.getRequiredPersistentEntity(PersonWithId.class);
assertThat(person.getIdProperty()).hasValueSatisfying(it -> assertThat(it.getType()).isEqualTo(String.class));
}
@@ -48,7 +48,7 @@ public class MappingMetadataTests {
@Test
public void testAssociations() {
PersistentEntity<?, SamplePersistentProperty> person = ctx.getPersistentEntity(PersonWithChildren.class);
PersistentEntity<?, SamplePersistentProperty> person = ctx.getRequiredPersistentEntity(PersonWithChildren.class);
person.doWithAssociations((AssociationHandler<SamplePersistentProperty>) association -> assertThat(
association.getInverse().getComponentType()).isEqualTo(Child.class));

View File

@@ -18,10 +18,7 @@ package org.springframework.data.mapping.context;
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;
@@ -29,6 +26,7 @@ import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
@@ -64,7 +62,8 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
public void createsPersistentEntityForInterfaceCorrectly() {
SampleMappingContext context = new SampleMappingContext();
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(InterfaceOnly.class);
PersistentEntity<Object, SamplePersistentProperty> entity = context
.getRequiredPersistentEntity(InterfaceOnly.class);
assertThat(entity.getIdProperty()).isNotNull();
}
@@ -74,25 +73,19 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
final DummyMappingContext context = new DummyMappingContext();
Thread a = new Thread(new Runnable() {
public void run() {
context.getPersistentEntity(Person.class);
}
});
Thread a = new Thread(() -> context.getPersistentEntity(Person.class));
Thread b = new Thread(new Runnable() {
public void run() {
PersistentEntity<Object, T> entity = context.getPersistentEntity(Person.class);
PersistentEntity<Object, T> entity = context.getRequiredPersistentEntity(Person.class);
entity.doWithProperties(new PropertyHandler<T>() {
public void doWithPersistentProperty(T persistentProperty) {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
entity.doWithProperties((PropertyHandler<T>) persistentProperty -> {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}
@@ -116,13 +109,13 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected T createPersistentProperty(Optional<Field> field, PropertyDescriptor descriptor,
final BasicPersistentEntity<Object, T> owner, SimpleTypeHolder simpleTypeHolder) {
protected T createPersistentProperty(Property property, BasicPersistentEntity<Object, T> owner,
SimpleTypeHolder simpleTypeHolder) {
PersistentProperty prop = mock(PersistentProperty.class);
when(prop.getTypeInformation()).thenReturn(owner.getTypeInformation());
when(prop.getName()).thenReturn(field.map(Field::getName).orElse(descriptor.getName()));
when(prop.getName()).thenReturn(property.getName());
when(prop.getPersistentEntityType()).thenReturn(Collections.EMPTY_SET);
try {

View File

@@ -111,7 +111,7 @@ public class AbstractMappingContextUnitTests {
public void returnsNullPersistentEntityForSimpleTypes() {
SampleMappingContext context = new SampleMappingContext();
assertThat(context.getPersistentEntity(String.class)).isNull();
assertThat(context.getPersistentEntity(String.class)).isEmpty();
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-214
@@ -130,7 +130,8 @@ public class AbstractMappingContextUnitTests {
SampleMappingContext mappingContext = new SampleMappingContext();
mappingContext.initialize();
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Sample.class);
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext
.getRequiredPersistentEntity(Sample.class);
assertThat(entity.getPersistentProperty("metaClass")).isNotPresent();
}
@@ -138,7 +139,8 @@ public class AbstractMappingContextUnitTests {
public void usesMostConcreteProperty() {
SampleMappingContext mappingContext = new SampleMappingContext();
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Extension.class);
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext
.getRequiredPersistentEntity(Extension.class);
assertThat(entity.getPersistentProperty("foo")).hasValueSatisfying(it -> {
assertThat(it.isIdProperty()).isTrue();
@@ -150,16 +152,14 @@ public class AbstractMappingContextUnitTests {
public void returnsEntityForComponentType() {
SampleMappingContext mappingContext = new SampleMappingContext();
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Sample.class);
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext
.getRequiredPersistentEntity(Sample.class);
assertThat(entity.getPersistentProperty("persons")).hasValueSatisfying(it -> {
PersistentEntity<Object, SamplePersistentProperty> propertyEntity = mappingContext.getPersistentEntity(it);
assertThat(propertyEntity).isNotNull();
assertThat(propertyEntity.getType()).isEqualTo(Person.class);
assertThat(mappingContext.getPersistentEntity(it)).hasValueSatisfying(inner -> {
assertThat(inner.getType()).isEqualTo(Person.class);
});
});
}
@Test // DATACMNS-380
@@ -197,7 +197,7 @@ public class AbstractMappingContextUnitTests {
public void shouldReturnNullForSimpleTypesIfInStrictIsEnabled() {
context.setStrict(true);
assertThat(context.getPersistentEntity(Integer.class)).isNull();
assertThat(context.getPersistentEntity(Integer.class)).isEmpty();
}
@Test // DATACMNS-462

View File

@@ -16,7 +16,7 @@
package org.springframework.data.mapping.context;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Matchers.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;

View File

@@ -27,8 +27,6 @@ import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory.PropertyIsNullIsNewStrategy;
import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory.PropertyIsNullOrZeroNumberIsNewStrategy;
import org.springframework.data.support.IsNewStrategy;
import org.springframework.data.support.IsNewStrategyFactory;
@@ -55,7 +53,6 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
public void returnsPropertyIsNullOrZeroIsNewStrategyForVersionedEntity() {
IsNewStrategy strategy = factory.getIsNewStrategy(VersionedEntity.class);
assertThat(strategy).isInstanceOf(PropertyIsNullOrZeroNumberIsNewStrategy.class);
Optional<VersionedEntity> entity = Optional.of(new VersionedEntity());
assertThat(strategy.isNew(entity)).isTrue();
@@ -74,7 +71,6 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
public void returnsPropertyIsNullOrZeroIsNewStrategyForPrimitiveVersionedEntity() {
IsNewStrategy strategy = factory.getIsNewStrategy(VersionedEntity.class);
assertThat(strategy).isInstanceOf(PropertyIsNullOrZeroNumberIsNewStrategy.class);
Optional<VersionedEntity> entity = Optional.of(new VersionedEntity());
assertThat(strategy.isNew(entity)).isTrue();
@@ -90,7 +86,6 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
public void returnsPropertyIsNullIsNewStrategyForEntity() {
IsNewStrategy strategy = factory.getIsNewStrategy(Entity.class);
assertThat(strategy).isInstanceOf(PropertyIsNullIsNewStrategy.class);
Optional<Entity> entity = Optional.of(new Entity());
assertThat(strategy.isNew(entity)).isTrue();

View File

@@ -52,10 +52,10 @@ public class PersistentEntitiesUnitTests {
new PersistentEntities(Arrays.asList(first, second)).getPersistentEntity(Sample.class);
verify(first, times(1)).hasPersistentEntityFor(Sample.class);
verify(first, times(0)).getPersistentEntity(Sample.class);
verify(first, times(0)).getRequiredPersistentEntity(Sample.class);
verify(second, times(1)).hasPersistentEntityFor(Sample.class);
verify(second, times(1)).getPersistentEntity(Sample.class);
verify(second, times(1)).getRequiredPersistentEntity(Sample.class);
}
@Test // DATACMNS-458
@@ -67,10 +67,14 @@ public class PersistentEntitiesUnitTests {
PersistentEntities entities = new PersistentEntities(Arrays.asList(context));
assertThat(entities.getPersistentEntity(Sample.class)).isNotNull();
assertThat(entities.getPersistentEntity(Object.class)).isNull();
assertThat(entities.getPersistentEntity(Sample.class)).isPresent();
assertThat(entities.getPersistentEntity(Object.class)).isNotPresent();
assertThat(entities.getManagedTypes()).contains(ClassTypeInformation.from(Sample.class));
assertThat(entities).contains(entities.getPersistentEntity(Sample.class));
assertThat(entities.getPersistentEntity(Sample.class)).hasValueSatisfying(it -> {
assertThat(entities).contains(it);
});
}
static class Sample {

View File

@@ -1,10 +1,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.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.TypeInformation;
@@ -19,9 +16,9 @@ public class SampleMappingContext
}
@Override
protected SamplePersistentProperty createPersistentProperty(Optional<Field> field, PropertyDescriptor descriptor,
protected SamplePersistentProperty createPersistentProperty(Property property,
BasicPersistentEntity<Object, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
return new SamplePersistentProperty(field, descriptor, owner, simpleTypeHolder);
return new SamplePersistentProperty(property, owner, simpleTypeHolder);
}
}

View File

@@ -15,24 +15,21 @@
*/
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;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
public class SamplePersistentProperty extends AnnotationBasedPersistentProperty<SamplePersistentProperty> {
public SamplePersistentProperty(Optional<Field> field, PropertyDescriptor propertyDescriptor,
BasicPersistentEntity<?, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
super(field, propertyDescriptor, owner, simpleTypeHolder);
public SamplePersistentProperty(Property property, BasicPersistentEntity<?, SamplePersistentProperty> owner,
SimpleTypeHolder simpleTypeHolder) {
super(property, owner, simpleTypeHolder);
}
@Override
protected Association<SamplePersistentProperty> createAssociation() {
return new Association<SamplePersistentProperty>(this, null);
return new Association<>(this, null);
}
}

View File

@@ -54,53 +54,35 @@ public class AbstractPersistentPropertyUnitTests {
public void setUp() {
typeInfo = ClassTypeInformation.from(TestClassComplex.class);
entity = new BasicPersistentEntity<TestClassComplex, SamplePersistentProperty>(typeInfo);
entity = new BasicPersistentEntity<>(typeInfo);
typeHolder = new SimpleTypeHolder();
}
@Test // DATACMNS-68
public void discoversComponentTypeCorrectly() throws Exception {
Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet");
SamplePersistentProperty property = new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
property.getComponentType();
assertThat(getProperty(TestClassComplex.class, "testClassSet").getComponentType()).isEqualTo(Object.class);
}
@Test // DATACMNS-101
public void returnsNestedEntityTypeCorrectly() {
Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet");
SamplePersistentProperty property = new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
assertThat(property.getPersistentEntityType().iterator().hasNext()).isFalse();
assertThat(getProperty(TestClassComplex.class, "testClassSet").getPersistentEntityType()).isEmpty();
}
@Test // DATACMNS-132
public void isEntityWorksForUntypedMaps() throws Exception {
Field field = ReflectionUtils.findField(TestClassComplex.class, "map");
SamplePersistentProperty property = new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
assertThat(property.isEntity()).isFalse();
assertThat(getProperty(TestClassComplex.class, "map").isEntity()).isFalse();
}
@Test // DATACMNS-132
public void isEntityWorksForUntypedCollection() throws Exception {
Field field = ReflectionUtils.findField(TestClassComplex.class, "collection");
SamplePersistentProperty property = new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
assertThat(property.isEntity()).isFalse();
assertThat(getProperty(TestClassComplex.class, "collection").isEntity()).isFalse();
}
@Test // DATACMNS-121
public void considersPropertiesEqualIfFieldEquals() {
Field first = ReflectionUtils.findField(FirstConcrete.class, "genericField");
Field second = ReflectionUtils.findField(SecondConcrete.class, "genericField");
SamplePersistentProperty firstProperty = new SamplePersistentProperty(Optional.of(first), null, entity, typeHolder);
SamplePersistentProperty secondProperty = new SamplePersistentProperty(Optional.of(second), null, entity,
typeHolder);
SamplePersistentProperty firstProperty = getProperty(FirstConcrete.class, "genericField");
SamplePersistentProperty secondProperty = getProperty(SecondConcrete.class, "genericField");
assertThat(firstProperty).isEqualTo(secondProperty);
assertThat(firstProperty.hashCode()).isEqualTo(secondProperty.hashCode());
@@ -108,67 +90,54 @@ public class AbstractPersistentPropertyUnitTests {
@Test // DATACMNS-180
public void doesNotConsiderJavaTransientFieldsTransient() {
Field transientField = ReflectionUtils.findField(TestClassComplex.class, "transientField");
PersistentProperty<?> property = new SamplePersistentProperty(Optional.of(transientField), null, entity,
typeHolder);
assertThat(property.isTransient()).isFalse();
assertThat(getProperty(TestClassComplex.class, "transientField").isTransient()).isFalse();
}
@Test // DATACMNS-206
public void findsSimpleGettersAndASetters() {
Field field = ReflectionUtils.findField(AccessorTestClass.class, "id");
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field),
getPropertyDescriptor(AccessorTestClass.class, "id"), entity, typeHolder);
SamplePersistentProperty property = getProperty(AccessorTestClass.class, "id");
assertThat(property.getGetter()).isNotNull();
assertThat(property.getSetter()).isNotNull();
assertThat(property.getGetter()).isPresent();
assertThat(property.getSetter()).isPresent();
}
@Test // DATACMNS-206
public void doesNotUseInvalidGettersAndASetters() {
Field field = ReflectionUtils.findField(AccessorTestClass.class, "anotherId");
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field),
getPropertyDescriptor(AccessorTestClass.class, "anotherId"), entity, typeHolder);
SamplePersistentProperty property = getProperty(AccessorTestClass.class, "anotherId");
assertThat(property.getGetter()).isNull();
assertThat(property.getSetter()).isNull();
assertThat(property.getGetter()).isNotPresent();
assertThat(property.getSetter()).isNotPresent();
}
@Test // DATACMNS-206
public void usesCustomGetter() {
Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetAnotherId");
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field),
getPropertyDescriptor(AccessorTestClass.class, "yetAnotherId"), entity, typeHolder);
SamplePersistentProperty property = getProperty(AccessorTestClass.class, "yetAnotherId");
assertThat(property.getGetter()).isNotNull();
assertThat(property.getSetter()).isNull();
assertThat(property.getGetter()).isPresent();
assertThat(property.getSetter()).isNotPresent();
}
@Test // DATACMNS-206
public void usesCustomSetter() {
Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetYetAnotherId");
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field),
getPropertyDescriptor(AccessorTestClass.class, "yetYetAnotherId"), entity, typeHolder);
SamplePersistentProperty property = getProperty(AccessorTestClass.class, "yetYetAnotherId");
assertThat(property.getGetter()).isNull();
assertThat(property.getSetter()).isNotNull();
assertThat(property.getGetter()).isNotPresent();
assertThat(property.getSetter()).isPresent();
}
@Test // DATACMNS-206
public void returnsNullGetterAndSetterIfNoPropertyDescriptorGiven() {
public void doesNotDiscoverGetterAndSetterIfNoPropertyDescriptorGiven() {
Field field = ReflectionUtils.findField(AccessorTestClass.class, "id");
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Optional.of(field), null,
entity, typeHolder);
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(Property.of(field),
getEntity(AccessorTestClass.class), typeHolder);
assertThat(property.getGetter()).isNull();
assertThat(property.getSetter()).isNull();
assertThat(property.getGetter()).isNotPresent();
assertThat(property.getSetter()).isNotPresent();
}
@Test // DATACMNS-337
@@ -223,25 +192,31 @@ public class AbstractPersistentPropertyUnitTests {
assertThat(property.isEntity()).isFalse();
}
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
BasicPersistentEntity<T, SamplePersistentProperty> entity = new BasicPersistentEntity<T, SamplePersistentProperty>(
ClassTypeInformation.from(type));
Field field = ReflectionUtils.findField(type, name);
return new SamplePersistentProperty(Optional.of(field), null, entity, typeHolder);
private <T> BasicPersistentEntity<T, SamplePersistentProperty> getEntity(Class<T> type) {
return new BasicPersistentEntity<>(ClassTypeInformation.from(type));
}
private static PropertyDescriptor getPropertyDescriptor(Class<?> type, String propertyName) {
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
Optional<Field> field = Optional.ofNullable(ReflectionUtils.findField(type, name));
Property property = field.map(it -> Property.of(it, getPropertyDescriptor(type, name)))
.orElseGet(() -> Property.of(getPropertyDescriptor(type, name).orElseThrow(
() -> new IllegalArgumentException(String.format("Couldn't find property %s on %s!", name, type)))));
return new SamplePersistentProperty(property, getEntity(type), typeHolder);
}
private static Optional<PropertyDescriptor> getPropertyDescriptor(Class<?> type, String propertyName) {
try {
return Arrays.stream(Introspector.getBeanInfo(type).getPropertyDescriptors())//
.filter(it -> it.getName().equals(propertyName))//
.findFirst().orElse(null);
.findFirst();
} catch (IntrospectionException e) {
return null;
} catch (IntrospectionException o_O) {
throw new RuntimeException(o_O);
}
}
@@ -311,9 +286,9 @@ public class AbstractPersistentPropertyUnitTests {
class SamplePersistentProperty extends AbstractPersistentProperty<SamplePersistentProperty> {
public SamplePersistentProperty(Optional<Field> field, PropertyDescriptor propertyDescriptor,
PersistentEntity<?, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
super(field, propertyDescriptor, owner, simpleTypeHolder);
public SamplePersistentProperty(Property property, PersistentEntity<?, SamplePersistentProperty> owner,
SimpleTypeHolder simpleTypeHolder) {
super(property, owner, simpleTypeHolder);
}
public boolean isIdProperty() {

View File

@@ -55,7 +55,7 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
public void setUp() {
context = new SampleMappingContext();
entity = context.getPersistentEntity(Sample.class);
entity = context.getRequiredPersistentEntity(Sample.class);
}
@Test // DATACMNS-269
@@ -215,7 +215,7 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
}
private SamplePersistentProperty getProperty(Class<?> type, String name) {
return context.getPersistentEntity(type).getPersistentProperty(name).orElse(null);
return context.getRequiredPersistentEntity(type).getPersistentProperty(name).orElse(null);
}
static class Sample {

View File

@@ -16,9 +16,6 @@
package org.springframework.data.mapping.model;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assume.*;
import static org.mockito.Mockito.*;
import java.lang.annotation.Retention;
@@ -28,8 +25,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -42,6 +37,7 @@ import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mapping.Alias;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentEntitySpec;
@@ -86,25 +82,22 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
public void returnsNullForTypeAliasIfNoneConfigured() {
PersistentEntity<Entity, T> entity = createEntity(Entity.class);
assertThat(entity.getTypeAlias()).isNotPresent();
assertThat(entity.getTypeAlias()).isEqualTo(Alias.NONE);
}
@Test
public void returnsTypeAliasIfAnnotated() {
PersistentEntity<AliasedEntity, T> entity = createEntity(AliasedEntity.class);
assertThat(entity.getTypeAlias()).isEqualTo("foo");
assertThat(entity.getTypeAlias()).isEqualTo(Alias.of("foo"));
}
@Test // DATACMNS-50
@SuppressWarnings("unchecked")
public void considersComparatorForPropertyOrder() {
BasicPersistentEntity<Person, T> entity = createEntity(Person.class, new Comparator<T>() {
public int compare(T o1, T o2) {
return o1.getName().compareTo(o2.getName());
}
});
BasicPersistentEntity<Person, T> entity = createEntity(Person.class,
Comparator.comparing(PersistentProperty::getName));
T lastName = (T) Mockito.mock(PersistentProperty.class);
when(lastName.getName()).thenReturn("lastName");
@@ -124,20 +117,21 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
assertThat(properties).hasSize(3);
Iterator<T> iterator = properties.iterator();
assertThat(iterator.next()).isEqualTo(entity.getPersistentProperty("firstName"));
assertThat(iterator.next()).isEqualTo(entity.getPersistentProperty("lastName"));
assertThat(iterator.next()).isEqualTo(entity.getPersistentProperty("ssn"));
assertThat(entity.getPersistentProperty("firstName")).hasValue(iterator.next());
assertThat(entity.getPersistentProperty("lastName")).hasValue(iterator.next());
assertThat(entity.getPersistentProperty("ssn")).hasValue(iterator.next());
}
@Test // DATACMNS-186
public void addingAndIdPropertySetsIdPropertyInternally() {
MutablePersistentEntity<Person, T> entity = createEntity(Person.class);
assertThat(entity.getIdProperty()).isNull();
assertThat(entity.getIdProperty()).isNotPresent();
when(property.isIdProperty()).thenReturn(true);
entity.addPersistentProperty(property);
assertThat(entity.getIdProperty()).isEqualTo(property);
assertThat(entity.getIdProperty()).hasValue(property);
}
@Test // DATACMNS-186
@@ -157,7 +151,7 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
public void detectsPropertyWithAnnotation() {
SampleMappingContext context = new SampleMappingContext();
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(Entity.class);
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Entity.class);
Optional<SamplePersistentProperty> property = entity.getPersistentProperty(LastModifiedBy.class);
@@ -192,15 +186,13 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
@Test // DATACMNS-809
public void returnsGeneratedPropertyAccessorForPropertyAccessor() {
assumeThat(System.getProperty("java.version"), not(CoreMatchers.startsWith("1.6")));
SampleMappingContext context = new SampleMappingContext();
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(Entity.class);
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Entity.class);
Entity value = new Entity();
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
assertThat(accessor).isNotEqualTo(instanceOf(BeanWrapper.class));
assertThat(accessor).isNotInstanceOf(BeanWrapper.class);
assertThat(accessor.getClass().getName()).contains("_Accessor_");
assertThat(accessor.getBean()).isEqualTo(value);
}
@@ -209,7 +201,7 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
public void rejectsNullBeanForPropertyAccessor() {
SampleMappingContext context = new SampleMappingContext();
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(Entity.class);
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Entity.class);
entity.getPropertyAccessor(null);
}
@@ -218,7 +210,7 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
public void rejectsNonMatchingBeanForPropertyAccessor() {
SampleMappingContext context = new SampleMappingContext();
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(Entity.class);
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Entity.class);
entity.getPropertyAccessor("foo");
}
@@ -227,7 +219,7 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
public void supportsSubtypeInstancesOnPropertyAccessorLookup() {
SampleMappingContext context = new SampleMappingContext();
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(Entity.class);
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Entity.class);
assertThat(entity.getPropertyAccessor(new Subtype())).isNotNull();
}
@@ -237,7 +229,7 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
PersistentEntity<AliasEntityUsingComposedAnnotation, T> entity = createEntity(
AliasEntityUsingComposedAnnotation.class);
assertThat(entity.getTypeAlias()).isEqualTo("bar");
assertThat(entity.getTypeAlias()).isEqualTo(Alias.of("bar"));
}
@Test // DATACMNS-866

View File

@@ -65,7 +65,8 @@ public class ClassGeneratingPropertyAccessorFactoryDatatypeTests {
public static List<Object[]> parameters() throws Exception {
List<Object[]> parameters = new ArrayList<>();
List<Class<?>> types = Arrays.asList(FieldAccess.class, PropertyAccess.class, PrivateFinalFieldAccess.class, PrivateFinalPropertyAccess.class);
List<Class<?>> types = Arrays.asList(FieldAccess.class, PropertyAccess.class, PrivateFinalFieldAccess.class,
PrivateFinalPropertyAccess.class);
parameters.addAll(parameters(types, "primitiveInteger", Integer.valueOf(1)));
parameters.addAll(parameters(types, "primitiveIntegerArray", new int[] { 1, 2, 3 }));
@@ -113,8 +114,8 @@ public class ClassGeneratingPropertyAccessorFactoryDatatypeTests {
Constructor<?>[] constructors = type.getDeclaredConstructors();
constructors[0].setAccessible(true);
parameters
.add(new Object[] { constructors[0].newInstance(), propertyName, value, type.getSimpleName() + "/" + propertyName });
parameters.add(new Object[] { constructors[0].newInstance(), propertyName, value,
type.getSimpleName() + "/" + propertyName });
}
return parameters;
@@ -136,20 +137,20 @@ public class ClassGeneratingPropertyAccessorFactoryDatatypeTests {
public void shouldUseClassPropertyAccessorFactory() throws Exception {
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = mappingContext
.getPersistentEntity(bean.getClass());
.getRequiredPersistentEntity(bean.getClass());
assertThat(ReflectionTestUtils.getField(persistentEntity, "propertyAccessorFactory"))
.isInstanceOf(ClassGeneratingPropertyAccessorFactory.class);
}
private PersistentPropertyAccessor getPersistentPropertyAccessor(Object bean) {
return factory.getPropertyAccessor(mappingContext.getPersistentEntity(bean.getClass()), bean);
return factory.getPropertyAccessor(mappingContext.getRequiredPersistentEntity(bean.getClass()), bean);
}
private Optional<? extends PersistentProperty<?>> getProperty(Object bean, String name) {
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = mappingContext
.getPersistentEntity(bean.getClass());
.getRequiredPersistentEntity(bean.getClass());
return persistentEntity.getPersistentProperty(name);
}

View File

@@ -42,7 +42,7 @@ public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
Algorithm quickSort = new QuickSort();
assertThat(getEntityInformation(Algorithm.class).getId(quickSort)).isEqualTo(quickSort.getName());
assertThat(getEntityInformation(Algorithm.class).getId(quickSort)).hasValue(quickSort.getName());
}
@Test // DATACMNS-853
@@ -50,12 +50,12 @@ public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
Person jonDoe = new Person("JonDoe");
assertThat(getEntityInformation(Person.class).getId(jonDoe)).isEqualTo(jonDoe.name);
assertThat(getEntityInformation(Person.class).getId(jonDoe)).hasValue(jonDoe.name);
}
private EntityInformation<Object, ?> getEntityInformation(Class<?> type) {
private EntityInformation<Object, Serializable> getEntityInformation(Class<?> type) {
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(type);
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getRequiredPersistentEntity(type);
return new PersistentEntityInformation<Object, Serializable>(entity);
}

View File

@@ -16,7 +16,8 @@
package org.springframework.data.mapping.model;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
@@ -28,6 +29,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.AccessType.Type;
import org.springframework.data.mapping.PersistentProperty;
@@ -68,7 +70,8 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
"privateProperty", "packageDefaultProperty", "protectedProperty", "publicProperty", "syntheticProperty");
parameters.addAll(parameters(new InnerPrivateType(), propertyNames, Object.class));
parameters.addAll(parameters(new InnerTypeWithPrivateAncestor(), propertyNames, InnerTypeWithPrivateAncestor.class));
parameters
.addAll(parameters(new InnerTypeWithPrivateAncestor(), propertyNames, InnerTypeWithPrivateAncestor.class));
parameters.addAll(parameters(new InnerPackageDefaultType(), propertyNames, InnerPackageDefaultType.class));
parameters.addAll(parameters(new InnerProtectedType(), propertyNames, InnerProtectedType.class));
parameters.addAll(parameters(new InnerPublicType(), propertyNames, InnerPublicType.class));
@@ -119,10 +122,10 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
@Test(expected = IllegalArgumentException.class) // DATACMNS-809
public void shouldFailOnNullBean() {
factory.getPropertyAccessor(mappingContext.getPersistentEntity(bean.getClass()), null);
factory.getPropertyAccessor(mappingContext.getRequiredPersistentEntity(bean.getClass()), null);
}
@Test(expected = UnsupportedOperationException.class) // DATACMNS-809
@Test // DATACMNS-809
public void getPropertyShouldFailOnUnhandledProperty() {
assertThat(getProperty(new Dummy(), "dummy")).hasValueSatisfying(property -> {
@@ -132,33 +135,34 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
});
}
@Test(expected = UnsupportedOperationException.class) // DATACMNS-809
@Test // DATACMNS-809
public void setPropertyShouldFailOnUnhandledProperty() {
assertThat(getProperty(new Dummy(), "dummy")).hasValueSatisfying(property -> {
getPersistentPropertyAccessor(bean).setProperty(property, Optional.empty());
});
assertThatExceptionOfType(UnsupportedOperationException.class)//
.isThrownBy(() -> getPersistentPropertyAccessor(bean).setProperty(property, Optional.empty()));
});
}
@Test // DATACMNS-809
public void shouldUseClassPropertyAccessorFactory() throws Exception {
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = mappingContext
.getPersistentEntity(bean.getClass());
.getRequiredPersistentEntity(bean.getClass());
assertThat(ReflectionTestUtils.getField(persistentEntity, "propertyAccessorFactory"))
.isInstanceOf(ClassGeneratingPropertyAccessorFactory.class);
}
private PersistentPropertyAccessor getPersistentPropertyAccessor(Object bean) {
return factory.getPropertyAccessor(mappingContext.getPersistentEntity(bean.getClass()), bean);
return factory.getPropertyAccessor(mappingContext.getRequiredPersistentEntity(bean.getClass()), bean);
}
private Optional<? extends PersistentProperty<?>> getProperty(Object bean, String name) {
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = mappingContext
.getPersistentEntity(bean.getClass());
.getRequiredPersistentEntity(bean.getClass());
return persistentEntity.getPersistentProperty(name);
}

View File

@@ -43,7 +43,7 @@ public class ConvertingPropertyAccessorUnitTests {
@Test(expected = IllegalArgumentException.class) // DATACMNS-596
public void rejectsNullConversionService() {
new ConvertingPropertyAccessor(new BeanWrapper<Object>(new Object()), null);
new ConvertingPropertyAccessor(new BeanWrapper<>(new Object()), null);
}
@Test // DATACMNS-596
@@ -111,14 +111,15 @@ public class ConvertingPropertyAccessorUnitTests {
private static ConvertingPropertyAccessor getAccessor(Object entity, ConversionService conversionService) {
PersistentPropertyAccessor wrapper = new BeanWrapper<Object>(entity);
PersistentPropertyAccessor wrapper = new BeanWrapper<>(entity);
return new ConvertingPropertyAccessor(wrapper, conversionService);
}
private static Optional<SamplePersistentProperty> getIdProperty() {
SampleMappingContext mappingContext = new SampleMappingContext();
BasicPersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Entity.class);
BasicPersistentEntity<Object, SamplePersistentProperty> entity = mappingContext
.getRequiredPersistentEntity(Entity.class);
return entity.getPersistentProperty("id");
}

View File

@@ -32,13 +32,13 @@ public class IdPropertyIdentifierAccessorUnitTests {
@Test(expected = IllegalArgumentException.class) // DATACMNS-599
public void rejectsEntityWithoutIdentifierProperty() {
new IdPropertyIdentifierAccessor(mappingContext.getPersistentEntity(Sample.class), new Sample());
new IdPropertyIdentifierAccessor(mappingContext.getRequiredPersistentEntity(Sample.class), new Sample());
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-599
public void rejectsNullBean() {
new IdPropertyIdentifierAccessor(mappingContext.getPersistentEntity(SampleWithId.class), null);
new IdPropertyIdentifierAccessor(mappingContext.getRequiredPersistentEntity(SampleWithId.class), null);
}
@Test // DATACMNS-599
@@ -48,9 +48,9 @@ public class IdPropertyIdentifierAccessorUnitTests {
sample.id = 1L;
IdentifierAccessor accessor = new IdPropertyIdentifierAccessor(
mappingContext.getPersistentEntity(SampleWithId.class), sample);
mappingContext.getRequiredPersistentEntity(SampleWithId.class), sample);
assertThat(accessor.getIdentifier()).isEqualTo(sample.id);
assertThat(accessor.getIdentifier()).hasValue(sample.id);
}
static class Sample {}

View File

@@ -16,7 +16,7 @@
package org.springframework.data.mapping.model;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Matchers.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Iterator;

View File

@@ -16,6 +16,7 @@
package org.springframework.data.mapping.model;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Optional;
@@ -24,7 +25,6 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
@@ -47,11 +47,9 @@ public class SpelExpressionParameterProviderUnitTests {
@Before
@SuppressWarnings("unchecked")
public void setUp() {
provider = new SpELExpressionParameterValueProvider<SamplePersistentProperty>(evaluator, conversionService,
delegate);
provider = new SpELExpressionParameterValueProvider<>(evaluator, conversionService, delegate);
parameter = mock(Parameter.class);
when(parameter.hasSpelExpression()).thenReturn(true);
when(parameter.getSpelExpression()).thenReturn(Optional.empty());
when(parameter.getRawType()).thenReturn(Object.class);
}
@@ -61,7 +59,6 @@ public class SpelExpressionParameterProviderUnitTests {
public void delegatesIfParameterDoesNotHaveASpELExpression() {
Parameter<Object, SamplePersistentProperty> parameter = mock(Parameter.class);
when(parameter.hasSpelExpression()).thenReturn(false);
provider.getParameterValue(parameter);
verify(delegate, times(1)).getParameterValue(parameter);
@@ -81,9 +78,11 @@ public class SpelExpressionParameterProviderUnitTests {
@Test
public void handsSpELValueToConversionService() {
when(evaluator.evaluate(Mockito.any(String.class))).thenReturn("value");
doReturn(Optional.of("source")).when(parameter).getSpelExpression();
doReturn("value").when(evaluator).evaluate(any());
provider.getParameterValue(parameter);
verify(delegate, times(0)).getParameterValue(parameter);
verify(conversionService, times(1)).convert("value", Object.class);
}
@@ -91,9 +90,11 @@ public class SpelExpressionParameterProviderUnitTests {
@Test
public void doesNotConvertNullValue() {
when(evaluator.evaluate(Mockito.any(String.class))).thenReturn(null);
doReturn(Optional.of("source")).when(parameter).getSpelExpression();
doReturn(null).when(evaluator).evaluate(any());
provider.getParameterValue(parameter);
verify(delegate, times(0)).getParameterValue(parameter);
verify(conversionService, times(0)).convert("value", Object.class);
}
@@ -103,6 +104,7 @@ public class SpelExpressionParameterProviderUnitTests {
provider = new SpELExpressionParameterValueProvider<SamplePersistentProperty>(evaluator, conversionService,
delegate) {
@Override
@SuppressWarnings("unchecked")
protected <T> T potentiallyConvertSpelValue(Object object, Parameter<T, SamplePersistentProperty> parameter) {
@@ -110,10 +112,11 @@ public class SpelExpressionParameterProviderUnitTests {
}
};
when(evaluator.evaluate(Mockito.anyString())).thenReturn("value");
doReturn(Optional.of("source")).when(parameter).getSpelExpression();
doReturn("value").when(evaluator).evaluate(any());
assertThat(provider.getParameterValue(parameter)).hasValue("FOO");
Object result = provider.getParameterValue(parameter);
assertThat(result).isEqualTo("FOO");
verify(delegate, times(0)).getParameterValue(parameter);
}
}