DATAMONGO-1068 - Fix getCritieriaObject returns empty DBO when no key defined.
We now check for the presence of a Critieria key. Original pull request: #232.
This commit is contained in:
committed by
Thomas Darimont
parent
a6728f851b
commit
914acfea16
@@ -31,7 +31,9 @@ import org.springframework.data.geo.Shape;
|
|||||||
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
|
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
|
||||||
import org.springframework.data.mongodb.core.geo.Sphere;
|
import org.springframework.data.mongodb.core.geo.Sphere;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.mongodb.BasicDBList;
|
import com.mongodb.BasicDBList;
|
||||||
import com.mongodb.BasicDBObject;
|
import com.mongodb.BasicDBObject;
|
||||||
@@ -529,8 +531,11 @@ public class Criteria implements CriteriaDefinition {
|
|||||||
* @see org.springframework.data.mongodb.core.query.CriteriaDefinition#getCriteriaObject()
|
* @see org.springframework.data.mongodb.core.query.CriteriaDefinition#getCriteriaObject()
|
||||||
*/
|
*/
|
||||||
public DBObject getCriteriaObject() {
|
public DBObject getCriteriaObject() {
|
||||||
|
|
||||||
if (this.criteriaChain.size() == 1) {
|
if (this.criteriaChain.size() == 1) {
|
||||||
return criteriaChain.get(0).getSingleCriteriaObject();
|
return criteriaChain.get(0).getSingleCriteriaObject();
|
||||||
|
} else if (CollectionUtils.isEmpty(this.criteriaChain) && !CollectionUtils.isEmpty(this.criteria)) {
|
||||||
|
return getSingleCriteriaObject();
|
||||||
} else {
|
} else {
|
||||||
DBObject criteriaObject = new BasicDBObject();
|
DBObject criteriaObject = new BasicDBObject();
|
||||||
for (Criteria c : this.criteriaChain) {
|
for (Criteria c : this.criteriaChain) {
|
||||||
@@ -564,6 +569,13 @@ public class Criteria implements CriteriaDefinition {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!StringUtils.hasText(this.key)) {
|
||||||
|
if (not) {
|
||||||
|
return new BasicDBObject("$not", dbo);
|
||||||
|
}
|
||||||
|
return dbo;
|
||||||
|
}
|
||||||
|
|
||||||
DBObject queryCriteria = new BasicDBObject();
|
DBObject queryCriteria = new BasicDBObject();
|
||||||
|
|
||||||
if (!NOT_SET.equals(isValue)) {
|
if (!NOT_SET.equals(isValue)) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2013 the original author or authors.
|
* Copyright 2010-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -22,12 +22,14 @@ import org.junit.Test;
|
|||||||
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
|
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
|
||||||
|
|
||||||
import com.mongodb.BasicDBObject;
|
import com.mongodb.BasicDBObject;
|
||||||
|
import com.mongodb.BasicDBObjectBuilder;
|
||||||
import com.mongodb.DBObject;
|
import com.mongodb.DBObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
* @author Thomas Darimont
|
* @author Thomas Darimont
|
||||||
*/
|
* @author Christoph Strobl
|
||||||
|
*/
|
||||||
public class CriteriaTests {
|
public class CriteriaTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -72,50 +74,94 @@ public class CriteriaTests {
|
|||||||
assertThat(left, is(not(right)));
|
assertThat(left, is(not(right)));
|
||||||
assertThat(right, is(not(left)));
|
assertThat(right, is(not(left)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATAMONGO-507
|
* @see DATAMONGO-507
|
||||||
*/
|
*/
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void shouldThrowExceptionWhenTryingToNegateAndOperation() {
|
public void shouldThrowExceptionWhenTryingToNegateAndOperation() {
|
||||||
|
|
||||||
new Criteria() //
|
new Criteria() //
|
||||||
.not() //
|
.not() //
|
||||||
.andOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
|
.andOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATAMONGO-507
|
* @see DATAMONGO-507
|
||||||
*/
|
*/
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void shouldThrowExceptionWhenTryingToNegateOrOperation() {
|
public void shouldThrowExceptionWhenTryingToNegateOrOperation() {
|
||||||
|
|
||||||
new Criteria() //
|
new Criteria() //
|
||||||
.not() //
|
.not() //
|
||||||
.orOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
|
.orOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATAMONGO-507
|
* @see DATAMONGO-507
|
||||||
*/
|
*/
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void shouldThrowExceptionWhenTryingToNegateNorOperation() {
|
public void shouldThrowExceptionWhenTryingToNegateNorOperation() {
|
||||||
|
|
||||||
new Criteria() //
|
new Criteria() //
|
||||||
.not() //
|
.not() //
|
||||||
.norOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
|
.norOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATAMONGO-507
|
* @see DATAMONGO-507
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void shouldNegateFollowingSimpleExpression() {
|
public void shouldNegateFollowingSimpleExpression() {
|
||||||
|
|
||||||
Criteria c = Criteria.where("age").not().gt(18).and("status").is("student");
|
Criteria c = Criteria.where("age").not().gt(18).and("status").is("student");
|
||||||
DBObject co = c.getCriteriaObject();
|
DBObject co = c.getCriteriaObject();
|
||||||
|
|
||||||
assertThat(co, is(notNullValue()));
|
assertThat(co, is(notNullValue()));
|
||||||
assertThat(co.toString(), is("{ \"age\" : { \"$not\" : { \"$gt\" : 18}} , \"status\" : \"student\"}"));
|
assertThat(co.toString(), is("{ \"age\" : { \"$not\" : { \"$gt\" : 18}} , \"status\" : \"student\"}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-1068
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getCriteriaObjectShouldReturnEmptyDBOWhenNoCriteriaSpecified() {
|
||||||
|
|
||||||
|
DBObject dbo = new Criteria().getCriteriaObject();
|
||||||
|
|
||||||
|
assertThat(dbo, equalTo(new BasicDBObjectBuilder().get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-1068
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getCriteriaObjectShouldUseCritieraValuesWhenNoKeyIsPresent() {
|
||||||
|
|
||||||
|
DBObject dbo = new Criteria().lt("foo").getCriteriaObject();
|
||||||
|
|
||||||
|
assertThat(dbo, equalTo(new BasicDBObjectBuilder().add("$lt", "foo").get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-1068
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getCriteriaObjectShouldUseCritieraValuesWhenNoKeyIsPresentButMultipleCriteriasPresent() {
|
||||||
|
|
||||||
|
DBObject dbo = new Criteria().lt("foo").gt("bar").getCriteriaObject();
|
||||||
|
|
||||||
|
assertThat(dbo, equalTo(new BasicDBObjectBuilder().add("$lt", "foo").add("$gt", "bar").get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-1068
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void getCriteriaObjectShouldRespectNotWhenNoKeyPresent() {
|
||||||
|
|
||||||
|
DBObject dbo = new Criteria().lt("foo").not().getCriteriaObject();
|
||||||
|
|
||||||
|
assertThat(dbo, equalTo(new BasicDBObjectBuilder().add("$not", new BasicDBObject("$lt", "foo")).get()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user