From a908e89ef7509782598f1bb284431ff102fd52d9 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 27 Feb 2014 14:47:18 +0100 Subject: [PATCH] DATAMONGO-829 - NearQuery should not default 'num' to zero. NearQuery now ignores query.getLimit() equal to zero, when adding Query to NearQuery. This has to be done as limit is defaulted to zero within Query which then results in unintended propagation of the parameter. In case 'num' should be explicitly set to zero one might use 'NearQuery.num(0)' as an alternative to the query approach. Introduced 'null' check for 'NearQuery.query(Query)' and 'NearQuery.with(Pageable)' along the way. Original Pull Request: #133 --- .../data/mongodb/core/query/NearQuery.java | 14 ++++++-- .../core/query/NearQueryUnitTests.java | 36 ++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java index 495744198..093c3fa61 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java @@ -31,6 +31,7 @@ import com.mongodb.DBObject; * * @author Oliver Gierke * @author Thomas Darimont + * @author Christoph Strobl */ public final class NearQuery { @@ -143,10 +144,12 @@ public final class NearQuery { /** * Configures the {@link Pageable} to use. * - * @param pageable + * @param pageable must not be {@literal null} * @return */ public NearQuery with(Pageable pageable) { + + Assert.notNull(pageable, "Pageable must not be 'null'."); this.num = pageable.getOffset() + pageable.getPageSize(); this.skip = pageable.getOffset(); return this; @@ -311,13 +314,18 @@ public final class NearQuery { /** * Adds an actual query to the {@link NearQuery} to restrict the objects considered for the actual near operation. * - * @param query + * @param query must not be {@literal null}. * @return */ public NearQuery query(Query query) { + + Assert.notNull(query, "Cannot apply 'null' query on NearQuery."); this.query = query; this.skip = query.getSkip(); - this.num = query.getLimit(); + + if (query.getLimit() != 0) { + this.num = query.getLimit(); + } return this; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/NearQueryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/NearQueryUnitTests.java index b97bf90d7..417dbf941 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/NearQueryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/NearQueryUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 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. @@ -21,6 +21,7 @@ import static org.junit.Assert.*; import org.junit.Test; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.mongodb.core.DBObjectTestUtils; import org.springframework.data.mongodb.core.geo.Distance; import org.springframework.data.mongodb.core.geo.Metric; import org.springframework.data.mongodb.core.geo.Metrics; @@ -31,6 +32,7 @@ import org.springframework.data.mongodb.core.geo.Point; * * @author Oliver Gierke * @author Thomas Darimont + * @author Christoph Strobl */ public class NearQueryUnitTests { @@ -123,4 +125,36 @@ public class NearQueryUnitTests { assertThat(query.getSkip(), is(pageable.getPageNumber() * pageable.getPageSize())); assertThat((Integer) query.toDBObject().get("num"), is((pageable.getPageNumber() + 1) * pageable.getPageSize())); } + + /** + * @see DATAMONGO-829 + */ + @Test + public void nearQueryShouldInoreZeroLimitFromQuery() { + + NearQuery query = NearQuery.near(new Point(1, 2)).query(Query.query(Criteria.where("foo").is("bar"))); + assertThat(query.toDBObject().get("num"), nullValue()); + } + + /** + * @see DATAMONOGO-829 + */ + @Test(expected = IllegalArgumentException.class) + public void nearQueryShouldThrowExceptionWhenGivenANullQuery() { + NearQuery.near(new Point(1, 2)).query(null); + } + + /** + * @see DATAMONGO-829 + */ + @Test + public void numShouldNotBeAlteredByQueryWithoutPageable() { + + int num = 100; + NearQuery query = NearQuery.near(new Point(1, 2)); + query.num(num); + query.query(Query.query(Criteria.where("foo").is("bar"))); + + assertThat(DBObjectTestUtils.getTypedValue(query.toDBObject(), "num", Integer.class), is(num)); + } }