1 4 package com.tc.test.collections; 5 6 import org.apache.commons.lang.builder.EqualsBuilder; 7 8 import com.tc.util.diff.DifferenceBuilder; 9 import com.tc.util.diff.DifferenceContext; 10 import com.tc.util.diff.Differenceable; 11 12 15 public class OrderedCollectionComparerTest extends CollectionComparerTestBase { 16 17 public void setUp() throws Exception { 18 super.setUp(); 19 this.comparer = new OrderedCollectionComparer(this.equalityComparator, this.describer); 20 } 21 22 public void testChecksOrder() throws Exception { 23 MyObj one = new MyObj("a"); 24 MyObj two = new MyObj("b"); 25 26 checkMismatches(new CollectionMismatch[] { 27 new UnequalObjectCollectionMismatch(one, two, true, 0, 0, this.describer), 28 new UnequalObjectCollectionMismatch(two, one, true, 1, 1, this.describer) }, this.comparer 29 .getMismatches(new Object [] { one, two, new MyObj("c") }, new Object [] { two, one, new MyObj("c") })); 30 } 31 32 public void testUsesEqualityComparator() throws Exception { 33 MyObj uppercase = new MyObj("FOO"); 34 MyObj lowercase = new MyObj("foo"); 35 36 CASE_INSENSITIVE = false; 37 checkMismatches(new CollectionMismatch[] { new UnequalObjectCollectionMismatch(uppercase, lowercase, true, 0, 0, 38 this.describer) }, this.comparer 39 .getMismatches(new Object [] { uppercase }, new Object [] { lowercase })); 40 41 CASE_INSENSITIVE = true; 42 43 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [] { uppercase }, new Object [] { lowercase })); 44 45 CASE_INSENSITIVE = false; 46 checkMismatches(new CollectionMismatch[] { new UnequalObjectCollectionMismatch(uppercase, lowercase, true, 0, 0, 47 this.describer) }, this.comparer 48 .getMismatches(new Object [] { uppercase }, new Object [] { lowercase })); 49 } 50 51 private static class Diff implements Differenceable { 52 private final Object a; 53 private final Object b; 54 private final Object c; 55 56 public Diff(Object a, Object b, Object c) { 57 this.a = a; 58 this.b = b; 59 this.c = c; 60 } 61 62 public boolean equals(Object that) { 63 if (!(that instanceof Diff)) return false; 64 65 Diff diffThat = (Diff) that; 66 67 return new EqualsBuilder().append(this.a, diffThat.a).append(this.b, diffThat.b).append(this.c, diffThat.c) 68 .isEquals(); 69 } 70 71 public void addDifferences(DifferenceContext context, Object that) { 72 new DifferenceBuilder(context).reflectionDifference(this, that); 73 } 74 75 public String toString() { 76 return "<Diff>"; 77 } 78 } 79 80 public void testDifferenceable() throws Exception { 81 Diff a = new Diff("foo", "bar", "baz"); 82 Diff b = new Diff("foo", "bar", "quux"); 83 84 CollectionMismatch[] mismatches = this.comparer.getMismatches(new Object [] { a }, new Object [] { b }); 85 assertEquals(1, mismatches.length); 86 assertTrue(mismatches[0].toString().indexOf("baz") >= 0); 87 assertTrue(mismatches[0].toString().indexOf("quux") >= 0); 88 assertTrue(mismatches[0].toString().indexOf("foo") < 0); 89 assertTrue(mismatches[0].toString().indexOf("bar") < 0); 90 } 91 92 public void testDifferentObjectTypes() throws Exception { 93 Object oneObj = new MyObj("foo"); 94 Object twoObj = "foo"; 95 96 checkMismatches(new CollectionMismatch[] { new UnequalObjectCollectionMismatch(oneObj, twoObj, true, 0, 0, 97 this.describer) }, this.comparer 98 .getMismatches(new Object [] { oneObj }, new Object [] { twoObj })); 99 100 checkMismatches(new CollectionMismatch[] { new UnequalObjectCollectionMismatch(twoObj, oneObj, true, 0, 0, 101 this.describer) }, this.comparer 102 .getMismatches(new Object [] { twoObj }, new Object [] { oneObj })); 103 } 104 105 public void testMultipleProblems() throws Exception { 106 MyObj firstZero = new MyObj("a"); 107 MyObj secondZero = new MyObj("x"); 108 MyObj bothOne = new MyObj("b"); 109 MyObj firstTwo = new MyObj("c"); 110 MyObj secondTwo = new MyObj("y"); 111 MyObj bothThree = new MyObj("d"); 112 MyObj secondFour = new MyObj("q"); 113 114 Object [] one = new Object [] { firstZero, bothOne, firstTwo, bothThree }; 115 Object [] two = new Object [] { secondZero, bothOne, secondTwo, bothThree, secondFour }; 116 117 CollectionMismatch[] expectedMismatches = new CollectionMismatch[] { 118 new UnequalObjectCollectionMismatch(firstZero, secondZero, true, 0, 0, this.describer), 119 new UnequalObjectCollectionMismatch(firstTwo, secondTwo, true, 2, 2, this.describer), 120 new MissingObjectCollectionMismatch(secondFour, false, 4, this.describer) }; 121 122 checkMismatches(expectedMismatches, this.comparer.getMismatches(one, two)); 123 } 124 125 } | Popular Tags |