diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java index 562338a5b..387f97a54 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java @@ -117,7 +117,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { protected List readCollection(Query query) { - MongoEntityInformation metadata = method.getEntityInformation(); + MongoEntityMetadata metadata = method.getEntityInformation(); String collectionName = metadata.getCollectionName(); return operations.find(query, metadata.getJavaType(), collectionName); @@ -175,7 +175,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { @SuppressWarnings({ "rawtypes", "unchecked" }) Object execute(Query query) { - MongoEntityInformation metadata = method.getEntityInformation(); + MongoEntityMetadata metadata = method.getEntityInformation(); long count = operations.count(query, metadata.getCollectionName()); List result = operations.find(query.with(pageable), metadata.getJavaType(), metadata.getCollectionName()); @@ -198,8 +198,8 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { @Override Object execute(Query query) { - MongoEntityInformation entityInformation = method.getEntityInformation(); - return operations.findOne(query, entityInformation.getJavaType()); + MongoEntityMetadata metadata = method.getEntityInformation(); + return operations.findOne(query, metadata.getJavaType()); } } @@ -236,8 +236,8 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { */ Object execute(Query query, Query countQuery) { - MongoEntityInformation information = method.getEntityInformation(); - long count = operations.count(countQuery, information.getCollectionName()); + MongoEntityMetadata metadata = method.getEntityInformation(); + long count = operations.count(countQuery, metadata.getCollectionName()); return new GeoPage(doExecuteQuery(query), accessor.getPageable(), count); } @@ -257,9 +257,8 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { nearQuery.maxDistance(maxDistance); } - MongoEntityInformation entityInformation = method.getEntityInformation(); - return (GeoResults) operations.geoNear(nearQuery, entityInformation.getJavaType(), - entityInformation.getCollectionName()); + MongoEntityMetadata metadata = method.getEntityInformation(); + return (GeoResults) operations.geoNear(nearQuery, metadata.getJavaType(), metadata.getCollectionName()); } private boolean isListOfGeoResult() { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/EntityInformationCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/EntityInformationCreator.java deleted file mode 100644 index dbbd028e4..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/EntityInformationCreator.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2011 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.repository.query; - -import java.io.Serializable; - -/** - * Interface for components being able to provide {@link EntityInformationCreator} for a given {@link Class}. - * - * @author Oliver Gierke - */ -public interface EntityInformationCreator { - - /** - * Returns a {@link MongoEntityInformation} for the given domain class. - * - * @param domainClass the domain class to create the {@link MongoEntityInformation} for, must not be {@literal null}. - * @return - */ - MongoEntityInformation getEntityInformation(Class domainClass); - - /** - * Returns a {@link MongoEntityInformation} for the given domain class and class to retrieve the collection to query - * against from. - * - * @param domainClass the domain class to create the {@link MongoEntityInformation} for, must not be {@literal null}. - * @param collectionClass the class to derive the collection from queries to retrieve the domain classes from shall be - * ran against, must not be {@literal null}. - * @return - */ - MongoEntityInformation getEntityInformation(Class domainClass, - Class collectionClass); - -} \ No newline at end of file diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoEntityMetadata.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoEntityMetadata.java new file mode 100644 index 000000000..8d951b9e9 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoEntityMetadata.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012 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.repository.query; + +import org.springframework.data.repository.core.EntityMetadata; + +/** + * Extension of {@link EntityMetadata} to additionally expose the collection name an entity shall be persisted to. + * + * @author Oliver Gierke + */ +public interface MongoEntityMetadata extends EntityMetadata { + + /** + * Returns the name of the collection the entity shall be persisted to. + * + * @return + */ + String getCollectionName(); +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java index 2c9be74c8..a5bbfd203 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2012 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. @@ -20,9 +20,12 @@ import java.util.Arrays; import java.util.List; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mongodb.core.geo.GeoPage; import org.springframework.data.mongodb.core.geo.GeoResult; import org.springframework.data.mongodb.core.geo.GeoResults; +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; +import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.repository.Query; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.query.Parameters; @@ -33,8 +36,7 @@ import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** - * TODO - Extract methods for {@link #getAnnotatedQuery()} into superclass as it is currently copied from Spring Data - * JPA + * Mongo specific implementation of {@link QueryMethod}. * * @author Oliver Gierke */ @@ -45,19 +47,24 @@ public class MongoQueryMethod extends QueryMethod { .asList(GeoResult.class, GeoResults.class, GeoPage.class); private final Method method; - private final MongoEntityInformation entityInformation; + private final MappingContext, MongoPersistentProperty> mappingContext; + + private MongoEntityMetadata metadata; /** * Creates a new {@link MongoQueryMethod} from the given {@link Method}. * * @param method */ - public MongoQueryMethod(Method method, RepositoryMetadata metadata, EntityInformationCreator entityInformationCreator) { + public MongoQueryMethod(Method method, RepositoryMetadata metadata, + MappingContext, MongoPersistentProperty> mappingContext) { + super(method, metadata); - Assert.notNull(entityInformationCreator, "DefaultEntityInformationCreator must not be null!"); + + Assert.notNull(mappingContext, "MappingContext must not be null!"); + this.method = method; - this.entityInformation = entityInformationCreator.getEntityInformation(metadata.getReturnedDomainClass(method), - getDomainClass()); + this.mappingContext = mappingContext; } /* @@ -101,14 +108,30 @@ public class MongoQueryMethod extends QueryMethod { return StringUtils.hasText(value) ? value : null; } - /* + /* * (non-Javadoc) * @see org.springframework.data.repository.query.QueryMethod#getEntityInformation() */ @Override - public MongoEntityInformation getEntityInformation() { + @SuppressWarnings("unchecked") + public MongoEntityMetadata getEntityInformation() { - return entityInformation; + if (metadata == null) { + + Class returnedObjectType = getReturnedObjectType(); + Class domainClass = getDomainClass(); + + MongoPersistentEntity returnedEntity = mappingContext.getPersistentEntity(getReturnedObjectType()); + MongoPersistentEntity managedEntity = mappingContext.getPersistentEntity(domainClass); + returnedEntity = returnedEntity == null ? managedEntity : returnedEntity; + MongoPersistentEntity collectionEntity = domainClass.isAssignableFrom(returnedObjectType) ? returnedEntity + : managedEntity; + + this.metadata = new SimpleMongoEntityMetadata((Class) returnedEntity.getType(), + collectionEntity.getCollection()); + } + + return this.metadata; } /* @@ -121,12 +144,11 @@ public class MongoQueryMethod extends QueryMethod { } /** - * Returns whether te query is a geoNear query. + * Returns whether te query is a geo near query. * * @return */ public boolean isGeoNearQuery() { - return isGeoNearQuery(this.method); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/SimpleMongoEntityMetadata.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/SimpleMongoEntityMetadata.java new file mode 100644 index 000000000..e248c604a --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/SimpleMongoEntityMetadata.java @@ -0,0 +1,60 @@ +/* + * Copyright 2012 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.repository.query; + +import org.springframework.util.Assert; + +/** + * Bean based implementation of {@link MongoEntityMetadata}. + * + * @author Oliver Gierke + */ +class SimpleMongoEntityMetadata implements MongoEntityMetadata { + + private final Class type; + private final String collectionName; + + /** + * Creates a new {@link SimpleMongoEntityMetadata} using the given type and collection name. + * + * @param type must not be {@literal null}. + * @param collectionName must not be {@literal null} or empty. + */ + public SimpleMongoEntityMetadata(Class type, String collectionName) { + + Assert.notNull(type, "Type must not be null!"); + Assert.hasText(collectionName, "Collection name must not be null or empty!"); + + this.type = type; + this.collectionName = collectionName; + } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.core.EntityMetadata#getJavaType() + */ + public Class getJavaType() { + return type; + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.repository.query.MongoEntityMetadata#getCollectionName() + */ + public String getCollectionName() { + return collectionName; + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/DefaultEntityInformationCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/DefaultEntityInformationCreator.java deleted file mode 100644 index c3d248d0a..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/DefaultEntityInformationCreator.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2011 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.repository.support; - -import java.io.Serializable; - -import org.springframework.data.mapping.context.MappingContext; -import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; -import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; -import org.springframework.data.mongodb.repository.query.EntityInformationCreator; -import org.springframework.data.mongodb.repository.query.MongoEntityInformation; -import org.springframework.util.Assert; - -/** - * Simple {@link EntityInformationCreator} to to create {@link MongoEntityInformation} instances based on a - * {@link MappingContext}. - * - * @author Oliver Gierke - */ -public class DefaultEntityInformationCreator implements EntityInformationCreator { - - private final MappingContext, MongoPersistentProperty> mappingContext; - - public DefaultEntityInformationCreator( - MappingContext, MongoPersistentProperty> mappingContext) { - Assert.notNull(mappingContext); - this.mappingContext = mappingContext; - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.repository.support.EntityInformationCreator#getEntityInformation(java.lang.Class) - */ - public MongoEntityInformation getEntityInformation(Class domainClass) { - return getEntityInformation(domainClass, null); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.repository.support.EntityInformationCreator#getEntityInformation(java.lang.Class, java.lang.Class) - */ - @SuppressWarnings("unchecked") - public MongoEntityInformation getEntityInformation(Class domainClass, - Class collectionClass) { - - MongoPersistentEntity persistentEntity = (MongoPersistentEntity) mappingContext - .getPersistentEntity(domainClass); - String customCollectionName = collectionClass == null ? null : mappingContext.getPersistentEntity(collectionClass) - .getCollection(); - - return new MappingMongoEntityInformation(persistentEntity, customCollectionName); - } -} \ No newline at end of file diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java index 380d15efc..414244e94 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2012 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. @@ -25,7 +25,7 @@ import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.query.Order; -import org.springframework.data.mongodb.repository.query.MongoEntityInformation; +import org.springframework.data.mongodb.repository.query.MongoEntityMetadata; import org.springframework.data.mongodb.repository.query.PartTreeMongoQuery; import org.springframework.data.mongodb.repository.query.QueryUtils; import org.springframework.data.repository.core.support.QueryCreationListener; @@ -85,7 +85,7 @@ class IndexEnsuringQueryCreationListener implements QueryCreationListener metadata = query.getQueryMethod().getEntityInformation(); + MongoEntityMetadata metadata = query.getQueryMethod().getEntityInformation(); operations.indexOps(metadata.getCollectionName()).ensureIndex(index); LOG.debug(String.format("Created %s!", index)); } @@ -99,4 +99,4 @@ class IndexEnsuringQueryCreationListener implements QueryCreationListener, MongoPersistentProperty> mappingContext; /** - * Creates a new {@link MongoRepositoryFactory} with the given {@link MongoTemplate} and {@link MappingContext}. + * Creates a new {@link MongoRepositoryFactory} with the given {@link MongoOperations}. * - * @param template must not be {@literal null} - * @param mappingContext + * @param mongoOperations must not be {@literal null} */ public MongoRepositoryFactory(MongoOperations mongoOperations) { Assert.notNull(mongoOperations); this.mongoOperations = mongoOperations; - this.entityInformationCreator = new DefaultEntityInformationCreator(mongoOperations.getConverter() - .getMappingContext()); + this.mappingContext = mongoOperations.getConverter().getMappingContext(); } /* @@ -117,7 +116,7 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { */ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) { - MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, entityInformationCreator); + MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, mappingContext); String namedQueryName = queryMethod.getNamedQueryName(); if (namedQueries.hasQuery(namedQueryName)) { @@ -136,7 +135,16 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class) */ @Override + @SuppressWarnings("unchecked") public MongoEntityInformation getEntityInformation(Class domainClass) { - return entityInformationCreator.getEntityInformation(domainClass); + + MongoPersistentEntity entity = mappingContext.getPersistentEntity(domainClass); + + if (entity == null) { + throw new MappingException(String.format("Could not lookup mapping metadata for domain class %s!", + domainClass.getName())); + } + + return new MappingMongoEntityInformation((MongoPersistentEntity) entity); } -} \ No newline at end of file +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessorUnitTests.java index 831f0110d..08502d127 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessorUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2012 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. @@ -27,7 +27,6 @@ import org.springframework.data.mongodb.core.geo.Metrics; import org.springframework.data.mongodb.core.geo.Point; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.repository.Person; -import org.springframework.data.mongodb.repository.support.DefaultEntityInformationCreator; import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; @@ -42,13 +41,12 @@ public class MongoParametersParameterAccessorUnitTests { private static final Distance DISTANCE = new Distance(2.5, Metrics.KILOMETERS); private static final RepositoryMetadata metadata = new DefaultRepositoryMetadata(PersonRepository.class); private static final MongoMappingContext context = new MongoMappingContext(); - private static final EntityInformationCreator creator = new DefaultEntityInformationCreator(context); @Test public void returnsNullForDistanceIfNoneAvailable() throws NoSuchMethodException, SecurityException { Method method = PersonRepository.class.getMethod("findByLocationNear", Point.class); - MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, creator); + MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, context); MongoParameterAccessor accessor = new MongoParametersParameterAccessor(queryMethod, new Object[] { new Point(10, 20) }); @@ -59,7 +57,7 @@ public class MongoParametersParameterAccessorUnitTests { public void returnsDistanceIfAvailable() throws NoSuchMethodException, SecurityException { Method method = PersonRepository.class.getMethod("findByLocationNear", Point.class, Distance.class); - MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, creator); + MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, context); MongoParameterAccessor accessor = new MongoParametersParameterAccessor(queryMethod, new Object[] { new Point(10, 20), DISTANCE }); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java index 88d0fe3de..8110eb7be 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java @@ -46,7 +46,6 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; -import org.springframework.data.mongodb.repository.support.DefaultEntityInformationCreator; import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; import org.springframework.data.repository.query.parser.PartTree; @@ -301,7 +300,7 @@ public class MongoQueryCreatorUnitTests { Method method = PersonRepository.class.getMethod("findByLocationNearAndFirstname", Point.class, Distance.class, String.class); MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), - new DefaultEntityInformationCreator(new MongoMappingContext())); + new MongoMappingContext()); MongoParameterAccessor accessor = new MongoParametersParameterAccessor(queryMethod, new Object[] { point, distance, "Dave" }); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryMethodUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryMethodUnitTests.java index 789f74e60..46a7d34d2 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryMethodUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryMethodUnitTests.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright 2011-2012 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 + * 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, @@ -35,7 +35,6 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.repository.Address; import org.springframework.data.mongodb.repository.Contact; import org.springframework.data.mongodb.repository.Person; -import org.springframework.data.mongodb.repository.support.DefaultEntityInformationCreator; import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; @@ -46,12 +45,11 @@ import org.springframework.data.repository.core.support.DefaultRepositoryMetadat */ public class MongoQueryMethodUnitTests { - EntityInformationCreator creator; + MongoMappingContext context; @Before public void setUp() { - MongoMappingContext context = new MongoMappingContext(); - creator = new DefaultEntityInformationCreator(context); + context = new MongoMappingContext(); } @Test @@ -60,11 +58,11 @@ public class MongoQueryMethodUnitTests { Method method = SampleRepository.class.getMethod("method"); MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), - creator); - MongoEntityInformation entityInformation = queryMethod.getEntityInformation(); + context); + MongoEntityMetadata metadata = queryMethod.getEntityInformation(); - assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Address.class))); - assertThat(entityInformation.getCollectionName(), is("contact")); + assertThat(metadata.getJavaType(), is(typeCompatibleWith(Address.class))); + assertThat(metadata.getCollectionName(), is("contact")); } @Test @@ -73,8 +71,8 @@ public class MongoQueryMethodUnitTests { Method method = SampleRepository2.class.getMethod("method"); MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), - creator); - MongoEntityInformation entityInformation = queryMethod.getEntityInformation(); + context); + MongoEntityMetadata entityInformation = queryMethod.getEntityInformation(); assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Person.class))); assertThat(entityInformation.getCollectionName(), is("person")); @@ -103,7 +101,7 @@ public class MongoQueryMethodUnitTests { } @Test(expected = IllegalArgumentException.class) - public void rejectsNullEntityCreator() throws Exception { + public void rejectsNullMappingContext() throws Exception { Method method = PersonRepository.class.getMethod("findByFirstname", String.class, Point.class); new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), null); } @@ -115,9 +113,16 @@ public class MongoQueryMethodUnitTests { assertThat(method.isCollectionQuery(), is(false)); } + @Test + public void createsMongoQueryMethodObjectForMethodReturningAnInterface() throws Exception { + + Method method = SampleRepository2.class.getMethod("methodReturningAnInterface"); + new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository2.class), context); + } + private MongoQueryMethod queryMethod(String name, Class... parameters) throws Exception { Method method = PersonRepository.class.getMethod(name, parameters); - return new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), creator); + return new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), context); } interface PersonRepository extends Repository { @@ -142,5 +147,11 @@ public class MongoQueryMethodUnitTests { interface SampleRepository2 extends Repository { List method(); + + Customer methodReturningAnInterface(); + } + + interface Customer { + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQueryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQueryUnitTests.java index a81b0be46..e23215a66 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQueryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQueryUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2012 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. @@ -54,8 +54,6 @@ public class StringBasedMongoQueryUnitTests { @Mock RepositoryMetadata metadata; @Mock - EntityInformationCreator creator; - @Mock MongoDbFactory factory; MongoConverter converter; @@ -70,7 +68,7 @@ public class StringBasedMongoQueryUnitTests { public void bindsSimplePropertyCorrectly() throws Exception { Method method = SampleRepository.class.getMethod("findByLastname", String.class); - MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, creator); + MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, converter.getMappingContext()); StringBasedMongoQuery mongoQuery = new StringBasedMongoQuery(queryMethod, operations); ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, "Matthews"); @@ -132,7 +130,7 @@ public class StringBasedMongoQueryUnitTests { private StringBasedMongoQuery createQueryForMethod(String name, Class... parameters) throws Exception { Method method = SampleRepository.class.getMethod(name, parameters); - MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, creator); + MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, converter.getMappingContext()); return new StringBasedMongoQuery(queryMethod, operations); }