KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashCodeBuilder;
7
8 import com.tc.test.TCTestCase;
9 import com.tc.util.EqualityComparator;
10 import com.tc.util.Stringifier;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 /**
20  * Base for all tests that test {@link CollectionComparer}s.
21  */

22 public class CollectionComparerTestBase extends TCTestCase {
23
24   protected static boolean CASE_INSENSITIVE = false;
25
26   protected static class MyObj {
27     public final String JavaDoc value;
28
29     public MyObj(String JavaDoc value) {
30       this.value = value;
31     }
32
33     public String JavaDoc toString() {
34       return "X" + this.value + "Y";
35     }
36
37     public int hashCode() {
38       return new HashCodeBuilder().append(this.value).toHashCode();
39     }
40   }
41
42   private static class MyStringifier implements Stringifier {
43     private static String JavaDoc PREFIX = "A";
44     private static String JavaDoc SUFFIX = "B";
45
46     public String JavaDoc toString(Object JavaDoc o) {
47       if (o == null) return "__NULL__";
48       else return PREFIX + o.toString() + SUFFIX;
49     }
50   }
51
52   private static class MyComparator implements EqualityComparator {
53     public boolean isEquals(Object JavaDoc one, Object JavaDoc two) {
54       if (!(one instanceof MyObj)) return false;
55       if (!(two instanceof MyObj)) return false;
56
57       MyObj myOne = (MyObj) one;
58       MyObj myTwo = (MyObj) two;
59
60       if ((one == null) != (two == null)) return false;
61       if (one == null) return true;
62
63       if (CASE_INSENSITIVE) return myOne.value.equalsIgnoreCase(myTwo.value);
64       else return myOne.value.equals(myTwo.value);
65     }
66   }
67
68   protected static final CollectionMismatch[] NO_MISMATCHES = new CollectionMismatch[0];
69
70   protected Stringifier describer;
71   protected EqualityComparator equalityComparator;
72   protected CollectionComparer comparer;
73
74   public void setUp() throws Exception JavaDoc {
75     this.describer = new MyStringifier();
76     this.equalityComparator = new MyComparator();
77   }
78
79   public void testEmpty() throws Exception JavaDoc {
80     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[0], new Object JavaDoc[0]));
81     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[0], iterator(new Object JavaDoc[0])));
82     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[0], list(new Object JavaDoc[0])));
83     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[0], set(new Object JavaDoc[0])));
84     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(iterator(new Object JavaDoc[0]), new Object JavaDoc[0]));
85     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(list(new Object JavaDoc[0]), new Object JavaDoc[0]));
86     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(set(new Object JavaDoc[0]), new Object JavaDoc[0]));
87   }
88
89   public void testSingleEquals() throws Exception JavaDoc {
90     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[] { new MyObj("foo") },
91                                                                new Object JavaDoc[] { new MyObj("foo") }));
92   }
93
94   public void testDifferentCollectionTypes() throws Exception JavaDoc {
95     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[] { new MyObj("foo") },
96                                                                new Object JavaDoc[] { new MyObj("foo") }));
97     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[] { new MyObj("foo") },
98                                                                iterator(new Object JavaDoc[] { new MyObj("foo") })));
99     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[] { new MyObj("foo") },
100                                                                list(new Object JavaDoc[] { new MyObj("foo") })));
101     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object JavaDoc[] { new MyObj("foo") },
102                                                                set(new Object JavaDoc[] { new MyObj("foo") })));
103     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(iterator(new Object JavaDoc[] { new MyObj("foo") }),
104                                                                new Object JavaDoc[] { new MyObj("foo") }));
105     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(list(new Object JavaDoc[] { new MyObj("foo") }),
106                                                                new Object JavaDoc[] { new MyObj("foo") }));
107     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(set(new Object JavaDoc[] { new MyObj("foo") }),
108                                                                new Object JavaDoc[] { new MyObj("foo") }));
109   }
110
111   public void testSingleDoesNotEqualNothing() throws Exception JavaDoc {
112     MyObj missingObj = new MyObj("foo");
113
114     checkMismatches(
115                     new CollectionMismatch[] { new MissingObjectCollectionMismatch(missingObj, true, 0, this.describer) },
116                     this.comparer.getMismatches(new Object JavaDoc[] { missingObj }, new Object JavaDoc[0]));
117   }
118
119   public void testNothingDoesNotEqualSingle() throws Exception JavaDoc {
120     MyObj missingObj = new MyObj("foo");
121
122     checkMismatches(
123                     new CollectionMismatch[] { new MissingObjectCollectionMismatch(missingObj, false, 0, this.describer) },
124                     this.comparer.getMismatches(new Object JavaDoc[0], new Object JavaDoc[] { missingObj }));
125   }
126
127   public void testSameCollections() throws Exception JavaDoc {
128     Object JavaDoc[] collectionOne = new Object JavaDoc[] { new MyObj("a"), new MyObj("q"), new MyObj("c"), new MyObj("BBBBBBB") };
129     Object JavaDoc[] collectionTwo = new Object JavaDoc[] { new MyObj("a"), new MyObj("q"), new MyObj("c"), new MyObj("BBBBBBB") };
130
131     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(collectionOne, collectionTwo));
132     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(collectionTwo, collectionOne));
133
134     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(iterator(collectionOne), collectionTwo));
135     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(iterator(collectionTwo), collectionOne));
136     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(collectionOne, iterator(collectionTwo)));
137     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(collectionTwo, iterator(collectionOne)));
138
139     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(list(collectionOne), collectionTwo));
140     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(list(collectionTwo), collectionOne));
141     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(collectionOne, list(collectionTwo)));
142     checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(collectionTwo, list(collectionOne)));
143   }
144
145   public void testUsesStringifier() throws Exception JavaDoc {
146     String JavaDoc oldPrefix = MyStringifier.PREFIX;
147     String JavaDoc oldSuffix = MyStringifier.SUFFIX;
148
149     try {
150       MyStringifier.PREFIX = "FOO";
151       MyStringifier.SUFFIX = "BAR";
152
153       CollectionMismatch[] actual = this.comparer.getMismatches(new Object JavaDoc[] { new MyObj("P") },
154                                                                 new Object JavaDoc[] { new MyObj("Q") });
155
156       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
157       for (int i = 0; i < actual.length; ++i) {
158         buf.append(actual[i].toString());
159       }
160
161       String JavaDoc theString = buf.toString();
162
163       assertTrue(theString.indexOf("FOOXPYBAR") >= 0);
164       assertTrue(theString.indexOf("FOOXQYBAR") >= 0);
165     } finally {
166       MyStringifier.PREFIX = oldPrefix;
167       MyStringifier.SUFFIX = oldSuffix;
168     }
169   }
170
171   public void testExtraElements() throws Exception JavaDoc {
172     MyObj extra = new MyObj("qz");
173
174     checkMismatches(new CollectionMismatch[] { new MissingObjectCollectionMismatch(extra, true, 1, this.describer) },
175                     this.comparer
176                         .getMismatches(new Object JavaDoc[] { new MyObj("a"), extra }, new Object JavaDoc[] { new MyObj("a") }));
177
178     checkMismatches(new CollectionMismatch[] { new MissingObjectCollectionMismatch(extra, false, 1, this.describer) },
179                     this.comparer
180                         .getMismatches(new Object JavaDoc[] { new MyObj("a") }, new Object JavaDoc[] { new MyObj("a"), extra }));
181   }
182
183   public void testChecksArguments() throws Exception JavaDoc {
184     try {
185       this.comparer.getMismatches(new Object JavaDoc[0], null);
186       fail("Didn't get NPE on no second collection");
187     } catch (NullPointerException JavaDoc npe) {
188       // ok
189
}
190
191     try {
192       this.comparer.getMismatches(null, new Object JavaDoc[0]);
193       fail("Didn't get NPE on no first collection");
194     } catch (NullPointerException JavaDoc npe) {
195       // ok
196
}
197
198     try {
199       this.comparer.getMismatches("foo", new Object JavaDoc[0]);
200       fail("Didn't get IAE on bogus first argument");
201     } catch (IllegalArgumentException JavaDoc iae) {
202       // ok
203
}
204
205     try {
206       this.comparer.getMismatches(new Object JavaDoc[0], "foo");
207       fail("Didn't get IAE on bogus second argument");
208     } catch (IllegalArgumentException JavaDoc iae) {
209       // ok
210
}
211   }
212
213   private Iterator JavaDoc iterator(Object JavaDoc[] data) {
214     return Arrays.asList(data).iterator();
215   }
216
217   private List JavaDoc list(Object JavaDoc[] data) {
218     ArrayList JavaDoc out = new ArrayList JavaDoc();
219     out.addAll(Arrays.asList(data));
220     return out;
221   }
222
223   private Set JavaDoc set(Object JavaDoc[] data) {
224     HashSet JavaDoc out = new HashSet JavaDoc();
225     out.addAll(Arrays.asList(data));
226     return out;
227   }
228
229   // NOTE 2004-12-27 andrew -- This compares in an ordered fashion. This could cause problems if the implementation is
230
// changed to return CollectionMismatches in a different order; if so, we'll need to enhance this method to not care
231
// about order. However, we should NOT use a CollectionComparer to do that, for the obvious reasons -- you don't want
232
// to use the code you're testing as part of the test's infrastructure itself.
233
protected final void checkMismatches(CollectionMismatch[] expected, CollectionMismatch[] actual) {
234     StringBuffer JavaDoc out = new StringBuffer JavaDoc();
235
236     for (int i = 0; i < expected.length; ++i) {
237       if (i >= actual.length) {
238         out.append("Missing an expected mismatch at index " + i + ": " + expected[i] + "\n");
239       } else if (!expected[i].equals(actual[i])) {
240         out.append("Wrong mismatch at index " + i + ": expected " + expected[i] + ", but got " + actual[i] + "\n");
241       }
242     }
243
244     if (actual.length > expected.length) {
245       for (int i = expected.length; i < actual.length; ++i) {
246         out.append("Got an unexpected mismatch at index " + i + ": " + actual[i]);
247       }
248     }
249
250     if (out.toString().length() > 0) {
251       fail(out.toString());
252     }
253   }
254
255 }
Popular Tags