DATAMONGO-1093 - Added hashCode() and equals(…) in BasicQuery.
We now have equals(…) and hashCode(…) methods on BasicQuery. Previously we solely relied on Query.hashCode()/equals(…) which didn't consider the fields of BasicQuery. Introduced equals verifier library to automatically test equals contracts. Added some additional test cases to BasicQueryUnitTests. Original pull request: #252.
This commit is contained in:
committed by
Oliver Gierke
parent
b56ca97f68
commit
5b8da8dd41
@@ -18,11 +18,12 @@
|
||||
<properties>
|
||||
<validation>1.0.0.GA</validation>
|
||||
<objenesis>1.3</objenesis>
|
||||
<equalsverifier>1.5</equalsverifier>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring -->
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
@@ -50,7 +51,7 @@
|
||||
<artifactId>spring-expression</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Data -->
|
||||
<!-- Spring Data -->
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
@@ -144,6 +145,12 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>nl.jqno.equalsverifier</groupId>
|
||||
<artifactId>equalsverifier</artifactId>
|
||||
<version>${equalsverifier}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.query;
|
||||
|
||||
import static org.springframework.util.ObjectUtils.*;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.util.JSON;
|
||||
@@ -25,6 +27,7 @@ import com.mongodb.util.JSON;
|
||||
* @author Thomas Risberg
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class BasicQuery extends Query {
|
||||
|
||||
@@ -97,4 +100,42 @@ public class BasicQuery extends Query {
|
||||
protected void setFieldsObject(DBObject fieldsObject) {
|
||||
this.fieldsObject = fieldsObject;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.query.Query#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof BasicQuery)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
BasicQuery that = (BasicQuery) o;
|
||||
|
||||
return querySettingsEquals(that) && //
|
||||
nullSafeEquals(fieldsObject, that.fieldsObject) && //
|
||||
nullSafeEquals(queryObject, that.queryObject) && //
|
||||
nullSafeEquals(sortObject, that.sortObject);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.query.Query#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = super.hashCode();
|
||||
result = 31 * result + nullSafeHashCode(queryObject);
|
||||
result = 31 * result + nullSafeHashCode(fieldsObject);
|
||||
result = 31 * result + nullSafeHashCode(sortObject);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,12 +385,21 @@ public class Query {
|
||||
return false;
|
||||
}
|
||||
|
||||
Query that = (Query) obj;
|
||||
return querySettingsEquals((Query) obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether the settings of the given {@link Query} are equal to this query.
|
||||
*
|
||||
* @param that
|
||||
* @return
|
||||
*/
|
||||
protected boolean querySettingsEquals(Query that) {
|
||||
|
||||
boolean criteriaEqual = this.criteria.equals(that.criteria);
|
||||
boolean fieldsEqual = this.fieldSpec == null ? that.fieldSpec == null : this.fieldSpec.equals(that.fieldSpec);
|
||||
boolean sortEqual = this.sort == null ? that.sort == null : this.sort.equals(that.sort);
|
||||
boolean hintEqual = this.hint == null ? that.hint == null : this.hint.equals(that.hint);
|
||||
boolean fieldsEqual = nullSafeEquals(this.fieldSpec, that.fieldSpec);
|
||||
boolean sortEqual = nullSafeEquals(this.sort, that.sort);
|
||||
boolean hintEqual = nullSafeEquals(this.hint, that.hint);
|
||||
boolean skipEqual = this.skip == that.skip;
|
||||
boolean limitEqual = this.limit == that.limit;
|
||||
boolean metaEqual = nullSafeEquals(this.meta, that.meta);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2012 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.
|
||||
@@ -18,6 +18,8 @@ package org.springframework.data.mongodb.core.query;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import nl.jqno.equalsverifier.Warning;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
@@ -29,6 +31,7 @@ import com.mongodb.DBObject;
|
||||
* Unit tests for {@link BasicQuery}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class BasicQueryUnitTests {
|
||||
|
||||
@@ -58,4 +61,80 @@ public class BasicQueryUnitTests {
|
||||
sortReference.put("lastname", 1);
|
||||
assertThat(query.getSortObject(), is(sortReference));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-1093
|
||||
*/
|
||||
@Test
|
||||
public void equalsContract() {
|
||||
|
||||
BasicQuery query1 = new BasicQuery("{ \"name\" : \"Thomas\"}", "{\"name\":1, \"age\":1}");
|
||||
query1.setSortObject(new BasicDBObject("name", -1));
|
||||
|
||||
BasicQuery query2 = new BasicQuery("{ \"name\" : \"Oliver\"}", "{\"name\":1, \"address\":1}");
|
||||
query2.setSortObject(new BasicDBObject("name", 1));
|
||||
|
||||
EqualsVerifier.forExamples(query1, query2) //
|
||||
.withRedefinedSuperclass() //
|
||||
.suppress(Warning.NONFINAL_FIELDS, Warning.NULL_FIELDS, Warning.STRICT_INHERITANCE) //
|
||||
.verify();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-1093
|
||||
*/
|
||||
@Test
|
||||
public void handlesEqualsAndHashCodeCorrectlyForExactCopies() {
|
||||
|
||||
String qry = "{ \"name\" : \"Thomas\"}";
|
||||
String fields = "{\"name\":1, \"age\":1}";
|
||||
|
||||
BasicQuery query1 = new BasicQuery(qry, fields);
|
||||
query1.setSortObject(new BasicDBObject("name", -1));
|
||||
|
||||
BasicQuery query2 = new BasicQuery(qry, fields);
|
||||
query2.setSortObject(new BasicDBObject("name", -1));
|
||||
|
||||
assertThat(query1, is(equalTo(query1)));
|
||||
assertThat(query1, is(equalTo(query2)));
|
||||
assertThat(query1.hashCode(), is(query2.hashCode()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-1093
|
||||
*/
|
||||
@Test
|
||||
public void handlesEqualsAndHashCodeCorrectlyWhenBasicQuerySettingsDiffer() {
|
||||
|
||||
String qry = "{ \"name\" : \"Thomas\"}";
|
||||
String fields = "{\"name\":1, \"age\":1}";
|
||||
|
||||
BasicQuery query1 = new BasicQuery(qry, fields);
|
||||
query1.setSortObject(new BasicDBObject("name", -1));
|
||||
|
||||
BasicQuery query2 = new BasicQuery(qry, fields);
|
||||
query2.setSortObject(new BasicDBObject("name", 1));
|
||||
|
||||
assertThat(query1, is(not(equalTo(query2))));
|
||||
assertThat(query1.hashCode(), is(not(query2.hashCode())));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-1093
|
||||
*/
|
||||
@Test
|
||||
public void handlesEqualsAndHashCodeCorrectlyWhenQuerySettingsDiffer() {
|
||||
|
||||
String qry = "{ \"name\" : \"Thomas\"}";
|
||||
String fields = "{\"name\":1, \"age\":1}";
|
||||
|
||||
BasicQuery query1 = new BasicQuery(qry, fields);
|
||||
query1.getMeta().setComment("foo");
|
||||
|
||||
BasicQuery query2 = new BasicQuery(qry, fields);
|
||||
query2.getMeta().setComment("bar");
|
||||
|
||||
assertThat(query1, is(not(equalTo(query2))));
|
||||
assertThat(query1.hashCode(), is(not(query2.hashCode())));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user