|
|
|
@ -36,6 +36,8 @@ import org.springframework.data.mongodb.repository.Address; |
|
|
|
import org.springframework.data.mongodb.repository.Contact; |
|
|
|
import org.springframework.data.mongodb.repository.Contact; |
|
|
|
import org.springframework.data.mongodb.repository.Meta; |
|
|
|
import org.springframework.data.mongodb.repository.Meta; |
|
|
|
import org.springframework.data.mongodb.repository.Person; |
|
|
|
import org.springframework.data.mongodb.repository.Person; |
|
|
|
|
|
|
|
import org.springframework.data.projection.ProjectionFactory; |
|
|
|
|
|
|
|
import org.springframework.data.projection.SpelAwareProxyProjectionFactory; |
|
|
|
import org.springframework.data.repository.Repository; |
|
|
|
import org.springframework.data.repository.Repository; |
|
|
|
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; |
|
|
|
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; |
|
|
|
|
|
|
|
|
|
|
|
@ -57,10 +59,7 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void detectsCollectionFromRepoTypeIfReturnTypeNotAssignable() throws Exception { |
|
|
|
public void detectsCollectionFromRepoTypeIfReturnTypeNotAssignable() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
Method method = SampleRepository.class.getMethod("method"); |
|
|
|
MongoQueryMethod queryMethod = queryMethod(SampleRepository.class, "method"); |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), |
|
|
|
|
|
|
|
context); |
|
|
|
|
|
|
|
MongoEntityMetadata<?> metadata = queryMethod.getEntityInformation(); |
|
|
|
MongoEntityMetadata<?> metadata = queryMethod.getEntityInformation(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(metadata.getJavaType(), is(typeCompatibleWith(Address.class))); |
|
|
|
assertThat(metadata.getJavaType(), is(typeCompatibleWith(Address.class))); |
|
|
|
@ -70,10 +69,7 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void detectsCollectionFromReturnTypeIfReturnTypeAssignable() throws Exception { |
|
|
|
public void detectsCollectionFromReturnTypeIfReturnTypeAssignable() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
Method method = SampleRepository2.class.getMethod("method"); |
|
|
|
MongoQueryMethod queryMethod = queryMethod(SampleRepository2.class, "method"); |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), |
|
|
|
|
|
|
|
context); |
|
|
|
|
|
|
|
MongoEntityMetadata<?> entityInformation = queryMethod.getEntityInformation(); |
|
|
|
MongoEntityMetadata<?> entityInformation = queryMethod.getEntityInformation(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Person.class))); |
|
|
|
assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Person.class))); |
|
|
|
@ -83,34 +79,44 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void discoversUserAsDomainTypeForGeoPageQueryMethod() throws Exception { |
|
|
|
public void discoversUserAsDomainTypeForGeoPageQueryMethod() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod queryMethod = queryMethod("findByLocationNear", Point.class, Distance.class, Pageable.class); |
|
|
|
MongoQueryMethod queryMethod = queryMethod(PersonRepository.class, "findByLocationNear", Point.class, |
|
|
|
|
|
|
|
Distance.class, Pageable.class); |
|
|
|
assertThat(queryMethod.isGeoNearQuery(), is(true)); |
|
|
|
assertThat(queryMethod.isGeoNearQuery(), is(true)); |
|
|
|
assertThat(queryMethod.isPageQuery(), is(true)); |
|
|
|
assertThat(queryMethod.isPageQuery(), is(true)); |
|
|
|
|
|
|
|
|
|
|
|
queryMethod = queryMethod("findByFirstname", String.class, Point.class); |
|
|
|
queryMethod = queryMethod(PersonRepository.class, "findByFirstname", String.class, Point.class); |
|
|
|
assertThat(queryMethod.isGeoNearQuery(), is(true)); |
|
|
|
assertThat(queryMethod.isGeoNearQuery(), is(true)); |
|
|
|
assertThat(queryMethod.isPageQuery(), is(false)); |
|
|
|
assertThat(queryMethod.isPageQuery(), is(false)); |
|
|
|
assertThat(queryMethod.getEntityInformation().getJavaType(), is(typeCompatibleWith(User.class))); |
|
|
|
assertThat(queryMethod.getEntityInformation().getJavaType(), is(typeCompatibleWith(User.class))); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(queryMethod("findByEmailAddress", String.class, Point.class).isGeoNearQuery(), is(true)); |
|
|
|
assertThat(queryMethod(PersonRepository.class, "findByEmailAddress", String.class, Point.class).isGeoNearQuery(), |
|
|
|
assertThat(queryMethod("findByFirstname", String.class, Point.class).isGeoNearQuery(), is(true)); |
|
|
|
is(true)); |
|
|
|
assertThat(queryMethod("findByLastname", String.class, Point.class).isGeoNearQuery(), is(true)); |
|
|
|
assertThat(queryMethod(PersonRepository.class, "findByFirstname", String.class, Point.class).isGeoNearQuery(), |
|
|
|
|
|
|
|
is(true)); |
|
|
|
|
|
|
|
assertThat(queryMethod(PersonRepository.class, "findByLastname", String.class, Point.class).isGeoNearQuery(), |
|
|
|
|
|
|
|
is(true)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
public void rejectsGeoPageQueryWithoutPageable() throws Exception { |
|
|
|
public void rejectsGeoPageQueryWithoutPageable() throws Exception { |
|
|
|
queryMethod("findByLocationNear", Point.class, Distance.class); |
|
|
|
queryMethod(PersonRepository.class, "findByLocationNear", Point.class, Distance.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
public void rejectsNullMappingContext() throws Exception { |
|
|
|
public void rejectsNullMappingContext() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
Method method = PersonRepository.class.getMethod("findByFirstname", String.class, Point.class); |
|
|
|
Method method = PersonRepository.class.getMethod("findByFirstname", String.class, Point.class); |
|
|
|
new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), null); |
|
|
|
|
|
|
|
|
|
|
|
new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), |
|
|
|
|
|
|
|
new SpelAwareProxyProjectionFactory(), null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void considersMethodReturningGeoPageAsPagingMethod() throws Exception { |
|
|
|
public void considersMethodReturningGeoPageAsPagingMethod() throws Exception { |
|
|
|
MongoQueryMethod method = queryMethod("findByLocationNear", Point.class, Distance.class, Pageable.class); |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod method = queryMethod(PersonRepository.class, "findByLocationNear", Point.class, Distance.class, |
|
|
|
|
|
|
|
Pageable.class); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(method.isPageQuery(), is(true)); |
|
|
|
assertThat(method.isPageQuery(), is(true)); |
|
|
|
assertThat(method.isCollectionQuery(), is(false)); |
|
|
|
assertThat(method.isCollectionQuery(), is(false)); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -118,8 +124,7 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void createsMongoQueryMethodObjectForMethodReturningAnInterface() throws Exception { |
|
|
|
public void createsMongoQueryMethodObjectForMethodReturningAnInterface() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
Method method = SampleRepository2.class.getMethod("methodReturningAnInterface"); |
|
|
|
queryMethod(SampleRepository2.class, "methodReturningAnInterface"); |
|
|
|
new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository2.class), context); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -128,7 +133,8 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void createsMongoQueryMethodWithEmptyMetaCorrectly() throws Exception { |
|
|
|
public void createsMongoQueryMethodWithEmptyMetaCorrectly() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod method = queryMethod("emptyMetaAnnotation"); |
|
|
|
MongoQueryMethod method = queryMethod(PersonRepository.class, "emptyMetaAnnotation"); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.getQueryMetaAttributes().hasValues(), is(false)); |
|
|
|
assertThat(method.getQueryMetaAttributes().hasValues(), is(false)); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -139,7 +145,8 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void createsMongoQueryMethodWithMaxExecutionTimeCorrectly() throws Exception { |
|
|
|
public void createsMongoQueryMethodWithMaxExecutionTimeCorrectly() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod method = queryMethod("metaWithMaxExecutionTime"); |
|
|
|
MongoQueryMethod method = queryMethod(PersonRepository.class, "metaWithMaxExecutionTime"); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.getQueryMetaAttributes().getMaxTimeMsec(), is(100L)); |
|
|
|
assertThat(method.getQueryMetaAttributes().getMaxTimeMsec(), is(100L)); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -150,7 +157,8 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void createsMongoQueryMethodWithMaxScanCorrectly() throws Exception { |
|
|
|
public void createsMongoQueryMethodWithMaxScanCorrectly() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod method = queryMethod("metaWithMaxScan"); |
|
|
|
MongoQueryMethod method = queryMethod(PersonRepository.class, "metaWithMaxScan"); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.getQueryMetaAttributes().getMaxScan(), is(10L)); |
|
|
|
assertThat(method.getQueryMetaAttributes().getMaxScan(), is(10L)); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -161,7 +169,8 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void createsMongoQueryMethodWithCommentCorrectly() throws Exception { |
|
|
|
public void createsMongoQueryMethodWithCommentCorrectly() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod method = queryMethod("metaWithComment"); |
|
|
|
MongoQueryMethod method = queryMethod(PersonRepository.class, "metaWithComment"); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.getQueryMetaAttributes().getComment(), is("foo bar")); |
|
|
|
assertThat(method.getQueryMetaAttributes().getComment(), is("foo bar")); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -172,7 +181,8 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void createsMongoQueryMethodWithSnapshotCorrectly() throws Exception { |
|
|
|
public void createsMongoQueryMethodWithSnapshotCorrectly() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod method = queryMethod("metaWithSnapshotUsage"); |
|
|
|
MongoQueryMethod method = queryMethod(PersonRepository.class, "metaWithSnapshotUsage"); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.hasQueryMetaAttributes(), is(true)); |
|
|
|
assertThat(method.getQueryMetaAttributes().getSnapshot(), is(true)); |
|
|
|
assertThat(method.getQueryMetaAttributes().getSnapshot(), is(true)); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -183,14 +193,16 @@ public class MongoQueryMethodUnitTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void fallsBackToRepositoryDomainTypeIfMethodDoesNotReturnADomainType() throws Exception { |
|
|
|
public void fallsBackToRepositoryDomainTypeIfMethodDoesNotReturnADomainType() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
MongoQueryMethod method = queryMethod("deleteByUserName", String.class); |
|
|
|
MongoQueryMethod method = queryMethod(PersonRepository.class, "deleteByUserName", String.class); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(method.getEntityInformation().getJavaType(), is(typeCompatibleWith(User.class))); |
|
|
|
assertThat(method.getEntityInformation().getJavaType(), is(typeCompatibleWith(User.class))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private MongoQueryMethod queryMethod(String name, Class<?>... parameters) throws Exception { |
|
|
|
private MongoQueryMethod queryMethod(Class<?> repository, String name, Class<?>... parameters) throws Exception { |
|
|
|
Method method = PersonRepository.class.getMethod(name, parameters); |
|
|
|
|
|
|
|
return new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), context); |
|
|
|
Method method = repository.getMethod(name, parameters); |
|
|
|
|
|
|
|
ProjectionFactory factory = new SpelAwareProxyProjectionFactory(); |
|
|
|
|
|
|
|
return new MongoQueryMethod(method, new DefaultRepositoryMetadata(repository), factory, context); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
interface PersonRepository extends Repository<User, Long> { |
|
|
|
interface PersonRepository extends Repository<User, Long> { |
|
|
|
|