From 54cee64610d10a6d31bc82efe54a8ca314363610 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 19 Nov 2014 18:36:05 +0100 Subject: [PATCH] DATAMONGO-1100 - Upgrade to new PersistentPropertyAccessor API. --- .../data/mongodb/core/MongoTemplate.java | 39 +-- .../convert/DefaultDbRefProxyHandler.java | 20 +- .../core/convert/MappingMongoConverter.java | 31 ++- .../MappingMongoEntityInformation.java | 40 +-- .../DbRefMappingMongoConverterUnitTests.java | 7 +- .../MappingMongoConverterUnitTests.java | 53 +--- ...ersistentEntityIndexResolverUnitTests.java | 34 +-- .../MongoPersistentEntityTestDummy.java | 227 ------------------ ...appingMongoEntityInformationUnitTests.java | 14 +- 9 files changed, 79 insertions(+), 386 deletions(-) delete mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/MongoPersistentEntityTestDummy.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 6eeb75617..9e433ce8f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -55,8 +55,9 @@ import org.springframework.data.geo.Distance; import org.springframework.data.geo.GeoResult; import org.springframework.data.geo.GeoResults; import org.springframework.data.geo.Metric; +import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.context.MappingContext; -import org.springframework.data.mapping.model.BeanWrapper; +import org.springframework.data.mapping.model.ConvertingPropertyAccessor; import org.springframework.data.mapping.model.MappingException; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.aggregation.Aggregation; @@ -745,8 +746,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { MongoPersistentEntity mongoPersistentEntity = getPersistentEntity(entity.getClass()); if (mongoPersistentEntity != null && mongoPersistentEntity.hasVersionProperty()) { - BeanWrapper wrapper = BeanWrapper.create(entity, this.mongoConverter.getConversionService()); - wrapper.setProperty(mongoPersistentEntity.getVersionProperty(), 0); + ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor( + mongoPersistentEntity.getPropertyAccessor(entity), mongoConverter.getConversionService()); + accessor.setProperty(mongoPersistentEntity.getVersionProperty(), 0); } } @@ -839,11 +841,14 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { private void doSaveVersioned(T objectToSave, MongoPersistentEntity entity, String collectionName) { - BeanWrapper beanWrapper = BeanWrapper.create(objectToSave, this.mongoConverter.getConversionService()); + ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor( + entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService()); + MongoPersistentProperty idProperty = entity.getIdProperty(); MongoPersistentProperty versionProperty = entity.getVersionProperty(); - Number version = beanWrapper.getProperty(versionProperty, Number.class); + Object version = convertingAccessor.getProperty(versionProperty); + Number versionNumber = convertingAccessor.getProperty(versionProperty, Number.class); // Fresh instance -> initialize version property if (version == null) { @@ -853,12 +858,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { assertUpdateableIdIfNotSet(objectToSave); // Create query for entity with the id and old version - Object id = beanWrapper.getProperty(idProperty); + Object id = convertingAccessor.getProperty(idProperty); Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(versionProperty.getName()).is(version)); // Bump version number - Number number = beanWrapper.getProperty(versionProperty, Number.class); - beanWrapper.setProperty(versionProperty, number.longValue() + 1); + convertingAccessor.setProperty(versionProperty, versionNumber.longValue() + 1); BasicDBObject dbObject = new BasicDBObject(); @@ -1089,12 +1093,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { MongoPersistentEntity entity = mappingContext.getPersistentEntity(objectType); MongoPersistentProperty idProp = entity == null ? null : entity.getIdProperty(); - if (idProp == null) { + if (idProp == null || entity == null) { throw new MappingException("No id property found for object of type " + objectType); } - Object idValue = BeanWrapper.create(object, mongoConverter.getConversionService()) - .getProperty(idProp, Object.class); + Object idValue = entity.getPropertyAccessor(object).getProperty(idProp); return Collections.singletonMap(idProp.getFieldName(), idValue).entrySet().iterator().next(); } @@ -1138,12 +1141,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { MongoPersistentEntity persistentEntity = mappingContext.getPersistentEntity(entity.getClass()); MongoPersistentProperty idProperty = persistentEntity == null ? null : persistentEntity.getIdProperty(); - if (idProperty == null) { + if (idProperty == null || persistentEntity == null) { return; } - ConversionService service = mongoConverter.getConversionService(); - Object idValue = BeanWrapper.create(entity, service).getProperty(idProperty, Object.class); + Object idValue = persistentEntity.getPropertyAccessor(entity).getProperty(idProperty); if (idValue == null && !MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(idProperty.getType())) { throw new InvalidDataAccessApiUsageException(String.format( @@ -1718,15 +1720,14 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } ConversionService conversionService = mongoConverter.getConversionService(); - BeanWrapper wrapper = BeanWrapper.create(savedObject, conversionService); - - Object idValue = wrapper.getProperty(idProp, idProp.getType()); + MongoPersistentEntity entity = mappingContext.getPersistentEntity(savedObject.getClass()); + PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject); - if (idValue != null) { + if (accessor.getProperty(idProp) != null) { return; } - wrapper.setProperty(idProp, id); + new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProp, id); } private DBCollection getAndPrepareCollection(DB db, String collectionName) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefProxyHandler.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefProxyHandler.java index a9bbc98f7..13759ed88 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefProxyHandler.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefProxyHandler.java @@ -15,8 +15,8 @@ */ package org.springframework.data.mongodb.core.convert; +import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.context.MappingContext; -import org.springframework.data.mapping.model.BeanWrapper; import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator; import org.springframework.data.mapping.model.SpELContext; import org.springframework.data.mapping.model.SpELExpressionEvaluator; @@ -60,19 +60,19 @@ class DefaultDbRefProxyHandler implements DbRefProxyHandler { return proxy; } - MongoPersistentEntity persistentEntity = mappingContext.getPersistentEntity(property); - MongoPersistentProperty idProperty = persistentEntity.getIdProperty(); - - if(idProperty.usePropertyAccess()) { + MongoPersistentEntity entity = mappingContext.getPersistentEntity(property); + MongoPersistentProperty idProperty = entity.getIdProperty(); + + if (idProperty.usePropertyAccess()) { return proxy; } - + SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(proxy, spELContext); - BeanWrapper proxyWrapper = BeanWrapper.create(proxy, null); - + PersistentPropertyAccessor accessor = entity.getPropertyAccessor(proxy); + DBObject object = new BasicDBObject(idProperty.getFieldName(), source.getId()); - ObjectPath objectPath = ObjectPath.ROOT.push(proxy, persistentEntity, null); - proxyWrapper.setProperty(idProperty, resolver.getValueInternal(idProperty, object, evaluator, objectPath)); + ObjectPath objectPath = ObjectPath.ROOT.push(proxy, entity, null); + accessor.setProperty(idProperty, resolver.getValueInternal(idProperty, object, evaluator, objectPath)); return proxy; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 72c778049..6fd397579 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -37,10 +37,11 @@ import org.springframework.data.convert.EntityInstantiator; import org.springframework.data.convert.TypeMapper; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.AssociationHandler; +import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.PreferredConstructor.Parameter; import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.context.MappingContext; -import org.springframework.data.mapping.model.BeanWrapper; +import org.springframework.data.mapping.model.ConvertingPropertyAccessor; import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator; import org.springframework.data.mapping.model.MappingException; import org.springframework.data.mapping.model.ParameterValueProvider; @@ -248,16 +249,18 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity); S instance = instantiator.createInstance(entity, provider); - final BeanWrapper wrapper = BeanWrapper.create(instance, conversionService); + final PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance), + conversionService); + final MongoPersistentProperty idProperty = entity.getIdProperty(); - final S result = wrapper.getBean(); + final S result = instance; // make sure id property is set before all other properties Object idValue = null; if (idProperty != null) { idValue = getValueInternal(idProperty, dbo, evaluator, path); - wrapper.setProperty(idProperty, idValue); + accessor.setProperty(idProperty, idValue); } final ObjectPath currentPath = path.push(result, entity, idValue); @@ -275,7 +278,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App return; } - wrapper.setProperty(prop, getValueInternal(prop, dbo, evaluator, currentPath)); + accessor.setProperty(prop, getValueInternal(prop, dbo, evaluator, currentPath)); } }); @@ -297,7 +300,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App DbRefResolverCallback callback = new DefaultDbRefResolverCallback(dbo, currentPath, evaluator, MappingMongoConverter.this); - wrapper.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler)); + accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler)); } }); @@ -397,13 +400,13 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App throw new MappingException("No mapping metadata found for entity of type " + obj.getClass().getName()); } - final BeanWrapper wrapper = BeanWrapper.create(obj, conversionService); + final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(obj); final MongoPersistentProperty idProperty = entity.getIdProperty(); if (!dbo.containsField("_id") && null != idProperty) { try { - Object id = wrapper.getProperty(idProperty, Object.class); + Object id = accessor.getProperty(idProperty); dbo.put("_id", idMapper.convertId(id)); } catch (ConversionException ignored) {} } @@ -416,7 +419,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App return; } - Object propertyObj = wrapper.getProperty(prop); + Object propertyObj = accessor.getProperty(prop); if (null != propertyObj) { @@ -430,10 +433,12 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App }); entity.doWithAssociations(new AssociationHandler() { + public void doWithAssociation(Association association) { + MongoPersistentProperty inverseProp = association.getInverse(); - Class type = inverseProp.getType(); - Object propertyObj = wrapper.getProperty(inverseProp, type); + Object propertyObj = accessor.getProperty(inverseProp); + if (null != propertyObj) { writePropertyInternal(propertyObj, dbo, inverseProp); } @@ -807,8 +812,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App if (target.getClass().equals(idProperty.getType())) { id = target; } else { - BeanWrapper wrapper = BeanWrapper.create(target, conversionService); - id = wrapper.getProperty(idProperty, Object.class); + PersistentPropertyAccessor accessor = targetEntity.getPropertyAccessor(target); + id = accessor.getProperty(idProperty); } if (null == id) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MappingMongoEntityInformation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MappingMongoEntityInformation.java index d28d0abfa..7824c1320 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MappingMongoEntityInformation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MappingMongoEntityInformation.java @@ -17,11 +17,9 @@ package org.springframework.data.mongodb.repository.support; import java.io.Serializable; -import org.springframework.data.mapping.model.BeanWrapper; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; -import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.repository.query.MongoEntityInformation; -import org.springframework.data.repository.core.support.AbstractEntityInformation; +import org.springframework.data.repository.core.support.PersistentEntityInformation; /** * {@link MongoEntityInformation} implementation using a {@link MongoPersistentEntity} instance to lookup the necessary @@ -30,7 +28,7 @@ import org.springframework.data.repository.core.support.AbstractEntityInformatio * * @author Oliver Gierke */ -public class MappingMongoEntityInformation extends AbstractEntityInformation +public class MappingMongoEntityInformation extends PersistentEntityInformation implements MongoEntityInformation { private final MongoPersistentEntity entityMetadata; @@ -50,40 +48,14 @@ public class MappingMongoEntityInformation extends A * collection name. * * @param entity must not be {@literal null}. - * @param customCollectionName + * @param customCollectionName can be {@literal null}. */ public MappingMongoEntityInformation(MongoPersistentEntity entity, String customCollectionName) { - super(entity.getType()); - this.entityMetadata = entity; - this.customCollectionName = customCollectionName; - } - - /* - * (non-Javadoc) - * @see org.springframework.data.repository.support.EntityInformation#getId(java.lang.Object) - */ - @SuppressWarnings("unchecked") - public ID getId(T entity) { - - MongoPersistentProperty idProperty = entityMetadata.getIdProperty(); - if (idProperty == null) { - return null; - } + super(entity); - try { - return (ID) BeanWrapper.create(entity, null).getProperty(idProperty); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - /* (non-Javadoc) - * @see org.springframework.data.repository.support.EntityInformation#getIdType() - */ - @SuppressWarnings("unchecked") - public Class getIdType() { - return (Class) entityMetadata.getIdProperty().getType(); + this.entityMetadata = entity; + this.customCollectionName = customCollectionName; } /* (non-Javadoc) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java index fec18436a..8b9682c15 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java @@ -40,8 +40,8 @@ import org.springframework.data.annotation.AccessType; import org.springframework.data.annotation.AccessType.Type; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.PersistenceConstructor; +import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.PropertyPath; -import org.springframework.data.mapping.model.BeanWrapper; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoExceptionTranslator; import org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.Person; @@ -505,6 +505,7 @@ public class DbRefMappingMongoConverterUnitTests { MongoPersistentEntity entity = mappingContext.getPersistentEntity(ClassWithLazyDbRefs.class); MongoPersistentProperty property = entity.getPersistentProperty("dbRefToConcreteType"); + MongoPersistentEntity propertyEntity = mappingContext.getPersistentEntity(property); String idValue = new ObjectId().toString(); DBRef dbRef = converter.toDBRef(new LazyDbRefTarget(idValue), property); @@ -513,10 +514,10 @@ public class DbRefMappingMongoConverterUnitTests { ClassWithLazyDbRefs result = converter.read(ClassWithLazyDbRefs.class, object); - BeanWrapper wrapper = BeanWrapper.create(result.dbRefToConcreteType, null); + PersistentPropertyAccessor accessor = propertyEntity.getPropertyAccessor(result.dbRefToConcreteType); MongoPersistentProperty idProperty = mappingContext.getPersistentEntity(LazyDbRefTarget.class).getIdProperty(); - assertThat(wrapper.getProperty(idProperty), is(notNullValue())); + assertThat(accessor.getProperty(idProperty), is(notNullValue())); assertProxyIsResolved(result.dbRefToConcreteType, false); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java index 2765e0f87..b41ef7cd3 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java @@ -1917,7 +1917,7 @@ public class MappingMongoConverterUnitTests { assertThat(withNamedIdField.id, is("A")); } - + /** * @see DATAMONGO-1050 */ @@ -1931,7 +1931,7 @@ public class MappingMongoConverterUnitTests { assertThat(withExplicitlyRenamedField.id, is("B")); } - + /** * @see DATAMONGO-1050 */ @@ -1945,31 +1945,6 @@ public class MappingMongoConverterUnitTests { assertThat(withAnnotatedIdField.key, is("A")); } - /** - * @see DATAMONGO-1086 - */ - @Test - public void shouldIncludeClassIdentifierForGenericAbstractTypesInCollections() { - - ConcreteRoot root = new ConcreteRoot(); - ConcreteSub sub = new ConcreteSub(); - sub.abstractType = new Concrete(); - sub.abstractType.content = "foo"; - root.listOfGenericSub = Collections.singletonList(sub); - - DBObject dbo = new BasicDBObject(); - converter.write(root, dbo); - - BasicDBList genericSubType = DBObjectTestUtils.getAsDBList(dbo, "listOfGenericSub"); - assertThat((String) dbo.get("_class"), is(ConcreteRoot.class.getName())); - - DBObject item0 = DBObjectTestUtils.getAsDBObject(genericSubType, 0); - assertThat(item0.get("_class"), nullValue()); - - DBObject abstractType = DBObjectTestUtils.getAsDBObject(item0, "abstractType"); - assertThat((String) abstractType.get("_class"), is(Concrete.class.getName())); - } - static class GenericType { T content; } @@ -2257,28 +2232,4 @@ public class MappingMongoConverterUnitTests { @Id String key; } - - static abstract class AbstractGenericRoot> { - List listOfGenericSub; - } - - static class ConcreteRoot extends AbstractGenericRoot { - - } - - static abstract class GenericSub { - T abstractType; - } - - static class ConcreteSub extends GenericSub { - - } - - static abstract class AbstractRoot { - - } - - static class Concrete extends AbstractRoot { - String content; - } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index 5df5d5391..45b110e13 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -19,7 +19,6 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.mockito.Mockito.*; -import java.lang.annotation.Annotation; import java.util.Collections; import java.util.List; @@ -28,7 +27,6 @@ import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; import org.springframework.data.geo.Point; -import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mongodb.core.DBObjectTestUtils; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolverUnitTests.CompoundIndexResolutionTests; @@ -36,14 +34,15 @@ import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexRes import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolverUnitTests.IndexResolutionTests; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolverUnitTests.MixedIndexResolutionTests; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolverUnitTests.TextIndexedResolutionTests; +import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; import org.springframework.data.mongodb.core.mapping.Language; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; -import org.springframework.data.mongodb.core.mapping.MongoPersistentEntityTestDummy.MongoPersistentEntityDummyBuilder; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; +import org.springframework.data.util.ClassTypeInformation; import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DBObject; @@ -741,39 +740,22 @@ public class MongoPersistentEntityIndexResolverUnitTests { * @see DATAMONGO-962 */ @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) public void shouldCatchCyclicReferenceExceptionOnRoot() { - Document documentDummy = new Document() { - - @Override - public Class annotationType() { - return Document.class; - } - - @Override - public String collection() { - return null; - } - - @Override - public String language() { - return null; - } - }; + MongoPersistentEntity entity = new BasicMongoPersistentEntity(ClassTypeInformation.from(Object.class)); MongoPersistentProperty propertyMock = mock(MongoPersistentProperty.class); when(propertyMock.isEntity()).thenReturn(true); - when(propertyMock.getOwner()).thenReturn( - (PersistentEntity) MongoPersistentEntityDummyBuilder.forClass(Object.class).build()); + when(propertyMock.getOwner()).thenReturn(entity); when(propertyMock.getActualType()).thenThrow( new MongoPersistentEntityIndexResolver.CyclicPropertyReferenceException("foo", Object.class, "bar")); - MongoPersistentEntity dummy = MongoPersistentEntityDummyBuilder - .forClass(SelfCyclingViaCollectionType.class).withCollection("foo").and(propertyMock).and(documentDummy) - .build(); + MongoPersistentEntity selfCyclingEntity = new BasicMongoPersistentEntity( + ClassTypeInformation.from(SelfCyclingViaCollectionType.class)); new MongoPersistentEntityIndexResolver(prepareMappingContext(SelfCyclingViaCollectionType.class)) - .resolveIndexForEntity(dummy); + .resolveIndexForEntity(selfCyclingEntity); } /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/MongoPersistentEntityTestDummy.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/MongoPersistentEntityTestDummy.java deleted file mode 100644 index a07cd1002..000000000 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/MongoPersistentEntityTestDummy.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Copyright 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core.mapping; - -import java.lang.annotation.Annotation; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.springframework.data.annotation.Id; -import org.springframework.data.annotation.Version; -import org.springframework.data.mapping.AssociationHandler; -import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.mapping.PreferredConstructor; -import org.springframework.data.mapping.PropertyHandler; -import org.springframework.data.mapping.SimpleAssociationHandler; -import org.springframework.data.mapping.SimplePropertyHandler; -import org.springframework.data.util.TypeInformation; - -/** - * Trivial dummy implementation of {@link MongoPersistentEntity} to be used in tests. - * - * @author Christoph Strobl - * @param - */ -public class MongoPersistentEntityTestDummy implements MongoPersistentEntity { - - private Map, Annotation> annotations = new HashMap, Annotation>(); - private Collection properties = new ArrayList(); - private String collection; - private String name; - private Class type; - - @Override - public String getName() { - return name; - } - - @Override - public PreferredConstructor getPersistenceConstructor() { - return null; - } - - @Override - public boolean isConstructorArgument(PersistentProperty property) { - return false; - } - - @Override - public boolean isIdProperty(PersistentProperty property) { - return property != null ? property.isIdProperty() : false; - } - - @Override - public boolean isVersionProperty(PersistentProperty property) { - return property != null ? property.isIdProperty() : false; - } - - @Override - public MongoPersistentProperty getIdProperty() { - return getPersistentProperty(Id.class); - } - - @Override - public MongoPersistentProperty getVersionProperty() { - return getPersistentProperty(Version.class); - } - - @Override - public MongoPersistentProperty getPersistentProperty(String name) { - - for (MongoPersistentProperty p : this.properties) { - if (p.getName().equals(name)) { - return p; - } - } - return null; - } - - @Override - public MongoPersistentProperty getPersistentProperty(Class annotationType) { - - for (MongoPersistentProperty p : this.properties) { - if (p.isAnnotationPresent(annotationType)) { - return p; - } - } - return null; - } - - @Override - public boolean hasIdProperty() { - return false; - } - - @Override - public boolean hasVersionProperty() { - return getVersionProperty() != null; - } - - @Override - public Class getType() { - return this.type; - } - - @Override - public Object getTypeAlias() { - return null; - } - - @Override - public TypeInformation getTypeInformation() { - return null; - } - - @Override - public void doWithProperties(PropertyHandler handler) { - - for (MongoPersistentProperty p : this.properties) { - handler.doWithPersistentProperty(p); - } - } - - @Override - public void doWithProperties(SimplePropertyHandler handler) { - - for (MongoPersistentProperty p : this.properties) { - handler.doWithPersistentProperty(p); - } - } - - @Override - public void doWithAssociations(AssociationHandler handler) { - - } - - @Override - public void doWithAssociations(SimpleAssociationHandler handler) { - - } - - @SuppressWarnings("unchecked") - @Override - public A findAnnotation(Class annotationType) { - return (A) this.annotations.get(annotationType); - } - - @Override - public String getCollection() { - return this.collection; - } - - /** - * Simple builder to create {@link MongoPersistentEntityTestDummy} with defined properties. - * - * @author Christoph Strobl - * @param - */ - public static class MongoPersistentEntityDummyBuilder { - - private MongoPersistentEntityTestDummy instance; - - private MongoPersistentEntityDummyBuilder(Class type) { - this.instance = new MongoPersistentEntityTestDummy(); - this.instance.type = type; - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static MongoPersistentEntityDummyBuilder forClass(Class type) { - return new MongoPersistentEntityDummyBuilder(type); - } - - public MongoPersistentEntityDummyBuilder withName(String name) { - this.instance.name = name; - return this; - } - - public MongoPersistentEntityDummyBuilder and(MongoPersistentProperty property) { - this.instance.properties.add(property); - return this; - } - - public MongoPersistentEntityDummyBuilder withCollection(String collection) { - this.instance.collection = collection; - return this; - } - - public MongoPersistentEntityDummyBuilder and(Annotation annotation) { - this.instance.annotations.put(annotation.annotationType(), annotation); - return this; - } - - public MongoPersistentEntityTestDummy build() { - return this.instance; - } - - } - - @Override - public String getLanguage() { - return null; - } - - @Override - public MongoPersistentProperty getTextScoreProperty() { - return null; - } - - @Override - public boolean hasTextScoreProperty() { - return false; - } -} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MappingMongoEntityInformationUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MappingMongoEntityInformationUnitTests.java index 32fcf5c33..821aab557 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MappingMongoEntityInformationUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MappingMongoEntityInformationUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright 2011-2014 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,23 +36,31 @@ import org.springframework.data.mongodb.repository.support.MappingMongoEntityInf @RunWith(MockitoJUnitRunner.class) public class MappingMongoEntityInformationUnitTests { - @Mock - MongoPersistentEntity info; + @Mock MongoPersistentEntity info; @Before public void setUp() { + when(info.getType()).thenReturn(Person.class); when(info.getCollection()).thenReturn("Person"); } + /** + * @see DATAMONGO-248 + */ @Test public void usesEntityCollectionIfNoCustomOneGiven() { + MongoEntityInformation information = new MappingMongoEntityInformation(info); assertThat(information.getCollectionName(), is("Person")); } + /** + * @see DATAMONGO-248 + */ @Test public void usesCustomCollectionIfGiven() { + MongoEntityInformation information = new MappingMongoEntityInformation(info, "foobar"); assertThat(information.getCollectionName(), is("foobar")); }