KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > collections > OrderedCollectionComparerTest


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.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 /**
13  * Unit test for {@link OrderedCollectionComparer}.
14  */

15 public class OrderedCollectionComparerTest extends CollectionComparerTestBase {
16
17   public void setUp() throws Exception JavaDoc {
18     super.setUp();
19     this.comparer = new OrderedCollectionComparer(this.equalityComparator, this.describer);
20   }
21
22   public void testChecksOrder() throws Exception JavaDoc {
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 JavaDoc[] { one, two, new MyObj("c") }, new Object JavaDoc[] { two, one, new MyObj("c") }));
30   }
31
32   public void testUsesEqualityComparator() throws Exception JavaDoc {
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 JavaDoc[] { uppercase }, new Object JavaDoc[] { lowercase }));
40
41     CASE_INSENSITIVE = true;
42
43     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[] { uppercase }, new Object JavaDoc[] { 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 JavaDoc[] { uppercase }, new Object JavaDoc[] { lowercase }));
49   }
50
51   private static class Diff implements Differenceable {
52     private final Object JavaDoc a;
53     private final Object JavaDoc b;
54     private final Object JavaDoc c;
55
56     public Diff(Object JavaDoc a, Object JavaDoc b, Object JavaDoc c) {
57       this.a = a;
58       this.b = b;
59       this.c = c;
60     }
61
62     public boolean equals(Object JavaDoc 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 JavaDoc that) {
72       new DifferenceBuilder(context).reflectionDifference(this, that);
73     }
74
75     public String JavaDoc toString() {
76       return "<Diff>";
77     }
78   }
79   
80   public void testDifferenceable() throws Exception JavaDoc {
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 JavaDoc[] { a }, new Object JavaDoc[] { 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 JavaDoc {
93     Object JavaDoc oneObj = new MyObj("foo");
94     Object JavaDoc twoObj = "foo";
95
96     checkMismatches(new CollectionMismatch[] { new UnequalObjectCollectionMismatch(oneObj, twoObj, true, 0, 0,
97                                                                                    this.describer) }, this.comparer
98         .getMismatches(new Object JavaDoc[] { oneObj }, new Object JavaDoc[] { twoObj }));
99
100     checkMismatches(new CollectionMismatch[] { new UnequalObjectCollectionMismatch(twoObj, oneObj, true, 0, 0,
101                                                                                    this.describer) }, this.comparer
102         .getMismatches(new Object JavaDoc[] { twoObj }, new Object JavaDoc[] { oneObj }));
103   }
104
105   public void testMultipleProblems() throws Exception JavaDoc {
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 JavaDoc[] one = new Object JavaDoc[] { firstZero, bothOne, firstTwo, bothThree };
115     Object JavaDoc[] two = new Object JavaDoc[] { 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