|
1 | 1 | /*
|
2 |
| - * Copyright 2010-2013 the original author or authors. |
| 2 | + * Copyright 2010-2014 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 | import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
|
23 | 23 |
|
24 | 24 | import com.mongodb.BasicDBObject;
|
| 25 | +import com.mongodb.BasicDBObjectBuilder; |
25 | 26 | import com.mongodb.DBObject;
|
26 | 27 |
|
27 |
| -/** |
28 |
| - * @author Oliver Gierke |
29 |
| - * @author Thomas Darimont |
30 |
| - */ |
| 28 | +/** |
| 29 | + * @author Oliver Gierke |
| 30 | + * @author Thomas Darimont |
| 31 | + * @author Christoph Strobl |
| 32 | + */ |
31 | 33 | public class CriteriaTests {
|
32 | 34 |
|
33 | 35 | @Test
|
@@ -72,50 +74,94 @@ public void equalIfCriteriaMatches() {
|
72 | 74 | assertThat(left, is(not(right)));
|
73 | 75 | assertThat(right, is(not(left)));
|
74 | 76 | }
|
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 | + } |
121 | 167 | }
|
0 commit comments