Skip to content

Commit 6cda9ab

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
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.
1 parent 831d667 commit 6cda9ab

File tree

2 files changed

+109
-51
lines changed
  • spring-data-mongodb/src

2 files changed

+109
-51
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
3232
import org.springframework.data.mongodb.core.geo.Sphere;
3333
import org.springframework.util.Assert;
34+
import org.springframework.util.CollectionUtils;
3435
import org.springframework.util.ObjectUtils;
36+
import org.springframework.util.StringUtils;
3537

3638
import com.mongodb.BasicDBList;
3739
import com.mongodb.BasicDBObject;
@@ -515,8 +517,11 @@ public String getKey() {
515517
* @see org.springframework.data.mongodb.core.query.CriteriaDefinition#getCriteriaObject()
516518
*/
517519
public DBObject getCriteriaObject() {
520+
518521
if (this.criteriaChain.size() == 1) {
519522
return criteriaChain.get(0).getSingleCriteriaObject();
523+
} else if (CollectionUtils.isEmpty(this.criteriaChain) && !CollectionUtils.isEmpty(this.criteria)) {
524+
return getSingleCriteriaObject();
520525
} else {
521526
DBObject criteriaObject = new BasicDBObject();
522527
for (Criteria c : this.criteriaChain) {
@@ -550,6 +555,13 @@ protected DBObject getSingleCriteriaObject() {
550555
}
551556
}
552557

558+
if (!StringUtils.hasText(this.key)) {
559+
if (not) {
560+
return new BasicDBObject("$not", dbo);
561+
}
562+
return dbo;
563+
}
564+
553565
DBObject queryCriteria = new BasicDBObject();
554566

555567
if (!NOT_SET.equals(isValue)) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaTests.java

Lines changed: 97 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2013 the original author or authors.
2+
* Copyright 2010-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,12 +22,14 @@
2222
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
2323

2424
import com.mongodb.BasicDBObject;
25+
import com.mongodb.BasicDBObjectBuilder;
2526
import com.mongodb.DBObject;
2627

27-
/**
28-
* @author Oliver Gierke
29-
* @author Thomas Darimont
30-
*/
28+
/**
29+
* @author Oliver Gierke
30+
* @author Thomas Darimont
31+
* @author Christoph Strobl
32+
*/
3133
public class CriteriaTests {
3234

3335
@Test
@@ -72,50 +74,94 @@ public void equalIfCriteriaMatches() {
7274
assertThat(left, is(not(right)));
7375
assertThat(right, is(not(left)));
7476
}
75-
76-
/**
77-
* @see DATAMONGO-507
78-
*/
79-
@Test(expected = IllegalArgumentException.class)
80-
public void shouldThrowExceptionWhenTryingToNegateAndOperation() {
81-
82-
new Criteria() //
83-
.not() //
84-
.andOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
85-
}
86-
87-
/**
88-
* @see DATAMONGO-507
89-
*/
90-
@Test(expected = IllegalArgumentException.class)
91-
public void shouldThrowExceptionWhenTryingToNegateOrOperation() {
92-
93-
new Criteria() //
94-
.not() //
95-
.orOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
96-
}
97-
98-
/**
99-
* @see DATAMONGO-507
100-
*/
101-
@Test(expected = IllegalArgumentException.class)
102-
public void shouldThrowExceptionWhenTryingToNegateNorOperation() {
103-
104-
new Criteria() //
105-
.not() //
106-
.norOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
107-
}
108-
109-
/**
110-
* @see DATAMONGO-507
111-
*/
112-
@Test
113-
public void shouldNegateFollowingSimpleExpression() {
114-
115-
Criteria c = Criteria.where("age").not().gt(18).and("status").is("student");
116-
DBObject co = c.getCriteriaObject();
117-
118-
assertThat(co, is(notNullValue()));
119-
assertThat(co.toString(), is("{ \"age\" : { \"$not\" : { \"$gt\" : 18}} , \"status\" : \"student\"}"));
120-
}
77+
78+
/**
79+
* @see DATAMONGO-507
80+
*/
81+
@Test(expected = IllegalArgumentException.class)
82+
public void shouldThrowExceptionWhenTryingToNegateAndOperation() {
83+
84+
new Criteria() //
85+
.not() //
86+
.andOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
87+
}
88+
89+
/**
90+
* @see DATAMONGO-507
91+
*/
92+
@Test(expected = IllegalArgumentException.class)
93+
public void shouldThrowExceptionWhenTryingToNegateOrOperation() {
94+
95+
new Criteria() //
96+
.not() //
97+
.orOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
98+
}
99+
100+
/**
101+
* @see DATAMONGO-507
102+
*/
103+
@Test(expected = IllegalArgumentException.class)
104+
public void shouldThrowExceptionWhenTryingToNegateNorOperation() {
105+
106+
new Criteria() //
107+
.not() //
108+
.norOperator(Criteria.where("delete").is(true).and("_id").is(42)); //
109+
}
110+
111+
/**
112+
* @see DATAMONGO-507
113+
*/
114+
@Test
115+
public void shouldNegateFollowingSimpleExpression() {
116+
117+
Criteria c = Criteria.where("age").not().gt(18).and("status").is("student");
118+
DBObject co = c.getCriteriaObject();
119+
120+
assertThat(co, is(notNullValue()));
121+
assertThat(co.toString(), is("{ \"age\" : { \"$not\" : { \"$gt\" : 18}} , \"status\" : \"student\"}"));
122+
}
123+
124+
/**
125+
* @see DATAMONGO-1068
126+
*/
127+
@Test
128+
public void getCriteriaObjectShouldReturnEmptyDBOWhenNoCriteriaSpecified() {
129+
130+
DBObject dbo = new Criteria().getCriteriaObject();
131+
132+
assertThat(dbo, equalTo(new BasicDBObjectBuilder().get()));
133+
}
134+
135+
/**
136+
* @see DATAMONGO-1068
137+
*/
138+
@Test
139+
public void getCriteriaObjectShouldUseCritieraValuesWhenNoKeyIsPresent() {
140+
141+
DBObject dbo = new Criteria().lt("foo").getCriteriaObject();
142+
143+
assertThat(dbo, equalTo(new BasicDBObjectBuilder().add("$lt", "foo").get()));
144+
}
145+
146+
/**
147+
* @see DATAMONGO-1068
148+
*/
149+
@Test
150+
public void getCriteriaObjectShouldUseCritieraValuesWhenNoKeyIsPresentButMultipleCriteriasPresent() {
151+
152+
DBObject dbo = new Criteria().lt("foo").gt("bar").getCriteriaObject();
153+
154+
assertThat(dbo, equalTo(new BasicDBObjectBuilder().add("$lt", "foo").add("$gt", "bar").get()));
155+
}
156+
157+
/**
158+
* @see DATAMONGO-1068
159+
*/
160+
@Test
161+
public void getCriteriaObjectShouldRespectNotWhenNoKeyPresent() {
162+
163+
DBObject dbo = new Criteria().lt("foo").not().getCriteriaObject();
164+
165+
assertThat(dbo, equalTo(new BasicDBObjectBuilder().add("$not", new BasicDBObject("$lt", "foo")).get()));
166+
}
121167
}

0 commit comments

Comments
 (0)