KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > EqualityChecker


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.test;
5
6 import junit.framework.Assert;
7
8 /**
9  * Contains a simple method that checks two arrays for equality. Checks that elements at the same index in both arrays
10  * are equal; checks that all other pairs of elements are not equal. (Checks equality both ways, to make sure it's
11  * properly transitive.) Also checks that hash codes are equal when objects are equal.
12  */

13 public class EqualityChecker {
14
15   public static void checkArraysForEquality(Object JavaDoc[] env1, Object JavaDoc[] env2) {
16     checkArraysForEquality(env1, env2, true);
17   }
18
19   public static void checkArraysForEquality(Object JavaDoc[] env1, Object JavaDoc[] env2, boolean checkHashCode) {
20     if (env1 == null && env2 == null) { return; }
21     for (int i = 0; i < env1.length; ++i) {
22       for (int j = 0; j < env2.length; ++j) {
23         if (i == j) {
24           Assert.assertEquals(env1[i], env2[j]);
25           if (checkHashCode) Assert.assertEquals(env1[i].hashCode(), env2[j].hashCode());
26
27           Assert.assertEquals(env1[i], env1[i]);
28           if (checkHashCode) Assert.assertEquals(env1[i].hashCode(), env1[j].hashCode());
29
30           Assert.assertEquals(env2[i], env2[i]);
31           if (checkHashCode) Assert.assertEquals(env2[i].hashCode(), env2[j].hashCode());
32         } else {
33           Assert.assertFalse("Object in array #1 at position " + i
34                              + " is equal to the object in the same array at position " + j, env1[i].equals(env1[j]));
35           Assert
36               .assertFalse("Object in array #1 at position " + i + " is equal to object in array #2 at position " + j,
37                            env1[i].equals(env2[j]));
38           Assert.assertFalse("Object in array #2 at position " + i
39                              + " is equal to the object in the same array at position " + j, env2[i].equals(env2[j]));
40         }
41       }
42     }
43   }
44
45 }
Popular Tags