1 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 ; 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 22 public class CollectionComparerTestBase extends TCTestCase { 23 24 protected static boolean CASE_INSENSITIVE = false; 25 26 protected static class MyObj { 27 public final String value; 28 29 public MyObj(String value) { 30 this.value = value; 31 } 32 33 public String 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 PREFIX = "A"; 44 private static String SUFFIX = "B"; 45 46 public String toString(Object 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 one, Object 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 { 75 this.describer = new MyStringifier(); 76 this.equalityComparator = new MyComparator(); 77 } 78 79 public void testEmpty() throws Exception { 80 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [0], new Object [0])); 81 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [0], iterator(new Object [0]))); 82 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [0], list(new Object [0]))); 83 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [0], set(new Object [0]))); 84 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(iterator(new Object [0]), new Object [0])); 85 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(list(new Object [0]), new Object [0])); 86 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(set(new Object [0]), new Object [0])); 87 } 88 89 public void testSingleEquals() throws Exception { 90 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [] { new MyObj("foo") }, 91 new Object [] { new MyObj("foo") })); 92 } 93 94 public void testDifferentCollectionTypes() throws Exception { 95 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [] { new MyObj("foo") }, 96 new Object [] { new MyObj("foo") })); 97 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [] { new MyObj("foo") }, 98 iterator(new Object [] { new MyObj("foo") }))); 99 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [] { new MyObj("foo") }, 100 list(new Object [] { new MyObj("foo") }))); 101 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(new Object [] { new MyObj("foo") }, 102 set(new Object [] { new MyObj("foo") }))); 103 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(iterator(new Object [] { new MyObj("foo") }), 104 new Object [] { new MyObj("foo") })); 105 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(list(new Object [] { new MyObj("foo") }), 106 new Object [] { new MyObj("foo") })); 107 checkMismatches(NO_MISMATCHES, this.comparer.getMismatches(set(new Object [] { new MyObj("foo") }), 108 new Object [] { new MyObj("foo") })); 109 } 110 111 public void testSingleDoesNotEqualNothing() throws Exception { 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 [] { missingObj }, new Object [0])); 117 } 118 119 public void testNothingDoesNotEqualSingle() throws Exception { 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 [0], new Object [] { missingObj })); 125 } 126 127 public void testSameCollections() throws Exception { 128 Object [] collectionOne = new Object [] { new MyObj("a"), new MyObj("q"), new MyObj("c"), new MyObj("BBBBBBB") }; 129 Object [] collectionTwo = new Object [] { 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 { 146 String oldPrefix = MyStringifier.PREFIX; 147 String oldSuffix = MyStringifier.SUFFIX; 148 149 try { 150 MyStringifier.PREFIX = "FOO"; 151 MyStringifier.SUFFIX = "BAR"; 152 153 CollectionMismatch[] actual = this.comparer.getMismatches(new Object [] { new MyObj("P") }, 154 new Object [] { new MyObj("Q") }); 155 156 StringBuffer buf = new StringBuffer (); 157 for (int i = 0; i < actual.length; ++i) { 158 buf.append(actual[i].toString()); 159 } 160 161 String 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 { 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 [] { new MyObj("a"), extra }, new Object [] { new MyObj("a") })); 177 178 checkMismatches(new CollectionMismatch[] { new MissingObjectCollectionMismatch(extra, false, 1, this.describer) }, 179 this.comparer 180 .getMismatches(new Object [] { new MyObj("a") }, new Object [] { new MyObj("a"), extra })); 181 } 182 183 public void testChecksArguments() throws Exception { 184 try { 185 this.comparer.getMismatches(new Object [0], null); 186 fail("Didn't get NPE on no second collection"); 187 } catch (NullPointerException npe) { 188 } 190 191 try { 192 this.comparer.getMismatches(null, new Object [0]); 193 fail("Didn't get NPE on no first collection"); 194 } catch (NullPointerException npe) { 195 } 197 198 try { 199 this.comparer.getMismatches("foo", new Object [0]); 200 fail("Didn't get IAE on bogus first argument"); 201 } catch (IllegalArgumentException iae) { 202 } 204 205 try { 206 this.comparer.getMismatches(new Object [0], "foo"); 207 fail("Didn't get IAE on bogus second argument"); 208 } catch (IllegalArgumentException iae) { 209 } 211 } 212 213 private Iterator iterator(Object [] data) { 214 return Arrays.asList(data).iterator(); 215 } 216 217 private List list(Object [] data) { 218 ArrayList out = new ArrayList (); 219 out.addAll(Arrays.asList(data)); 220 return out; 221 } 222 223 private Set set(Object [] data) { 224 HashSet out = new HashSet (); 225 out.addAll(Arrays.asList(data)); 226 return out; 227 } 228 229 protected final void checkMismatches(CollectionMismatch[] expected, CollectionMismatch[] actual) { 234 StringBuffer out = new StringBuffer (); 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 |