1 /*
2 * Copyright 2002-2005 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.commons.lang.builder;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 /**
24 * Unit tests {@link org.apache.commons.lang.builder.HashCodeBuilder}.
25 *
26 * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
27 * @version $Id: HashCodeBuilderTest.java 161244 2005-04-14 06:16:36Z ggregory $
28 */
29 public class HashCodeBuilderTest extends TestCase {
30
31 public HashCodeBuilderTest(String name) {
32 super(name);
33 }
34
35 public static void main(String[] args) {
36 TestRunner.run(suite());
37 }
38
39 public static Test suite() {
40 TestSuite suite = new TestSuite(HashCodeBuilderTest.class);
41 suite.setName("HashCodeBuilder Tests");
42 return suite;
43 }
44
45 protected void setUp() throws Exception {
46 super.setUp();
47 }
48
49 protected void tearDown() throws Exception {
50 super.tearDown();
51 }
52
53 //-----------------------------------------------------------------------
54
55 public void testConstructorEx1() {
56 try {
57 new HashCodeBuilder(0, 0);
58
59 } catch (IllegalArgumentException ex) {
60 return;
61 }
62 fail();
63 }
64
65 public void testConstructorEx2() {
66 try {
67 new HashCodeBuilder(2, 2);
68
69 } catch (IllegalArgumentException ex) {
70 return;
71 }
72 fail();
73 }
74
75 static class TestObject {
76 private int a;
77 public TestObject(int a) {
78 this.a = a;
79 }
80 public boolean equals(Object o) {
81 if (o == this) {
82 return true;
83 }
84 if (!(o instanceof TestObject)) {
85 return false;
86 }
87 TestObject rhs = (TestObject) o;
88 return (a == rhs.a);
89 }
90
91 public void setA(int a) {
92 this.a = a;
93 }
94
95 public int getA() {
96 return a;
97 }
98 }
99
100 static class TestSubObject extends TestObject {
101 private int b;
102 transient private int t;
103 public TestSubObject() {
104 super(0);
105 }
106 public TestSubObject(int a, int b, int t) {
107 super(a);
108 this.b = b;
109 this.t = t;
110 }
111 public boolean equals(Object o) {
112 if (o == this) {
113 return true;
114 }
115 if (!(o instanceof TestSubObject)) {
116 return false;
117 }
118 TestSubObject rhs = (TestSubObject) o;
119 return super.equals(o) && (b == rhs.b);
120 }
121 }
122
123 public void testReflectionHashCode() {
124 assertEquals(17 * 37, HashCodeBuilder.reflectionHashCode(new TestObject(0)));
125 assertEquals(17 * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestObject(123456)));
126 }
127
128 public void testReflectionHierarchyHashCode() {
129 assertEquals(17 * 37 * 37, HashCodeBuilder.reflectionHashCode(new TestSubObject(0, 0, 0)));
130 assertEquals(17 * 37 * 37 * 37, HashCodeBuilder.reflectionHashCode(new TestSubObject(0, 0, 0), true));
131 assertEquals((17 * 37 + 7890) * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestSubObject(123456, 7890, 0)));
132 assertEquals(((17 * 37 + 7890) * 37 + 0) * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestSubObject(123456, 7890, 0), true));
133 }
134
135 public void testReflectionHierarchyHashCodeEx1() {
136 try {
137 HashCodeBuilder.reflectionHashCode(0, 0, new TestSubObject(0, 0, 0), true);
138 } catch (IllegalArgumentException ex) {
139 return;
140 }
141 fail();
142 }
143
144 public void testReflectionHierarchyHashCodeEx2() {
145 try {
146 HashCodeBuilder.reflectionHashCode(2, 2, new TestSubObject(0, 0, 0), true);
147 } catch (IllegalArgumentException ex) {
148 return;
149 }
150 fail();
151 }
152
153 public void testReflectionHashCodeEx1() {
154 try {
155 HashCodeBuilder.reflectionHashCode(0, 0, new TestObject(0), true);
156 } catch (IllegalArgumentException ex) {
157 return;
158 }
159 fail();
160 }
161
162 public void testReflectionHashCodeEx2() {
163 try {
164 HashCodeBuilder.reflectionHashCode(2, 2, new TestObject(0), true);
165 } catch (IllegalArgumentException ex) {
166 return;
167 }
168 fail();
169 }
170
171 public void testReflectionHashCodeEx3() {
172 try {
173 HashCodeBuilder.reflectionHashCode(13, 19, null, true);
174 } catch (IllegalArgumentException ex) {
175 return;
176 }
177 fail();
178 }
179
180 public void testSuper() {
181 Object obj = new Object();
182 assertEquals(17 * 37 + (19 * 41 + obj.hashCode()), new HashCodeBuilder(17, 37).appendSuper(
183 new HashCodeBuilder(19, 41).append(obj).toHashCode()
184 ).toHashCode());
185 }
186
187 public void testObject() {
188 Object obj = null;
189 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
190 obj = new Object();
191 assertEquals(17 * 37 + obj.hashCode(), new HashCodeBuilder(17, 37).append(obj).toHashCode());
192 }
193
194 public void testLong() {
195 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((long) 0L).toHashCode());
196 assertEquals(17 * 37 + (int) (123456789L ^ (123456789L >> 32)), new HashCodeBuilder(17, 37).append((long) 123456789L).toHashCode());
197 }
198
199 public void testInt() {
200 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((int) 0).toHashCode());
201 assertEquals(17 * 37 + 123456, new HashCodeBuilder(17, 37).append((int) 123456).toHashCode());
202 }
203
204 public void testShort() {
205 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((short) 0).toHashCode());
206 assertEquals(17 * 37 + 12345, new HashCodeBuilder(17, 37).append((short) 12345).toHashCode());
207 }
208
209 public void testChar() {
210 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((char) 0).toHashCode());
211 assertEquals(17 * 37 + 1234, new HashCodeBuilder(17, 37).append((char) 1234).toHashCode());
212 }
213
214 public void testByte() {
215 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((byte) 0).toHashCode());
216 assertEquals(17 * 37 + 123, new HashCodeBuilder(17, 37).append((byte) 123).toHashCode());
217 }
218
219 public void testDouble() {
220 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((double) 0d).toHashCode());
221 double d = 1234567.89;
222 long l = Double.doubleToLongBits(d);
223 assertEquals(17 * 37 + (int) (l ^ (l >> 32)), new HashCodeBuilder(17, 37).append(d).toHashCode());
224 }
225
226 public void testFloat() {
227 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((float) 0f).toHashCode());
228 float f = 1234.89f;
229 int i = Float.floatToIntBits(f);
230 assertEquals(17 * 37 + i, new HashCodeBuilder(17, 37).append(f).toHashCode());
231 }
232
233 public void testBoolean() {
234 assertEquals(17 * 37 + 0, new HashCodeBuilder(17, 37).append(true).toHashCode());
235 assertEquals(17 * 37 + 1, new HashCodeBuilder(17, 37).append(false).toHashCode());
236 }
237
238 public void testObjectArray() {
239 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((Object[]) null).toHashCode());
240 Object[] obj = new Object[2];
241 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
242 obj[0] = new Object();
243 assertEquals((17 * 37 + obj[0].hashCode()) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
244 obj[1] = new Object();
245 assertEquals( (17 * 37 + obj[0].hashCode()) * 37 + obj[1].hashCode(), new HashCodeBuilder(17, 37).append(obj).toHashCode());
246 }
247
248 public void testObjectArrayAsObject() {
249 Object[] obj = new Object[2];
250 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
251 obj[0] = new Object();
252 assertEquals((17 * 37 + obj[0].hashCode()) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
253 obj[1] = new Object();
254 assertEquals( (17 * 37 + obj[0].hashCode()) * 37 + obj[1].hashCode(), new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
255 }
256
257 public void testLongArray() {
258 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((long[]) null).toHashCode());
259 long[] obj = new long[2];
260 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
261 obj[0] = 5L;
262 int h1 = (int) (5L ^ (5L >> 32));
263 assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
264 obj[1] = 6L;
265 int h2 = (int) (6L ^ (6L >> 32));
266 assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
267 }
268
269 public void testLongArrayAsObject() {
270 long[] obj = new long[2];
271 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
272 obj[0] = 5L;
273 int h1 = (int) (5L ^ (5L >> 32));
274 assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
275 obj[1] = 6L;
276 int h2 = (int) (6L ^ (6L >> 32));
277 assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
278 }
279
280 public void testIntArray() {
281 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((int[]) null).toHashCode());
282 int[] obj = new int[2];
283 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
284 obj[0] = 5;
285 assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
286 obj[1] = 6;
287 assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
288 }
289
290 public void testIntArrayAsObject() {
291 int[] obj = new int[2];
292 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
293 obj[0] = 5;
294 assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
295 obj[1] = 6;
296 assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
297 }
298
299 public void testShortArray() {
300 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((short[]) null).toHashCode());
301 short[] obj = new short[2];
302 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
303 obj[0] = (short) 5;
304 assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
305 obj[1] = (short) 6;
306 assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
307 }
308
309 public void testShortArrayAsObject() {
310 short[] obj = new short[2];
311 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
312 obj[0] = (short) 5;
313 assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
314 obj[1] = (short) 6;
315 assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
316 }
317
318 public void testCharArray() {
319 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((char[]) null).toHashCode());
320 char[] obj = new char[2];
321 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
322 obj[0] = (char) 5;
323 assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
324 obj[1] = (char) 6;
325 assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
326 }
327
328 public void testCharArrayAsObject() {
329 char[] obj = new char[2];
330 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
331 obj[0] = (char) 5;
332 assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
333 obj[1] = (char) 6;
334 assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
335 }
336
337 public void testByteArray() {
338 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((byte[]) null).toHashCode());
339 byte[] obj = new byte[2];
340 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
341 obj[0] = (byte) 5;
342 assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
343 obj[1] = (byte) 6;
344 assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
345 }
346
347 public void testByteArrayAsObject() {
348 byte[] obj = new byte[2];
349 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
350 obj[0] = (byte) 5;
351 assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
352 obj[1] = (byte) 6;
353 assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
354 }
355
356 public void testDoubleArray() {
357 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((double[]) null).toHashCode());
358 double[] obj = new double[2];
359 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
360 obj[0] = 5.4d;
361 long l1 = Double.doubleToLongBits(5.4d);
362 int h1 = (int) (l1 ^ (l1 >> 32));
363 assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
364 obj[1] = 6.3d;
365 long l2 = Double.doubleToLongBits(6.3d);
366 int h2 = (int) (l2 ^ (l2 >> 32));
367 assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
368 }
369
370 public void testDoubleArrayAsObject() {
371 double[] obj = new double[2];
372 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
373 obj[0] = 5.4d;
374 long l1 = Double.doubleToLongBits(5.4d);
375 int h1 = (int) (l1 ^ (l1 >> 32));
376 assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
377 obj[1] = 6.3d;
378 long l2 = Double.doubleToLongBits(6.3d);
379 int h2 = (int) (l2 ^ (l2 >> 32));
380 assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
381 }
382
383 public void testFloatArray() {
384 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((float[]) null).toHashCode());
385 float[] obj = new float[2];
386 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
387 obj[0] = 5.4f;
388 int h1 = Float.floatToIntBits(5.4f);
389 assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
390 obj[1] = 6.3f;
391 int h2 = Float.floatToIntBits(6.3f);
392 assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
393 }
394
395 public void testFloatArrayAsObject() {
396 float[] obj = new float[2];
397 assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
398 obj[0] = 5.4f;
399 int h1 = Float.floatToIntBits(5.4f);
400 assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
401 obj[1] = 6.3f;
402 int h2 = Float.floatToIntBits(6.3f);
403 assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
404 }
405
406 public void testBooleanArray() {
407 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((boolean[]) null).toHashCode());
408 boolean[] obj = new boolean[2];
409 assertEquals((17 * 37 + 1) * 37 + 1 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
410 obj[0] = true;
411 assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
412 obj[1] = false;
413 assertEquals( (17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
414 }
415
416 public void testBooleanArrayAsObject() {
417 boolean[] obj = new boolean[2];
418 assertEquals((17 * 37 + 1) * 37 + 1 , new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
419 obj[0] = true;
420 assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
421 obj[1] = false;
422 assertEquals( (17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append((Object) obj).toHashCode());
423 }
424
425 public void testBooleanMultiArray() {
426 boolean[][] obj = new boolean[2][];
427 assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
428 obj[0] = new boolean[0];
429 assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
430 obj[0] = new boolean[1];
431 assertEquals((17 * 37 + 1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
432 obj[0] = new boolean[2];
433 assertEquals(((17 * 37 + 1) * 37 + 1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
434 obj[0][0] = true;
435 assertEquals(((17 * 37 + 0) * 37 + 1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
436 obj[1] = new boolean[1];
437 assertEquals( (((17 * 37 + 0) * 37 + 1) * 37 + 1), new HashCodeBuilder(17, 37).append(obj).toHashCode());
438 }
439
440 }