From 0fb21aa2a1ae63bb033fae05b465ac13b22bab50 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 23 Aug 2011 13:42:32 +0200 Subject: [PATCH] DATADOC-248 - Customized MappingMongoEntityInformation to allow taking a custom collection. In case a repository query method returns a domain type not assignable to the repository domain type we have to use the repositories domain type to determine the collection but still use the returned domain type to hand to the unmarshalling. Thus, we need to set up a custom MongoEntityInformation to reflect this scenario. Extended MappingMongoEntityInformation to allow manually defining a custom collection name. EntityInformationCreator was extended accordingly and MongoQueryMethod now sets up the EntityInformation accordingly. --- .../MappingMongoEntityInformation.java | 24 ++++-- .../mongodb/repository/MongoQueryMethod.java | 3 +- .../MongoRepositoryFactoryBean.java | 33 ++++---- ...appingMongoEntityInformationUnitTests.java | 57 +++++++++++++ .../repository/MongoQueryMethodUnitTests.java | 81 +++++++++++++++++++ 5 files changed, 177 insertions(+), 21 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MappingMongoEntityInformationUnitTests.java create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MongoQueryMethodUnitTests.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MappingMongoEntityInformation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MappingMongoEntityInformation.java index ec1f9825f..410be7fab 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MappingMongoEntityInformation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MappingMongoEntityInformation.java @@ -17,14 +17,14 @@ package org.springframework.data.mongodb.repository; import java.io.Serializable; import org.springframework.data.mapping.model.BeanWrapper; -import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.repository.core.support.AbstractEntityInformation; /** - * {@link MongoEntityInformation} implementation using a {@link BasicMongoPersistentEntity} instance to lookup the - * necessary information. + * {@link MongoEntityInformation} implementation using a {@link MongoPersistentEntity} instance to lookup the necessary + * information. Can be configured with a custom collection to be returned which will trump the one returned by the + * {@link MongoPersistentEntity} if given. * * @author Oliver Gierke */ @@ -32,16 +32,28 @@ public class MappingMongoEntityInformation extends A implements MongoEntityInformation { private final MongoPersistentEntity entityMetadata; + private final String customCollectionName; /** * Creates a new {@link MappingMongoEntityInformation} for the given {@link MongoPersistentEntity}. * - * @param domainClass - * @param entity + * @param entity must not be {@literal null}. */ public MappingMongoEntityInformation(MongoPersistentEntity entity) { + this(entity, null); + } + + /** + * Creates a new {@link MappingMongoEntityInformation} for the given {@link MongoPersistentEntity} and custom + * collection name. + * + * @param entity must not be {@literal null}. + * @param customCollectionName + */ + public MappingMongoEntityInformation(MongoPersistentEntity entity, String customCollectionName) { super(entity.getType()); this.entityMetadata = entity; + this.customCollectionName = customCollectionName; } /* (non-Javadoc) @@ -71,7 +83,7 @@ public class MappingMongoEntityInformation extends A * @see org.springframework.data.mongodb.repository.MongoEntityInformation#getCollectionName() */ public String getCollectionName() { - return entityMetadata.getCollection(); + return customCollectionName == null ? entityMetadata.getCollection() : customCollectionName; } /* (non-Javadoc) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoQueryMethod.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoQueryMethod.java index b345a9a82..3ead5214b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoQueryMethod.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoQueryMethod.java @@ -44,7 +44,8 @@ class MongoQueryMethod extends QueryMethod { public MongoQueryMethod(Method method, RepositoryMetadata metadata, EntityInformationCreator entityInformationCreator) { super(method, metadata); this.method = method; - this.entityInformation = entityInformationCreator.getEntityInformation(ClassUtils.getReturnedDomainClass(method)); + this.entityInformation = entityInformationCreator.getEntityInformation( + ClassUtils.getReturnedDomainClass(method), getDomainClass()); } /* diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepositoryFactoryBean.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepositoryFactoryBean.java index 8ed3c86b4..9a8b9b54b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepositoryFactoryBean.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepositoryFactoryBean.java @@ -64,14 +64,13 @@ public class MongoRepositoryFactoryBean, S, ID exten /** * Configures the {@link MongoTemplate} to be used. * - * @param template - * the template to set + * @param template the template to set */ public void setTemplate(MongoTemplate template) { this.template = template; } - + /** * Configures whether to automatically create indexes for the properties referenced in a query method. * @@ -92,14 +91,14 @@ public class MongoRepositoryFactoryBean, S, ID exten protected final RepositoryFactorySupport createRepositoryFactory() { RepositoryFactorySupport factory = getFactoryInstance(template); - + if (createIndexesForQueryMethods) { factory.addQueryCreationListener(new IndexEnsuringQueryCreationListener(template)); } return factory; } - + /** * Creates and initializes a {@link RepositoryFactorySupport} instance. * @@ -137,16 +136,14 @@ public class MongoRepositoryFactoryBean, S, ID exten /** * Creates a new {@link MongoRepositoryFactory} with the given {@link MongoTemplate} and {@link MappingContext}. * - * @param template - * must not be {@literal null} + * @param template must not be {@literal null} * @param mappingContext */ public MongoRepositoryFactory(MongoTemplate template) { Assert.notNull(template); this.template = template; - this.entityInformationCreator = new EntityInformationCreator(template.getConverter() - .getMappingContext()); + this.entityInformationCreator = new EntityInformationCreator(template.getConverter().getMappingContext()); } /* @@ -222,7 +219,7 @@ public class MongoRepositoryFactoryBean, S, ID exten MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, entityInformationCreator); String namedQueryName = queryMethod.getNamedQueryName(); - + if (namedQueries.hasQuery(namedQueryName)) { String namedQuery = namedQueries.getQuery(namedQueryName); return new StringBasedMongoQuery(namedQuery, queryMethod, template); @@ -271,16 +268,24 @@ public class MongoRepositoryFactoryBean, S, ID exten private final MappingContext, MongoPersistentProperty> mappingContext; - public EntityInformationCreator(MappingContext, MongoPersistentProperty> mappingContext) { + public EntityInformationCreator( + MappingContext, MongoPersistentProperty> mappingContext) { Assert.notNull(mappingContext); this.mappingContext = mappingContext; } - @SuppressWarnings("unchecked") public MongoEntityInformation getEntityInformation(Class domainClass) { + return getEntityInformation(domainClass, null); + } + + @SuppressWarnings("unchecked") + public MongoEntityInformation getEntityInformation(Class domainClass, + Class collectionClass) { MongoPersistentEntity persistentEntity = (MongoPersistentEntity) mappingContext .getPersistentEntity(domainClass); - return new MappingMongoEntityInformation(persistentEntity); + String customCollectionName = collectionClass == null ? null : mappingContext + .getPersistentEntity(collectionClass).getCollection(); + return new MappingMongoEntityInformation(persistentEntity, customCollectionName); } } @@ -324,7 +329,7 @@ public class MongoRepositoryFactoryBean, S, ID exten Order order = toOrder(sort, property); index.on(property, order); } - + // Add fixed sorting criteria to index if (sort != null) { for (Sort.Order order : sort) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MappingMongoEntityInformationUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MappingMongoEntityInformationUnitTests.java new file mode 100644 index 000000000..0e3e4430f --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MappingMongoEntityInformationUnitTests.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2011 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. + * 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.repository; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; + +/** + * Unit tests for {@link MappingMongoEntityInformation}. + * + * @author Oliver Gierke + */ +@RunWith(MockitoJUnitRunner.class) +public class MappingMongoEntityInformationUnitTests { + + @Mock + MongoPersistentEntity info; + + @Before + public void setUp() { + when(info.getType()).thenReturn(Person.class); + when(info.getCollection()).thenReturn("Person"); + } + + @Test + public void usesEntityCollectionIfNoCustomOneGiven() { + MongoEntityInformation information = new MappingMongoEntityInformation(info); + assertThat(information.getCollectionName(), is("Person")); + } + + @Test + public void usesCustomCollectionIfGiven() { + MongoEntityInformation information = new MappingMongoEntityInformation(info, "foobar"); + assertThat(information.getCollectionName(), is("foobar")); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MongoQueryMethodUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MongoQueryMethodUnitTests.java new file mode 100644 index 000000000..b153f29bf --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MongoQueryMethodUnitTests.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2011 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. + * 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.repository; + + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + +import java.lang.reflect.Method; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.data.mongodb.core.mapping.MongoMappingContext; +import org.springframework.data.mongodb.repository.MongoRepositoryFactoryBean.EntityInformationCreator; +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; + +/** + * Unit test for {@link MongoQueryMethod}. + * + * @author Oliver Gierke + */ +public class MongoQueryMethodUnitTests { + + EntityInformationCreator creator; + + @Before + public void setUp() { + MongoMappingContext context = new MongoMappingContext(); + creator = new MongoRepositoryFactoryBean.EntityInformationCreator(context); + } + + @Test + public void detectsCollectionFromRepoTypeIfReturnTypeNotAssignable() throws Exception { + + Method method = SampleRepository.class.getMethod("method"); + + MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), creator); + MongoEntityInformation entityInformation = queryMethod.getEntityInformation(); + + assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Address.class))); + assertThat(entityInformation.getCollectionName(), is("contact")); + } + + @Test + public void detectsCollectionFromReturnTypeIfReturnTypeAssignable() throws Exception { + + Method method = SampleRepository2.class.getMethod("method"); + + MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), creator); + MongoEntityInformation entityInformation = queryMethod.getEntityInformation(); + + assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Person.class))); + assertThat(entityInformation.getCollectionName(), is("person")); + } + + + interface SampleRepository extends Repository { + + List
method(); + } + + interface SampleRepository2 extends Repository { + + List method(); + } +}