From 29e2f87ee112a817f22644122d0f02dd063cf80d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 10 Nov 2014 16:33:33 +0100 Subject: [PATCH] DATAMONGO-1078 - Polishing. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Polished test cases. Simplified equals(…)/hashCode() for sample entity and its identifier type. Original pull request: #239. --- .../ComplexIdRepositoryIntegrationTests.java | 25 +++++------ .../data/mongodb/repository/MyId.java | 43 ++++++++----------- .../mongodb/repository/UserWithComplexId.java | 39 ++++++----------- 3 files changed, 42 insertions(+), 65 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java index 2a55f0f84..17ab38b47 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java @@ -15,14 +15,13 @@ */ package org.springframework.data.mongodb.repository; -import static org.hamcrest.collection.IsCollectionWithSize.*; -import static org.hamcrest.collection.IsIterableContainingInOrder.*; -import static org.hamcrest.core.IsEqual.*; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.util.Collections; import java.util.List; +import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -39,6 +38,7 @@ import com.mongodb.MongoClient; /** * @author Christoph Strobl + * @author Oliver Gierke */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @@ -61,14 +61,14 @@ public class ComplexIdRepositoryIntegrationTests { } @Autowired UserWithComplexIdRepository repo; - @Autowired MongoTemplate template; - private MyId id; - private UserWithComplexId userWithId; + MyId id; + UserWithComplexId userWithId; @Before public void setUp() { + repo.deleteAll(); id = new MyId(); @@ -88,9 +88,7 @@ public class ComplexIdRepositoryIntegrationTests { repo.save(userWithId); - UserWithComplexId loaded = repo.getUserByComplexId(id); - - assertThat(loaded, equalTo(userWithId)); + assertThat(repo.getUserByComplexId(id), is(userWithId)); } /** @@ -115,9 +113,7 @@ public class ComplexIdRepositoryIntegrationTests { repo.save(userWithId); - UserWithComplexId loaded = repo.findOne(id); - - assertThat(loaded, equalTo(userWithId)); + assertThat(repo.findOne(id), is(userWithId)); } /** @@ -128,10 +124,9 @@ public class ComplexIdRepositoryIntegrationTests { repo.save(userWithId); - List loaded = (List) repo.findAll(Collections.singleton(id)); + Iterable loaded = repo.findAll(Collections.singleton(id)); - assertThat(loaded, hasSize(1)); + assertThat(loaded, is(Matchers. iterableWithSize(1))); assertThat(loaded, contains(userWithId)); } - } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MyId.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MyId.java index b5f6dadee..086065b21 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MyId.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MyId.java @@ -17,50 +17,43 @@ package org.springframework.data.mongodb.repository; import java.io.Serializable; +import org.springframework.util.ObjectUtils; + /** * @author Christoph Strobl + * @author Oliver Gierke */ public class MyId implements Serializable { + private static final long serialVersionUID = -7129201311241750831L; + String val1; String val2; @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((val1 == null) ? 0 : val1.hashCode()); - result = prime * result + ((val2 == null) ? 0 : val2.hashCode()); + + int result = 31; + + result += 17 * ObjectUtils.nullSafeHashCode(val1); + result += 17 * ObjectUtils.nullSafeHashCode(val2); + return result; } @Override public boolean equals(Object obj) { - if (this == obj) { + + if (obj == this) { return true; } - if (obj == null) { - return false; - } + if (!(obj instanceof MyId)) { return false; } - MyId other = (MyId) obj; - if (val1 == null) { - if (other.val1 != null) { - return false; - } - } else if (!val1.equals(other.val1)) { - return false; - } - if (val2 == null) { - if (other.val2 != null) { - return false; - } - } else if (!val2.equals(other.val2)) { - return false; - } - return true; - } + MyId that = (MyId) obj; + + return ObjectUtils.nullSafeEquals(this.val1, that.val1) && ObjectUtils.nullSafeEquals(this.val2, that.val2); + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexId.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexId.java index b9235a78c..d2f49cdb8 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexId.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexId.java @@ -17,9 +17,11 @@ package org.springframework.data.mongodb.repository; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.util.ObjectUtils; /** * @author Christoph Strobl + * @author Oliver Gierke */ @Document public class UserWithComplexId { @@ -29,40 +31,27 @@ public class UserWithComplexId { @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((firstname == null) ? 0 : firstname.hashCode()); - result = prime * result + ((id == null) ? 0 : id.hashCode()); + + int result = 31; + + result += 17 * ObjectUtils.nullSafeHashCode(id); + return result; } @Override public boolean equals(Object obj) { - if (this == obj) { + + if (obj == this) { return true; } - if (obj == null) { - return false; - } + if (!(obj instanceof UserWithComplexId)) { return false; } - UserWithComplexId other = (UserWithComplexId) obj; - if (firstname == null) { - if (other.firstname != null) { - return false; - } - } else if (!firstname.equals(other.firstname)) { - return false; - } - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - return true; - } + UserWithComplexId that = (UserWithComplexId) obj; + + return ObjectUtils.nullSafeEquals(this.id, that.id); + } }