1 package tests.jfun.yan.tck; 2 3 import junit.framework.TestCase; 4 5 import java.util.ArrayList ; 6 import java.util.List ; 7 8 15 public class EqualsTestCase extends TestCase { 16 public static interface Tester { 17 void compare(Object o1, Object o2); 18 } 19 20 private List objects = new ArrayList (); 21 22 protected void setUp() throws Exception { 23 objects.add("hello"); 24 objects.add("world"); 25 objects.add("blah"); 26 } 27 28 public void testSameObjectsShouldBeReflexive() { 29 testAll(objects.toArray(), new Tester(){ 30 public void compare(Object o1, Object o2) { 31 if(o1 == o2) { 32 assertTrue("Should be reflexive", o1.equals(o2)); 33 } 34 } 35 }); 36 } 37 38 public void testObjectsShouldBeSymetricallyEqual() { 39 testAll(objects.toArray(), new Tester(){ 40 public void compare(Object o1, Object o2) { 41 if(o1.equals(o2)) { 42 assertTrue("Should be symmetric", o2.equals(o1)); 43 } 44 } 45 }); 46 } 47 48 public void testEqualityShouldBeTransitive() { 49 testAll(objects.toArray(), new Tester(){ 50 public void compare(Object o1, Object o2) { 51 if(o1.equals(o2)) { 52 assertTrue("Should be symmetric", o2.equals(o1)); 53 } 54 } 55 }); 56 } 57 58 public void testAll(Object [] oa, Tester tester) { 59 for (int i = 0; i < oa.length; i++) { 60 Object o1 = oa[i]; 61 for (int j = oa.length - 1; j >= 0; j--) { 62 Object o2 = oa[j]; 63 tester.compare(o1, o2); 64 } 65 } 66 } 67 68 } 69 | Popular Tags |