Skip to content

Commit aa5bdcd

Browse files
DATAMONGO-1387 - Polishing.
Added a few more tests and append values if present on Query. Original Pull Request: #345
1 parent 2354b66 commit aa5bdcd

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2014 the original author or authors.
2+
* Copyright 2010-2016 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.
@@ -71,11 +71,20 @@ public DBObject getQueryObject() {
7171

7272
@Override
7373
public DBObject getFieldsObject() {
74-
if(fieldsObject != null) {
75-
return fieldsObject;
76-
} else {
74+
75+
if (fieldsObject == null) {
7776
return super.getFieldsObject();
7877
}
78+
79+
if (super.getFieldsObject() != null) {
80+
81+
DBObject combinedFieldsObject = new BasicDBObject();
82+
combinedFieldsObject.putAll(fieldsObject);
83+
combinedFieldsObject.putAll(super.getFieldsObject());
84+
return combinedFieldsObject;
85+
}
86+
87+
return fieldsObject;
7988
}
8089

8190
@Override

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2016 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.
@@ -18,15 +18,17 @@
1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020
import static org.springframework.data.mongodb.core.query.Criteria.*;
21-
import nl.jqno.equalsverifier.EqualsVerifier;
22-
import nl.jqno.equalsverifier.Warning;
21+
import static org.springframework.data.mongodb.test.util.IsBsonObject.*;
2322

2423
import org.junit.Test;
2524
import org.springframework.data.domain.Sort.Direction;
2625

2726
import com.mongodb.BasicDBObject;
2827
import com.mongodb.DBObject;
2928

29+
import nl.jqno.equalsverifier.EqualsVerifier;
30+
import nl.jqno.equalsverifier.Warning;
31+
3032
/**
3133
* Unit tests for {@link BasicQuery}.
3234
*
@@ -138,21 +140,48 @@ public void handlesEqualsAndHashCodeCorrectlyWhenQuerySettingsDiffer() {
138140
assertThat(query1, is(not(equalTo(query2))));
139141
assertThat(query1.hashCode(), is(not(query2.hashCode())));
140142
}
141-
143+
144+
/**
145+
* @see DATAMONGO-1387
146+
*/
147+
@Test
148+
public void returnsFieldsCorrectly() {
149+
150+
String qry = "{ \"name\" : \"Thomas\"}";
151+
String fields = "{\"name\":1, \"age\":1}";
152+
153+
BasicQuery query1 = new BasicQuery(qry, fields);
154+
155+
assertThat(query1.getFieldsObject(), isBsonObject().containing("name").containing("age"));
156+
}
157+
142158
/**
143159
* @see DATAMONGO-1387
144160
*/
145161
@Test
146162
public void handlesFieldsIncludeCorrectly() {
147-
163+
148164
String qry = "{ \"name\" : \"Thomas\"}";
149-
165+
150166
BasicQuery query1 = new BasicQuery(qry);
151167
query1.fields().include("name");
152-
153-
DBObject fieldsObject = query1.getFieldsObject();
154-
fieldsObject.containsField("name");
155-
assertThat(query1.getFieldsObject(), notNullValue());
156-
assertThat(query1.getFieldsObject().containsField("name"), is(true));
168+
169+
assertThat(query1.getFieldsObject(), isBsonObject().containing("name"));
157170
}
171+
172+
/**
173+
* @see DATAMONGO-1387
174+
*/
175+
@Test
176+
public void combinesFieldsIncludeCorrectly() {
177+
178+
String qry = "{ \"name\" : \"Thomas\"}";
179+
String fields = "{\"name\":1, \"age\":1}";
180+
181+
BasicQuery query1 = new BasicQuery(qry, fields);
182+
query1.fields().include("gender");
183+
184+
assertThat(query1.getFieldsObject(), isBsonObject().containing("name").containing("age").containing("gender"));
185+
}
186+
158187
}

0 commit comments

Comments
 (0)