Browse Source

DATACMNS-462 - AbstractPersistentProperty considers collections and maps entities now.

Removed the rejecting check for collections and maps and solely rely on the evaluation of the actual type for the ….isEntity() decision in AbstractPersistentProperty. This will cause collection and map properties also being inspected for persistent types as soon as the owner type gets added to the mapping context.
pull/74/merge
Oliver Gierke 12 years ago
parent
commit
dbb7c355ee
  1. 2
      src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java
  2. 18
      src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java
  3. 85
      src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

2
src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

@ -257,7 +257,7 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
TypeInformation<?> actualType = information.getActualType(); TypeInformation<?> actualType = information.getActualType();
boolean isComplexType = actualType == null ? false : !simpleTypeHolder.isSimpleType(actualType.getType()); boolean isComplexType = actualType == null ? false : !simpleTypeHolder.isSimpleType(actualType.getType());
return isComplexType && !isTransient() && !isCollectionLike() && !isMap(); return isComplexType && !isTransient();
} }
/* /*

18
src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java

@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import groovy.lang.MetaClass; import groovy.lang.MetaClass;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -224,6 +225,23 @@ public class AbstractMappingContextUnitTests {
assertThat(context.getPersistentEntity(Integer.class), is(nullValue())); assertThat(context.getPersistentEntity(Integer.class), is(nullValue()));
} }
/**
* @see DATACMNS-462
*/
@Test
public void hasPersistentEntityForCollectionPropertiesAfterInitialization() {
context.getPersistentEntity(Sample.class);
for (BasicPersistentEntity<Object, SamplePersistentProperty> entity : context.getPersistentEntities()) {
if (entity.getType().equals(Person.class)) {
return;
}
}
fail("Expected to find persistent entity for Person!");
}
class Person { class Person {
String name; String name;
} }

85
src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2013 the original author or authors. * Copyright 2011-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -51,6 +51,7 @@ public class AbstractPersistentPropertyUnitTests {
@Before @Before
public void setUp() { public void setUp() {
typeInfo = ClassTypeInformation.from(TestClassComplex.class); typeInfo = ClassTypeInformation.from(TestClassComplex.class);
entity = new BasicPersistentEntity<TestClassComplex, SamplePersistentProperty>(typeInfo); entity = new BasicPersistentEntity<TestClassComplex, SamplePersistentProperty>(typeInfo);
typeHolder = new SimpleTypeHolder(); typeHolder = new SimpleTypeHolder();
@ -68,6 +69,9 @@ public class AbstractPersistentPropertyUnitTests {
property.getComponentType(); property.getComponentType();
} }
/**
* @see DATACMNS-101
*/
@Test @Test
public void returnsNestedEntityTypeCorrectly() { public void returnsNestedEntityTypeCorrectly() {
@ -197,25 +201,6 @@ public class AbstractPersistentPropertyUnitTests {
assertThat(property.getSetter(), is(nullValue())); assertThat(property.getSetter(), is(nullValue()));
} }
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;
} catch (IntrospectionException e) {
return null;
}
}
/** /**
* @see DATACMNS-337 * @see DATACMNS-337
*/ */
@ -235,6 +220,46 @@ public class AbstractPersistentPropertyUnitTests {
assertThat(property.getActualType(), is((Object) Person.class)); assertThat(property.getActualType(), is((Object) Person.class));
} }
/**
* @see DATACMNS-462
*/
@Test
public void considersCollectionPropertyEntitiesIfComponentTypeIsEntity() {
SamplePersistentProperty property = getProperty(Sample.class, "persons");
assertThat(property.isEntity(), is(true));
}
/**
* @see DATACMNS-462
*/
@Test
public void considersMapPropertyEntitiesIfValueTypeIsEntity() {
SamplePersistentProperty property = getProperty(Sample.class, "personMap");
assertThat(property.isEntity(), is(true));
}
/**
* @see DATACMNS-462
*/
@Test
public void considersArrayPropertyEntitiesIfComponentTypeIsEntity() {
SamplePersistentProperty property = getProperty(Sample.class, "personArray");
assertThat(property.isEntity(), is(true));
}
/**
* @see DATACMNS-462
*/
@Test
public void considersCollectionPropertySimpleIfComponentTypeIsSimple() {
SamplePersistentProperty property = getProperty(Sample.class, "strings");
assertThat(property.isEntity(), is(false));
}
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) { private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
BasicPersistentEntity<T, SamplePersistentProperty> entity = new BasicPersistentEntity<T, SamplePersistentProperty>( BasicPersistentEntity<T, SamplePersistentProperty> entity = new BasicPersistentEntity<T, SamplePersistentProperty>(
@ -244,6 +269,25 @@ public class AbstractPersistentPropertyUnitTests {
return new SamplePersistentProperty(field, null, entity, typeHolder); return new SamplePersistentProperty(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;
} catch (IntrospectionException e) {
return null;
}
}
class Generic<T> { class Generic<T> {
T genericField; T genericField;
@ -345,5 +389,6 @@ public class AbstractPersistentPropertyUnitTests {
Collection<Person> persons; Collection<Person> persons;
Person[] personArray; Person[] personArray;
Map<String, Person> personMap; Map<String, Person> personMap;
Collection<String> strings;
} }
} }

Loading…
Cancel
Save