Browse Source

DATAMONGO-990 - Polishing.

Removed EvaluationExpressionContext from all AbstractMongoQuery implementations that don't actually need it and from AbstractMongoQuery itself, too. Cleaned up test cases after that.

Moved SpEL related tests into AbstractPersonRepositoryIntegrationTests to make sure they're executed for all sub-types. JavaDoc and assertion polishes.

Original pull request: #285.
pull/299/merge
Oliver Gierke 11 years ago
parent
commit
2c27e8576f
  1. 20
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java
  2. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/ConvertingParameterAccessor.java
  3. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessor.java
  4. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQuery.java
  5. 59
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQuery.java
  6. 19
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java
  7. 90
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java
  8. 49
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepositoryIntegrationTests.java
  9. 3
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/config/MongoNamespaceIntegrationTests.java
  10. 19
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractMongoQueryUnitTests.java
  11. 12
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java
  12. 50
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQueryUnitTests.java
  13. 7
      spring-data-mongodb/src/test/resources/org/springframework/data/mongodb/repository/PersonRepositoryIntegrationTests-context.xml
  14. 2
      spring-data-mongodb/src/test/resources/org/springframework/data/mongodb/repository/config/MongoNamespaceIntegrationTests-context.xml

20
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java

@ -31,7 +31,6 @@ import org.springframework.data.geo.Point; @@ -31,7 +31,6 @@ import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.query.EvaluationContextProvider;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.util.CloseableIterator;
@ -52,25 +51,20 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { @@ -52,25 +51,20 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
private final MongoQueryMethod method;
private final MongoOperations operations;
private final EvaluationContextProvider evaluationContextProvider;
/**
* Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoOperations}.
*
* @param method must not be {@literal null}.
* @param operations must not be {@literal null}.
* @param evaluationContextProvider must not be {@literal null}.
*/
public AbstractMongoQuery(MongoQueryMethod method, MongoOperations operations,
EvaluationContextProvider evaluationContextProvider) {
public AbstractMongoQuery(MongoQueryMethod method, MongoOperations operations) {
Assert.notNull(operations);
Assert.notNull(method);
Assert.notNull(evaluationContextProvider, "ExpressionEvaluationContextProvider must not be null!");
Assert.notNull(operations, "MongoOperations must not be null!");
Assert.notNull(method, "MongoQueryMethod must not be null!");
this.method = method;
this.operations = operations;
this.evaluationContextProvider = evaluationContextProvider;
}
/*
@ -164,10 +158,6 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { @@ -164,10 +158,6 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
*/
protected abstract boolean isDeleteQuery();
public EvaluationContextProvider getEvaluationContextProvider() {
return evaluationContextProvider;
}
private abstract class Execution {
abstract Object execute(Query query);
@ -318,8 +308,8 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { @@ -318,8 +308,8 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
Object execute(Query query) {
MongoEntityMetadata<?> metadata = method.getEntityInformation();
return countProjection ? operations.count(query, metadata.getJavaType()) : operations.findOne(query,
metadata.getJavaType(), metadata.getCollectionName());
return countProjection ? operations.count(query, metadata.getJavaType())
: operations.findOne(query, metadata.getJavaType(), metadata.getCollectionName());
}
}

7
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/ConvertingParameterAccessor.java

@ -217,7 +217,7 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor { @@ -217,7 +217,7 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor {
/**
* Returns the given object as {@link Collection}. Will do a copy of it if it implements {@link Iterable} or is an
* array. Will return an empty {@link Collection} in case {@literal null} is given. Will wrap all other types into a
* single-element collction
* single-element collection.
*
* @param source
* @return
@ -240,8 +240,9 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor { @@ -240,8 +240,9 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor {
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.query.MongoParameterAccessor#getValues()
*/
@Override

11
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessor.java

@ -120,12 +120,13 @@ public class MongoParametersParameterAccessor extends ParametersParameterAccesso @@ -120,12 +120,13 @@ public class MongoParametersParameterAccessor extends ParametersParameterAccesso
return ((TextCriteria) fullText);
}
throw new IllegalArgumentException(String.format(
"Expected full text parameter to be one of String, Term or TextCriteria but found %s.",
ClassUtils.getShortName(fullText.getClass())));
throw new IllegalArgumentException(
String.format("Expected full text parameter to be one of String, Term or TextCriteria but found %s.",
ClassUtils.getShortName(fullText.getClass())));
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.query.MongoParameterAccessor#getValues()
*/
@Override

7
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQuery.java

@ -22,7 +22,6 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; @@ -22,7 +22,6 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.TextCriteria;
import org.springframework.data.repository.query.EvaluationContextProvider;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.parser.PartTree;
@ -48,12 +47,10 @@ public class PartTreeMongoQuery extends AbstractMongoQuery { @@ -48,12 +47,10 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
*
* @param method must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
* @param evaluationContextProvider must not be {@literal null}.
*/
public PartTreeMongoQuery(MongoQueryMethod method, MongoOperations mongoOperations,
EvaluationContextProvider evaluationContextProvider) {
public PartTreeMongoQuery(MongoQueryMethod method, MongoOperations mongoOperations) {
super(method, mongoOperations, evaluationContextProvider);
super(method, mongoOperations);
this.tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType());
this.isGeoNearQuery = method.isGeoNearQuery();
this.context = mongoOperations.getConverter().getMappingContext();

59
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQuery.java

@ -57,36 +57,40 @@ public class StringBasedMongoQuery extends AbstractMongoQuery { @@ -57,36 +57,40 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
private final List<ParameterBinding> queryParameterBindings;
private final List<ParameterBinding> fieldSpecParameterBindings;
private final SpelExpressionParser expressionParser;
private final EvaluationContextProvider evaluationContextProvider;
/**
* Creates a new {@link StringBasedMongoQuery} for the given {@link MongoQueryMethod} and {@link MongoOperations}.
*
* @param method must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
* @param evaluationContextProvider must not be {@literal null}.
* @param expressionParser must not be {@literal null}.
* @param evaluationContextProvider must not be {@literal null}.
*/
public StringBasedMongoQuery(MongoQueryMethod method, MongoOperations mongoOperations,
EvaluationContextProvider evaluationContextProvider, SpelExpressionParser expressionParser) {
this(method.getAnnotatedQuery(), method, mongoOperations, evaluationContextProvider, expressionParser);
SpelExpressionParser expressionParser, EvaluationContextProvider evaluationContextProvider) {
this(method.getAnnotatedQuery(), method, mongoOperations, expressionParser, evaluationContextProvider);
}
/**
* Creates a new {@link StringBasedMongoQuery} for the given {@link String}, {@link MongoQueryMethod} and
* {@link MongoOperations}.
*
* Creates a new {@link StringBasedMongoQuery} for the given {@link String}, {@link MongoQueryMethod},
* {@link MongoOperations}, {@link SpelExpressionParser} and {@link EvaluationContextProvider}.
*
* @param query must not be {@literal null}.
* @param method must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
* @param expressionParser must not be {@literal null}.
* @param template must not be {@literal null}.
*/
public StringBasedMongoQuery(String query, MongoQueryMethod method, MongoOperations mongoOperations,
EvaluationContextProvider evaluationContextProvider, SpelExpressionParser expressionParser) {
SpelExpressionParser expressionParser, EvaluationContextProvider evaluationContextProvider) {
super(method, mongoOperations, evaluationContextProvider);
super(method, mongoOperations);
Assert.notNull(query, "Query must not be null!");
Assert.notNull(expressionParser, "SpelExpressionParser must not be null!");
this.expressionParser = expressionParser;
this.evaluationContextProvider = evaluationContextProvider;
this.queryParameterBindings = new ArrayList<ParameterBinding>();
this.query = BINDING_PARSER.parseAndCollectParameterBindingsFromQueryIntoBindings(query,
@ -157,7 +161,8 @@ public class StringBasedMongoQuery extends AbstractMongoQuery { @@ -157,7 +161,8 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
* @param bindings
* @return
*/
private String replacePlaceholders(String input, ConvertingParameterAccessor accessor, List<ParameterBinding> bindings) {
private String replacePlaceholders(String input, ConvertingParameterAccessor accessor,
List<ParameterBinding> bindings) {
if (bindings.isEmpty()) {
return input;
@ -187,12 +192,8 @@ public class StringBasedMongoQuery extends AbstractMongoQuery { @@ -187,12 +192,8 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
*/
private String getParameterValueForBinding(ConvertingParameterAccessor accessor, ParameterBinding binding) {
Object value = null;
if (binding.isExpression()) {
value = evaluateExpression(binding.getExpression(), accessor.getValues());
} else {
value = accessor.getBindableValue(binding.getParameterIndex());
}
Object value = binding.isExpression() ? evaluateExpression(binding.getExpression(), accessor.getValues())
: accessor.getBindableValue(binding.getParameterIndex());
if (value instanceof String && binding.isQuoted()) {
return (String) value;
@ -210,8 +211,8 @@ public class StringBasedMongoQuery extends AbstractMongoQuery { @@ -210,8 +211,8 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
*/
private Object evaluateExpression(String expressionString, Object[] parameterValues) {
EvaluationContext evaluationContext = getEvaluationContextProvider().getEvaluationContext(
getQueryMethod().getParameters(), parameterValues);
EvaluationContext evaluationContext = evaluationContextProvider
.getEvaluationContext(getQueryMethod().getParameters(), parameterValues);
Expression expression = expressionParser.parseExpression(expressionString);
return expression.getValue(evaluationContext, Object.class);
}
@ -237,8 +238,8 @@ public class StringBasedMongoQuery extends AbstractMongoQuery { @@ -237,8 +238,8 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
* Returns a list of {@link ParameterBinding}s found in the given {@code input} or an
* {@link Collections#emptyList()}.
*
* @param input
* @param bindings
* @param input can be {@literal null} or empty.
* @param bindings must not be {@literal null}.
* @return
*/
public String parseAndCollectParameterBindingsFromQueryIntoBindings(String input, List<ParameterBinding> bindings) {
@ -247,8 +248,9 @@ public class StringBasedMongoQuery extends AbstractMongoQuery { @@ -247,8 +248,9 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
return input;
}
String transformedInput = transformQueryAndCollectExpressionParametersIntoBindings(bindings, input);
Assert.notNull(bindings, "Parameter bindings must not be null!");
String transformedInput = transformQueryAndCollectExpressionParametersIntoBindings(input, bindings);
String parseableInput = makeParameterReferencesParseable(transformedInput);
collectParameterReferencesIntoBindings(bindings, JSON.parse(parseableInput));
@ -256,28 +258,26 @@ public class StringBasedMongoQuery extends AbstractMongoQuery { @@ -256,28 +258,26 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
return transformedInput;
}
private String transformQueryAndCollectExpressionParametersIntoBindings(List<ParameterBinding> bindings,
String input) {
private String transformQueryAndCollectExpressionParametersIntoBindings(String input,
List<ParameterBinding> bindings) {
Matcher matcher = PARAMETER_EXPRESSION_PATTERN.matcher(input);
StringBuilder result = new StringBuilder();
int lastPos = 0;
int exprIndex = 0;
while (matcher.find()) {
int startOffSet = matcher.start();
result.append(input.subSequence(lastPos, startOffSet));
String expression = matcher.group(3);
result.append(input.subSequence(lastPos, startOffSet));
result.append("'?expr").append(exprIndex).append("'");
lastPos = matcher.end();
bindings.add(new ParameterBinding(exprIndex, true, expression));
bindings.add(new ParameterBinding(exprIndex, true, matcher.group(3)));
exprIndex++;
}
@ -305,9 +305,10 @@ public class StringBasedMongoQuery extends AbstractMongoQuery { @@ -305,9 +305,10 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
} else if (value instanceof Pattern) {
String string = ((Pattern) value).toString().trim();
Matcher valueMatcher = PARSEABLE_BINDING_PATTERN.matcher(string);
while (valueMatcher.find()) {
int paramIndex = Integer.parseInt(valueMatcher.group(PARAMETER_INDEX_GROUP));
/*

19
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
*/
package org.springframework.data.mongodb.repository.support;
import static org.springframework.data.querydsl.QueryDslUtils.QUERY_DSL_PRESENT;
import static org.springframework.data.querydsl.QueryDslUtils.*;
import java.io.Serializable;
import java.lang.reflect.Method;
@ -51,7 +51,7 @@ import org.springframework.util.Assert; @@ -51,7 +51,7 @@ import org.springframework.util.Assert;
public class MongoRepositoryFactory extends RepositoryFactorySupport {
private static final SpelExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();
private final MongoOperations mongoOperations;
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
@ -92,8 +92,8 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { @@ -92,8 +92,8 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
return getTargetRepositoryViaReflection(information, entityInformation, mongoOperations);
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key, org.springframework.data.repository.query.EvaluationContextProvider)
*/
@Override
@ -112,8 +112,8 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { @@ -112,8 +112,8 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(domainClass);
if (entity == null) {
throw new MappingException(String.format("Could not lookup mapping metadata for domain class %s!",
domainClass.getName()));
throw new MappingException(
String.format("Could not lookup mapping metadata for domain class %s!", domainClass.getName()));
}
return new MappingMongoEntityInformation<T, ID>((MongoPersistentEntity<T>) entity);
@ -144,11 +144,12 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { @@ -144,11 +144,12 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
if (namedQueries.hasQuery(namedQueryName)) {
String namedQuery = namedQueries.getQuery(namedQueryName);
return new StringBasedMongoQuery(namedQuery, queryMethod, mongoOperations, evaluationContextProvider, EXPRESSION_PARSER);
return new StringBasedMongoQuery(namedQuery, queryMethod, mongoOperations, EXPRESSION_PARSER,
evaluationContextProvider);
} else if (queryMethod.hasAnnotatedQuery()) {
return new StringBasedMongoQuery(queryMethod, mongoOperations, evaluationContextProvider, EXPRESSION_PARSER);
return new StringBasedMongoQuery(queryMethod, mongoOperations, EXPRESSION_PARSER, evaluationContextProvider);
} else {
return new PartTreeMongoQuery(queryMethod, mongoOperations, evaluationContextProvider);
return new PartTreeMongoQuery(queryMethod, mongoOperations);
}
}
}

90
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

@ -52,6 +52,7 @@ import org.springframework.data.geo.Polygon; @@ -52,6 +52,7 @@ import org.springframework.data.geo.Polygon;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.repository.Person.Sex;
import org.springframework.data.mongodb.repository.SampleEvaluationContextExtension.SampleSecurityContextHolder;
import org.springframework.data.querydsl.QSort;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -174,8 +175,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -174,8 +175,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Test
public void executesPagedFinderCorrectly() throws Exception {
Page<Person> page = repository.findByLastnameLike("*a*", new PageRequest(0, 2, Direction.ASC, "lastname",
"firstname"));
Page<Person> page = repository.findByLastnameLike("*a*",
new PageRequest(0, 2, Direction.ASC, "lastname", "firstname"));
assertThat(page.isFirst(), is(true));
assertThat(page.isLast(), is(false));
assertThat(page.getNumberOfElements(), is(2));
@ -185,8 +186,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -185,8 +186,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Test
public void executesPagedFinderWithAnnotatedQueryCorrectly() throws Exception {
Page<Person> page = repository.findByLastnameLikeWithPageable(".*a.*", new PageRequest(0, 2, Direction.ASC,
"lastname", "firstname"));
Page<Person> page = repository.findByLastnameLikeWithPageable(".*a.*",
new PageRequest(0, 2, Direction.ASC, "lastname", "firstname"));
assertThat(page.isFirst(), is(true));
assertThat(page.isLast(), is(false));
assertThat(page.getNumberOfElements(), is(2));
@ -310,8 +311,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -310,8 +311,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Test
public void findsPagedPeopleByPredicate() throws Exception {
Page<Person> page = repository.findAll(person.lastname.contains("a"), new PageRequest(0, 2, Direction.ASC,
"lastname"));
Page<Person> page = repository.findAll(person.lastname.contains("a"),
new PageRequest(0, 2, Direction.ASC, "lastname"));
assertThat(page.isFirst(), is(true));
assertThat(page.isLast(), is(false));
assertThat(page.getNumberOfElements(), is(2));
@ -397,8 +398,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -397,8 +398,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
dave.setLocation(point);
repository.save(dave);
GeoResults<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73), new Distance(2000,
Metrics.KILOMETERS));
GeoResults<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73),
new Distance(2000, Metrics.KILOMETERS));
assertThat(results.getContent().isEmpty(), is(false));
}
@ -409,8 +410,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -409,8 +410,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
dave.setLocation(point);
repository.save(dave);
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73), new Distance(2000,
Metrics.KILOMETERS), new PageRequest(0, 20));
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73),
new Distance(2000, Metrics.KILOMETERS), new PageRequest(0, 20));
assertThat(results.getContent().isEmpty(), is(false));
// DATAMONGO-607
@ -620,8 +621,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -620,8 +621,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
repository.save(Arrays.asList(dave, oliver, carter, boyd, leroi));
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73), new Distance(2000,
Metrics.KILOMETERS), new PageRequest(1, 2));
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73),
new Distance(2000, Metrics.KILOMETERS), new PageRequest(1, 2));
assertThat(results.getContent().isEmpty(), is(false));
assertThat(results.getNumberOfElements(), is(2));
@ -645,8 +646,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -645,8 +646,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
repository.save(Arrays.asList(dave, oliver, carter));
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73), new Distance(2000,
Metrics.KILOMETERS), new PageRequest(1, 2));
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73),
new Distance(2000, Metrics.KILOMETERS), new PageRequest(1, 2));
assertThat(results.getContent().isEmpty(), is(false));
assertThat(results.getNumberOfElements(), is(1));
assertThat(results.isFirst(), is(false));
@ -664,8 +665,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -664,8 +665,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
dave.setLocation(point);
repository.save(dave);
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73), new Distance(2000,
Metrics.KILOMETERS), new PageRequest(0, 2));
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73),
new Distance(2000, Metrics.KILOMETERS), new PageRequest(0, 2));
assertThat(results.getContent().isEmpty(), is(false));
assertThat(results.getNumberOfElements(), is(1));
@ -683,8 +684,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -683,8 +684,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
dave.setLocation(new Point(-73.99171, 40.738868));
repository.save(dave);
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73), new Distance(2000,
Metrics.KILOMETERS), new PageRequest(1, 2));
GeoPage<Person> results = repository.findByLocationNear(new Point(-73.99, 40.73),
new Distance(2000, Metrics.KILOMETERS), new PageRequest(1, 2));
assertThat(results.getContent().isEmpty(), is(true));
assertThat(results.getNumberOfElements(), is(0));
@ -934,8 +935,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -934,8 +935,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Test
public void shouldLimitCollectionQueryToMaxResultsWhenPresent() {
repository.save(Arrays.asList(new Person("Bob-1", "Dylan"), new Person("Bob-2", "Dylan"), new Person("Bob-3",
"Dylan"), new Person("Bob-4", "Dylan"), new Person("Bob-5", "Dylan")));
repository.save(Arrays.asList(new Person("Bob-1", "Dylan"), new Person("Bob-2", "Dylan"),
new Person("Bob-3", "Dylan"), new Person("Bob-4", "Dylan"), new Person("Bob-5", "Dylan")));
List<Person> result = repository.findTop3ByLastnameStartingWith("Dylan");
assertThat(result.size(), is(3));
}
@ -946,8 +947,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -946,8 +947,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Test
public void shouldNotLimitPagedQueryWhenPageRequestWithinBounds() {
repository.save(Arrays.asList(new Person("Bob-1", "Dylan"), new Person("Bob-2", "Dylan"), new Person("Bob-3",
"Dylan"), new Person("Bob-4", "Dylan"), new Person("Bob-5", "Dylan")));
repository.save(Arrays.asList(new Person("Bob-1", "Dylan"), new Person("Bob-2", "Dylan"),
new Person("Bob-3", "Dylan"), new Person("Bob-4", "Dylan"), new Person("Bob-5", "Dylan")));
Page<Person> result = repository.findTop3ByLastnameStartingWith("Dylan", new PageRequest(0, 2));
assertThat(result.getContent().size(), is(2));
}
@ -958,8 +959,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -958,8 +959,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Test
public void shouldLimitPagedQueryWhenPageRequestExceedsUpperBoundary() {
repository.save(Arrays.asList(new Person("Bob-1", "Dylan"), new Person("Bob-2", "Dylan"), new Person("Bob-3",
"Dylan"), new Person("Bob-4", "Dylan"), new Person("Bob-5", "Dylan")));
repository.save(Arrays.asList(new Person("Bob-1", "Dylan"), new Person("Bob-2", "Dylan"),
new Person("Bob-3", "Dylan"), new Person("Bob-4", "Dylan"), new Person("Bob-5", "Dylan")));
Page<Person> result = repository.findTop3ByLastnameStartingWith("Dylan", new PageRequest(1, 2));
assertThat(result.getContent().size(), is(1));
}
@ -970,8 +971,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -970,8 +971,8 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Test
public void shouldReturnEmptyWhenPageRequestedPageIsTotallyOutOfScopeForLimit() {
repository.save(Arrays.asList(new Person("Bob-1", "Dylan"), new Person("Bob-2", "Dylan"), new Person("Bob-3",
"Dylan"), new Person("Bob-4", "Dylan"), new Person("Bob-5", "Dylan")));
repository.save(Arrays.asList(new Person("Bob-1", "Dylan"), new Person("Bob-2", "Dylan"),
new Person("Bob-3", "Dylan"), new Person("Bob-4", "Dylan"), new Person("Bob-5", "Dylan")));
Page<Person> result = repository.findTop3ByLastnameStartingWith("Dylan", new PageRequest(2, 2));
assertThat(result.getContent().size(), is(0));
}
@ -1183,4 +1184,41 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -1183,4 +1184,41 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
GeoResults<Person> results = repository.findPersonByLocationNear(new Point(-73.99, 40.73), range);
assertThat(results.getContent().isEmpty(), is(false));
}
/**
* @see DATAMONGO-990
*/
@Test
public void shouldFindByFirstnameForSpELExpressionWithParameterIndexOnly() {
List<Person> users = repository.findWithSpelByFirstnameForSpELExpressionWithParameterIndexOnly("Dave");
assertThat(users, hasSize(1));
assertThat(users.get(0), is(dave));
}
/**
* @see DATAMONGO-990
*/
@Test
public void shouldFindByFirstnameAndCurrentUserWithCustomQuery() {
SampleSecurityContextHolder.getCurrent().setPrincipal(dave);
List<Person> users = repository.findWithSpelByFirstnameAndCurrentUserWithCustomQuery("Dave");
assertThat(users, hasSize(1));
assertThat(users.get(0), is(dave));
}
/**
* @see DATAMONGO-990
*/
@Test
public void shouldFindByFirstnameForSpELExpressionWithParameterVariableOnly() {
List<Person> users = repository.findWithSpelByFirstnameForSpELExpressionWithParameterVariableOnly("Dave");
assertThat(users, hasSize(1));
assertThat(users.get(0), is(dave));
}
}

49
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepositoryIntegrationTests.java

@ -15,14 +15,6 @@ @@ -15,14 +15,6 @@
*/
package org.springframework.data.mongodb.repository;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.junit.Test;
import org.springframework.data.mongodb.repository.SampleEvaluationContextExtension.SampleSecurityContextHolder;
import org.springframework.test.context.ContextConfiguration;
/**
@ -32,43 +24,4 @@ import org.springframework.test.context.ContextConfiguration; @@ -32,43 +24,4 @@ import org.springframework.test.context.ContextConfiguration;
* @author Thomas Darimont
*/
@ContextConfiguration
public class PersonRepositoryIntegrationTests extends AbstractPersonRepositoryIntegrationTests {
/**
* @see DATAMONGO-990
*/
@Test
public void shouldFindByFirstnameForSpELExpressionWithParameterIndexOnly() {
List<Person> users = repository.findWithSpelByFirstnameForSpELExpressionWithParameterIndexOnly("Dave");
assertThat(users, hasSize(1));
assertThat(users.get(0), is(dave));
}
/**
* @see DATAMONGO-990
*/
@Test
public void shouldFindByFirstnameAndCurrentUserWithCustomQuery() {
SampleSecurityContextHolder.getCurrent().setPrincipal(dave);
List<Person> users = repository.findWithSpelByFirstnameAndCurrentUserWithCustomQuery("Dave");
assertThat(users, hasSize(1));
assertThat(users.get(0), is(dave));
}
/**
* @see DATAMONGO-990
*/
@Test
public void shouldFindByFirstnameForSpELExpressionWithParameterVariableOnly() {
List<Person> users = repository.findWithSpelByFirstnameForSpELExpressionWithParameterVariableOnly("Dave");
assertThat(users, hasSize(1));
assertThat(users.get(0), is(dave));
}
}
public class PersonRepositoryIntegrationTests extends AbstractPersonRepositoryIntegrationTests {}

3
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/config/MongoNamespaceIntegrationTests.java

@ -45,8 +45,7 @@ public class MongoNamespaceIntegrationTests extends AbstractPersonRepositoryInte @@ -45,8 +45,7 @@ public class MongoNamespaceIntegrationTests extends AbstractPersonRepositoryInte
DefaultListableBeanFactory factory;
BeanDefinitionReader reader;
@Autowired
ApplicationContext context;
@Autowired ApplicationContext context;
@Before
@Override

19
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractMongoQueryUnitTests.java

@ -15,14 +15,10 @@ @@ -15,14 +15,10 @@
*/
package org.springframework.data.mongodb.repository.query;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.Arrays;
@ -58,7 +54,6 @@ import org.springframework.data.mongodb.core.query.Query; @@ -58,7 +54,6 @@ import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.Meta;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
@ -148,8 +143,8 @@ public class AbstractMongoQueryUnitTests { @@ -148,8 +143,8 @@ public class AbstractMongoQueryUnitTests {
public void testDeleteExecutionReturnsNrDocumentsDeletedFromWriteResult() {
when(writeResultMock.getN()).thenReturn(100);
when(this.mongoOperationsMock.remove(Matchers.any(Query.class), eq(Person.class), eq("persons"))).thenReturn(
writeResultMock);
when(this.mongoOperationsMock.remove(Matchers.any(Query.class), eq(Person.class), eq("persons")))
.thenReturn(writeResultMock);
MongoQueryFake query = createQueryForMethod("deletePersonByLastname", String.class);
query.setDeleteQuery(true);
@ -320,7 +315,7 @@ public class AbstractMongoQueryUnitTests { @@ -320,7 +315,7 @@ public class AbstractMongoQueryUnitTests {
private boolean isDeleteQuery;
public MongoQueryFake(MongoQueryMethod method, MongoOperations operations) {
super(method, operations, DefaultEvaluationContextProvider.INSTANCE);
super(method, operations);
}
@Override

12
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java

@ -15,11 +15,10 @@ @@ -15,11 +15,10 @@
*/
package org.springframework.data.mongodb.repository.query;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.data.mongodb.core.query.IsTextQuery.isTextQuery;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.mongodb.core.query.IsTextQuery.*;
import java.lang.reflect.Method;
@ -44,7 +43,6 @@ import org.springframework.data.mongodb.repository.MongoRepository; @@ -44,7 +43,6 @@ import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Person;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.util.JSONParseException;
@ -172,7 +170,7 @@ public class PartTreeMongoQueryUnitTests { @@ -172,7 +170,7 @@ public class PartTreeMongoQueryUnitTests {
Method method = Repo.class.getMethod(methodName, paramTypes);
MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadataMock, mappingContext);
return new PartTreeMongoQuery(queryMethod, mongoOperationsMock, DefaultEvaluationContextProvider.INSTANCE);
return new PartTreeMongoQuery(queryMethod, mongoOperationsMock);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(e.getMessage(), e);
} catch (SecurityException e) {

50
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQueryUnitTests.java

@ -15,10 +15,9 @@ @@ -15,10 +15,9 @@
*/
package org.springframework.data.mongodb.repository.query;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.Collections;
@ -61,7 +60,7 @@ import com.mongodb.DBRef; @@ -61,7 +60,7 @@ import com.mongodb.DBRef;
public class StringBasedMongoQueryUnitTests {
SpelExpressionParser PARSER = new SpelExpressionParser();
@Mock MongoOperations operations;
@Mock RepositoryMetadata metadata;
@Mock DbRefResolver factory;
@ -81,8 +80,8 @@ public class StringBasedMongoQueryUnitTests { @@ -81,8 +80,8 @@ public class StringBasedMongoQueryUnitTests {
Method method = SampleRepository.class.getMethod("findByLastname", String.class);
MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, converter.getMappingContext());
StringBasedMongoQuery mongoQuery = new StringBasedMongoQuery(queryMethod, operations,
DefaultEvaluationContextProvider.INSTANCE, PARSER);
StringBasedMongoQuery mongoQuery = new StringBasedMongoQuery(queryMethod, operations, PARSER,
DefaultEvaluationContextProvider.INSTANCE);
ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, "Matthews");
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accesor);
@ -215,8 +214,9 @@ public class StringBasedMongoQueryUnitTests { @@ -215,8 +214,9 @@ public class StringBasedMongoQueryUnitTests {
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accessor);
assertThat(query.getQueryObject(), is(new BasicQuery(
"{$where: 'return this.date.getUTCMonth() == 3 && this.date.getUTCDay() == 4;'}").getQueryObject()));
assertThat(query.getQueryObject(),
is(new BasicQuery("{$where: 'return this.date.getUTCMonth() == 3 && this.date.getUTCDay() == 4;'}")
.getQueryObject()));
}
/**
@ -264,21 +264,6 @@ public class StringBasedMongoQueryUnitTests { @@ -264,21 +264,6 @@ public class StringBasedMongoQueryUnitTests {
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
}
/**
* @see DATAMONGO-990
*/
@Test
public void shouldSupportExpressionsInCustomQueries() throws Exception {
ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, "Matthews");
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByQueryWithExpression", String.class);
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accesor);
org.springframework.data.mongodb.core.query.Query reference = new BasicQuery("{'lastname' : 'Matthews'}");
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
}
/**
* @see DATAMONGO-1070
*/
@ -310,11 +295,26 @@ public class StringBasedMongoQueryUnitTests { @@ -310,11 +295,26 @@ public class StringBasedMongoQueryUnitTests {
assertThat(query.getQueryObject(), is(new BasicDBObjectBuilder().add("key", "value").get()));
}
/**
* @see DATAMONGO-990
*/
@Test
public void shouldSupportExpressionsInCustomQueries() throws Exception {
ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, "Matthews");
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByQueryWithExpression", String.class);
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accesor);
org.springframework.data.mongodb.core.query.Query reference = new BasicQuery("{'lastname' : 'Matthews'}");
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
}
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
Method method = SampleRepository.class.getMethod(name, parameters);
MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, converter.getMappingContext());
return new StringBasedMongoQuery(queryMethod, operations, DefaultEvaluationContextProvider.INSTANCE, PARSER);
return new StringBasedMongoQuery(queryMethod, operations, PARSER, DefaultEvaluationContextProvider.INSTANCE);
}
private interface SampleRepository {

7
spring-data-mongodb/src/test/resources/org/springframework/data/mongodb/repository/PersonRepositoryIntegrationTests-context.xml

@ -29,9 +29,10 @@ @@ -29,9 +29,10 @@
<property name="evaluationContextProvider" ref="extensionAwareEvaluationContextProvider"/>
</bean>
<bean id="sampleEvaluationContextExtension" class="org.springframework.data.mongodb.repository.SampleEvaluationContextExtension"/>
<bean id="extensionAwareEvaluationContextProvider" class="org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider">
<constructor-arg ref="sampleEvaluationContextExtension"/>
<constructor-arg>
<bean class="org.springframework.data.mongodb.repository.SampleEvaluationContextExtension"/>
</constructor-arg>
</bean>
</beans>

2
spring-data-mongodb/src/test/resources/org/springframework/data/mongodb/repository/config/MongoNamespaceIntegrationTests-context.xml

@ -22,4 +22,6 @@ @@ -22,4 +22,6 @@
<repository:exclude-filter type="regex" expression=".*MongoRepository"/>
</mongo:repositories>
<bean class="org.springframework.data.mongodb.repository.SampleEvaluationContextExtension"/>
</beans>

Loading…
Cancel
Save