Browse Source

DATAMONGO-1078 - Polishing.

Polished test cases. Simplified equals(…)/hashCode() for sample entity and its identifier type.

Original pull request: #239.
pull/237/merge
Oliver Gierke 11 years ago
parent
commit
3b70b6aeee
  1. 25
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java
  2. 43
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MyId.java
  3. 39
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexId.java

25
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java

@ -15,14 +15,13 @@
*/ */
package org.springframework.data.mongodb.repository; package org.springframework.data.mongodb.repository;
import static org.hamcrest.collection.IsCollectionWithSize.*; import static org.hamcrest.Matchers.*;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.hamcrest.Matchers;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -39,6 +38,7 @@ import com.mongodb.MongoClient;
/** /**
* @author Christoph Strobl * @author Christoph Strobl
* @author Oliver Gierke
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration @ContextConfiguration
@ -61,14 +61,14 @@ public class ComplexIdRepositoryIntegrationTests {
} }
@Autowired UserWithComplexIdRepository repo; @Autowired UserWithComplexIdRepository repo;
@Autowired MongoTemplate template; @Autowired MongoTemplate template;
private MyId id; MyId id;
private UserWithComplexId userWithId; UserWithComplexId userWithId;
@Before @Before
public void setUp() { public void setUp() {
repo.deleteAll(); repo.deleteAll();
id = new MyId(); id = new MyId();
@ -88,9 +88,7 @@ public class ComplexIdRepositoryIntegrationTests {
repo.save(userWithId); repo.save(userWithId);
UserWithComplexId loaded = repo.getUserByComplexId(id); assertThat(repo.getUserByComplexId(id), is(userWithId));
assertThat(loaded, equalTo(userWithId));
} }
/** /**
@ -115,9 +113,7 @@ public class ComplexIdRepositoryIntegrationTests {
repo.save(userWithId); repo.save(userWithId);
UserWithComplexId loaded = repo.findOne(id); assertThat(repo.findOne(id), is(userWithId));
assertThat(loaded, equalTo(userWithId));
} }
/** /**
@ -128,10 +124,9 @@ public class ComplexIdRepositoryIntegrationTests {
repo.save(userWithId); repo.save(userWithId);
List<UserWithComplexId> loaded = (List<UserWithComplexId>) repo.findAll(Collections.singleton(id)); Iterable<UserWithComplexId> loaded = repo.findAll(Collections.singleton(id));
assertThat(loaded, hasSize(1)); assertThat(loaded, is(Matchers.<UserWithComplexId> iterableWithSize(1)));
assertThat(loaded, contains(userWithId)); assertThat(loaded, contains(userWithId));
} }
} }

43
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 java.io.Serializable;
import org.springframework.util.ObjectUtils;
/** /**
* @author Christoph Strobl * @author Christoph Strobl
* @author Oliver Gierke
*/ */
public class MyId implements Serializable { public class MyId implements Serializable {
private static final long serialVersionUID = -7129201311241750831L;
String val1; String val1;
String val2; String val2;
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31;
int result = 1; int result = 31;
result = prime * result + ((val1 == null) ? 0 : val1.hashCode());
result = prime * result + ((val2 == null) ? 0 : val2.hashCode()); result += 17 * ObjectUtils.nullSafeHashCode(val1);
result += 17 * ObjectUtils.nullSafeHashCode(val2);
return result; return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) {
if (obj == this) {
return true; return true;
} }
if (obj == null) {
return false;
}
if (!(obj instanceof MyId)) { if (!(obj instanceof MyId)) {
return false; 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);
}
} }

39
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.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.util.ObjectUtils;
/** /**
* @author Christoph Strobl * @author Christoph Strobl
* @author Oliver Gierke
*/ */
@Document @Document
public class UserWithComplexId { public class UserWithComplexId {
@ -29,40 +31,27 @@ public class UserWithComplexId {
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31;
int result = 1; int result = 31;
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode()); result += 17 * ObjectUtils.nullSafeHashCode(id);
return result; return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) {
if (obj == this) {
return true; return true;
} }
if (obj == null) {
return false;
}
if (!(obj instanceof UserWithComplexId)) { if (!(obj instanceof UserWithComplexId)) {
return false; 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);
}
} }

Loading…
Cancel
Save