KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > builder > EqualsBuilderTest


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 java.util.Arrays JavaDoc;
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23 import junit.textui.TestRunner;
24
25 /**
26  * Unit tests {@link org.apache.commons.lang.builder.EqualsBuilder}.
27  *
28  * @author <a HREF="mailto:sdowney@panix.com">Steve Downey</a>
29  * @author <a HREF="mailto:scolebourne@joda.org">Stephen Colebourne</a>
30  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
31  * @author Maarten Coene
32  * @version $Id: EqualsBuilderTest.java 161244 2005-04-14 06:16:36Z ggregory $
33  */

34 public class EqualsBuilderTest extends TestCase {
35
36     public EqualsBuilderTest(String JavaDoc name) {
37         super(name);
38     }
39
40     public static void main(String JavaDoc[] args) {
41         TestRunner.run(suite());
42     }
43
44     public static Test suite() {
45         TestSuite suite = new TestSuite(EqualsBuilderTest.class);
46         suite.setName("EqualsBuilder Tests");
47         return suite;
48     }
49
50     protected void setUp() throws Exception JavaDoc {
51         super.setUp();
52     }
53
54     protected void tearDown() throws Exception JavaDoc {
55         super.tearDown();
56     }
57
58     //-----------------------------------------------------------------------
59

60     static class TestObject {
61         private int a;
62         public TestObject() {
63         }
64         public TestObject(int a) {
65             this.a = a;
66         }
67         public boolean equals(Object JavaDoc o) {
68             if (o == this) {
69                 return true;
70             }
71             if (!(o instanceof TestObject)) {
72                 return false;
73             }
74             TestObject rhs = (TestObject) o;
75             return (a == rhs.a);
76         }
77
78         public void setA(int a) {
79             this.a = a;
80         }
81
82         public int getA() {
83             return a;
84         }
85     }
86
87     static class TestSubObject extends TestObject {
88         private int b;
89         public TestSubObject() {
90             super(0);
91         }
92         public TestSubObject(int a, int b) {
93             super(a);
94             this.b = b;
95         }
96         public boolean equals(Object JavaDoc o) {
97             if (o == this) {
98                 return true;
99             }
100             if (!(o instanceof TestSubObject)) {
101                 return false;
102             }
103             TestSubObject rhs = (TestSubObject) o;
104             return super.equals(o) && (b == rhs.b);
105         }
106
107         public void setB(int b) {
108             this.b = b;
109         }
110
111         public int getB() {
112             return b;
113         }
114     }
115     
116     static class TestEmptySubObject extends TestObject {
117         public TestEmptySubObject(int a) {
118             super(a);
119         }
120     }
121
122     static class TestTSubObject extends TestObject {
123         private transient int t;
124         public TestTSubObject(int a, int t) {
125             super(a);
126             this.t = t;
127         }
128     }
129
130     static class TestTTSubObject extends TestTSubObject {
131         private transient int tt;
132         public TestTTSubObject(int a, int t, int tt) {
133             super(a, t);
134             this.tt = tt;
135         }
136     }
137
138     static class TestTTLeafObject extends TestTTSubObject {
139         private int leafValue;
140         public TestTTLeafObject(int a, int t, int tt, int leafValue) {
141             super(a, t, tt);
142             this.leafValue = leafValue;
143         }
144     }
145
146     static class TestTSubObject2 extends TestObject {
147         private transient int t;
148         public TestTSubObject2(int a, int t) {
149             super(a);
150         }
151         public int getT() {
152             return t;
153         }
154         public void setT(int t) {
155             this.t = t;
156         }
157     }
158
159     public void testReflectionEquals() {
160         TestObject o1 = new TestObject(4);
161         TestObject o2 = new TestObject(5);
162         assertTrue(EqualsBuilder.reflectionEquals(o1, o1));
163         assertTrue(!EqualsBuilder.reflectionEquals(o1, o2));
164         o2.setA(4);
165         assertTrue(EqualsBuilder.reflectionEquals(o1, o2));
166
167         assertTrue(!EqualsBuilder.reflectionEquals(o1, this));
168
169         assertTrue(!EqualsBuilder.reflectionEquals(o1, null));
170         assertTrue(!EqualsBuilder.reflectionEquals(null, o2));
171         assertTrue(EqualsBuilder.reflectionEquals((Object JavaDoc) null, (Object JavaDoc) null));
172     }
173     
174     public void testReflectionHierarchyEquals() {
175         testReflectionHierarchyEquals(false);
176         testReflectionHierarchyEquals(true);
177         // Transients
178
assertTrue(EqualsBuilder.reflectionEquals(new TestTTLeafObject(1, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 4), true));
179         assertTrue(EqualsBuilder.reflectionEquals(new TestTTLeafObject(1, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 4), false));
180         assertTrue(!EqualsBuilder.reflectionEquals(new TestTTLeafObject(1, 0, 0, 4), new TestTTLeafObject(1, 2, 3, 4), true));
181         assertTrue(!EqualsBuilder.reflectionEquals(new TestTTLeafObject(1, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 0), true));
182         assertTrue(!EqualsBuilder.reflectionEquals(new TestTTLeafObject(0, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 4), true));
183     }
184
185     public void testReflectionHierarchyEquals(boolean testTransients) {
186         TestObject to1 = new TestObject(4);
187         TestObject to1Bis = new TestObject(4);
188         TestObject to1Ter = new TestObject(4);
189         TestObject to2 = new TestObject(5);
190         TestEmptySubObject teso = new TestEmptySubObject(4);
191         TestTSubObject ttso = new TestTSubObject(4, 1);
192         TestTTSubObject tttso = new TestTTSubObject(4, 1, 2);
193         TestTTLeafObject ttlo = new TestTTLeafObject(4, 1, 2, 3);
194         TestSubObject tso1 = new TestSubObject(1, 4);
195         TestSubObject tso1bis = new TestSubObject(1, 4);
196         TestSubObject tso1ter = new TestSubObject(1, 4);
197         TestSubObject tso2 = new TestSubObject(2, 5);
198
199         testReflectionEqualsEquivalenceRelationship(to1, to1Bis, to1Ter, to2, new TestObject(), testTransients);
200         testReflectionEqualsEquivalenceRelationship(tso1, tso1bis, tso1ter, tso2, new TestSubObject(), testTransients);
201
202         // More sanity checks:
203

204         // same values
205
assertTrue(EqualsBuilder.reflectionEquals(ttlo, ttlo, testTransients));
206         assertTrue(EqualsBuilder.reflectionEquals(new TestSubObject(1, 10), new TestSubObject(1, 10), testTransients));
207         // same super values, diff sub values
208
assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(1, 10), new TestSubObject(1, 11), testTransients));
209         assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(1, 11), new TestSubObject(1, 10), testTransients));
210         // diff super values, same sub values
211
assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(0, 10), new TestSubObject(1, 10), testTransients));
212         assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(1, 10), new TestSubObject(0, 10), testTransients));
213
214         // mix super and sub types: equals
215
assertTrue(EqualsBuilder.reflectionEquals(to1, teso, testTransients));
216         assertTrue(EqualsBuilder.reflectionEquals(teso, to1, testTransients));
217
218         assertTrue(EqualsBuilder.reflectionEquals(to1, ttso, false)); // Force testTransients = false for this assert
219
assertTrue(EqualsBuilder.reflectionEquals(ttso, to1, false)); // Force testTransients = false for this assert
220

