KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.lang.builder.EqualsBuilder;
7
8 import com.tc.test.TCTestCase;
9 import com.tc.util.StandardStringifier;
10 import com.tc.util.Stringifier;
11 import com.tc.util.TCAssertionError;
12 import com.tc.util.ToStringStringifier;
13
14 /**
15  * Unit test for {@link DifferenceContext}.
16  */

17 public class DifferenceContextTest extends TCTestCase {
18
19   public void testInitial() throws Exception JavaDoc {
20     assertEquals("", DifferenceContext.createInitial().toString());
21     assertNotNull(DifferenceContext.createInitial().collection());
22     assertFalse(DifferenceContext.createInitial().collection() == DifferenceContext.createInitial().collection());
23     
24     try {
25       DifferenceContext.createInitial(null);
26       fail("Didn't get NPE on no stringifier");
27     } catch (NullPointerException JavaDoc npe) {
28       // ok
29
}
30   }
31
32   public void testStringifier() throws Exception JavaDoc {
33     assertSame(StandardStringifier.INSTANCE, DifferenceContext.createInitial().stringifier());
34     assertSame(ToStringStringifier.INSTANCE, DifferenceContext.createInitial(ToStringStringifier.INSTANCE)
35         .stringifier());
36
37     Stringifier s = new Stringifier() {
38       public String JavaDoc toString(Object JavaDoc o) {
39         return "X" + o + "Y";
40       }
41     };
42     
43     assertEquals("XaY", DifferenceContext.createInitial(s).describe("a"));
44     assertEquals("XbY", DifferenceContext.createInitial(s).sub("Q").describe("b"));
45   }
46
47   public void testSubAndToString() throws Exception JavaDoc {
48     DifferenceContext initial = DifferenceContext.createInitial();
49
50     DifferenceContext a = initial.sub("a");
51     assertNotSame(a, initial);
52     assertEquals("/a", a.toString());
53     assertEquals("", initial.toString());
54     assertSame(initial.collection(), a.collection());
55
56     DifferenceContext b = a.sub("b");
57     assertNotSame(a, b);
58     assertEquals("/a/b", b.toString());
59     assertEquals("/a", a.toString());
60     assertEquals("", initial.toString());
61     assertSame(initial.collection(), b.collection());
62
63     DifferenceContext c = b.sub("c");
64     assertNotSame(c, b);
65     assertNotSame(c, a);
66     assertEquals("/a/b/c", c.toString());
67     assertEquals("/a/b", b.toString());
68     assertEquals("/a", a.toString());
69     assertEquals("", initial.toString());
70     assertSame(initial.collection(), c.collection());
71
72     DifferenceContext d = a.sub("d");
73     assertNotSame(d, c);
74     assertNotSame(d, b);
75     assertNotSame(d, a);
76     assertEquals("/a/d", d.toString());
77     assertEquals("/a/b/c", c.toString());
78     assertEquals("/a/b", b.toString());
79     assertEquals("/a", a.toString());
80     assertEquals("", initial.toString());
81     assertSame(initial.collection(), d.collection());
82   }
83
84   public void testDifferences() throws Exception JavaDoc {
85     DifferenceContext initial = DifferenceContext.createInitial();
86     DifferenceContext otherInitial = DifferenceContext.createInitial();
87
88     assertNotSame(initial, otherInitial);
89
90     assertEquals(initial, otherInitial);
91     assertFalse(initial.hasDifferences());
92     assertEqualsOrdered(new Object JavaDoc[0], initial.getDifferences());
93
94     MockDifference initialDifference = new MockDifference(initial, "foo", "bar");
95     initial.addDifference(initialDifference);
96     assertFalse(initial.equals(otherInitial));
97     assertTrue(initial.hasDifferences());
98     assertFalse(otherInitial.hasDifferences());
99     assertEqualsOrdered(new Object JavaDoc[] { initialDifference }, initial.getDifferences());
100     assertEqualsOrdered(new Object JavaDoc[0], otherInitial.getDifferences());
101
102     MockDifference otherInitialDifference = new MockDifference(otherInitial, "foo", "bar");
103     otherInitial.addDifference(otherInitialDifference);
104     assertEquals(initial, otherInitial);
105     assertTrue(initial.hasDifferences());
106     assertTrue(otherInitial.hasDifferences());
107     assertEqualsOrdered(new Object JavaDoc[] { initialDifference }, initial.getDifferences());
108     assertEqualsOrdered(new Object JavaDoc[] { otherInitialDifference }, otherInitial.getDifferences());
109
110     initial.addDifference(new MockDifference(initial, "foo", "baz"));
111     assertFalse(initial.equals(otherInitial));
112     assertFalse(otherInitial.equals(initial));
113
114     otherInitial.addDifference(new MockDifference(otherInitial, "foo", "quux"));
115     assertFalse(initial.equals(otherInitial));
116     assertFalse(otherInitial.equals(initial));
117
118     initial = DifferenceContext.createInitial();
119     otherInitial = DifferenceContext.createInitial();
120
121     assertEquals(initial, otherInitial);
122     assertNotSame(initial, otherInitial);
123
124     DifferenceContext initA = initial.sub("a");
125     DifferenceContext otherInitA = otherInitial.sub("a");
126     DifferenceContext initB = initA.sub("b");
127     DifferenceContext otherInitB = otherInitA.sub("b");
128     DifferenceContext initC = initial.sub("c");
129     DifferenceContext otherInitC = otherInitial.sub("c");
130
131     assertEquals(initial, otherInitial);
132     assertEquals(initA, otherInitA);
133     assertEquals(initB, otherInitB);
134     assertEquals(initC, otherInitC);
135
136     MockDifference diffInit = new MockDifference(initial, "1", "2");
137     MockDifference diffOtherInit = new MockDifference(otherInitial, "1", "2");
138     MockDifference diffInitA = new MockDifference(initA, "3", "4");
139     MockDifference diffOtherInitA = new MockDifference(otherInitA, "3", "4");
140     MockDifference diffInitB = new MockDifference(initB, "5", "6");
141     MockDifference diffOtherInitB = new MockDifference(otherInitB, "5", "6");
142     MockDifference diffInitC = new MockDifference(initC, "7", "8");
143     MockDifference diffOtherInitC = new MockDifference(otherInitC, "7", "8");
144
145     DifferenceContext[] initialContexts = new DifferenceContext[] { initial, initA, initB, initC };
146     DifferenceContext[] otherInitialContexts = new DifferenceContext[] { otherInitial, otherInitA, otherInitB,
147         otherInitC };
148
149     initial.addDifference(diffInit);
150     checkDifferences(initialContexts, new Object JavaDoc[] { diffInit }, otherInitialContexts, new Object JavaDoc[0]);
151     otherInitial.addDifference(diffOtherInit);
152     checkDifferences(initialContexts, new Object JavaDoc[] { diffInit }, otherInitialContexts, new Object JavaDoc[] { diffOtherInit });
153
154     initA.addDifference(diffInitA);
155     checkDifferences(initialContexts, new Object JavaDoc[] { diffInit, diffInitA }, otherInitialContexts,
156                      new Object JavaDoc[] { diffOtherInit });
157     otherInitA.addDifference(diffOtherInitA);
158     checkDifferences(initialContexts, new Object JavaDoc[] { diffInit, diffInitA }, otherInitialContexts, new Object JavaDoc[] {
159         diffOtherInit, diffOtherInitA });
160
161     initB.addDifference(diffInitB);
162     checkDifferences(initialContexts, new Object JavaDoc[] { diffInit, diffInitA, diffInitB }, otherInitialContexts,
163                      new Object JavaDoc[] { diffOtherInit, diffOtherInitA });
164     otherInitB.addDifference(diffOtherInitB);
165     checkDifferences(initialContexts, new Object JavaDoc[] { diffInit, diffInitA, diffInitB }, otherInitialContexts,
166                      new Object JavaDoc[] { diffOtherInit, diffOtherInitA, diffOtherInitB });
167
168     initC.addDifference(diffInitC);
169     checkDifferences(initialContexts, new Object JavaDoc[] { diffInit, diffInitA, diffInitB, diffInitC }, otherInitialContexts,
170                      new Object JavaDoc[] { diffOtherInit, diffOtherInitA, diffOtherInitB });
171     otherInitC.addDifference(diffOtherInitC);
172     checkDifferences(initialContexts, new Object JavaDoc[] { diffInit, diffInitA, diffInitB, diffInitC }, otherInitialContexts,
173                      new Object JavaDoc[] { diffOtherInit, diffOtherInitA, diffOtherInitB, diffOtherInitC });
174
175     try {
176       initial.addDifference(null);
177       fail("Didn't get NPE on adding null difference");
178     } catch (NullPointerException JavaDoc npe) {
179       // ok
180
}
181
182     try {
183       initial.addDifference(otherInitialDifference);
184       fail("Didn't get TCAE on add of difference with different context");
185     } catch (TCAssertionError tcae) {
186       // ok
187
}
188
189     try {
190       initial.addDifference(new MockDifference(initial.sub("a")));
191       fail("Didn't get TCAE on add of difference with different context");
192     } catch (TCAssertionError tcae) {
193       // ok
194
}
195
196     try {
197       initial.sub("a").addDifference(otherInitialDifference);
198       fail("Didn't get TCAE on add of difference with different context");
199     } catch (TCAssertionError tcae) {
200       // ok
201
}
202   }
203
204   private void checkDifferences(DifferenceContext[] oneContexts, Object JavaDoc[] expectedOneDifferences,
205                                 DifferenceContext[] twoContexts, Object JavaDoc[] expectedTwoDifferences) {
206     for (int i = 0; i < oneContexts.length; ++i) {
207       assertEqualsOrdered(expectedOneDifferences, oneContexts[i].getDifferences());
208       if (expectedOneDifferences.length > 0) assertTrue(oneContexts[i].hasDifferences());
209       else assertFalse(oneContexts[i].hasDifferences());
210       assertEquals(expectedOneDifferences.length, oneContexts[i].countDifferences());
211     }
212
213     for (int i = 0; i < twoContexts.length; ++i) {
214       assertEqualsOrdered(expectedTwoDifferences, twoContexts[i].getDifferences());
215       if (expectedTwoDifferences.length > 0) assertTrue(twoContexts[i].hasDifferences());
216       else assertFalse(twoContexts[i].hasDifferences());
217       assertEquals(expectedTwoDifferences.length, twoContexts[i].countDifferences());
218     }
219
220     boolean shouldBeEqual = new EqualsBuilder().append(expectedOneDifferences, expectedTwoDifferences).isEquals();
221     for (int i = 0; i < oneContexts.length; ++i) {
222       for (int j = 0; j < twoContexts.length; ++j) {
223         if (shouldBeEqual) {
224           assertEquals(oneContexts[i], twoContexts[i]);
225           assertEquals(twoContexts[i], oneContexts[i]);
226         } else {
227           assertFalse(oneContexts[i].equals(twoContexts[i]));
228           assertFalse(twoContexts[i].equals(oneContexts[i]));
229         }
230       }
231     }
232   }
233
234   public void testEquals() throws Exception JavaDoc {
235     DifferenceContext initial = DifferenceContext.createInitial();
236     DifferenceContext otherInitial = DifferenceContext.createInitial();
237
238     assertEquals(initial, otherInitial);
239     assertEquals(otherInitial, initial);
240
241     assertEquals(initial.sub("a"), otherInitial.sub("a"));
242     assertEquals(initial.sub("a").sub("b"), otherInitial.sub("a").sub("b"));
243
244     assertEquals(initial, initial);
245     assertFalse(initial.equals(initial.sub("a")));
246     assertFalse(initial.sub("a").equals(initial));
247
248     assertEquals(initial.sub("a"), initial.sub("a"));
249     assertFalse(initial.sub("a").equals(initial.sub("b")));
250     assertFalse(initial.sub("a").equals(initial.sub("a").sub("b")));
251
252     assertEquals(initial.sub("a").sub("b"), initial.sub("a").sub("b"));
253     assertFalse(initial.sub("a").sub("b").equals(initial.sub("a")));
254     assertFalse(initial.sub("a").sub("b").equals(initial.sub("a").sub("c")));
255     assertFalse(initial.sub("a").sub("b").equals(initial.sub("b").sub("a")));
256   }
257
258 }
Popular Tags