diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java index 4f3e08334..5c470dbda 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java @@ -29,354 +29,378 @@ import org.springframework.data.document.mongodb.geo.Point; public class Criteria implements CriteriaDefinition { - private String key; - - private List criteriaChain; + private String key; - private LinkedHashMap criteria = new LinkedHashMap(); + private List criteriaChain; - private Object isValue = null; + private LinkedHashMap criteria = new LinkedHashMap(); + private Object isValue = null; - public Criteria(String key) { - this.criteriaChain = new ArrayList(); - this.criteriaChain.add(this); - this.key = key; - } + public Criteria(String key) { + this.criteriaChain = new ArrayList(); + this.criteriaChain.add(this); + this.key = key; + } - protected Criteria(List criteriaChain, String key) { - this.criteriaChain = criteriaChain; - this.criteriaChain.add(this); - this.key = key; - } + protected Criteria(List criteriaChain, String key) { + this.criteriaChain = criteriaChain; + this.criteriaChain.add(this); + this.key = key; + } - /** - * Static factory method to create a Criteria using the provided key - * - * @param key - * @return - */ - public static Criteria where(String key) { - return new Criteria(key); - } - - public static Criteria whereId() { - return new Criteria("id"); - } + /** + * Static factory method to create a Criteria using the provided key + * + * @param key + * @return + */ + public static Criteria where(String key) { + return new Criteria(key); + } - /** - * Static factory method to create a Criteria using the provided key - * - * @param key - * @return - */ - public Criteria and(String key) { - return new Criteria(this.criteriaChain, key); - } + public static Criteria whereId() { + return new Criteria("id"); + } - /** - * Creates a criterion using the $is operator - * - * @param o - * @return - */ - public Criteria is(Object o) { - if (isValue != null) { - throw new InvalidDocumentStoreApiUsageException("Multiple 'is' values declared."); - } - this.isValue = o; - return this; - } + /** + * Static factory method to create a Criteria using the provided key + * + * @param key + * @return + */ + public Criteria and(String key) { + return new Criteria(this.criteriaChain, key); + } - /** - * Creates a criterion using the $lt operator - * - * @param o - * @return - */ - public Criteria lt(Object o) { - criteria.put("$lt", o); - return this; - } + /** + * Creates a criterion using the $is operator + * + * @param o + * @return + */ + public Criteria is(Object o) { + if (isValue != null) { + throw new InvalidDocumentStoreApiUsageException( + "Multiple 'is' values declared."); + } + this.isValue = o; + return this; + } - /** - * Creates a criterion using the $lte operator - * - * @param o - * @return - */ - public Criteria lte(Object o) { - criteria.put("$lte", o); - return this; - } + /** + * Creates a criterion using the $ne operator + * + * @param o + * @return + */ + public Criteria ne(Object o) { + criteria.put("$ne", o); + return this; + } - /** - * Creates a criterion using the $gt operator - * - * @param o - * @return - */ - public Criteria gt(Object o) { - criteria.put("$gt", o); - return this; - } + /** + * Creates a criterion using the $lt operator + * + * @param o + * @return + */ + public Criteria lt(Object o) { + criteria.put("$lt", o); + return this; + } - /** - * Creates a criterion using the $gte operator - * - * @param o - * @return - */ - public Criteria gte(Object o) { - criteria.put("$gte", o); - return this; - } + /** + * Creates a criterion using the $lte operator + * + * @param o + * @return + */ + public Criteria lte(Object o) { + criteria.put("$lte", o); + return this; + } - /** - * Creates a criterion using the $in operator - * - * @param o - * @return - */ - public Criteria in(Object... o) { - criteria.put("$in", o); - return this; - } + /** + * Creates a criterion using the $gt operator + * + * @param o + * @return + */ + public Criteria gt(Object o) { + criteria.put("$gt", o); + return this; + } - /** - * Creates a criterion using the $nin operator - * - * @param o - * @return - */ - public Criteria nin(Object... o) { - criteria.put("$nin", o); - return this; - } + /** + * Creates a criterion using the $gte operator + * + * @param o + * @return + */ + public Criteria gte(Object o) { + criteria.put("$gte", o); + return this; + } - /** - * Creates a criterion using the $mod operator - * - * @param value - * @param remainder - * @return - */ - public Criteria mod(Number value, Number remainder) { - List l = new ArrayList(); - l.add(value); - l.add(remainder); - criteria.put("$mod", l); - return this; - } + /** + * Creates a criterion using the $in operator + * + * @param o + * @return + */ + public Criteria in(Object... o) { + criteria.put("$in", o); + return this; + } - /** - * Creates a criterion using the $all operator - * - * @param o - * @return - */ - public Criteria all(Object o) { - criteria.put("$is", o); - return this; - } + /** + * Creates a criterion using the $nin operator + * + * @param o + * @return + */ + public Criteria nin(Object... o) { + criteria.put("$nin", o); + return this; + } - /** - * Creates a criterion using the $size operator - * - * @param s - * @return - */ - public Criteria size(int s) { - criteria.put("$size", s); - return this; - } + /** + * Creates a criterion using the $mod operator + * + * @param value + * @param remainder + * @return + */ + public Criteria mod(Number value, Number remainder) { + List l = new ArrayList(); + l.add(value); + l.add(remainder); + criteria.put("$mod", l); + return this; + } - /** - * Creates a criterion using the $exists operator - * - * @param b - * @return - */ - public Criteria exists(boolean b) { - criteria.put("$exists", b); - return this; - } + /** + * Creates a criterion using the $all operator + * + * @param o + * @return + */ + public Criteria all(Object o) { + criteria.put("$is", o); + return this; + } - /** - * Creates a criterion using the $type operator - * - * @param t - * @return - */ - public Criteria type(int t) { - criteria.put("$type", t); - return this; - } + /** + * Creates a criterion using the $size operator + * + * @param s + * @return + */ + public Criteria size(int s) { + criteria.put("$size", s); + return this; + } - /** - * Creates a criterion using the $not meta operator which affects the clause directly following - * - * @return - */ - public Criteria not() { - criteria.put("$not", null); - return this; - } + /** + * Creates a criterion using the $exists operator + * + * @param b + * @return + */ + public Criteria exists(boolean b) { + criteria.put("$exists", b); + return this; + } - /** - * Creates a criterion using a $regex - * - * @param re - * @return - */ - public Criteria regex(String re) { - criteria.put("$regex", re); - return this; - } - - - /** - * Creates a geospatial criterion using a $within $center operation - * @param circle - * @return - */ - public Criteria withinCenter(Circle circle) { - LinkedList list = new LinkedList(); - list.addLast(circle.getCenter()); - list.add(circle.getRadius()); - criteria.put("$within", new BasicDBObject("$center", list)); - return this; - } - - /** - * Creates a geospatial criterion using a $within $center operation. This is only available for Mongo 1.7 and higher. - * @param circle - * @return - */ - public Criteria withinCenterSphere(Circle circle) { - LinkedList list = new LinkedList(); - list.addLast(circle.getCenter()); - list.add(circle.getRadius()); - criteria.put("$within", new BasicDBObject("$centerSphere", list)); - return this; - } - - /** - * Creates a geospatial criterion using a $within $box operation - * @param circle - * @return - */ - public Criteria withinBox(Box box) { - LinkedList list = new LinkedList(); - list.addLast(new double[]{ box.getLowerLeft().getX(), box.getLowerLeft().getY()} ); - list.addLast(new double[]{ box.getUpperRight().getX(), box.getUpperRight().getY()} ); - criteria.put("$within", new BasicDBObject("$box", list)); - return this; - } - - /** - * Creates a geospatial criterion using a $near operation - * @param point - * @return - */ - public Criteria near(Point point) { - criteria.put("$near", new double[]{point.getX(), point.getY()}); - return this; - } - - /** - * Creates a geospatial criterion using a $nearSphere operation. This is only available for Mongo 1.7 and higher. - * @param point - * @return - */ - public Criteria nearSphere(Point point) { - criteria.put("$nearSphere", new double[]{point.getX(), point.getY()}); - return this; - } - - /** - * Creates a geospatical criterion using a $maxDistance operation, for use with $near - * @param maxDistance - * @return - */ - public Criteria maxDistance(double maxDistance) { - criteria.put("$maxDistance", maxDistance); - return this; - } + /** + * Creates a criterion using the $type operator + * + * @param t + * @return + */ + public Criteria type(int t) { + criteria.put("$type", t); + return this; + } - /** - * Creates a criterion using the $elemMatch operator - * - * @param t - * @return - */ - public Criteria elemMatch(Criteria c) { - criteria.put("$elemMatch", c.getCriteriaObject()); - return this; - } - /** - * Creates an or query using the $or operator for all of the provided queries - * - * @param queries - */ - public void or(List queries) { - criteria.put("$or", queries); - } - + /** + * Creates a criterion using the $not meta operator which affects the clause + * directly following + * + * @return + */ + public Criteria not() { + criteria.put("$not", null); + return this; + } + /** + * Creates a criterion using a $regex + * + * @param re + * @return + */ + public Criteria regex(String re) { + criteria.put("$regex", re); + return this; + } - public String getKey() { - return this.key; - } + /** + * Creates a geospatial criterion using a $within $center operation + * + * @param circle + * @return + */ + public Criteria withinCenter(Circle circle) { + LinkedList list = new LinkedList(); + list.addLast(circle.getCenter()); + list.add(circle.getRadius()); + criteria.put("$within", new BasicDBObject("$center", list)); + return this; + } - /* (non-Javadoc) - * @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String) - */ - public DBObject getCriteriaObject() { - if (this.criteriaChain.size() == 1) { - return criteriaChain.get(0).getSingleCriteriaObject(); - } - else { - DBObject criteriaObject = new BasicDBObject(); - for (Criteria c : this.criteriaChain) { - criteriaObject.putAll(c.getSingleCriteriaObject()); - } - return criteriaObject; - } - } - - protected DBObject getSingleCriteriaObject() { - DBObject dbo = new BasicDBObject(); - boolean not = false; - for (String k : this.criteria.keySet()) { - if (not) { - DBObject notDbo = new BasicDBObject(); - notDbo.put(k, convertValueIfNecessary(this.criteria.get(k))); - dbo.put("$not", notDbo); - not = false; - } else { - if ("$not".equals(k)) { - not = true; - } else { - dbo.put(k, convertValueIfNecessary(this.criteria.get(k))); - } - } - } - DBObject queryCriteria = new BasicDBObject(); - if (isValue != null) { - queryCriteria.put(this.key, convertValueIfNecessary(this.isValue)); - queryCriteria.putAll(dbo); - } else { - queryCriteria.put(this.key, dbo); - } - return queryCriteria; - } + /** + * Creates a geospatial criterion using a $within $center operation. This is + * only available for Mongo 1.7 and higher. + * + * @param circle + * @return + */ + public Criteria withinCenterSphere(Circle circle) { + LinkedList list = new LinkedList(); + list.addLast(circle.getCenter()); + list.add(circle.getRadius()); + criteria.put("$within", new BasicDBObject("$centerSphere", list)); + return this; + } - private Object convertValueIfNecessary(Object value) { - if (value instanceof Enum) { - return ((Enum) value).name(); - } - return value; - } + /** + * Creates a geospatial criterion using a $within $box operation + * + * @param circle + * @return + */ + public Criteria withinBox(Box box) { + LinkedList list = new LinkedList(); + list.addLast(new double[] { box.getLowerLeft().getX(), + box.getLowerLeft().getY() }); + list.addLast(new double[] { box.getUpperRight().getX(), + box.getUpperRight().getY() }); + criteria.put("$within", new BasicDBObject("$box", list)); + return this; + } + + /** + * Creates a geospatial criterion using a $near operation + * + * @param point + * @return + */ + public Criteria near(Point point) { + criteria.put("$near", new double[] { point.getX(), point.getY() }); + return this; + } + + /** + * Creates a geospatial criterion using a $nearSphere operation. This is + * only available for Mongo 1.7 and higher. + * + * @param point + * @return + */ + public Criteria nearSphere(Point point) { + criteria.put("$nearSphere", new double[] { point.getX(), point.getY() }); + return this; + } + + /** + * Creates a geospatical criterion using a $maxDistance operation, for use + * with $near + * + * @param maxDistance + * @return + */ + public Criteria maxDistance(double maxDistance) { + criteria.put("$maxDistance", maxDistance); + return this; + } + + /** + * Creates a criterion using the $elemMatch operator + * + * @param t + * @return + */ + public Criteria elemMatch(Criteria c) { + criteria.put("$elemMatch", c.getCriteriaObject()); + return this; + } + + /** + * Creates an or query using the $or operator for all of the provided + * queries + * + * @param queries + */ + public void or(List queries) { + criteria.put("$or", queries); + } + + public String getKey() { + return this.key; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.datastore.document.mongodb.query.Criteria# + * getCriteriaObject(java.lang.String) + */ + public DBObject getCriteriaObject() { + if (this.criteriaChain.size() == 1) { + return criteriaChain.get(0).getSingleCriteriaObject(); + } else { + DBObject criteriaObject = new BasicDBObject(); + for (Criteria c : this.criteriaChain) { + criteriaObject.putAll(c.getSingleCriteriaObject()); + } + return criteriaObject; + } + } + + protected DBObject getSingleCriteriaObject() { + DBObject dbo = new BasicDBObject(); + boolean not = false; + for (String k : this.criteria.keySet()) { + if (not) { + DBObject notDbo = new BasicDBObject(); + notDbo.put(k, convertValueIfNecessary(this.criteria.get(k))); + dbo.put("$not", notDbo); + not = false; + } else { + if ("$not".equals(k)) { + not = true; + } else { + dbo.put(k, convertValueIfNecessary(this.criteria.get(k))); + } + } + } + DBObject queryCriteria = new BasicDBObject(); + if (isValue != null) { + queryCriteria.put(this.key, convertValueIfNecessary(this.isValue)); + queryCriteria.putAll(dbo); + } else { + queryCriteria.put(this.key, dbo); + } + return queryCriteria; + } + + private Object convertValueIfNecessary(Object value) { + if (value instanceof Enum) { + return ((Enum) value).name(); + } + return value; + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/NorCriteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/NorCriteria.java new file mode 100644 index 000000000..6137c002a --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/NorCriteria.java @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.query; + +public class NorCriteria extends OrCriteria { + + public NorCriteria(Query[] queries) { + super(queries); + } + + @Override + protected String getOperator() { + return "$nor"; + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/NorQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/NorQuery.java new file mode 100644 index 000000000..fd5deb9c6 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/NorQuery.java @@ -0,0 +1,9 @@ +package org.springframework.data.document.mongodb.query; + +public class NorQuery extends Query { + + public NorQuery(Query... q) { + super.nor(q); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/OrCriteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/OrCriteria.java index 4676c2795..4551b3ea1 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/OrCriteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/OrCriteria.java @@ -21,25 +21,31 @@ import org.bson.types.BasicBSONList; public class OrCriteria implements CriteriaDefinition { - Query[] queries = null; + Query[] queries = null; - public OrCriteria(Query[] queries) { - super(); - this.queries = queries; - } + public OrCriteria(Query[] queries) { + super(); + this.queries = queries; + } + /* + * (non-Javadoc) + * + * @see org.springframework.datastore.document.mongodb.query.Criteria# + * getCriteriaObject(java.lang.String) + */ + public DBObject getCriteriaObject() { + DBObject dbo = new BasicDBObject(); + BasicBSONList l = new BasicBSONList(); + for (Query q : queries) { + l.add(q.getQueryObject()); + } + dbo.put(getOperator(), l); + return dbo; + } - /* (non-Javadoc) - * @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String) - */ - public DBObject getCriteriaObject() { - DBObject dbo = new BasicDBObject(); - BasicBSONList l = new BasicBSONList(); - for (Query q : queries) { - l.add(q.getQueryObject()); - } - dbo.put("$or", l); - return dbo; - } + protected String getOperator() { + return "$or"; + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/OrQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/OrQuery.java index 8a94d2558..43b57f33f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/OrQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/OrQuery.java @@ -2,8 +2,8 @@ package org.springframework.data.document.mongodb.query; public class OrQuery extends Query { - public OrQuery(Query... q) { - super.or(q); - } + public OrQuery(Query... q) { + super.or(q); + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Query.java index e771df452..d671534da 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Query.java @@ -55,6 +55,11 @@ public class Query { return this; } + public Query nor(Query... queries) { + this.criteria.put("$nor", new NorCriteria(queries)); + return this; + } + public Field fields() { synchronized (this) { if (fieldSpec == null) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/CriteriaTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/CriteriaTests.java index 2d83943aa..25c25e9a1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/CriteriaTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/CriteriaTests.java @@ -27,6 +27,13 @@ public class CriteriaTests { Assert.assertEquals("{ \"name\" : \"Bubba\"}", c.getCriteriaObject().toString()); } + @Test + public void testNotEqualCriteria() { + Criteria c = new Criteria("name").ne("Bubba"); + Assert.assertEquals("{ \"name\" : { \"$ne\" : \"Bubba\"}}", c.getCriteriaObject().toString()); + } + + @Test public void testChainedCriteria() { Criteria c = new Criteria("name").is("Bubba").and("age").lt(21); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/QueryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/QueryTests.java index dde630f22..cb2c4ef22 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/QueryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/query/QueryTests.java @@ -22,94 +22,112 @@ import org.junit.Test; public class QueryTests { - @Test - public void testSimpleQuery() { - Query q = new Query(where("name").is("Thomas").and("age").lt(80)); - String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - } + @Test + public void testSimpleQuery() { + Query q = new Query(where("name").is("Thomas").and("age").lt(80)); + String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + } - @Test - public void testQueryWithNot() { - Query q = new Query(where("name").is("Thomas").and("age").not().mod(10, 0)); - String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$not\" : { \"$mod\" : [ 10 , 0]}}}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - } + @Test + public void testQueryWithNot() { + Query q = new Query(where("name").is("Thomas").and("age").not() + .mod(10, 0)); + String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$not\" : { \"$mod\" : [ 10 , 0]}}}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + } - @Test - public void testOrQuery() { - Query q = new OrQuery( - new Query(where("name").is("Sven").and("age").lt(50)), - new Query(where("age").lt(50)), - new BasicQuery("{'name' : 'Thomas'}") - ); - String expected = "{ \"$or\" : [ { \"name\" : \"Sven\" , \"age\" : { \"$lt\" : 50}} , { \"age\" : { \"$lt\" : 50}} , { \"name\" : \"Thomas\"}]}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - } + @Test + public void testOrQuery() { + Query q = new OrQuery(new Query(where("name").is("Sven").and("age") + .lt(50)), new Query(where("age").lt(50)), new BasicQuery( + "{'name' : 'Thomas'}")); + String expected = "{ \"$or\" : [ { \"name\" : \"Sven\" , \"age\" : { \"$lt\" : 50}} , { \"age\" : { \"$lt\" : 50}} , { \"name\" : \"Thomas\"}]}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + } - @Test - public void testQueryWithLimit() { - Query q = new Query(where("name").gte("M").lte("T").and("age").not().gt(22)); - q.limit(50); - String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - Assert.assertEquals(50, q.getLimit()); - } + @Test + public void testNorQuery() { + Query q = new NorQuery(new Query(where("name").is("Sven")), new Query( + where("age").lt(50)), new BasicQuery("{'name' : 'Thomas'}")); + String expected = "{ \"$nor\" : [ { \"name\" : \"Sven\"} , { \"age\" : { \"$lt\" : 50}} , { \"name\" : \"Thomas\"}]}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + } - @Test - public void testQueryWithFieldsAndSlice() { - Query q = new Query(where("name").gte("M").lte("T").and("age").not().gt(22)); - q.fields().exclude("address").include("name").slice("orders", 10); + @Test + public void testQueryWithLimit() { + Query q = new Query(where("name").gte("M").lte("T").and("age").not() + .gt(22)); + q.limit(50); + String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + Assert.assertEquals(50, q.getLimit()); + } - String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - String expectedFields = "{ \"address\" : 0 , \"name\" : 1 , \"orders\" : { \"$slice\" : 10}}"; - Assert.assertEquals(expectedFields, q.getFieldsObject().toString()); - } + @Test + public void testQueryWithFieldsAndSlice() { + Query q = new Query(where("name").gte("M").lte("T").and("age").not() + .gt(22)); + q.fields().exclude("address").include("name").slice("orders", 10); - @Test - public void testBasicQuery() { - Query q = new BasicQuery("{ \"name\" : \"Thomas\"}").addCriteria(where("age").lt(80)); - String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - } + String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + String expectedFields = "{ \"address\" : 0 , \"name\" : 1 , \"orders\" : { \"$slice\" : 10}}"; + Assert.assertEquals(expectedFields, q.getFieldsObject().toString()); + } - @Test - public void testSimpleQueryWithChainedCriteria() { - Query q = new Query(where("name").is("Thomas").and("age").lt(80)); - String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - } + @Test + public void testBasicQuery() { + Query q = new BasicQuery("{ \"name\" : \"Thomas\"}").addCriteria(where( + "age").lt(80)); + String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + } - @Test - public void testComplexQueryWithMultipleChainedCriteria() { - Query q = new Query(where("name").regex("^T.*").and("age").gt(20).lt(80).and("city").in("Stockholm", "London", "New York")); - String expected = "{ \"name\" : { \"$regex\" : \"^T.*\"} , \"age\" : { \"$gt\" : 20 , \"$lt\" : 80} , " + - "\"city\" : { \"$in\" : [ \"Stockholm\" , \"London\" , \"New York\"]}}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - } + @Test + public void testSimpleQueryWithChainedCriteria() { + Query q = new Query(where("name").is("Thomas").and("age").lt(80)); + String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + } - @Test - public void testAddCriteriaWithComplexQueryWithMultipleChainedCriteria() { - Query q1 = new Query(where("name").regex("^T.*").and("age").gt(20).lt(80).and("city").in("Stockholm", "London", "New York")); - Query q2 = new Query(where("name").regex("^T.*").and("age").gt(20).lt(80)).addCriteria(where("city").in("Stockholm", "London", "New York")); - Assert.assertEquals(q1.getQueryObject().toString(), q2.getQueryObject().toString()); - Query q3 = new Query(where("name").regex("^T.*")).addCriteria(where("age").gt(20).lt(80)).addCriteria(where("city").in("Stockholm", "London", "New York")); - Assert.assertEquals(q1.getQueryObject().toString(), q3.getQueryObject().toString()); - } + @Test + public void testComplexQueryWithMultipleChainedCriteria() { + Query q = new Query(where("name").regex("^T.*").and("age").gt(20) + .lt(80).and("city").in("Stockholm", "London", "New York")); + String expected = "{ \"name\" : { \"$regex\" : \"^T.*\"} , \"age\" : { \"$gt\" : 20 , \"$lt\" : 80} , " + + "\"city\" : { \"$in\" : [ \"Stockholm\" , \"London\" , \"New York\"]}}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + } - @Test - public void testQueryWithElemMatch() { - Query q = new Query(where("openingHours").elemMatch(where("dayOfWeek").is("Monday").and("open").lte("1800"))); - String expected = "{ \"openingHours\" : { \"$elemMatch\" : { \"dayOfWeek\" : \"Monday\" , \"open\" : { \"$lte\" : \"1800\"}}}}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - } + @Test + public void testAddCriteriaWithComplexQueryWithMultipleChainedCriteria() { + Query q1 = new Query(where("name").regex("^T.*").and("age").gt(20) + .lt(80).and("city").in("Stockholm", "London", "New York")); + Query q2 = new Query(where("name").regex("^T.*").and("age").gt(20) + .lt(80)).addCriteria(where("city").in("Stockholm", "London", + "New York")); + Assert.assertEquals(q1.getQueryObject().toString(), q2.getQueryObject() + .toString()); + Query q3 = new Query(where("name").regex("^T.*")).addCriteria( + where("age").gt(20).lt(80)).addCriteria( + where("city").in("Stockholm", "London", "New York")); + Assert.assertEquals(q1.getQueryObject().toString(), q3.getQueryObject() + .toString()); + } - @Test - public void testQueryWithIn() { - Query q = new Query(where("state").in("NY", "NJ", "PA")); - System.out.println(q.getQueryObject().toString()); - String expected = "{ \"state\" : { \"$in\" : [ \"NY\" , \"NJ\" , \"PA\"]}}"; - Assert.assertEquals(expected, q.getQueryObject().toString()); - } + @Test + public void testQueryWithElemMatch() { + Query q = new Query(where("openingHours").elemMatch( + where("dayOfWeek").is("Monday").and("open").lte("1800"))); + String expected = "{ \"openingHours\" : { \"$elemMatch\" : { \"dayOfWeek\" : \"Monday\" , \"open\" : { \"$lte\" : \"1800\"}}}}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + } + + @Test + public void testQueryWithIn() { + Query q = new Query(where("state").in("NY", "NJ", "PA")); + String expected = "{ \"state\" : { \"$in\" : [ \"NY\" , \"NJ\" , \"PA\"]}}"; + Assert.assertEquals(expected, q.getQueryObject().toString()); + } }