001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util.diff;
005:
006: import org.apache.commons.lang.builder.EqualsBuilder;
007:
008: import com.tc.test.TCTestCase;
009: import com.tc.util.StandardStringifier;
010: import com.tc.util.Stringifier;
011: import com.tc.util.TCAssertionError;
012: import com.tc.util.ToStringStringifier;
013:
014: /**
015: * Unit test for {@link DifferenceContext}.
016: */
017: public class DifferenceContextTest extends TCTestCase {
018:
019: public void testInitial() throws Exception {
020: assertEquals("", DifferenceContext.createInitial().toString());
021: assertNotNull(DifferenceContext.createInitial().collection());
022: assertFalse(DifferenceContext.createInitial().collection() == DifferenceContext
023: .createInitial().collection());
024:
025: try {
026: DifferenceContext.createInitial(null);
027: fail("Didn't get NPE on no stringifier");
028: } catch (NullPointerException npe) {
029: // ok
030: }
031: }
032:
033: public void testStringifier() throws Exception {
034: assertSame(StandardStringifier.INSTANCE, DifferenceContext
035: .createInitial().stringifier());
036: assertSame(ToStringStringifier.INSTANCE, DifferenceContext
037: .createInitial(ToStringStringifier.INSTANCE)
038: .stringifier());
039:
040: Stringifier s = new Stringifier() {
041: public String toString(Object o) {
042: return "X" + o + "Y";
043: }
044: };
045:
046: assertEquals("XaY", DifferenceContext.createInitial(s)
047: .describe("a"));
048: assertEquals("XbY", DifferenceContext.createInitial(s).sub("Q")
049: .describe("b"));
050: }
051:
052: public void testSubAndToString() throws Exception {
053: DifferenceContext initial = DifferenceContext.createInitial();
054:
055: DifferenceContext a = initial.sub("a");
056: assertNotSame(a, initial);
057: assertEquals("/a", a.toString());
058: assertEquals("", initial.toString());
059: assertSame(initial.collection(), a.collection());
060:
061: DifferenceContext b = a.sub("b");
062: assertNotSame(a, b);
063: assertEquals("/a/b", b.toString());
064: assertEquals("/a", a.toString());
065: assertEquals("", initial.toString());
066: assertSame(initial.collection(), b.collection());
067:
068: DifferenceContext c = b.sub("c");
069: assertNotSame(c, b);
070: assertNotSame(c, a);
071: assertEquals("/a/b/c", c.toString());
072: assertEquals("/a/b", b.toString());
073: assertEquals("/a", a.toString());
074: assertEquals("", initial.toString());
075: assertSame(initial.collection(), c.collection());
076:
077: DifferenceContext d = a.sub("d");
078: assertNotSame(d, c);
079: assertNotSame(d, b);
080: assertNotSame(d, a);
081: assertEquals("/a/d", d.toString());
082: assertEquals("/a/b/c", c.toString());
083: assertEquals("/a/b", b.toString());
084: assertEquals("/a", a.toString());
085: assertEquals("", initial.toString());
086: assertSame(initial.collection(), d.collection());
087: }
088:
089: public void testDifferences() throws Exception {
090: DifferenceContext initial = DifferenceContext.createInitial();
091: DifferenceContext otherInitial = DifferenceContext
092: .createInitial();
093:
094: assertNotSame(initial, otherInitial);
095:
096: assertEquals(initial, otherInitial);
097: assertFalse(initial.hasDifferences());
098: assertEqualsOrdered(new Object[0], initial.getDifferences());
099:
100: MockDifference initialDifference = new MockDifference(initial,
101: "foo", "bar");
102: initial.addDifference(initialDifference);
103: assertFalse(initial.equals(otherInitial));
104: assertTrue(initial.hasDifferences());
105: assertFalse(otherInitial.hasDifferences());
106: assertEqualsOrdered(new Object[] { initialDifference }, initial
107: .getDifferences());
108: assertEqualsOrdered(new Object[0], otherInitial
109: .getDifferences());
110:
111: MockDifference otherInitialDifference = new MockDifference(
112: otherInitial, "foo", "bar");
113: otherInitial.addDifference(otherInitialDifference);
114: assertEquals(initial, otherInitial);
115: assertTrue(initial.hasDifferences());
116: assertTrue(otherInitial.hasDifferences());
117: assertEqualsOrdered(new Object[] { initialDifference }, initial
118: .getDifferences());
119: assertEqualsOrdered(new Object[] { otherInitialDifference },
120: otherInitial.getDifferences());
121:
122: initial
123: .addDifference(new MockDifference(initial, "foo", "baz"));
124: assertFalse(initial.equals(otherInitial));
125: assertFalse(otherInitial.equals(initial));
126:
127: otherInitial.addDifference(new MockDifference(otherInitial,
128: "foo", "quux"));
129: assertFalse(initial.equals(otherInitial));
130: assertFalse(otherInitial.equals(initial));
131:
132: initial = DifferenceContext.createInitial();
133: otherInitial = DifferenceContext.createInitial();
134:
135: assertEquals(initial, otherInitial);
136: assertNotSame(initial, otherInitial);
137:
138: DifferenceContext initA = initial.sub("a");
139: DifferenceContext otherInitA = otherInitial.sub("a");
140: DifferenceContext initB = initA.sub("b");
141: DifferenceContext otherInitB = otherInitA.sub("b");
142: DifferenceContext initC = initial.sub("c");
143: DifferenceContext otherInitC = otherInitial.sub("c");
144:
145: assertEquals(initial, otherInitial);
146: assertEquals(initA, otherInitA);
147: assertEquals(initB, otherInitB);
148: assertEquals(initC, otherInitC);
149:
150: MockDifference diffInit = new MockDifference(initial, "1", "2");
151: MockDifference diffOtherInit = new MockDifference(otherInitial,
152: "1", "2");
153: MockDifference diffInitA = new MockDifference(initA, "3", "4");
154: MockDifference diffOtherInitA = new MockDifference(otherInitA,
155: "3", "4");
156: MockDifference diffInitB = new MockDifference(initB, "5", "6");
157: MockDifference diffOtherInitB = new MockDifference(otherInitB,
158: "5", "6");
159: MockDifference diffInitC = new MockDifference(initC, "7", "8");
160: MockDifference diffOtherInitC = new MockDifference(otherInitC,
161: "7", "8");
162:
163: DifferenceContext[] initialContexts = new DifferenceContext[] {
164: initial, initA, initB, initC };
165: DifferenceContext[] otherInitialContexts = new DifferenceContext[] {
166: otherInitial, otherInitA, otherInitB, otherInitC };
167:
168: initial.addDifference(diffInit);
169: checkDifferences(initialContexts, new Object[] { diffInit },
170: otherInitialContexts, new Object[0]);
171: otherInitial.addDifference(diffOtherInit);
172: checkDifferences(initialContexts, new Object[] { diffInit },
173: otherInitialContexts, new Object[] { diffOtherInit });
174:
175: initA.addDifference(diffInitA);
176: checkDifferences(initialContexts, new Object[] { diffInit,
177: diffInitA }, otherInitialContexts,
178: new Object[] { diffOtherInit });
179: otherInitA.addDifference(diffOtherInitA);
180: checkDifferences(initialContexts, new Object[] { diffInit,
181: diffInitA }, otherInitialContexts, new Object[] {
182: diffOtherInit, diffOtherInitA });
183:
184: initB.addDifference(diffInitB);
185: checkDifferences(initialContexts, new Object[] { diffInit,
186: diffInitA, diffInitB }, otherInitialContexts,
187: new Object[] { diffOtherInit, diffOtherInitA });
188: otherInitB.addDifference(diffOtherInitB);
189: checkDifferences(initialContexts, new Object[] { diffInit,
190: diffInitA, diffInitB }, otherInitialContexts,
191: new Object[] { diffOtherInit, diffOtherInitA,
192: diffOtherInitB });
193:
194: initC.addDifference(diffInitC);
195: checkDifferences(initialContexts, new Object[] { diffInit,
196: diffInitA, diffInitB, diffInitC },
197: otherInitialContexts, new Object[] { diffOtherInit,
198: diffOtherInitA, diffOtherInitB });
199: otherInitC.addDifference(diffOtherInitC);
200: checkDifferences(initialContexts, new Object[] { diffInit,
201: diffInitA, diffInitB, diffInitC },
202: otherInitialContexts,
203: new Object[] { diffOtherInit, diffOtherInitA,
204: diffOtherInitB, diffOtherInitC });
205:
206: try {
207: initial.addDifference(null);
208: fail("Didn't get NPE on adding null difference");
209: } catch (NullPointerException npe) {
210: // ok
211: }
212:
213: try {
214: initial.addDifference(otherInitialDifference);
215: fail("Didn't get TCAE on add of difference with different context");
216: } catch (TCAssertionError tcae) {
217: // ok
218: }
219:
220: try {
221: initial.addDifference(new MockDifference(initial.sub("a")));
222: fail("Didn't get TCAE on add of difference with different context");
223: } catch (TCAssertionError tcae) {
224: // ok
225: }
226:
227: try {
228: initial.sub("a").addDifference(otherInitialDifference);
229: fail("Didn't get TCAE on add of difference with different context");
230: } catch (TCAssertionError tcae) {
231: // ok
232: }
233: }
234:
235: private void checkDifferences(DifferenceContext[] oneContexts,
236: Object[] expectedOneDifferences,
237: DifferenceContext[] twoContexts,
238: Object[] expectedTwoDifferences) {
239: for (int i = 0; i < oneContexts.length; ++i) {
240: assertEqualsOrdered(expectedOneDifferences, oneContexts[i]
241: .getDifferences());
242: if (expectedOneDifferences.length > 0)
243: assertTrue(oneContexts[i].hasDifferences());
244: else
245: assertFalse(oneContexts[i].hasDifferences());
246: assertEquals(expectedOneDifferences.length, oneContexts[i]
247: .countDifferences());
248: }
249:
250: for (int i = 0; i < twoContexts.length; ++i) {
251: assertEqualsOrdered(expectedTwoDifferences, twoContexts[i]
252: .getDifferences());
253: if (expectedTwoDifferences.length > 0)
254: assertTrue(twoContexts[i].hasDifferences());
255: else
256: assertFalse(twoContexts[i].hasDifferences());
257: assertEquals(expectedTwoDifferences.length, twoContexts[i]
258: .countDifferences());
259: }
260:
261: boolean shouldBeEqual = new EqualsBuilder().append(
262: expectedOneDifferences, expectedTwoDifferences)
263: .isEquals();
264: for (int i = 0; i < oneContexts.length; ++i) {
265: for (int j = 0; j < twoContexts.length; ++j) {
266: if (shouldBeEqual) {
267: assertEquals(oneContexts[i], twoContexts[i]);
268: assertEquals(twoContexts[i], oneContexts[i]);
269: } else {
270: assertFalse(oneContexts[i].equals(twoContexts[i]));
271: assertFalse(twoContexts[i].equals(oneContexts[i]));
272: }
273: }
274: }
275: }
276:
277: public void testEquals() throws Exception {
278: DifferenceContext initial = DifferenceContext.createInitial();
279: DifferenceContext otherInitial = DifferenceContext
280: .createInitial();
281:
282: assertEquals(initial, otherInitial);
283: assertEquals(otherInitial, initial);
284:
285: assertEquals(initial.sub("a"), otherInitial.sub("a"));
286: assertEquals(initial.sub("a").sub("b"), otherInitial.sub("a")
287: .sub("b"));
288:
289: assertEquals(initial, initial);
290: assertFalse(initial.equals(initial.sub("a")));
291: assertFalse(initial.sub("a").equals(initial));
292:
293: assertEquals(initial.sub("a"), initial.sub("a"));
294: assertFalse(initial.sub("a").equals(initial.sub("b")));
295: assertFalse(initial.sub("a").equals(initial.sub("a").sub("b")));
296:
297: assertEquals(initial.sub("a").sub("b"), initial.sub("a").sub(
298: "b"));
299: assertFalse(initial.sub("a").sub("b").equals(initial.sub("a")));
300: assertFalse(initial.sub("a").sub("b").equals(
301: initial.sub("a").sub("c")));
302: assertFalse(initial.sub("a").sub("b").equals(
303: initial.sub("b").sub("a")));
304: }
305:
306: }
|