1 4 package com.tc.util.diff; 5 6 import org.apache.commons.lang.builder.EqualsBuilder; 7 8 import com.tc.test.TCTestCase; 9 import com.tc.util.Stringifier; 10 11 14 public class DifferenceBuilderTest extends TCTestCase { 15 16 private static boolean USE_REFLECTION = false; 17 18 public static class TestObject implements Differenceable { 19 private final boolean aBoolean; 20 private final char aChar; 21 private final byte aByte; 22 private final short aShort; 23 private final int anInt; 24 private final long aLong; 25 private final float aFloat; 26 private final double aDouble; 27 private final Object anObject; 28 29 private final boolean[] arrBoolean; 30 private final char[] arrChar; 31 private final byte[] arrByte; 32 private final short[] arrShort; 33 private final int[] arrInt; 34 private final long[] arrLong; 35 private final float[] arrFloat; 36 private final double[] arrDouble; 37 private final Object [] arrObject; 38 39 public TestObject(boolean boolean1, char char1, byte byte1, short short1, int anInt, long long1, float float1, 40 double double1, Object anObject, boolean[] arrBoolean, char[] arrChar, byte[] arrByte, 41 short[] arrShort, int[] arrInt, long[] arrLong, float[] arrFloat, double[] arrDouble, 42 Object [] arrObject) { 43 super(); 44 45 this.aBoolean = boolean1; 46 this.aChar = char1; 47 this.aByte = byte1; 48 this.aShort = short1; 49 this.anInt = anInt; 50 this.aLong = long1; 51 this.aFloat = float1; 52 this.aDouble = double1; 53 this.anObject = anObject; 54 this.arrBoolean = arrBoolean; 55 this.arrChar = arrChar; 56 this.arrByte = arrByte; 57 this.arrShort = arrShort; 58 this.arrInt = arrInt; 59 this.arrLong = arrLong; 60 this.arrFloat = arrFloat; 61 this.arrDouble = arrDouble; 62 this.arrObject = arrObject; 63 } 64 65 public TestObject() { 66 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 67 new boolean[] { true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 68 5, 6 }, new int[] { 7, 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, 69 new double[] { 0.4, 0.5, 0.6 }, new Object [] { "foo", "bar", "baz" }); 70 } 71 72 public TestObject(boolean bool) { 73 this(bool, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 74 new boolean[] { true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 75 5, 6 }, new int[] { 7, 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, 76 new double[] { 0.4, 0.5, 0.6 }, new Object [] { "foo", "bar", "baz" }); 77 } 78 79 public TestObject(char ch) { 80 this(true, ch, (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", new boolean[] { 81 true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, 82 new int[] { 7, 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 83 0.6 }, new Object [] { "foo", "bar", "baz" }); 84 } 85 86 public TestObject(byte b) { 87 this(true, 'x', b, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", new boolean[] { true, 88 false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, new int[] { 7, 89 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, 90 new Object [] { "foo", "bar", "baz" }); 91 } 92 93 public TestObject(short s) { 94 this(true, 'x', (byte) 37, s, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", new boolean[] { true, 95 false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, new int[] { 7, 96 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, 97 new Object [] { "foo", "bar", "baz" }); 98 } 99 100 public TestObject(int i) { 101 this(true, 'x', (byte) 37, (short) 137, i, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", new boolean[] { 102 true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, 103 new int[] { 7, 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 104 0.6 }, new Object [] { "foo", "bar", "baz" }); 105 } 106 107 public TestObject(long l) { 108 this(true, 'x', (byte) 37, (short) 137, 1238415, l, 4.34e+27f, 1.32523482759e+109, "foo", new boolean[] { true, 109 false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, new int[] { 7, 110 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, 111 new Object [] { "foo", "bar", "baz" }); 112 } 113 114 public TestObject(float f) { 115 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, f, 1.32523482759e+109, "foo", new boolean[] { true, 116 false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, new int[] { 7, 117 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, 118 new Object [] { "foo", "bar", "baz" }); 119 } 120 121 public TestObject(double d) { 122 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, d, "foo", new boolean[] { true, false, 123 true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, new int[] { 7, 8, 9 }, 124 new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, new Object [] { 125 "foo", "bar", "baz" }); 126 } 127 128 public TestObject(Object o) { 129 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, o, new boolean[] { 130 true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, 131 new int[] { 7, 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 132 0.6 }, new Object [] { "foo", "bar", "baz" }); 133 } 134 135 public TestObject(boolean[] ab) { 136 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", ab, 137 new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, new int[] { 7, 8, 9 }, 138 new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, new Object [] { 139 "foo", "bar", "baz" }); 140 } 141 142 public TestObject(char[] ac) { 143 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 144 new boolean[] { true, false, true }, ac, new byte[] { 3, 4, 5 }, new short[] { 4, 5, 6 }, new int[] { 7, 8, 145 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, 146 new Object [] { "foo", "bar", "baz" }); 147 } 148 149 public TestObject(byte[] ab) { 150 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 151 new boolean[] { true, false, true }, new char[] { 'a', 'b', 'c' }, ab, new short[] { 4, 5, 6 }, new int[] { 152 7, 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, 153 new Object [] { "foo", "bar", "baz" }); 154 } 155 156 public TestObject(short[] as) { 157 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 158 new boolean[] { true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, as, new int[] { 159 7, 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, 160 new Object [] { "foo", "bar", "baz" }); 161 } 162 163 public TestObject(int[] ai) { 164 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 165 new boolean[] { true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 166 5, 6 }, ai, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, 167 new Object [] { "foo", "bar", "baz" }); 168 } 169 170 public TestObject(long[] al) { 171 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 172 new boolean[] { true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 173 5, 6 }, new int[] { 7, 8, 9 }, al, new float[] { 0.1f, 0.2f, 0.3f }, new double[] { 0.4, 0.5, 0.6 }, 174 new Object [] { "foo", "bar", "baz" }); 175 } 176 177 public TestObject(float[] af) { 178 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 179 new boolean[] { true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 180 5, 6 }, new int[] { 7, 8, 9 }, new long[] { 10, 11, 12 }, af, new double[] { 0.4, 0.5, 0.6 }, 181 new Object [] { "foo", "bar", "baz" }); 182 } 183 184 public TestObject(double[] ad) { 185 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 186 new boolean[] { true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 187 5, 6 }, new int[] { 7, 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, ad, 188 new Object [] { "foo", "bar", "baz" }); 189 } 190 191 public TestObject(Object [] ao) { 192 this(true, 'x', (byte) 37, (short) 137, 1238415, 1238947198L, 4.34e+27f, 1.32523482759e+109, "foo", 193 new boolean[] { true, false, true }, new char[] { 'a', 'b', 'c' }, new byte[] { 3, 4, 5 }, new short[] { 4, 194 5, 6 }, new int[] { 7, 8, 9 }, new long[] { 10, 11, 12 }, new float[] { 0.1f, 0.2f, 0.3f }, 195 new double[] { 0.4, 0.5, 0.6 }, ao); 196 } 197 198 public void addDifferences(DifferenceContext context, Object rawThat) { 199 TestObject that = (TestObject) rawThat; 200 201 if (USE_REFLECTION) { 202 new DifferenceBuilder(context).reflectionDifference(this, rawThat); 203 } else { 204 new DifferenceBuilder(context).append("aBoolean", this.aBoolean, that.aBoolean).append("aChar", this.aChar, 205 that.aChar) 206 .append("aByte", this.aByte, that.aByte).append("aShort", this.aShort, that.aShort).append("anInt", 207 this.anInt, 208 that.anInt) 209 .append("aLong", this.aLong, that.aLong).append("aFloat", this.aFloat, that.aFloat).append("aDouble", 210 this.aDouble, 211 that.aDouble) 212 .append("anObject", this.anObject, that.anObject).append("arrBoolean", this.arrBoolean, that.arrBoolean) 213 .append("arrChar", this.arrChar, that.arrChar).append("arrByte", this.arrByte, that.arrByte) 214 .append("arrShort", this.arrShort, that.arrShort).append("arrInt", this.arrInt, that.arrInt) 215 .append("arrLong", this.arrLong, that.arrLong).append("arrFloat", this.arrFloat, that.arrFloat) 216 .append("arrDouble", this.arrDouble, that.arrDouble).append("arrObject", this.arrObject, that.arrObject); 217 } 218 } 219 220 public boolean equals(Object that) { 221 if (!(that instanceof TestObject)) return false; 222 223 TestObject testThat = (TestObject) that; 224 225 return EqualsBuilder.reflectionEquals(this, testThat); 226 } 227 } 228 229 public void testConstruction() throws Exception { 230 try { 231 new DifferenceBuilder(null); 232 fail("Didn't get NPE on no context"); 233 } catch (NullPointerException npe) { 234 } 236 } 237 238 public void testNoDifference() throws Exception { 239 TestObject one = new TestObject(); 240 TestObject two = new TestObject(); 241 242 assertEqualsUnordered(new Object [0], DifferenceBuilder.getDifferences(one, two)); 243 assertEqualsUnordered(new Object [0], DifferenceBuilder.getDifferencesAsArray(one, two)); 244 } 245 246 public void testPrimitives() throws Exception { 247 USE_REFLECTION = false; 248 checkPrimitives(); 249 USE_REFLECTION = true; 250 checkPrimitives(); 251 } 252 253 public void testPrimitiveArrays() throws Exception { 254 USE_REFLECTION = false; 255 checkPrimitiveArrays(); 256 USE_REFLECTION = true; 257 checkPrimitiveArrays(); 258 } 259 260 public void testObjects() throws Exception { 261 USE_REFLECTION = true; 262 checkObjects(); 263 USE_REFLECTION = false; 264 checkObjects(); 265 } 266 267 public void testObjectArrays() throws Exception { 268 USE_REFLECTION = true; 269 checkObjectArrays(); 270 USE_REFLECTION = false; 271 checkObjectArrays(); 272 } 273 274 public void testNestedObjects() throws Exception { 275 USE_REFLECTION = true; 276 checkNestedObjects(); 277 USE_REFLECTION = false; 278 checkNestedObjects(); 279 } 280 281 public void testUsesStringifier() throws Exception { 282 USE_REFLECTION = true; 283 checkStringifier(); 284 USE_REFLECTION = false; 285 checkStringifier(); 286 } 287 288 public void testDifferentClasses() throws Exception { 289 USE_REFLECTION = true; 290 checkDifferentClasses(); 291 USE_REFLECTION = false; 292 checkDifferentClasses(); 293 } 294 295 public void testDerivedClasses() throws Exception { 296 USE_REFLECTION = true; 297 checkDerivedClasses(); 298 USE_REFLECTION = false; 299 checkDerivedClasses(); 300 } 301 302 private static class OtherDifferenceable implements Differenceable { 303 private final Object a; 304 305 public OtherDifferenceable(Object a) { 306 this.a = a; 307 } 308 309 public void addDifferences(DifferenceContext context, Object that) { 310 if (USE_REFLECTION) { 311 new DifferenceBuilder(context).reflectionDifference(this, that); 312 } else { 313 new DifferenceBuilder(context).append("a", this.a, ((OtherDifferenceable) that).a); 314 } 315 } 316 317 public boolean equals(Object o) { 318 return (o instanceof OtherDifferenceable) && (this.a.equals(((OtherDifferenceable) o).a)); 319 } 320 } 321 322 private static class FieldAdded extends OtherDifferenceable { 323 private final Object b; 324 325 public FieldAdded(Object a, Object b) { 326 super(a); 327 this.b = b; 328 } 329 330 public void addDifferences(DifferenceContext context, Object that) { 331 super.addDifferences(context, that); 332 if (USE_REFLECTION) { 333 new DifferenceBuilder(context).reflectionDifference(this, that); 334 } else { 335 new DifferenceBuilder(context).append("b", this.b, ((FieldAdded) that).b); 336 } 337 } 338 339 public boolean equals(Object that) { 340 if (!super.equals(that)) return false; 341 if (!(that instanceof FieldAdded)) return false; 342 return ((FieldAdded) that).b.equals(this.b); 343 } 344 } 345 346 private static class FieldIgnored extends OtherDifferenceable { 347 private final Object b; 348 349 public FieldIgnored(Object a, Object b) { 350 super(a); 351 this.b = b; 352 } 353 354 Object getB() { 355 return this.b; 356 } 357 } 358 359 private void checkDifferentClasses() throws Exception { 360 TestObject oneSub = new TestObject(); 361 TestObject one = new TestObject(oneSub); 362 OtherDifferenceable twoSub = new OtherDifferenceable("foo"); 363 TestObject two = new TestObject(twoSub); 364 OtherDifferenceable three = new OtherDifferenceable(oneSub); 365 366 DifferenceContext initial = DifferenceContext.createInitial(), context; 367 context = initial.sub("anObject"); 368 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, oneSub, twoSub)) }, 369 DifferenceBuilder.getDifferences(one, two)); 370 371 initial = DifferenceContext.createInitial(); 372 context = initial.sub("anObject"); 373 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, twoSub, oneSub)) }, 374 DifferenceBuilder.getDifferences(two, one)); 375 376 initial = DifferenceContext.createInitial(); 377 context = initial; 378 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, one, three)) }, 379 DifferenceBuilder.getDifferences(one, three)); 380 381 initial = DifferenceContext.createInitial(); 382 context = initial; 383 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, three, one)) }, 384 DifferenceBuilder.getDifferences(three, one)); 385 } 386 387 private void checkDerivedClasses() throws Exception { 388 OtherDifferenceable base = new OtherDifferenceable("a"); 389 OtherDifferenceable added = new FieldAdded("a", "b"); 390 OtherDifferenceable ignored = new FieldIgnored("a", "b"); 391 392 DifferenceContext initial = DifferenceContext.createInitial(), context; 393 context = initial; 394 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, base, added)) }, 395 DifferenceBuilder.getDifferences(base, added)); 396 397 initial = DifferenceContext.createInitial(); 398 context = initial; 399 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, added, base)) }, 400 DifferenceBuilder.getDifferences(added, base)); 401 402 initial = DifferenceContext.createInitial(); 403 context = initial; 404 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, base, ignored)) }, 405 DifferenceBuilder.getDifferences(base, ignored)); 406 407 initial = DifferenceContext.createInitial(); 408 context = initial; 409 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, ignored, base)) }, 410 DifferenceBuilder.getDifferences(ignored, base)); 411 } 412 413 private void checkStringifier() throws Exception { 414 Stringifier s = new Stringifier() { 415 public String toString(Object o) { 416 return "X" + o + "Y"; 417 } 418 }; 419 420 TestObject one, two; 421 DifferenceContext initial = DifferenceContext.createInitial(s); 422 423 one = new TestObject("foo"); 424 two = new TestObject("foof"); 425 initial.sub("anObject"); 426 Difference[] differences = DifferenceBuilder.getDifferencesAsArray(one, two, s); 427 String theString = differences[0].toString(); 428 assertTrue(theString.indexOf("XfooY") >= 0); 429 assertTrue(theString.indexOf("XfoofY") >= 0); 430 } 431 432 private void checkNestedObjects() throws Exception { 433 TestObject one, two; 434 DifferenceContext initial = DifferenceContext.createInitial(), context; 435 436 TestObject diffOne = new TestObject("foo"); 437 TestObject diffTwo = new TestObject("bar"); 438 439 one = new TestObject(new TestObject(new Object [] { new TestObject(), 440 new TestObject(new Object [] { "a", "b", "c", "d", new TestObject(diffOne) }) })); 441 two = new TestObject(new TestObject(new Object [] { new TestObject(), 442 new TestObject(new Object [] { "a", "b", "c", "d", new TestObject(diffTwo) }) })); 443 444 context = initial.sub("anObject").sub("arrObject[1]").sub("arrObject[4]").sub("anObject").sub("anObject"); 445 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, "foo", "bar")) }, 446 DifferenceBuilder.getDifferences(one, two)); 447 } 448 449 private void checkObjects() throws Exception { 450 TestObject one, two; 451 DifferenceContext initial = DifferenceContext.createInitial(), context; 452 453 one = new TestObject("foo"); 454 two = new TestObject("foof"); 455 context = initial.sub("anObject"); 456 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, "foo", "foof")) }, 457 DifferenceBuilder.getDifferences(one, two)); 458 } 459 460 private void checkObjectArrays() throws Exception { 461 TestObject one, two; 462 DifferenceContext initial = DifferenceContext.createInitial(), context; 463 464 one = new TestObject(new String [] { "a", "foo", "b" }); 465 two = new TestObject(new String [] { "a", "foof", "b" }); 466 context = initial.sub("arrObject[1]"); 467 assertEqualsUnordered(new Object [] { contextify(context, new BasicObjectDifference(context, "foo", "foof")) }, 468 DifferenceBuilder.getDifferences(one, two)); 469 } 470 471 private void checkPrimitives() throws Exception { 472 TestObject one, two; 473 DifferenceContext initial = DifferenceContext.createInitial(), context; 474 475 one = new TestObject(true); 476 two = new TestObject(false); 477 context = initial.sub("aBoolean"); 478 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, true, false)) }, 479 DifferenceBuilder.getDifferences(one, two)); 480 481 one = new TestObject('x'); 482 two = new TestObject('y'); 483 context = initial.sub("aChar"); 484 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 'x', 'y')) }, 485 DifferenceBuilder.getDifferences(one, two)); 486 487 one = new TestObject((byte) 14); 488 two = new TestObject((byte) 15); 489 context = initial.sub("aByte"); 490 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, (byte) 14, (byte) 15)) }, 491 DifferenceBuilder.getDifferences(one, two)); 492 493 one = new TestObject((short) 14); 494 two = new TestObject((short) 15); 495 context = initial.sub("aShort"); 496 assertEqualsUnordered( 497 new Object [] { contextify(context, new PrimitiveDifference(context, (short) 14, (short) 15)) }, 498 DifferenceBuilder.getDifferences(one, two)); 499 500 one = new TestObject(14); 501 two = new TestObject(15); 502 context = initial.sub("anInt"); 503 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 14, 15)) }, 504 DifferenceBuilder.getDifferences(one, two)); 505 506 one = new TestObject(14L); 507 two = new TestObject(15L); 508 context = initial.sub("aLong"); 509 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 14L, 15L)) }, 510 DifferenceBuilder.getDifferences(one, two)); 511 512 one = new TestObject(14.0f); 513 two = new TestObject(15.0f); 514 context = initial.sub("aFloat"); 515 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 14.0f, 15.0f)) }, 516 DifferenceBuilder.getDifferences(one, two)); 517 518 one = new TestObject(14.0); 519 two = new TestObject(15.0); 520 context = initial.sub("aDouble"); 521 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 14.0, 15.0)) }, 522 DifferenceBuilder.getDifferences(one, two)); 523 } 524 525 private void checkPrimitiveArrays() throws Exception { 526 TestObject one, two; 527 DifferenceContext initial = DifferenceContext.createInitial(), context; 528 529 one = new TestObject(new boolean[] { true, false, true }); 530 two = new TestObject(new boolean[] { true, false, false }); 531 context = initial.sub("arrBoolean[2]"); 532 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, true, false)) }, 533 DifferenceBuilder.getDifferences(one, two)); 534 535 one = new TestObject(new char[] { 'a', 'x', 'c' }); 536 two = new TestObject(new char[] { 'a', 'y', 'c' }); 537 context = initial.sub("arrChar[1]"); 538 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 'x', 'y')) }, 539 DifferenceBuilder.getDifferences(one, two)); 540 541 one = new TestObject(new byte[] { (byte) 14, (byte) 14, (byte) 15 }); 542 two = new TestObject(new byte[] { (byte) 15, (byte) 14, (byte) 15 }); 543 context = initial.sub("arrByte[0]"); 544 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, (byte) 14, (byte) 15)) }, 545 DifferenceBuilder.getDifferences(one, two)); 546 547 one = new TestObject(new short[] { (short) 3, (short) 4, (short) 14 }); 548 two = new TestObject(new short[] { (short) 3, (short) 4, (short) 15 }); 549 context = initial.sub("arrShort[2]"); 550 assertEqualsUnordered( 551 new Object [] { contextify(context, new PrimitiveDifference(context, (short) 14, (short) 15)) }, 552 DifferenceBuilder.getDifferences(one, two)); 553 554 one = new TestObject(new int[] { 3, 14, 5 }); 555 two = new TestObject(new int[] { 3, 15, 5 }); 556 context = initial.sub("arrInt[1]"); 557 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 14, 15)) }, 558 DifferenceBuilder.getDifferences(one, two)); 559 560 one = new TestObject(new long[] { 14, 3, 4 }); 561 two = new TestObject(new long[] { 15, 3, 4 }); 562 context = initial.sub("arrLong[0]"); 563 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 14L, 15L)) }, 564 DifferenceBuilder.getDifferences(one, two)); 565 566 one = new TestObject(new float[] { 3.0f, 4.0f, 14.0f }); 567 two = new TestObject(new float[] { 3.0f, 4.0f, 15.0f }); 568 context = initial.sub("arrFloat[2]"); 569 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 14.0f, 15.0f)) }, 570 DifferenceBuilder.getDifferences(one, two)); 571 572 one = new TestObject(new double[] { 3.0, 14.0, 5.0 }); 573 two = new TestObject(new double[] { 3.0, 15.0, 5.0 }); 574 context = initial.sub("arrDouble[1]"); 575 assertEqualsUnordered(new Object [] { contextify(context, new PrimitiveDifference(context, 14.0, 15.0)) }, 576 DifferenceBuilder.getDifferences(one, two)); 577 } 578 579 private Difference contextify(DifferenceContext context, Difference difference) { 580 context.addDifference(difference); 581 return difference; 582 } 583 584 } | Popular Tags |