1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang.builder;
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 import junit.textui.TestRunner;
23
24
25
26
27
28
29
30
31
32 public class HashCodeBuilderAndEqualsBuilderTest extends TestCase {
33
34
35
36
37
38 public HashCodeBuilderAndEqualsBuilderTest(String name) {
39 super(name);
40 }
41
42 public static void main(String[] args) {
43 TestRunner.run(suite());
44 }
45
46 public static Test suite() {
47 TestSuite suite = new TestSuite(HashCodeBuilderAndEqualsBuilderTest.class);
48 suite.setName("HashCodeBuilderAndEqualsBuilder Tests");
49 return suite;
50 }
51
52 protected void setUp() throws Exception {
53 super.setUp();
54 }
55
56 protected void tearDown() throws Exception {
57 super.tearDown();
58 }
59
60
61
62 public void testInteger(boolean testTransients) {
63 Integer i1 = new Integer(12345);
64 Integer i2 = new Integer(12345);
65 assertEqualsAndHashCodeContract(i1, i2, testTransients);
66 }
67
68 public void testInteger() {
69 testInteger(false);
70 }
71
72 public void testIntegerWithTransients() {
73 testInteger(true);
74 }
75
76 public void testFixture() {
77 testFixture(false);
78 }
79
80 public void testFixtureWithTransients() {
81 testFixture(true);
82 }
83
84 public void testFixture(boolean testTransients) {
85 assertEqualsAndHashCodeContract(new TestFixture(2, 'c', "Test", (short) 2), new TestFixture(2, 'c', "Test", (short) 2), testTransients);
86 assertEqualsAndHashCodeContract(
87 new AllTransientFixture(2, 'c', "Test", (short) 2),
88 new AllTransientFixture(2, 'c', "Test", (short) 2),
89 testTransients);
90 assertEqualsAndHashCodeContract(
91 new SubTestFixture(2, 'c', "Test", (short) 2, "Same"),
92 new SubTestFixture(2, 'c', "Test", (short) 2, "Same"),
93 testTransients);
94 assertEqualsAndHashCodeContract(
95 new SubAllTransientFixture(2, 'c', "Test", (short) 2, "Same"),
96 new SubAllTransientFixture(2, 'c', "Test", (short) 2, "Same"),
97 testTransients);
98 }
99
100
101
102
103
104
105
106
107
108 public void assertEqualsAndHashCodeContract(Object lhs, Object rhs, boolean testTransients) {
109 if (EqualsBuilder.reflectionEquals(lhs, rhs, testTransients)) {
110
111 assertEquals(HashCodeBuilder.reflectionHashCode(lhs, testTransients), HashCodeBuilder.reflectionHashCode(rhs, testTransients));
112 assertEquals(HashCodeBuilder.reflectionHashCode(lhs, testTransients), HashCodeBuilder.reflectionHashCode(rhs, testTransients));
113 assertEquals(HashCodeBuilder.reflectionHashCode(lhs, testTransients), HashCodeBuilder.reflectionHashCode(rhs, testTransients));
114 }
115 }
116
117 static class TestFixture {
118 int i;
119 char c;
120 String string;
121 short s;
122
123 TestFixture(int i, char c, String string, short s) {
124 this.i = i;
125 this.c = c;
126 this.string = string;
127 this.s = s;
128 }
129 }
130
131 static class SubTestFixture extends TestFixture {
132 transient String tString;
133
134 SubTestFixture(int i, char c, String string, short s, String tString) {
135 super(i, c, string, s);
136 this.tString = tString;
137 }
138 }
139
140 static class AllTransientFixture {
141 transient int i;
142 transient char c;
143 transient String string;
144 transient short s;
145
146 AllTransientFixture(int i, char c, String string, short s) {
147 this.i = i;
148 this.c = c;
149 this.string = string;
150 this.s = s;
151 }
152 }
153
154 static class SubAllTransientFixture extends AllTransientFixture {
155 transient String tString;
156
157 SubAllTransientFixture(int i, char c, String string, short s, String tString) {
158 super(i, c, string, s);
159 this.tString = tString;
160 }
161 }
162
163
164 }