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.test.collections;
005:
006: import org.apache.commons.lang.builder.EqualsBuilder;
007:
008: import com.tc.util.diff.DifferenceBuilder;
009: import com.tc.util.diff.DifferenceContext;
010: import com.tc.util.diff.Differenceable;
011:
012: /**
013: * Unit test for {@link OrderedCollectionComparer}.
014: */
015: public class OrderedCollectionComparerTest extends
016: CollectionComparerTestBase {
017:
018: public void setUp() throws Exception {
019: super .setUp();
020: this .comparer = new OrderedCollectionComparer(
021: this .equalityComparator, this .describer);
022: }
023:
024: public void testChecksOrder() throws Exception {
025: MyObj one = new MyObj("a");
026: MyObj two = new MyObj("b");
027:
028: checkMismatches(new CollectionMismatch[] {
029: new UnequalObjectCollectionMismatch(one, two, true, 0,
030: 0, this .describer),
031: new UnequalObjectCollectionMismatch(two, one, true, 1,
032: 1, this .describer) }, this .comparer
033: .getMismatches(
034: new Object[] { one, two, new MyObj("c") },
035: new Object[] { two, one, new MyObj("c") }));
036: }
037:
038: public void testUsesEqualityComparator() throws Exception {
039: MyObj uppercase = new MyObj("FOO");
040: MyObj lowercase = new MyObj("foo");
041:
042: CASE_INSENSITIVE = false;
043: checkMismatches(
044: new CollectionMismatch[] { new UnequalObjectCollectionMismatch(
045: uppercase, lowercase, true, 0, 0,
046: this .describer) }, this .comparer.getMismatches(
047: new Object[] { uppercase },
048: new Object[] { lowercase }));
049:
050: CASE_INSENSITIVE = true;
051:
052: checkMismatches(NO_MISMATCHES, this .comparer.getMismatches(
053: new Object[] { uppercase }, new Object[] { lowercase }));
054:
055: CASE_INSENSITIVE = false;
056: checkMismatches(
057: new CollectionMismatch[] { new UnequalObjectCollectionMismatch(
058: uppercase, lowercase, true, 0, 0,
059: this .describer) }, this .comparer.getMismatches(
060: new Object[] { uppercase },
061: new Object[] { lowercase }));
062: }
063:
064: private static class Diff implements Differenceable {
065: private final Object a;
066: private final Object b;
067: private final Object c;
068:
069: public Diff(Object a, Object b, Object c) {
070: this .a = a;
071: this .b = b;
072: this .c = c;
073: }
074:
075: public boolean equals(Object that) {
076: if (!(that instanceof Diff))
077: return false;
078:
079: Diff diffThat = (Diff) that;
080:
081: return new EqualsBuilder().append(this .a, diffThat.a)
082: .append(this .b, diffThat.b).append(this .c,
083: diffThat.c).isEquals();
084: }
085:
086: public void addDifferences(DifferenceContext context,
087: Object that) {
088: new DifferenceBuilder(context).reflectionDifference(this ,
089: that);
090: }
091:
092: public String toString() {
093: return "<Diff>";
094: }
095: }
096:
097: public void testDifferenceable() throws Exception {
098: Diff a = new Diff("foo", "bar", "baz");
099: Diff b = new Diff("foo", "bar", "quux");
100:
101: CollectionMismatch[] mismatches = this .comparer.getMismatches(
102: new Object[] { a }, new Object[] { b });
103: assertEquals(1, mismatches.length);
104: assertTrue(mismatches[0].toString().indexOf("baz") >= 0);
105: assertTrue(mismatches[0].toString().indexOf("quux") >= 0);
106: assertTrue(mismatches[0].toString().indexOf("foo") < 0);
107: assertTrue(mismatches[0].toString().indexOf("bar") < 0);
108: }
109:
110: public void testDifferentObjectTypes() throws Exception {
111: Object oneObj = new MyObj("foo");
112: Object twoObj = "foo";
113:
114: checkMismatches(
115: new CollectionMismatch[] { new UnequalObjectCollectionMismatch(
116: oneObj, twoObj, true, 0, 0, this .describer) },
117: this .comparer.getMismatches(new Object[] { oneObj },
118: new Object[] { twoObj }));
119:
120: checkMismatches(
121: new CollectionMismatch[] { new UnequalObjectCollectionMismatch(
122: twoObj, oneObj, true, 0, 0, this .describer) },
123: this .comparer.getMismatches(new Object[] { twoObj },
124: new Object[] { oneObj }));
125: }
126:
127: public void testMultipleProblems() throws Exception {
128: MyObj firstZero = new MyObj("a");
129: MyObj secondZero = new MyObj("x");
130: MyObj bothOne = new MyObj("b");
131: MyObj firstTwo = new MyObj("c");
132: MyObj secondTwo = new MyObj("y");
133: MyObj bothThree = new MyObj("d");
134: MyObj secondFour = new MyObj("q");
135:
136: Object[] one = new Object[] { firstZero, bothOne, firstTwo,
137: bothThree };
138: Object[] two = new Object[] { secondZero, bothOne, secondTwo,
139: bothThree, secondFour };
140:
141: CollectionMismatch[] expectedMismatches = new CollectionMismatch[] {
142: new UnequalObjectCollectionMismatch(firstZero,
143: secondZero, true, 0, 0, this .describer),
144: new UnequalObjectCollectionMismatch(firstTwo,
145: secondTwo, true, 2, 2, this .describer),
146: new MissingObjectCollectionMismatch(secondFour, false,
147: 4, this.describer) };
148:
149: checkMismatches(expectedMismatches, this.comparer
150: .getMismatches(one, two));
151: }
152:
153: }
|