KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > diff > BasicObjectDifferenceTest


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.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 /**
13  * Unit test for {@link BasicObjectDifference}.
14  */

15 public class BasicObjectDifferenceTest extends TCTestCase {
16
17   private DifferenceContext context;
18
19   public void setUp() throws Exception JavaDoc {
20     this.context = DifferenceContext.createInitial().sub("a");
21   }
22
23   private static class OtherDiff implements Differenceable {
24     private final String JavaDoc a;
25
26     public OtherDiff(String JavaDoc a) {
27       this.a = a;
28     }
29
30     public boolean equals(Object JavaDoc that) {
31       return (that instanceof OtherDiff) && ((OtherDiff) that).a.equals(this.a);
32     }
33
34     public void addDifferences(DifferenceContext context, Object JavaDoc that) {
35       throw new ImplementMe();
36     }
37   }
38
39   private static class SubOtherDiff extends OtherDiff {
40     public SubOtherDiff(String JavaDoc a) {
41       super(a);
42     }
43   }
44
45   public void testConstruction() throws Exception JavaDoc {
46     try {
47       new BasicObjectDifference(null, "a", "b");
48       fail("Didn't get NPE on no context");
49     } catch (NullPointerException JavaDoc npe) {
50       // ok
51
}
52
53     try {
54       new BasicObjectDifference(this.context, null, null);
55       fail("Didn't get TCAE on both null");
56     } catch (TCAssertionError tcae) {
57       // ok
58
}
59
60     try {
61       new BasicObjectDifference(this.context, "a", "a");
62       fail("Didn't get TCAE on both equal");
63     } catch (TCAssertionError tcae) {
64       // ok
65
}
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       // ok
75
}
76
77     // Both equal, but not same class
78
new BasicObjectDifference(this.context, new OtherDiff("a"), new SubOtherDiff("a"));
79     // Only one is Differenceable
80
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 JavaDoc {
85     String JavaDoc one = new String JavaDoc("foobar");
86     String JavaDoc two = new String JavaDoc("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 JavaDoc {
106     Stringifier myStringifier = new Stringifier() {
107       public String JavaDoc toString(Object JavaDoc 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 JavaDoc {
121     Stringifier myStringifier = new Stringifier() {
122       public String JavaDoc toString(Object JavaDoc 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