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 com.tc.exception.ImplementMe;
007: import com.tc.test.EqualityChecker;
008: import com.tc.test.TCTestCase;
009: import com.tc.util.Stringifier;
010: import com.tc.util.TCAssertionError;
011:
012: /**
013: * Unit test for {@link BasicObjectDifference}.
014: */
015: public class BasicObjectDifferenceTest extends TCTestCase {
016:
017: private DifferenceContext context;
018:
019: public void setUp() throws Exception {
020: this .context = DifferenceContext.createInitial().sub("a");
021: }
022:
023: private static class OtherDiff implements Differenceable {
024: private final String a;
025:
026: public OtherDiff(String a) {
027: this .a = a;
028: }
029:
030: public boolean equals(Object that) {
031: return (that instanceof OtherDiff)
032: && ((OtherDiff) that).a.equals(this .a);
033: }
034:
035: public void addDifferences(DifferenceContext context,
036: Object that) {
037: throw new ImplementMe();
038: }
039: }
040:
041: private static class SubOtherDiff extends OtherDiff {
042: public SubOtherDiff(String a) {
043: super (a);
044: }
045: }
046:
047: public void testConstruction() throws Exception {
048: try {
049: new BasicObjectDifference(null, "a", "b");
050: fail("Didn't get NPE on no context");
051: } catch (NullPointerException npe) {
052: // ok
053: }
054:
055: try {
056: new BasicObjectDifference(this .context, null, null);
057: fail("Didn't get TCAE on both null");
058: } catch (TCAssertionError tcae) {
059: // ok
060: }
061:
062: try {
063: new BasicObjectDifference(this .context, "a", "a");
064: fail("Didn't get TCAE on both equal");
065: } catch (TCAssertionError tcae) {
066: // ok
067: }
068:
069: try {
070: OtherDiff diff = new OtherDiff("a");
071: OtherDiff diff2 = new OtherDiff("a");
072:
073: new BasicObjectDifference(this .context, diff, diff2);
074: fail("Didn't get TCAE on equal arguments");
075: } catch (TCAssertionError tcae) {
076: // ok
077: }
078:
079: // Both equal, but not same class
080: new BasicObjectDifference(this .context, new OtherDiff("a"),
081: new SubOtherDiff("a"));
082: // Only one is Differenceable
083: new BasicObjectDifference(this .context, new OtherDiff("a"), "a");
084: new BasicObjectDifference(this .context, "a", new OtherDiff("a"));
085: }
086:
087: public void testABAndToString() throws Exception {
088: String one = new String("foobar");
089: String two = new String("bazbar");
090:
091: BasicObjectDifference diff = new BasicObjectDifference(
092: this .context, one, two);
093: assertSame(one, diff.a());
094: assertSame(two, diff.b());
095:
096: diff = new BasicObjectDifference(this .context, null, two);
097: assertSame(null, diff.a());
098: assertSame(two, diff.b());
099:
100: diff = new BasicObjectDifference(this .context, one, null);
101: assertSame(one, diff.a());
102: assertSame(null, diff.b());
103:
104: diff = new BasicObjectDifference(this .context, one, two);
105: assertTrue(diff.toString().indexOf(one) >= 0);
106: assertTrue(diff.toString().indexOf(two) >= 0);
107: }
108:
109: public void testUsesStringifier() throws Exception {
110: Stringifier myStringifier = new Stringifier() {
111: public String toString(Object o) {
112: return "XX" + o + "YY";
113: }
114: };
115:
116: DifferenceContext theContext = DifferenceContext
117: .createInitial(myStringifier);
118: assertSame(myStringifier, theContext.stringifier());
119:
120: BasicObjectDifference diff = new BasicObjectDifference(
121: theContext, "a", "b");
122: assertTrue(diff.toString().indexOf("XXaYY") >= 0);
123: assertTrue(diff.toString().indexOf("XXbYY") >= 0);
124: }
125:
126: public void testEquals() throws Exception {
127: Stringifier myStringifier = new Stringifier() {
128: public String toString(Object o) {
129: return "XX" + o + "YY";
130: }
131: };
132:
133: DifferenceContext theContext = DifferenceContext
134: .createInitial(myStringifier);
135:
136: DifferenceContext secondContext = DifferenceContext
137: .createInitial().sub("b");
138: DifferenceContext thirdContext = DifferenceContext
139: .createInitial().sub("b");
140:
141: BasicObjectDifference[] one = new BasicObjectDifference[] {
142: new BasicObjectDifference(theContext, "a", "b"),
143: new BasicObjectDifference(theContext, "a", null),
144: new BasicObjectDifference(theContext, null, "b"),
145: new BasicObjectDifference(secondContext, "a", "b"),
146: new BasicObjectDifference(theContext, "c", "d"), };
147:
148: BasicObjectDifference[] two = new BasicObjectDifference[] {
149: new BasicObjectDifference(theContext, "a", "b"),
150: new BasicObjectDifference(theContext, "a", null),
151: new BasicObjectDifference(theContext, null, "b"),
152: new BasicObjectDifference(thirdContext, "a", "b"),
153: new BasicObjectDifference(theContext, "c", "d"), };
154:
155: EqualityChecker.checkArraysForEquality(one, two, false);
156: }
157:
158: }
|