221         assertTrue(EqualsBuilder.reflectionEquals(to1, tttso, false)); // Force testTransients = false for this assert
222
assertTrue(EqualsBuilder.reflectionEquals(tttso, to1, false)); // Force testTransients = false for this assert
223

224         assertTrue(EqualsBuilder.reflectionEquals(ttso, tttso, false)); // Force testTransients = false for this assert
225
assertTrue(EqualsBuilder.reflectionEquals(tttso, ttso, false)); // Force testTransients = false for this assert
226

227         // mix super and sub types: NOT equals
228
assertTrue(!EqualsBuilder.reflectionEquals(new TestObject(0), new TestEmptySubObject(1), testTransients));
229         assertTrue(!EqualsBuilder.reflectionEquals(new TestEmptySubObject(1), new TestObject(0), testTransients));
230
231         assertTrue(!EqualsBuilder.reflectionEquals(new TestObject(0), new TestTSubObject(1, 1), testTransients));
232         assertTrue(!EqualsBuilder.reflectionEquals(new TestTSubObject(1, 1), new TestObject(0), testTransients));
233
234         assertTrue(!EqualsBuilder.reflectionEquals(new TestObject(1), new TestSubObject(0, 10), testTransients));
235         assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(0, 10), new TestObject(1), testTransients));
236
237         assertTrue(!EqualsBuilder.reflectionEquals(to1, ttlo));
238         assertTrue(!EqualsBuilder.reflectionEquals(tso1, this));
239     }
240
241     /**
242      * Equivalence relationship tests inspired by "Effective Java":
243      * <ul>
244      * <li>reflection</li>
245      * <li>symmetry</li>
246      * <li>transitive</li>
247      * <li>consistency</li>
248      * <li>non-null reference</li>
249      * </ul>
250      * @param to a TestObject
251      * @param toBis a TestObject, equal to to and toTer
252      * @param toTer Left hand side, equal to to and toBis
253      * @param to2 a different TestObject
254      * @param oToChange a TestObject that will be changed
255      */

