001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.lang.builder;
019:
020: import org.apache.commons.lang.builder.ToStringBuilderTest.ReflectionTestCycleA;
021: import org.apache.commons.lang.builder.ToStringBuilderTest.ReflectionTestCycleB;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026: import junit.textui.TestRunner;
027:
028: /**
029: * Unit tests {@link org.apache.commons.lang.builder.HashCodeBuilder}.
030: *
031: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
032: * @version $Id: HashCodeBuilderTest.java 451406 2006-09-29 19:56:41Z ggregory $
033: */
034: public class HashCodeBuilderTest extends TestCase {
035:
036: /**
037: * A reflection test fixture.
038: */
039: static class ReflectionTestCycleA {
040: ReflectionTestCycleB b;
041:
042: public int hashCode() {
043: return HashCodeBuilder.reflectionHashCode(this );
044: }
045: }
046:
047: /**
048: * A reflection test fixture.
049: */
050: static class ReflectionTestCycleB {
051: ReflectionTestCycleA a;
052:
053: public int hashCode() {
054: return HashCodeBuilder.reflectionHashCode(this );
055: }
056: }
057:
058: public HashCodeBuilderTest(String name) {
059: super (name);
060: }
061:
062: public static void main(String[] args) {
063: TestRunner.run(suite());
064: }
065:
066: public static Test suite() {
067: TestSuite suite = new TestSuite(HashCodeBuilderTest.class);
068: suite.setName("HashCodeBuilder Tests");
069: return suite;
070: }
071:
072: protected void setUp() throws Exception {
073: super .setUp();
074: }
075:
076: protected void tearDown() throws Exception {
077: super .tearDown();
078: }
079:
080: // -----------------------------------------------------------------------
081:
082: public void testConstructorEx1() {
083: try {
084: new HashCodeBuilder(0, 0);
085:
086: } catch (IllegalArgumentException ex) {
087: return;
088: }
089: fail();
090: }
091:
092: public void testConstructorEx2() {
093: try {
094: new HashCodeBuilder(2, 2);
095:
096: } catch (IllegalArgumentException ex) {
097: return;
098: }
099: fail();
100: }
101:
102: static class TestObject {
103: private int a;
104:
105: public TestObject(int a) {
106: this .a = a;
107: }
108:
109: public boolean equals(Object o) {
110: if (o == this ) {
111: return true;
112: }
113: if (!(o instanceof TestObject)) {
114: return false;
115: }
116: TestObject rhs = (TestObject) o;
117: return (a == rhs.a);
118: }
119:
120: public void setA(int a) {
121: this .a = a;
122: }
123:
124: public int getA() {
125: return a;
126: }
127: }
128:
129: static class TestSubObject extends TestObject {
130: private int b;
131:
132: transient private int t;
133:
134: public TestSubObject() {
135: super (0);
136: }
137:
138: public TestSubObject(int a, int b, int t) {
139: super (a);
140: this .b = b;
141: this .t = t;
142: }
143:
144: public boolean equals(Object o) {
145: if (o == this ) {
146: return true;
147: }
148: if (!(o instanceof TestSubObject)) {
149: return false;
150: }
151: TestSubObject rhs = (TestSubObject) o;
152: return super .equals(o) && (b == rhs.b);
153: }
154: }
155:
156: public void testReflectionHashCode() {
157: assertEquals(17 * 37, HashCodeBuilder
158: .reflectionHashCode(new TestObject(0)));
159: assertEquals(17 * 37 + 123456, HashCodeBuilder
160: .reflectionHashCode(new TestObject(123456)));
161: }
162:
163: public void testReflectionHierarchyHashCode() {
164: assertEquals(17 * 37 * 37, HashCodeBuilder
165: .reflectionHashCode(new TestSubObject(0, 0, 0)));
166: assertEquals(17 * 37 * 37 * 37, HashCodeBuilder
167: .reflectionHashCode(new TestSubObject(0, 0, 0), true));
168: assertEquals((17 * 37 + 7890) * 37 + 123456, HashCodeBuilder
169: .reflectionHashCode(new TestSubObject(123456, 7890, 0)));
170: assertEquals(((17 * 37 + 7890) * 37 + 0) * 37 + 123456,
171: HashCodeBuilder.reflectionHashCode(new TestSubObject(
172: 123456, 7890, 0), true));
173: }
174:
175: public void testReflectionHierarchyHashCodeEx1() {
176: try {
177: HashCodeBuilder.reflectionHashCode(0, 0, new TestSubObject(
178: 0, 0, 0), true);
179: } catch (IllegalArgumentException ex) {
180: return;
181: }
182: fail();
183: }
184:
185: public void testReflectionHierarchyHashCodeEx2() {
186: try {
187: HashCodeBuilder.reflectionHashCode(2, 2, new TestSubObject(
188: 0, 0, 0), true);
189: } catch (IllegalArgumentException ex) {
190: return;
191: }
192: fail();
193: }
194:
195: public void testReflectionHashCodeEx1() {
196: try {
197: HashCodeBuilder.reflectionHashCode(0, 0, new TestObject(0),
198: true);
199: } catch (IllegalArgumentException ex) {
200: return;
201: }
202: fail();
203: }
204:
205: public void testReflectionHashCodeEx2() {
206: try {
207: HashCodeBuilder.reflectionHashCode(2, 2, new TestObject(0),
208: true);
209: } catch (IllegalArgumentException ex) {
210: return;
211: }
212: fail();
213: }
214:
215: public void testReflectionHashCodeEx3() {
216: try {
217: HashCodeBuilder.reflectionHashCode(13, 19, null, true);
218: } catch (IllegalArgumentException ex) {
219: return;
220: }
221: fail();
222: }
223:
224: public void testSuper() {
225: Object obj = new Object();
226: assertEquals(17 * 37 + (19 * 41 + obj.hashCode()),
227: new HashCodeBuilder(17, 37).appendSuper(
228: new HashCodeBuilder(19, 41).append(obj)
229: .toHashCode()).toHashCode());
230: }
231:
232: public void testObject() {
233: Object obj = null;
234: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj)
235: .toHashCode());
236: obj = new Object();
237: assertEquals(17 * 37 + obj.hashCode(), new HashCodeBuilder(17,
238: 37).append(obj).toHashCode());
239: }
240:
241: public void testLong() {
242: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
243: (long) 0L).toHashCode());
244: assertEquals(17 * 37 + (int) (123456789L ^ (123456789L >> 32)),
245: new HashCodeBuilder(17, 37).append((long) 123456789L)
246: .toHashCode());
247: }
248:
249: public void testInt() {
250: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
251: (int) 0).toHashCode());
252: assertEquals(17 * 37 + 123456, new HashCodeBuilder(17, 37)
253: .append((int) 123456).toHashCode());
254: }
255:
256: public void testShort() {
257: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
258: (short) 0).toHashCode());
259: assertEquals(17 * 37 + 12345, new HashCodeBuilder(17, 37)
260: .append((short) 12345).toHashCode());
261: }
262:
263: public void testChar() {
264: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
265: (char) 0).toHashCode());
266: assertEquals(17 * 37 + 1234, new HashCodeBuilder(17, 37)
267: .append((char) 1234).toHashCode());
268: }
269:
270: public void testByte() {
271: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
272: (byte) 0).toHashCode());
273: assertEquals(17 * 37 + 123, new HashCodeBuilder(17, 37).append(
274: (byte) 123).toHashCode());
275: }
276:
277: public void testDouble() {
278: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
279: (double) 0d).toHashCode());
280: double d = 1234567.89;
281: long l = Double.doubleToLongBits(d);
282: assertEquals(17 * 37 + (int) (l ^ (l >> 32)),
283: new HashCodeBuilder(17, 37).append(d).toHashCode());
284: }
285:
286: public void testFloat() {
287: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
288: (float) 0f).toHashCode());
289: float f = 1234.89f;
290: int i = Float.floatToIntBits(f);
291: assertEquals(17 * 37 + i, new HashCodeBuilder(17, 37).append(f)
292: .toHashCode());
293: }
294:
295: public void testBoolean() {
296: assertEquals(17 * 37 + 0, new HashCodeBuilder(17, 37).append(
297: true).toHashCode());
298: assertEquals(17 * 37 + 1, new HashCodeBuilder(17, 37).append(
299: false).toHashCode());
300: }
301:
302: public void testObjectArray() {
303: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
304: (Object[]) null).toHashCode());
305: Object[] obj = new Object[2];
306: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
307: .append(obj).toHashCode());
308: obj[0] = new Object();
309: assertEquals((17 * 37 + obj[0].hashCode()) * 37,
310: new HashCodeBuilder(17, 37).append(obj).toHashCode());
311: obj[1] = new Object();
312: assertEquals((17 * 37 + obj[0].hashCode()) * 37
313: + obj[1].hashCode(), new HashCodeBuilder(17, 37)
314: .append(obj).toHashCode());
315: }
316:
317: public void testObjectArrayAsObject() {
318: Object[] obj = new Object[2];
319: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
320: .append((Object) obj).toHashCode());
321: obj[0] = new Object();
322: assertEquals((17 * 37 + obj[0].hashCode()) * 37,
323: new HashCodeBuilder(17, 37).append((Object) obj)
324: .toHashCode());
325: obj[1] = new Object();
326: assertEquals((17 * 37 + obj[0].hashCode()) * 37
327: + obj[1].hashCode(), new HashCodeBuilder(17, 37)
328: .append((Object) obj).toHashCode());
329: }
330:
331: public void testLongArray() {
332: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
333: (long[]) null).toHashCode());
334: long[] obj = new long[2];
335: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
336: .append(obj).toHashCode());
337: obj[0] = 5L;
338: int h1 = (int) (5L ^ (5L >> 32));
339: assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37)
340: .append(obj).toHashCode());
341: obj[1] = 6L;
342: int h2 = (int) (6L ^ (6L >> 32));
343: assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17,
344: 37).append(obj).toHashCode());
345: }
346:
347: public void testLongArrayAsObject() {
348: long[] obj = new long[2];
349: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
350: .append((Object) obj).toHashCode());
351: obj[0] = 5L;
352: int h1 = (int) (5L ^ (5L >> 32));
353: assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37)
354: .append((Object) obj).toHashCode());
355: obj[1] = 6L;
356: int h2 = (int) (6L ^ (6L >> 32));
357: assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17,
358: 37).append((Object) obj).toHashCode());
359: }
360:
361: public void testIntArray() {
362: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
363: (int[]) null).toHashCode());
364: int[] obj = new int[2];
365: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
366: .append(obj).toHashCode());
367: obj[0] = 5;
368: assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37)
369: .append(obj).toHashCode());
370: obj[1] = 6;
371: assertEquals((17 * 37 + 5) * 37 + 6,
372: new HashCodeBuilder(17, 37).append(obj).toHashCode());
373: }
374:
375: public void testIntArrayAsObject() {
376: int[] obj = new int[2];
377: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
378: .append((Object) obj).toHashCode());
379: obj[0] = 5;
380: assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37)
381: .append((Object) obj).toHashCode());
382: obj[1] = 6;
383: assertEquals((17 * 37 + 5) * 37 + 6,
384: new HashCodeBuilder(17, 37).append((Object) obj)
385: .toHashCode());
386: }
387:
388: public void testShortArray() {
389: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
390: (short[]) null).toHashCode());
391: short[] obj = new short[2];
392: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
393: .append(obj).toHashCode());
394: obj[0] = (short) 5;
395: assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37)
396: .append(obj).toHashCode());
397: obj[1] = (short) 6;
398: assertEquals((17 * 37 + 5) * 37 + 6,
399: new HashCodeBuilder(17, 37).append(obj).toHashCode());
400: }
401:
402: public void testShortArrayAsObject() {
403: short[] obj = new short[2];
404: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
405: .append((Object) obj).toHashCode());
406: obj[0] = (short) 5;
407: assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37)
408: .append((Object) obj).toHashCode());
409: obj[1] = (short) 6;
410: assertEquals((17 * 37 + 5) * 37 + 6,
411: new HashCodeBuilder(17, 37).append((Object) obj)
412: .toHashCode());
413: }
414:
415: public void testCharArray() {
416: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
417: (char[]) null).toHashCode());
418: char[] obj = new char[2];
419: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
420: .append(obj).toHashCode());
421: obj[0] = (char) 5;
422: assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37)
423: .append(obj).toHashCode());
424: obj[1] = (char) 6;
425: assertEquals((17 * 37 + 5) * 37 + 6,
426: new HashCodeBuilder(17, 37).append(obj).toHashCode());
427: }
428:
429: public void testCharArrayAsObject() {
430: char[] obj = new char[2];
431: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
432: .append((Object) obj).toHashCode());
433: obj[0] = (char) 5;
434: assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37)
435: .append((Object) obj).toHashCode());
436: obj[1] = (char) 6;
437: assertEquals((17 * 37 + 5) * 37 + 6,
438: new HashCodeBuilder(17, 37).append((Object) obj)
439: .toHashCode());
440: }
441:
442: public void testByteArray() {
443: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
444: (byte[]) null).toHashCode());
445: byte[] obj = new byte[2];
446: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
447: .append(obj).toHashCode());
448: obj[0] = (byte) 5;
449: assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37)
450: .append(obj).toHashCode());
451: obj[1] = (byte) 6;
452: assertEquals((17 * 37 + 5) * 37 + 6,
453: new HashCodeBuilder(17, 37).append(obj).toHashCode());
454: }
455:
456: public void testByteArrayAsObject() {
457: byte[] obj = new byte[2];
458: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
459: .append((Object) obj).toHashCode());
460: obj[0] = (byte) 5;
461: assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37)
462: .append((Object) obj).toHashCode());
463: obj[1] = (byte) 6;
464: assertEquals((17 * 37 + 5) * 37 + 6,
465: new HashCodeBuilder(17, 37).append((Object) obj)
466: .toHashCode());
467: }
468:
469: public void testDoubleArray() {
470: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
471: (double[]) null).toHashCode());
472: double[] obj = new double[2];
473: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
474: .append(obj).toHashCode());
475: obj[0] = 5.4d;
476: long l1 = Double.doubleToLongBits(5.4d);
477: int h1 = (int) (l1 ^ (l1 >> 32));
478: assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37)
479: .append(obj).toHashCode());
480: obj[1] = 6.3d;
481: long l2 = Double.doubleToLongBits(6.3d);
482: int h2 = (int) (l2 ^ (l2 >> 32));
483: assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17,
484: 37).append(obj).toHashCode());
485: }
486:
487: public void testDoubleArrayAsObject() {
488: double[] obj = new double[2];
489: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
490: .append((Object) obj).toHashCode());
491: obj[0] = 5.4d;
492: long l1 = Double.doubleToLongBits(5.4d);
493: int h1 = (int) (l1 ^ (l1 >> 32));
494: assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37)
495: .append((Object) obj).toHashCode());
496: obj[1] = 6.3d;
497: long l2 = Double.doubleToLongBits(6.3d);
498: int h2 = (int) (l2 ^ (l2 >> 32));
499: assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17,
500: 37).append((Object) obj).toHashCode());
501: }
502:
503: public void testFloatArray() {
504: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
505: (float[]) null).toHashCode());
506: float[] obj = new float[2];
507: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
508: .append(obj).toHashCode());
509: obj[0] = 5.4f;
510: int h1 = Float.floatToIntBits(5.4f);
511: assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37)
512: .append(obj).toHashCode());
513: obj[1] = 6.3f;
514: int h2 = Float.floatToIntBits(6.3f);
515: assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17,
516: 37).append(obj).toHashCode());
517: }
518:
519: public void testFloatArrayAsObject() {
520: float[] obj = new float[2];
521: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
522: .append((Object) obj).toHashCode());
523: obj[0] = 5.4f;
524: int h1 = Float.floatToIntBits(5.4f);
525: assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37)
526: .append((Object) obj).toHashCode());
527: obj[1] = 6.3f;
528: int h2 = Float.floatToIntBits(6.3f);
529: assertEquals((17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17,
530: 37).append((Object) obj).toHashCode());
531: }
532:
533: public void testBooleanArray() {
534: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(
535: (boolean[]) null).toHashCode());
536: boolean[] obj = new boolean[2];
537: assertEquals((17 * 37 + 1) * 37 + 1,
538: new HashCodeBuilder(17, 37).append(obj).toHashCode());
539: obj[0] = true;
540: assertEquals((17 * 37 + 0) * 37 + 1,
541: new HashCodeBuilder(17, 37).append(obj).toHashCode());
542: obj[1] = false;
543: assertEquals((17 * 37 + 0) * 37 + 1,
544: new HashCodeBuilder(17, 37).append(obj).toHashCode());
545: }
546:
547: public void testBooleanArrayAsObject() {
548: boolean[] obj = new boolean[2];
549: assertEquals((17 * 37 + 1) * 37 + 1,
550: new HashCodeBuilder(17, 37).append((Object) obj)
551: .toHashCode());
552: obj[0] = true;
553: assertEquals((17 * 37 + 0) * 37 + 1,
554: new HashCodeBuilder(17, 37).append((Object) obj)
555: .toHashCode());
556: obj[1] = false;
557: assertEquals((17 * 37 + 0) * 37 + 1,
558: new HashCodeBuilder(17, 37).append((Object) obj)
559: .toHashCode());
560: }
561:
562: public void testBooleanMultiArray() {
563: boolean[][] obj = new boolean[2][];
564: assertEquals((17 * 37) * 37, new HashCodeBuilder(17, 37)
565: .append(obj).toHashCode());
566: obj[0] = new boolean[0];
567: assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj)
568: .toHashCode());
569: obj[0] = new boolean[1];
570: assertEquals((17 * 37 + 1) * 37, new HashCodeBuilder(17, 37)
571: .append(obj).toHashCode());
572: obj[0] = new boolean[2];
573: assertEquals(((17 * 37 + 1) * 37 + 1) * 37,
574: new HashCodeBuilder(17, 37).append(obj).toHashCode());
575: obj[0][0] = true;
576: assertEquals(((17 * 37 + 0) * 37 + 1) * 37,
577: new HashCodeBuilder(17, 37).append(obj).toHashCode());
578: obj[1] = new boolean[1];
579: assertEquals((((17 * 37 + 0) * 37 + 1) * 37 + 1),
580: new HashCodeBuilder(17, 37).append(obj).toHashCode());
581: }
582:
583: public void testReflectionHashCodeExcludeFields() throws Exception {
584: TestObjectWithMultipleFields x = new TestObjectWithMultipleFields(
585: 1, 2, 3);
586:
587: assertEquals((((17 * 37 + 1) * 37 + 2) * 37 + 3),
588: HashCodeBuilder.reflectionHashCode(x));
589:
590: assertEquals((((17 * 37 + 1) * 37 + 2) * 37 + 3),
591: HashCodeBuilder.reflectionHashCode(x, (String[]) null));
592: assertEquals((((17 * 37 + 1) * 37 + 2) * 37 + 3),
593: HashCodeBuilder.reflectionHashCode(x, new String[] {}));
594: assertEquals((((17 * 37 + 1) * 37 + 2) * 37 + 3),
595: HashCodeBuilder.reflectionHashCode(x,
596: new String[] { "xxx" }));
597:
598: assertEquals(((17 * 37 + 1) * 37 + 3), HashCodeBuilder
599: .reflectionHashCode(x, new String[] { "two" }));
600: assertEquals(((17 * 37 + 1) * 37 + 2), HashCodeBuilder
601: .reflectionHashCode(x, new String[] { "three" }));
602:
603: assertEquals((17 * 37 + 1), HashCodeBuilder.reflectionHashCode(
604: x, new String[] { "two", "three" }));
605:
606: assertEquals(17, HashCodeBuilder.reflectionHashCode(x,
607: new String[] { "one", "two", "three" }));
608: assertEquals(17, HashCodeBuilder.reflectionHashCode(x,
609: new String[] { "one", "two", "three", "xxx" }));
610: }
611:
612: static class TestObjectWithMultipleFields {
613: private int one = 0;
614:
615: private int two = 0;
616:
617: private int three = 0;
618:
619: public TestObjectWithMultipleFields(int one, int two, int three) {
620: this .one = one;
621: this .two = two;
622: this .three = three;
623: }
624: }
625:
626: /**
627: * Test Objects pointing to each other.
628: */
629: public void testReflectionObjectCycle() {
630: ReflectionTestCycleA a = new ReflectionTestCycleA();
631: ReflectionTestCycleB b = new ReflectionTestCycleB();
632: a.b = b;
633: b.a = a;
634:
635: // Used to caused:
636: // java.lang.StackOverflowError
637: // at java.lang.ClassLoader.getCallerClassLoader(Native Method)
638: // at java.lang.Class.getDeclaredFields(Class.java:992)
639: // at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:373)
640: // at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:349)
641: // at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:155)
642: // at
643: // org.apache.commons.lang.builder.HashCodeBuilderTest$ReflectionTestCycleB.hashCode(HashCodeBuilderTest.java:53)
644: // at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:422)
645: // at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:383)
646: // at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:349)
647: // at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:155)
648: // at
649: // org.apache.commons.lang.builder.HashCodeBuilderTest$ReflectionTestCycleA.hashCode(HashCodeBuilderTest.java:42)
650: // at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:422)
651:
652: a.hashCode();
653: b.hashCode();
654: }
655:
656: }
|