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 @@ @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -128,10 +124,9 @@ public class ComplexIdRepositoryIntegrationTests {
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));
}
}

43
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MyId.java

@ -17,50 +17,43 @@ package org.springframework.data.mongodb.repository; @@ -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);
}
}

39
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UserWithComplexId.java

@ -17,9 +17,11 @@ package org.springframework.data.mongodb.repository; @@ -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 { @@ -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);
}
}

Loading…
Cancel
Save