1 4 package com.tc.test.collections; 5 6 import com.tc.util.EqualityComparator; 7 import com.tc.util.EqualsEqualityComparator; 8 import com.tc.util.StandardStringifier; 9 import com.tc.util.Stringifier; 10 11 import junit.framework.Assert; 12 13 16 public class CollectionAssert { 17 18 private static final int MAX_LENGTH_TO_SHOW_COLLECTIONS = 500; 19 private static final int MAX_NEWLINES_TO_SHOW_COLLECTIONS = 15; 20 21 private static final EqualityComparator STANDARD_COMPARATOR = EqualsEqualityComparator.INSTANCE; 22 private static final Stringifier STANDARD_STRINGIFIER = StandardStringifier.INSTANCE; 23 24 public static void assertEqualUnorderedUncounted(Object one, Object two) { 25 assertEqualUnorderedUncounted(one, two, STANDARD_STRINGIFIER); 26 } 27 28 public static void assertEqualUnorderedUncounted(Object one, Object two, Stringifier stringifier) { 29 assertEqualUnorderedUncounted(one, two, STANDARD_COMPARATOR, stringifier); 30 } 31 32 public static void assertEqualUnorderedUncounted(Object one, Object two, EqualityComparator comparator) { 33 assertEqualUnorderedUncounted(one, two, comparator, STANDARD_STRINGIFIER); 34 } 35 36 public static void assertEqualUnorderedUncounted(Object one, Object two, EqualityComparator comparator, 37 Stringifier stringifier) { 38 assertEqual(one, two, new UnorderedUncountedCollectionComparer(comparator, stringifier), stringifier); 39 } 40 41 public static void assertEqualUnordered(Object one, Object two) { 42 assertEqualUnordered(one, two, STANDARD_STRINGIFIER); 43 } 44 45 public static void assertEqualUnordered(Object one, Object two, Stringifier stringifier) { 46 assertEqualUnordered(one, two, STANDARD_COMPARATOR, stringifier); 47 } 48 49 public static void assertEqualUnordered(Object one, Object two, EqualityComparator comparator) { 50 assertEqualUnordered(one, two, comparator, STANDARD_STRINGIFIER); 51 } 52 53 public static void assertEqualUnordered(Object one, Object two, EqualityComparator comparator, Stringifier stringifier) { 54 assertEqual(one, two, new UnorderedCollectionComparer(comparator, stringifier), stringifier); 55 } 56 57 public static void assertEqualOrdered(Object one, Object two) { 58 assertEqualOrdered(one, two, STANDARD_STRINGIFIER); 59 } 60 61 public static void assertEqualOrdered(Object one, Object two, Stringifier stringifier) { 62 assertEqualOrdered(one, two, STANDARD_COMPARATOR, stringifier); 63 } 64 65 public static void assertEqualOrdered(Object one, Object two, EqualityComparator comparator) { 66 assertEqualOrdered(one, two, comparator, STANDARD_STRINGIFIER); 67 } 68 69 public static void assertEqualOrdered(Object one, Object two, EqualityComparator comparator, Stringifier stringifier) { 70 assertEqual(one, two, new OrderedCollectionComparer(comparator, stringifier), stringifier); 71 } 72 73 private static void assertEqual(Object one, Object two, CollectionComparer comparer, Stringifier describer) { 74 com.tc.util.Assert.assertNotNull(one); 75 com.tc.util.Assert.assertNotNull(two); 76 77 String oneString = describer.toString(one); 78 String twoString = describer.toString(two); 79 80 int totalLength = oneString.length() + twoString.length(); 81 int totalNewlines = countNewlines(oneString) + countNewlines(twoString); 82 83 boolean showCollections = (totalLength <= MAX_LENGTH_TO_SHOW_COLLECTIONS) 84 && (totalNewlines <= MAX_NEWLINES_TO_SHOW_COLLECTIONS); 85 86 assertEqual(one, two, comparer, describer, showCollections); 87 } 88 89 private static int countNewlines(String s) { 90 int out = 0; 91 int length = s.length(); 92 93 boolean justWasCR = false; 94 95 for (int i = 0; i < length; ++i) { 96 char ch = s.charAt(i); 97 if (ch == '\r' || (ch == '\n' && (!justWasCR))) { 98 ++out; 99 } 100 101 justWasCR = (ch == '\r'); 102 } 103 104 return out; 105 } 106 107 private static void assertEqual(Object one, Object two, CollectionComparer comparer, Stringifier describer, 108 boolean showCollections) { 109 com.tc.util.Assert.assertNotNull(one); 110 com.tc.util.Assert.assertNotNull(two); 111 112 CollectionMismatch[] mismatches = comparer.getMismatches(one, two); 113 if (mismatches.length > 0) { 114 StringBuffer descrip = new StringBuffer (); 115 116 if (showCollections) { 117 descrip 118 .append("Collections " + describer.toString(one) + " and " + describer.toString(two) + " aren't equal: "); 119 } else { 120 descrip.append("Collections aren't equal:"); 121 } 122 123 for (int i = 0; i < mismatches.length; ++i) { 124 descrip.append(mismatches[i].toString()); 125 descrip.append("\n"); 126 } 127 128 Assert.fail(descrip.toString()); 129 } 130 } 131 132 } | Popular Tags |