1 4 package com.tc.util.diff; 5 6 import com.tc.exception.ImplementMe; 7 import com.tc.test.EqualityChecker; 8 import com.tc.test.TCTestCase; 9 import com.tc.util.Stringifier; 10 import com.tc.util.TCAssertionError; 11 12 15 public class BasicObjectDifferenceTest extends TCTestCase { 16 17 private DifferenceContext context; 18 19 public void setUp() throws Exception { 20 this.context = DifferenceContext.createInitial().sub("a"); 21 } 22 23 private static class OtherDiff implements Differenceable { 24 private final String a; 25 26 public OtherDiff(String a) { 27 this.a = a; 28 } 29 30 public boolean equals(Object that) { 31 return (that instanceof OtherDiff) && ((OtherDiff) that).a.equals(this.a); 32 } 33 34 public void addDifferences(DifferenceContext context, Object that) { 35 throw new ImplementMe(); 36 } 37 } 38 39 private static class SubOtherDiff extends OtherDiff { 40 public SubOtherDiff(String a) { 41 super(a); 42 } 43 } 44 45 public void testConstruction() throws Exception { 46 try { 47 new BasicObjectDifference(null, "a", "b"); 48 fail("Didn't get NPE on no context"); 49 } catch (NullPointerException npe) { 50 } 52 53 try { 54 new BasicObjectDifference(this.context, null, null); 55 fail("Didn't get TCAE on both null"); 56 } catch (TCAssertionError tcae) { 57 } 59 60 try { 61 new BasicObjectDifference(this.context, "a", "a"); 62 fail("Didn't get TCAE on both equal"); 63 } catch (TCAssertionError tcae) { 64 } 66 67 try { 68 OtherDiff diff = new OtherDiff("a"); 69 OtherDiff diff2 = new OtherDiff("a"); 70 71 new BasicObjectDifference(this.context, diff, diff2); 72 fail("Didn't get TCAE on equal arguments"); 73 } catch (TCAssertionError tcae) { 74 } 76 77 new BasicObjectDifference(this.context, new OtherDiff("a"), new SubOtherDiff("a")); 79 new BasicObjectDifference(this.context, new OtherDiff("a"), "a"); 81 new BasicObjectDifference(this.context, "a", new OtherDiff("a")); 82 } 83 84 public void testABAndToString() throws Exception { 85 String one = new String ("foobar"); 86 String two = new String ("bazbar"); 87 88 BasicObjectDifference diff = new BasicObjectDifference(this.context, one, two); 89 assertSame(one, diff.a()); 90 assertSame(two, diff.b()); 91 92 diff = new BasicObjectDifference(this.context, null, two); 93 assertSame(null, diff.a()); 94 assertSame(two, diff.b()); 95 96 diff = new BasicObjectDifference(this.context, one, null); 97 assertSame(one, diff.a()); 98 assertSame(null, diff.b()); 99 100 diff = new BasicObjectDifference(this.context, one, two); 101 assertTrue(diff.toString().indexOf(one) >= 0); 102 assertTrue(diff.toString().indexOf(two) >= 0); 103 } 104 105 public void testUsesStringifier() throws Exception { 106 Stringifier myStringifier = new Stringifier() { 107 public String toString(Object o) { 108 return "XX" + o + "YY"; 109 } 110 }; 111 112 DifferenceContext theContext = DifferenceContext.createInitial(myStringifier); 113 assertSame(myStringifier, theContext.stringifier()); 114 115 BasicObjectDifference diff = new BasicObjectDifference(theContext, "a", "b"); 116 assertTrue(diff.toString().indexOf("XXaYY") >= 0); 117 assertTrue(diff.toString().indexOf("XXbYY") >= 0); 118 } 119 120 public void testEquals() throws Exception { 121 Stringifier myStringifier = new Stringifier() { 122 public String toString(Object o) { 123 return "XX" + o + "YY"; 124 } 125 }; 126 127 DifferenceContext theContext = DifferenceContext.createInitial(myStringifier); 128 129 DifferenceContext secondContext = DifferenceContext.createInitial().sub("b"); 130 DifferenceContext thirdContext = DifferenceContext.createInitial().sub("b"); 131 132 BasicObjectDifference[] one = new BasicObjectDifference[] { new BasicObjectDifference(theContext, "a", "b"), 133 new BasicObjectDifference(theContext, "a", null), new BasicObjectDifference(theContext, null, "b"), 134 new BasicObjectDifference(secondContext, "a", "b"), new BasicObjectDifference(theContext, "c", "d"), }; 135 136 BasicObjectDifference[] two = new BasicObjectDifference[] { new BasicObjectDifference(theContext, "a", "b"), 137 new BasicObjectDifference(theContext, "a", null), new BasicObjectDifference(theContext, null, "b"), 138 new BasicObjectDifference(thirdContext, "a", "b"), new BasicObjectDifference(theContext, "c", "d"), }; 139 140 EqualityChecker.checkArraysForEquality(one, two, false); 141 } 142 143 } | Popular Tags |