Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cc4f82a

Browse files
thymingreda-alaoui
authored andcommitted
Add equals/hashCode implementations to the EntityGraph classes (#22)
Add equals/hashCode implementations to the EntityGraph classes
1 parent 2f63315 commit cc4f82a

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

‎src/main/java/com/cosium/spring/data/jpa/entity/graph/domain/DynamicEntityGraph.java‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.cosium.spring.data.jpa.entity.graph.domain;
22

33
import com.google.common.base.MoreObjects;
4-
54
import java.util.Collections;
65
import java.util.List;
6+
import java.util.Objects;
77

88
/**
99
* Created on 22/11/16.
@@ -37,4 +37,23 @@ public final String getEntityGraphName() {
3737
public String toString() {
3838
return MoreObjects.toStringHelper(this).add("attributePaths", attributePaths).toString();
3939
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) {
44+
return true;
45+
}
46+
if (o == null || getClass() != o.getClass()) {
47+
return false;
48+
}
49+
DynamicEntityGraph that = (DynamicEntityGraph) o;
50+
return attributePaths.equals(that.attributePaths)
51+
&& getEntityGraphType() == that.getEntityGraphType()
52+
&& isOptional() == that.isOptional();
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Objects.hash(attributePaths, getEntityGraphType(), isOptional());
58+
}
4059
}

‎src/main/java/com/cosium/spring/data/jpa/entity/graph/domain/NamedEntityGraph.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cosium.spring.data.jpa.entity.graph.domain;
22

33
import com.google.common.base.MoreObjects;
4+
import java.util.Objects;
45
import org.springframework.util.Assert;
56

67
import java.util.List;
@@ -42,4 +43,23 @@ public final List<String> getEntityGraphAttributePaths() {
4243
public String toString() {
4344
return MoreObjects.toStringHelper(this).add("name", name).toString();
4445
}
46+
47+
@Override
48+
public boolean equals(Object o) {
49+
if (this == o) {
50+
return true;
51+
}
52+
if (o == null || getClass() != o.getClass()) {
53+
return false;
54+
}
55+
NamedEntityGraph that = (NamedEntityGraph) o;
56+
return name.equals(that.name)
57+
&& getEntityGraphType() == that.getEntityGraphType()
58+
&& isOptional() == that.isOptional();
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return Objects.hash(name, getEntityGraphType(), isOptional());
64+
}
4565
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.cosium.spring.data.jpa.entity.graph.domain;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotEquals;
5+
6+
import com.google.common.collect.Lists;
7+
import java.util.List;
8+
import org.junit.Test;
9+
10+
public class DynamicEntityGraphTest {
11+
12+
@Test
13+
public void testGraphsWithSamePathsEqual() {
14+
List<String> paths = Lists.newArrayList("path1", "path2", "path3");
15+
final DynamicEntityGraph graph1 = new DynamicEntityGraph(paths);
16+
final DynamicEntityGraph graph2 = new DynamicEntityGraph(paths);
17+
assertEquals(graph1, graph2);
18+
assertEquals(graph1.hashCode(), graph2.hashCode());
19+
}
20+
21+
@Test
22+
public void testGraphsWithDifferentTypesNotEqual() {
23+
List<String> paths = Lists.newArrayList("path1", "path2", "path3");
24+
final DynamicEntityGraph graph1 = new DynamicEntityGraph(EntityGraphType.FETCH, paths);
25+
final DynamicEntityGraph graph2 = new DynamicEntityGraph(EntityGraphType.LOAD, paths);
26+
assertNotEquals(graph1, graph2);
27+
assertNotEquals(graph1.hashCode(), graph2.hashCode());
28+
}
29+
30+
@Test
31+
public void testGraphsWithDifferentOptionalityNotEqual() {
32+
List<String> paths = Lists.newArrayList("path1", "path2", "path3");
33+
final DynamicEntityGraph optionalGraph = new DynamicEntityGraph(EntityGraphType.LOAD, paths);
34+
optionalGraph.setOptional(true);
35+
final DynamicEntityGraph requiredGraph = new DynamicEntityGraph(EntityGraphType.LOAD, paths);
36+
requiredGraph.setOptional(false);
37+
38+
assertNotEquals(optionalGraph, requiredGraph);
39+
assertNotEquals(optionalGraph.hashCode(), requiredGraph.hashCode());
40+
}
41+
42+
@Test
43+
public void testGraphsWithDifferentClassNotEqual() {
44+
final EntityGraph namedEntityGraph = new NamedEntityGraph(EntityGraphType.LOAD, "graph");
45+
final EntityGraph dynamicEntityGraph =
46+
new DynamicEntityGraph(EntityGraphType.LOAD, Lists.newArrayList("path"));
47+
48+
assertNotEquals(dynamicEntityGraph, namedEntityGraph);
49+
assertNotEquals(dynamicEntityGraph.hashCode(), namedEntityGraph.hashCode());
50+
}
51+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.cosium.spring.data.jpa.entity.graph.domain;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotEquals;
5+
6+
import com.google.common.collect.Lists;
7+
import org.junit.Test;
8+
9+
public class NamedEntityGraphTest {
10+
11+
@Test
12+
public void testGraphsWithSameNamesEqual() {
13+
final EntityGraph graph1 = EntityGraphs.named("graph");
14+
final EntityGraph graph2 = EntityGraphs.named("graph");
15+
assertEquals(graph1, graph2);
16+
assertEquals(graph1.hashCode(), graph2.hashCode());
17+
}
18+
19+
@Test
20+
public void testGraphsWithDifferentNamesNotEqual() {
21+
final EntityGraph graph1 = EntityGraphs.named("graph1");
22+
final EntityGraph graph2 = EntityGraphs.named("graph2");
23+
assertNotEquals(graph1, graph2);
24+
assertNotEquals(graph1.hashCode(), graph2.hashCode());
25+
}
26+
27+
@Test
28+
public void testGraphsWithDifferentTypesNotEqual() {
29+
30+
final NamedEntityGraph graph1 = new NamedEntityGraph(EntityGraphType.LOAD, "graph");
31+
final NamedEntityGraph graph2 = new NamedEntityGraph(EntityGraphType.FETCH, "graph");
32+
assertNotEquals(graph1, graph2);
33+
assertNotEquals(graph1.hashCode(), graph2.hashCode());
34+
}
35+
36+
@Test
37+
public void testGraphsWithDifferentOptionalityNotEqual() {
38+
final EntityGraph optionalGraph = new NamedEntityGraph(EntityGraphType.LOAD, true, "graph");
39+
final EntityGraph requiredGraph = new NamedEntityGraph(EntityGraphType.LOAD, false, "graph");
40+
41+
assertNotEquals(optionalGraph, requiredGraph);
42+
assertNotEquals(optionalGraph.hashCode(), requiredGraph.hashCode());
43+
}
44+
45+
@Test
46+
public void testGraphsWithDifferentClassNotEqual() {
47+
final EntityGraph namedEntityGraph = new NamedEntityGraph(EntityGraphType.LOAD, "graph");
48+
final EntityGraph dynamicEntityGraph =
49+
new DynamicEntityGraph(EntityGraphType.LOAD, Lists.newArrayList("path"));
50+
51+
assertNotEquals(namedEntityGraph, dynamicEntityGraph);
52+
assertNotEquals(namedEntityGraph.hashCode(), dynamicEntityGraph.hashCode());
53+
}
54+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /