Skip to content

Commit 7b29a87

Browse files
DATAMONGO-1387 - Polishing.
Added a few more tests and fixed equals/hashcode. Removed equalsverifyer.
1 parent 06bc4fd commit 7b29a87

File tree

3 files changed

+56
-47
lines changed

3 files changed

+56
-47
lines changed

spring-data-mongodb/pom.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<properties>
1919
<validation>1.0.0.GA</validation>
2020
<objenesis>1.3</objenesis>
21-
<equalsverifier>1.5</equalsverifier>
2221
</properties>
2322

2423
<dependencies>
@@ -159,13 +158,6 @@
159158
<scope>test</scope>
160159
</dependency>
161160

162-
<dependency>
163-
<groupId>nl.jqno.equalsverifier</groupId>
164-
<artifactId>equalsverifier</artifactId>
165-
<version>${equalsverifier}</version>
166-
<scope>test</scope>
167-
</dependency>
168-
169161
<dependency>
170162
<groupId>org.springframework</groupId>
171163
<artifactId>spring-webmvc</artifactId>

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

Lines changed: 19 additions & 10 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
@@ -124,9 +133,9 @@ public boolean equals(Object o) {
124133
BasicQuery that = (BasicQuery) o;
125134

126135
return querySettingsEquals(that) && //
127-
nullSafeEquals(fieldsObject, that.fieldsObject) && //
128-
nullSafeEquals(queryObject, that.queryObject) && //
129-
nullSafeEquals(sortObject, that.sortObject);
136+
nullSafeEquals(getFieldsObject(), that.getFieldsObject()) && //
137+
nullSafeEquals(getQueryObject(), that.getQueryObject()) && //
138+
nullSafeEquals(getSortObject(), that.getSortObject());
130139
}
131140

132141
/*
@@ -137,9 +146,9 @@ public boolean equals(Object o) {
137146
public int hashCode() {
138147

139148
int result = super.hashCode();
140-
result = 31 * result + nullSafeHashCode(queryObject);
141-
result = 31 * result + nullSafeHashCode(fieldsObject);
142-
result = 31 * result + nullSafeHashCode(sortObject);
149+
result = 31 * result + nullSafeHashCode(getQueryObject());
150+
result = 31 * result + nullSafeHashCode(getFieldsObject());
151+
result = 31 * result + nullSafeHashCode(getSortObject());
143152

144153
return result;
145154
}

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

Lines changed: 37 additions & 29 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,8 +18,7 @@
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;
@@ -63,24 +62,6 @@ public void overridesSortCorrectly() {
6362
assertThat(query.getSortObject(), is(sortReference));
6463
}
6564

66-
/**
67-
* @see DATAMONGO-1093
68-
*/
69-
@Test
70-
public void equalsContract() {
71-
72-
BasicQuery query1 = new BasicQuery("{ \"name\" : \"Thomas\"}", "{\"name\":1, \"age\":1}");
73-
query1.setSortObject(new BasicDBObject("name", -1));
74-
75-
BasicQuery query2 = new BasicQuery("{ \"name\" : \"Oliver\"}", "{\"name\":1, \"address\":1}");
76-
query2.setSortObject(new BasicDBObject("name", 1));
77-
78-
EqualsVerifier.forExamples(query1, query2) //
79-
.withRedefinedSuperclass() //
80-
.suppress(Warning.NONFINAL_FIELDS, Warning.NULL_FIELDS, Warning.STRICT_INHERITANCE) //
81-
.verify();
82-
}
83-
8465
/**
8566
* @see DATAMONGO-1093
8667
*/
@@ -138,21 +119,48 @@ public void handlesEqualsAndHashCodeCorrectlyWhenQuerySettingsDiffer() {
138119
assertThat(query1, is(not(equalTo(query2))));
139120
assertThat(query1.hashCode(), is(not(query2.hashCode())));
140121
}
141-
122+
123+
/**
124+
* @see DATAMONGO-1387
125+
*/
126+
@Test
127+
public void returnsFieldsCorrectly() {
128+
129+
String qry = "{ \"name\" : \"Thomas\"}";
130+
String fields = "{\"name\":1, \"age\":1}";
131+
132+
BasicQuery query1 = new BasicQuery(qry, fields);
133+
134+
assertThat(query1.getFieldsObject(), isBsonObject().containing("name").containing("age"));
135+
}
136+
142137
/**
143138
* @see DATAMONGO-1387
144139
*/
145140
@Test
146141
public void handlesFieldsIncludeCorrectly() {
147-
142+
148143
String qry = "{ \"name\" : \"Thomas\"}";
149-
144+
150145
BasicQuery query1 = new BasicQuery(qry);
151146
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));
147+
148+
assertThat(query1.getFieldsObject(), isBsonObject().containing("name"));
149+
}
150+
151+
/**
152+
* @see DATAMONGO-1387
153+
*/
154+
@Test
155+
public void combinesFieldsIncludeCorrectly() {
156+
157+
String qry = "{ \"name\" : \"Thomas\"}";
158+
String fields = "{\"name\":1, \"age\":1}";
159+
160+
BasicQuery query1 = new BasicQuery(qry, fields);
161+
query1.fields().include("gender");
162+
163+
assertThat(query1.getFieldsObject(), isBsonObject().containing("name").containing("age").containing("gender"));
157164
}
165+
158166
}

0 commit comments

Comments
 (0)