256     public void testReflectionEqualsEquivalenceRelationship(
257         TestObject to,
258         TestObject toBis,
259         TestObject toTer,
260         TestObject to2,
261         TestObject oToChange,
262         boolean testTransients) {
263
264         // reflection test
265
assertTrue(EqualsBuilder.reflectionEquals(to, to, testTransients));
266         assertTrue(EqualsBuilder.reflectionEquals(to2, to2, testTransients));
267
268         // symmetry test
269
assertTrue(EqualsBuilder.reflectionEquals(to, toBis, testTransients) && EqualsBuilder.reflectionEquals(toBis, to, testTransients));
270
271         // transitive test
272
assertTrue(
273             EqualsBuilder.reflectionEquals(to, toBis, testTransients)
274                 && EqualsBuilder.reflectionEquals(toBis, toTer, testTransients)
275                 && EqualsBuilder.reflectionEquals(to, toTer, testTransients));
276
277         // consistency test
278
oToChange.setA(to.getA());
279         if (oToChange instanceof TestSubObject) {
280             ((TestSubObject) oToChange).setB(((TestSubObject) to).getB());
281         }
282         assertTrue(EqualsBuilder.reflectionEquals(oToChange, to, testTransients));
283         assertTrue(EqualsBuilder.reflectionEquals(oToChange, to, testTransients));
284         oToChange.setA(to.getA() + 1);
285         if (oToChange instanceof TestSubObject) {
286             ((TestSubObject) oToChange).setB(((TestSubObject) to).getB() + 1);
287         }
288         assertTrue(!EqualsBuilder.reflectionEquals(oToChange, to, testTransients));
289         assertTrue(!EqualsBuilder.reflectionEquals(oToChange, to, testTransients));
290
291         // non-null reference test
292
assertTrue(!EqualsBuilder.reflectionEquals(to, null, testTransients));
293         assertTrue(!EqualsBuilder.reflectionEquals(to2, null, testTransients));
294         assertTrue(!EqualsBuilder.reflectionEquals(null, to, testTransients));
295         assertTrue(!EqualsBuilder.reflectionEquals(null, to2, testTransients));
296         assertTrue(EqualsBuilder.reflectionEquals((Object JavaDoc) null, (Object JavaDoc) null, testTransients));
297     }
298
299     public void testSuper() {
300         TestObject o1 = new TestObject(4);
301         TestObject o2 = new TestObject(5);
302         assertEquals(true, new EqualsBuilder().appendSuper(true).append(o1, o1).isEquals());
303         assertEquals(false, new EqualsBuilder().appendSuper(false).append(o1, o1).isEquals());
304         assertEquals(false, new EqualsBuilder().appendSuper(true).append(o1, o2).isEquals());
305         assertEquals(false, new EqualsBuilder().appendSuper(false).append(o1, o2).isEquals());
306     }
307
308     public void testObject() {
309         TestObject o1 = new TestObject(4);
310         TestObject o2 = new TestObject(5);
311         assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
312         assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
313         o2.setA(4);
314         assertTrue(new EqualsBuilder().append(o1, o2).isEquals());
315
316         assertTrue(!new EqualsBuilder().append(o1, this).isEquals());
317         
318         assertTrue(!new EqualsBuilder().append(o1, null).isEquals());
319         assertTrue(!new EqualsBuilder().append(null, o2).isEquals());
320         assertTrue(new EqualsBuilder().append((Object JavaDoc) null, (Object JavaDoc) null).isEquals());
321     }
322
323     public void testLong() {
324         long o1 = 1L;
325         long o2 = 2L;
326         assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
327         assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
328     }
329
330     public void testInt() {
331         int o1 = 1;
332         int o2 = 2;
333         assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
334         assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
335     }
336
337     public void testShort() {
338         short o1 = 1;
339         short o2 = 2;
340         assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
341         assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
342     }
343
344     public void testChar() {
345         char o1 = 1;
346         char o2 = 2;
347         assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
348         assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
349     }
350
351     public void testByte() {
352         byte o1 = 1;
353         byte o2 = 2;
354         assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
355         assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
356     }
357
358     public void testDouble() {
359         double o1 = 1;
360         double o2 = 2;
361         assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
362         assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
363         assertTrue(!new EqualsBuilder().append(o1, Double.NaN).isEquals());
364         assertTrue(new EqualsBuilder().append(Double.NaN, Double.NaN).isEquals());
365         assertTrue(new EqualsBuilder().append(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY).isEquals());
366     }
367
368     public void testFloat() {
369         float o1 = 1;
370         float o2 = 2;
371         assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
372         assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
373         assertTrue(!new EqualsBuilder().append(o1, Float.NaN).isEquals());
374         assertTrue(new EqualsBuilder().append(Float.NaN, Float.NaN).isEquals());
375         assertTrue(new EqualsBuilder().append(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY).isEquals());
376     }
377
378     public void testAccessors() {
379         EqualsBuilder equalsBuilder = new EqualsBuilder();
380         assertTrue(equalsBuilder.isEquals());
381         equalsBuilder.setEquals(true);
382         assertTrue(equalsBuilder.isEquals());
383         equalsBuilder.setEquals(false);
384         assertFalse(equalsBuilder.isEquals());
385     }
386     
387     public void testBoolean() {
388         boolean o1 = true;
389         boolean o2 = false;
390         assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
391         assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
392     }
393
394     public void testObjectArray() {
395         TestObject[] obj1 = new TestObject[3];
396         obj1[0] = new TestObject(4);
397         obj1[1] = new TestObject(5);
398         obj1[2] = null;
399         TestObject[] obj2 = new TestObject[3];
400         obj2[0] = new TestObject(4);
401         obj2[1] = new TestObject(5);
402         obj2[2] = null;
403         
404         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
405         assertTrue(new EqualsBuilder().append(obj2, obj2).isEquals());
406         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
407         obj1[1].setA(6);
408         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
409         obj1[1].setA(5);
410         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
411         obj1[2] = obj1[1];
412         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
413         obj1[2] = null;
414         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
415                        
416         obj2 = null;
417         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
418         obj1 = null;
419         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
420     }
421
422     public void testLongArray() {
423         long[] obj1 = new long[2];
424         obj1[0] = 5L;
425         obj1[1] = 6L;
426         long[] obj2 = new long[2];
427         obj2[0] = 5L;
428         obj2[1] = 6L;
429         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
430         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
431         obj1[1] = 7;
432         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
433
434         obj2 = null;
435         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
436         obj1 = null;
437         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
438     }
439
440     public void testIntArray() {
441         int[] obj1 = new int[2];
442         obj1[0] = 5;
443         obj1[1] = 6;
444         int[] obj2 = new int[2];
445         obj2[0] = 5;
446         obj2[1] = 6;
447         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
448         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
449         obj1[1] = 7;
450         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
451
452         obj2 = null;
453         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
454         obj1 = null;
455         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
456     }
457
458     public void testShortArray() {
459         short[] obj1 = new short[2];
460         obj1[0] = 5;
461         obj1[1] = 6;
462         short[] obj2 = new short[2];
463         obj2[0] = 5;
464         obj2[1] = 6;
465         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
466         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
467         obj1[1] = 7;
468         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
469
470         obj2 = null;
471         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
472         obj1 = null;
473         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
474     }
475
476     public void testCharArray() {
477         char[] obj1 = new char[2];
478         obj1[0] = 5;
479         obj1[1] = 6;
480         char[] obj2 = new char[2];
481         obj2[0] = 5;
482         obj2[1] = 6;
483         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
484         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
485         obj1[1] = 7;
486         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
487
488         obj2 = null;
489         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
490         obj1 = null;
491         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
492     }
493
494     public void testByteArray() {
495         byte[] obj1 = new byte[2];
496         obj1[0] = 5;
497         obj1[1] = 6;
498         byte[] obj2 = new byte[2];
499         obj2[0] = 5;
500         obj2[1] = 6;
501         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
502         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
503         obj1[1] = 7;
504         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
505
506         obj2 = null;
507         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
508         obj1 = null;
509         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
510     }
511
512     public void testDoubleArray() {
513         double[] obj1 = new double[2];
514         obj1[0] = 5;
515         obj1[1] = 6;
516         double[] obj2 = new double[2];
517         obj2[0] = 5;
518         obj2[1] = 6;
519         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
520         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
521         obj1[1] = 7;
522         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
523
524         obj2 = null;
525         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
526         obj1 = null;
527         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
528     }
529
530     public void testFloatArray() {
531         float[] obj1 = new float[2];
532         obj1[0] = 5;
533         obj1[1] = 6;
534         float[] obj2 = new float[2];
535         obj2[0] = 5;
536         obj2[1] = 6;
537         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
538         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
539         obj1[1] = 7;
540         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
541
542         obj2 = null;
543         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
544         obj1 = null;
545         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
546     }
547
548     public void testBooleanArray() {
549         boolean[] obj1 = new boolean[2];
550         obj1[0] = true;
551         obj1[1] = false;
552         boolean[] obj2 = new boolean[2];
553         obj2[0] = true;
554         obj2[1] = false;
555         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
556         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
557         obj1[1] = true;
558         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
559
560         obj2 = null;
561         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
562         obj1 = null;
563         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
564     }
565
566     public void testMultiLongArray() {
567         long[][] array1 = new long[2][2];
568         long[][] array2 = new long[2][2];
569         for (int i = 0; i < array1.length; ++i) {
570             for (int j = 0; j < array1[0].length; j++) {
571                 array1[i][j] = (i + 1) * (j + 1);
572                 array2[i][j] = (i + 1) * (j + 1);
573             }
574         }
575         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
576         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
577         array1[1][1] = 0;
578         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
579     }
580
581     public void testMultiIntArray() {
582         int[][] array1 = new int[2][2];
583         int[][] array2 = new int[2][2];
584         for (int i = 0; i < array1.length; ++i) {
585             for (int j = 0; j < array1[0].length; j++) {
586                 array1[i][j] = (i + 1) * (j + 1);
587                 array2[i][j] = (i + 1) * (j + 1);
588             }
589         }
590         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
591         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
592         array1[1][1] = 0;
593         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
594     }
595
596     public void testMultiShortArray() {
597         short[][] array1 = new short[2][2];
598         short[][] array2 = new short[2][2];
599         for (short i = 0; i < array1.length; ++i) {
600             for (short j = 0; j < array1[0].length; j++) {
601                 array1[i][j] = i;
602                 array2[i][j] = i;
603             }
604         }
605         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
606         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
607         array1[1][1] = 0;
608         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
609     }
610
611     public void testMultiCharArray() {
612         char[][] array1 = new char[2][2];
613         char[][] array2 = new char[2][2];
614         for (char i = 0; i < array1.length; ++i) {
615             for (char j = 0; j < array1[0].length; j++) {
616                 array1[i][j] = i;
617                 array2[i][j] = i;
618             }
619         }
620         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
621         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
622         array1[1][1] = 0;
623         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
624     }
625
626     public void testMultiByteArray() {
627         byte[][] array1 = new byte[2][2];
628         byte[][] array2 = new byte[2][2];
629         for (byte i = 0; i < array1.length; ++i) {
630             for (byte j = 0; j < array1[0].length; j++) {
631                 array1[i][j] = i;
632                 array2[i][j] = i;
633             }
634         }
635         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
636         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
637         array1[1][1] = 0;
638         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
639     }
640     public void testMultiFloatArray() {
641         float[][] array1 = new float[2][2];
642         float[][] array2 = new float[2][2];
643         for (int i = 0; i < array1.length; ++i) {
644             for (int j = 0; j < array1[0].length; j++) {
645                 array1[i][j] = (i + 1) * (j + 1);
646                 array2[i][j] = (i + 1) * (j + 1);
647             }
648         }
649         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
650         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
651         array1[1][1] = 0;
652         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
653     }
654
655     public void testMultiDoubleArray() {
656         double[][] array1 = new double[2][2];
657         double[][] array2 = new double[2][2];
658         for (int i = 0; i < array1.length; ++i) {
659             for (int j = 0; j < array1[0].length; j++) {
660                 array1[i][j] = (i + 1) * (j + 1);
661                 array2[i][j] = (i + 1) * (j + 1);
662             }
663         }
664         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
665         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
666         array1[1][1] = 0;
667         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
668     }
669
670     public void testMultiBooleanArray() {
671         boolean[][] array1 = new boolean[2][2];
672         boolean[][] array2 = new boolean[2][2];
673         for (int i = 0; i < array1.length; ++i) {
674             for (int j = 0; j < array1[0].length; j++) {
675                 array1[i][j] = (i == 1) || (j == 1);
676                 array2[i][j] = (i == 1) || (j == 1);
677             }
678         }
679         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
680         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
681         array1[1][1] = false;
682         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
683         
684         // compare 1 dim to 2.
685
boolean[] array3 = new boolean[]{true, true};
686         assertFalse(new EqualsBuilder().append(array1, array3).isEquals());
687         assertFalse(new EqualsBuilder().append(array3, array1).isEquals());
688         assertFalse(new EqualsBuilder().append(array2, array3).isEquals());
689         assertFalse(new EqualsBuilder().append(array3, array2).isEquals());
690     }
691
692     public void testRaggedArray() {
693         long array1[][] = new long[2][];
694         long array2[][] = new long[2][];
695         for (int i = 0; i < array1.length; ++i) {
696             array1[i] = new long[2];
697             array2[i] = new long[2];
698             for (int j = 0; j < array1[i].length; ++j) {
699                 array1[i][j] = (i + 1) * (j + 1);
700                 array2[i][j] = (i + 1) * (j + 1);
701             }
702         }
703         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
704         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
705         array1[1][1] = 0;
706         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
707     }
708
709     public void testMixedArray() {
710         Object JavaDoc array1[] = new Object JavaDoc[2];
711         Object JavaDoc array2[] = new Object JavaDoc[2];
712         for (int i = 0; i < array1.length; ++i) {
713             array1[i] = new long[2];
714             array2[i] = new long[2];
715             for (int j = 0; j < 2; ++j) {
716                 ((long[]) array1[i])[j] = (i + 1) * (j + 1);
717                 ((long[]) array2[i])[j] = (i + 1) * (j + 1);
718             }
719         }
720         assertTrue(new EqualsBuilder().append(array1, array1).isEquals());
721         assertTrue(new EqualsBuilder().append(array1, array2).isEquals());
722         ((long[]) array1[1])[1] = 0;
723         assertTrue(!new EqualsBuilder().append(array1, array2).isEquals());
724     }
725
726     public void testObjectArrayHiddenByObject() {
727         TestObject[] array1 = new TestObject[2];
728         array1[0] = new TestObject(4);
729         array1[1] = new TestObject(5);
730         TestObject[] array2 = new TestObject[2];
731         array2[0] = new TestObject(4);
732         array2[1] = new TestObject(5);
733         Object JavaDoc obj1 = array1;
734         Object JavaDoc obj2 = array2;
735         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
736         assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
737         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
738         assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
739         array1[1].setA(6);
740         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
741     }
742
743     public void testLongArrayHiddenByObject() {
744         long[] array1 = new long[2];
745         array1[0] = 5L;
746         array1[1] = 6L;
747         long[] array2 = new long[2];
748         array2[0] = 5L;
749         array2[1] = 6L;
750         Object JavaDoc obj1 = array1;
751         Object JavaDoc obj2 = array2;
752         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
753         assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
754         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
755         assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
756         array1[1] = 7;
757         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
758     }
759
760     public void testIntArrayHiddenByObject() {
761         int[] array1 = new int[2];
762         array1[0] = 5;
763         array1[1] = 6;
764         int[] array2 = new int[2];
765         array2[0] = 5;
766         array2[1] = 6;
767         Object JavaDoc obj1 = array1;
768         Object JavaDoc obj2 = array2;
769         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
770         assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
771         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
772         assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
773         array1[1] = 7;
774         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
775     }
776
777     public void testShortArrayHiddenByObject() {
778         short[] array1 = new short[2];
779         array1[0] = 5;
780         array1[1] = 6;
781         short[] array2 = new short[2];
782         array2[0] = 5;
783         array2[1] = 6;
784         Object JavaDoc obj1 = array1;
785         Object JavaDoc obj2 = array2;
786         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
787         assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
788         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
789         assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
790         array1[1] = 7;
791         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
792     }
793
794     public void testCharArrayHiddenByObject() {
795         char[] array1 = new char[2];
796         array1[0] = 5;
797         array1[1] = 6;
798         char[] array2 = new char[2];
799         array2[0] = 5;
800         array2[1] = 6;
801         Object JavaDoc obj1 = array1;
802         Object JavaDoc obj2 = array2;
803         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
804         assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
805         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
806         assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
807         array1[1] = 7;
808         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
809     }
810
811     public void testByteArrayHiddenByObject() {
812         byte[] array1 = new byte[2];
813         array1[0] = 5;
814         array1[1] = 6;
815         byte[] array2 = new byte[2];
816         array2[0] = 5;
817         array2[1] = 6;
818         Object JavaDoc obj1 = array1;
819         Object JavaDoc obj2 = array2;
820         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
821         assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
822         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
823         assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
824         array1[1] = 7;
825         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
826     }
827
828     public void testDoubleArrayHiddenByObject() {
829         double[] array1 = new double[2];
830         array1[0] = 5;
831         array1[1] = 6;
832         double[] array2 = new double[2];
833         array2[0] = 5;
834         array2[1] = 6;
835         Object JavaDoc obj1 = array1;
836         Object JavaDoc obj2 = array2;
837         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
838         assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
839         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
840         assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
841         array1[1] = 7;
842         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
843     }
844
845     public void testFloatArrayHiddenByObject() {
846         float[] array1 = new float[2];
847         array1[0] = 5;
848         array1[1] = 6;
849         float[] array2 = new float[2];
850         array2[0] = 5;
851         array2[1] = 6;
852         Object JavaDoc obj1 = array1;
853         Object JavaDoc obj2 = array2;
854         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
855         assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
856         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
857         assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
858         array1[1] = 7;
859         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
860     }
861
862     public void testBooleanArrayHiddenByObject() {
863         boolean[] array1 = new boolean[2];
864         array1[0] = true;
865         array1[1] = false;
866         boolean[] array2 = new boolean[2];
867         array2[0] = true;
868         array2[1] = false;
869         Object JavaDoc obj1 = array1;
870         Object JavaDoc obj2 = array2;
871         assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
872         assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
873         assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
874         assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
875         array1[1] = true;
876         assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
877     }
878     
879     public static class TestACanEqualB {
880         private int a;
881
882         public TestACanEqualB(int a) {
883             this.a = a;
884         }
885
886         public boolean equals(Object JavaDoc o) {
887             if (o == this)
888                 return true;
889             if (o instanceof TestACanEqualB)
890                 return this.a == ((TestACanEqualB) o).getA();
891             if (o instanceof TestBCanEqualA)
892                 return this.a == ((TestBCanEqualA) o).getB();
893             return false;
894         }
895
896         public int getA() {
897             return this.a;
898         }
899     }
900
901     public static class TestBCanEqualA {
902         private int b;
903
904         public TestBCanEqualA(int b) {
905             this.b = b;
906         }
907
908         public boolean equals(Object JavaDoc o) {
909             if (o == this)
910                 return true;
911             if (o instanceof TestACanEqualB)
912                 return this.b == ((TestACanEqualB) o).getA();
913             if (o instanceof TestBCanEqualA)
914                 return this.b == ((TestBCanEqualA) o).getB();
915             return false;
916         }
917
918         public int getB() {
919             return this.b;
920         }
921     }
922     
923     /**
924      * Tests two instances of classes that can be equal and that are not "related". The two classes are not subclasses
925      * of each other and do not share a parent aside from Object.
926      * See http://issues.apache.org/bugzilla/show_bug.cgi?id=33069
927      */

