Skip to content

Commit 3b70b6a

Browse files
committed
DATAMONGO-1078 - Polishing.
Polished test cases. Simplified equals(…)/hashCode() for sample entity and its identifier type. Original pull request: #239.
1 parent 163762e commit 3b70b6a

File tree

3 files changed

+42
-65
lines changed

3 files changed

+42
-65
lines changed

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
*/
1616
package org.springframework.data.mongodb.repository;
1717

18-
import static org.hamcrest.collection.IsCollectionWithSize.*;
19-
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
20-
import static org.hamcrest.core.IsEqual.*;
18+
import static org.hamcrest.Matchers.*;
2119
import static org.junit.Assert.*;
2220

2321
import java.util.Collections;
2422
import java.util.List;
2523

24+
import org.hamcrest.Matchers;
2625
import org.junit.Before;
2726
import org.junit.Test;
2827
import org.junit.runner.RunWith;
@@ -39,6 +38,7 @@
3938

4039
/**
4140
* @author Christoph Strobl
41+
* @author Oliver Gierke
4242
*/
4343
@RunWith(SpringJUnit4ClassRunner.class)
4444
@ContextConfiguration
@@ -61,14 +61,14 @@ public Mongo mongo() throws Exception {
6161
}
6262

6363
@Autowired UserWithComplexIdRepository repo;
64-
6564
@Autowired MongoTemplate template;
6665

67-
private MyId id;
68-
private UserWithComplexId userWithId;
66+
MyId id;
67+
UserWithComplexId userWithId;
6968

7069
@Before
7170
public void setUp() {
71+
7272
repo.deleteAll();
7373

7474
id = new MyId();
@@ -88,9 +88,7 @@ public void annotatedFindQueryShouldWorkWhenUsingComplexId() {
8888

8989
repo.save(userWithId);
9090

91-
UserWithComplexId loaded = repo.getUserByComplexId(id);
92-
93-
assertThat(loaded, equalTo(userWithId));
91+
assertThat(repo.getUserByComplexId(id), is(userWithId));
9492
}
9593

9694
/**
@@ -115,9 +113,7 @@ public void findOneShouldWorkWhenUsingComplexId() {
115113

116114
repo.save(userWithId);
117115

118-
UserWithComplexId loaded = repo.findOne(id);
119-
120-
assertThat(loaded, equalTo(userWithId));
116+
assertThat(repo.findOne(id), is(userWithId));
121117
}
122118

123119
/**
@@ -128,10 +124,9 @@ public void findAllShouldWorkWhenUsingComplexId() {
128124

129125
repo.save(userWithId);
130126

131-
List<UserWithComplexId> loaded = (List<UserWithComplexId>) repo.findAll(Collections.singleton(id));
127+
Iterable<UserWithComplexId> loaded = repo.findAll(Collections.singleton(id));
132128

133-
assertThat(loaded, hasSize(1));
129+
assertThat(loaded, is(Matchers.<UserWithComplexId> iterableWithSize(1)));
134130
assertThat(loaded, contains(userWithId));
135131
}
136-
137132
}

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

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,43 @@
1717

1818
import java.io.Serializable;
1919

20+
import org.springframework.util.ObjectUtils;
21+
2022
/**
2123
* @author Christoph Strobl
24+
* @author Oliver Gierke
2225
*/
2326
public class MyId implements Serializable {
2427

28+
private static final long serialVersionUID = -7129201311241750831L;
29+
2530
String val1;
2631
String val2;
2732

2833
@Override
2934
public int hashCode() {
30-
final int prime = 31;
31-
int result = 1;
32-
result = prime * result + ((val1 == null) ? 0 : val1.hashCode());
33-
result = prime * result + ((val2 == null) ? 0 : val2.hashCode());
35+
36+
int result = 31;
37+
38+
result += 17 * ObjectUtils.nullSafeHashCode(val1);
39+
result += 17 * ObjectUtils.nullSafeHashCode(val2);
40+
3441
return result;
3542
}
3643

3744
@Override
3845
public boolean equals(Object obj) {
39-
if (this == obj) {
46+
47+
if (obj == this) {
4048
return true;
4149
}
42-
if (obj == null) {
43-
return false;
44-
}
50+
4551
if (!(obj instanceof MyId)) {
4652
return false;
4753
}
48-
MyId other = (MyId) obj;
49-
if (val1 == null) {
50-
if (other.val1 != null) {
51-
return false;
52-
}
53-
} else if (!val1.equals(other.val1)) {
54-
return false;
55-
}
56-
if (val2 == null) {
57-
if (other.val2 != null) {
58-
return false;
59-
}
60-
} else if (!val2.equals(other.val2)) {
61-
return false;
62-
}
63-
return true;
64-
}
6554

55+
MyId that = (MyId) obj;
56+
57+
return ObjectUtils.nullSafeEquals(this.val1, that.val1) && ObjectUtils.nullSafeEquals(this.val2, that.val2);
58+
}
6659
}

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

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
import org.springframework.data.annotation.Id;
1919
import org.springframework.data.mongodb.core.mapping.Document;
20+
import org.springframework.util.ObjectUtils;
2021

2122
/**
2223
* @author Christoph Strobl
24+
* @author Oliver Gierke
2325
*/
2426
@Document
2527
public class UserWithComplexId {
@@ -29,40 +31,27 @@ public class UserWithComplexId {
2931

3032
@Override
3133
public int hashCode() {
32-
final int prime = 31;
33-
int result = 1;
34-
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
35-
result = prime * result + ((id == null) ? 0 : id.hashCode());
34+
35+
int result = 31;
36+
37+
result += 17 * ObjectUtils.nullSafeHashCode(id);
38+
3639
return result;
3740
}
3841

3942
@Override
4043
public boolean equals(Object obj) {
41-
if (this == obj) {
44+
45+
if (obj == this) {
4246
return true;
4347
}
44-
if (obj == null) {
45-
return false;
46-
}
48+
4749
if (!(obj instanceof UserWithComplexId)) {
4850
return false;
4951
}
50-
UserWithComplexId other = (UserWithComplexId) obj;
51-
if (firstname == null) {
52-
if (other.firstname != null) {
53-
return false;
54-
}
55-
} else if (!firstname.equals(other.firstname)) {
56-
return false;
57-
}
58-
if (id == null) {
59-
if (other.id != null) {
60-
return false;
61-
}
62-
} else if (!id.equals(other.id)) {
63-
return false;
64-
}
65-
return true;
66-
}
6752

53+
UserWithComplexId that = (UserWithComplexId) obj;
54+
55+
return ObjectUtils.nullSafeEquals(this.id, that.id);
56+
}
6857
}

0 commit comments

Comments
 (0)