From dbb7c355eefefce7d559367e07b29ac51135a4d3 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 6 Mar 2014 08:36:16 +0100 Subject: [PATCH] DATACMNS-462 - AbstractPersistentProperty considers collections and maps entities now. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../model/AbstractPersistentProperty.java | 2 +- .../AbstractMappingContextUnitTests.java | 18 ++++ .../AbstractPersistentPropertyUnitTests.java | 85 ++++++++++++++----- 3 files changed, 84 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index 87bb00e35..c4b7a1176 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -257,7 +257,7 @@ public abstract class AbstractPersistentProperty

TypeInformation actualType = information.getActualType(); boolean isComplexType = actualType == null ? false : !simpleTypeHolder.isSimpleType(actualType.getType()); - return isComplexType && !isTransient() && !isCollectionLike() && !isMap(); + return isComplexType && !isTransient(); } /* diff --git a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java index a1fba1f11..cca004b81 100644 --- a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java +++ b/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 groovy.lang.MetaClass; +import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -224,6 +225,23 @@ public class AbstractMappingContextUnitTests { assertThat(context.getPersistentEntity(Integer.class), is(nullValue())); } + /** + * @see DATACMNS-462 + */ + @Test + public void hasPersistentEntityForCollectionPropertiesAfterInitialization() { + + context.getPersistentEntity(Sample.class); + + for (BasicPersistentEntity entity : context.getPersistentEntities()) { + if (entity.getType().equals(Person.class)) { + return; + } + } + + fail("Expected to find persistent entity for Person!"); + } + class Person { String name; } diff --git a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index ebaf53d44..7723426aa 100644 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -51,6 +51,7 @@ public class AbstractPersistentPropertyUnitTests { @Before public void setUp() { + typeInfo = ClassTypeInformation.from(TestClassComplex.class); entity = new BasicPersistentEntity(typeInfo); typeHolder = new SimpleTypeHolder(); @@ -68,6 +69,9 @@ public class AbstractPersistentPropertyUnitTests { property.getComponentType(); } + /** + * @see DATACMNS-101 + */ @Test public void returnsNestedEntityTypeCorrectly() { @@ -197,25 +201,6 @@ public class AbstractPersistentPropertyUnitTests { 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 */ @@ -235,6 +220,46 @@ public class AbstractPersistentPropertyUnitTests { 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 SamplePersistentProperty getProperty(Class type, String name) { BasicPersistentEntity entity = new BasicPersistentEntity( @@ -244,6 +269,25 @@ public class AbstractPersistentPropertyUnitTests { 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 genericField; @@ -345,5 +389,6 @@ public class AbstractPersistentPropertyUnitTests { Collection persons; Person[] personArray; Map personMap; + Collection strings; } }