928     public void testUnrelatedClasses() {
929         Object JavaDoc[] x = new Object JavaDoc[]{new TestACanEqualB(1)};
930         Object JavaDoc[] y = new Object JavaDoc[]{new TestBCanEqualA(1)};
931
932         // sanity checks:
933
assertTrue(Arrays.equals(x, x));
934         assertTrue(Arrays.equals(y, y));
935         assertTrue(Arrays.equals(x, y));
936         assertTrue(Arrays.equals(y, x));
937         // real tests:
938
assertTrue(x[0].equals(x[0]));
939         assertTrue(y[0].equals(y[0]));
940         assertTrue(x[0].equals(y[0]));
941         assertTrue(y[0].equals(x[0]));
942         assertTrue(new EqualsBuilder().append(x, x).isEquals());
943         assertTrue(new EqualsBuilder().append(y, y).isEquals());
944         assertTrue(new EqualsBuilder().append(x, y).isEquals());
945         assertTrue(new EqualsBuilder().append(y, x).isEquals());
946     }
947     
948     /**
949      * Test from http://issues.apache.org/bugzilla/show_bug.cgi?id=33067
950      */

951     public void testNpeForNullElement() {
952         Object JavaDoc[] x1 = new Object JavaDoc[] { new Integer JavaDoc(1), null, new Integer JavaDoc(3) };
953         Object JavaDoc[] x2 = new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3) };
954
955         // causes an NPE in 2.0 according to:
956
// http://issues.apache.org/bugzilla/show_bug.cgi?id=33067
957
new EqualsBuilder().append(x1, x2);
958     }
959 }
960
Popular Tags