001: /**
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */package com.tc.test.collections;
004:
005: import com.tc.util.EqualityComparator;
006: import com.tc.util.EqualsEqualityComparator;
007: import com.tc.util.StandardStringifier;
008: import com.tc.util.Stringifier;
009:
010: import junit.framework.Assert;
011:
012: /**
013: * A set of primitives for comparing collections in tests.
014: */
015: public class CollectionAssert {
016:
017: private static final int MAX_LENGTH_TO_SHOW_COLLECTIONS = 500;
018: private static final int MAX_NEWLINES_TO_SHOW_COLLECTIONS = 15;
019:
020: private static final EqualityComparator STANDARD_COMPARATOR = EqualsEqualityComparator.INSTANCE;
021: private static final Stringifier STANDARD_STRINGIFIER = StandardStringifier.INSTANCE;
022:
023: public static void assertEqualUnorderedUncounted(Object one,
024: Object two) {
025: assertEqualUnorderedUncounted(one, two, STANDARD_STRINGIFIER);
026: }
027:
028: public static void assertEqualUnorderedUncounted(Object one,
029: Object two, Stringifier stringifier) {
030: assertEqualUnorderedUncounted(one, two, STANDARD_COMPARATOR,
031: stringifier);
032: }
033:
034: public static void assertEqualUnorderedUncounted(Object one,
035: Object two, EqualityComparator comparator) {
036: assertEqualUnorderedUncounted(one, two, comparator,
037: STANDARD_STRINGIFIER);
038: }
039:
040: public static void assertEqualUnorderedUncounted(Object one,
041: Object two, EqualityComparator comparator,
042: Stringifier stringifier) {
043: assertEqual(one, two, new UnorderedUncountedCollectionComparer(
044: comparator, stringifier), stringifier);
045: }
046:
047: public static void assertEqualUnordered(Object one, Object two) {
048: assertEqualUnordered(one, two, STANDARD_STRINGIFIER);
049: }
050:
051: public static void assertEqualUnordered(Object one, Object two,
052: Stringifier stringifier) {
053: assertEqualUnordered(one, two, STANDARD_COMPARATOR, stringifier);
054: }
055:
056: public static void assertEqualUnordered(Object one, Object two,
057: EqualityComparator comparator) {
058: assertEqualUnordered(one, two, comparator, STANDARD_STRINGIFIER);
059: }
060:
061: public static void assertEqualUnordered(Object one, Object two,
062: EqualityComparator comparator, Stringifier stringifier) {
063: assertEqual(one, two, new UnorderedCollectionComparer(
064: comparator, stringifier), stringifier);
065: }
066:
067: public static void assertEqualOrdered(Object one, Object two) {
068: assertEqualOrdered(one, two, STANDARD_STRINGIFIER);
069: }
070:
071: public static void assertEqualOrdered(Object one, Object two,
072: Stringifier stringifier) {
073: assertEqualOrdered(one, two, STANDARD_COMPARATOR, stringifier);
074: }
075:
076: public static void assertEqualOrdered(Object one, Object two,
077: EqualityComparator comparator) {
078: assertEqualOrdered(one, two, comparator, STANDARD_STRINGIFIER);
079: }
080:
081: public static void assertEqualOrdered(Object one, Object two,
082: EqualityComparator comparator, Stringifier stringifier) {
083: assertEqual(one, two, new OrderedCollectionComparer(comparator,
084: stringifier), stringifier);
085: }
086:
087: private static void assertEqual(Object one, Object two,
088: CollectionComparer comparer, Stringifier describer) {
089: com.tc.util.Assert.assertNotNull(one);
090: com.tc.util.Assert.assertNotNull(two);
091:
092: String oneString = describer.toString(one);
093: String twoString = describer.toString(two);
094:
095: int totalLength = oneString.length() + twoString.length();
096: int totalNewlines = countNewlines(oneString)
097: + countNewlines(twoString);
098:
099: boolean showCollections = (totalLength <= MAX_LENGTH_TO_SHOW_COLLECTIONS)
100: && (totalNewlines <= MAX_NEWLINES_TO_SHOW_COLLECTIONS);
101:
102: assertEqual(one, two, comparer, describer, showCollections);
103: }
104:
105: private static int countNewlines(String s) {
106: int out = 0;
107: int length = s.length();
108:
109: boolean justWasCR = false;
110:
111: for (int i = 0; i < length; ++i) {
112: char ch = s.charAt(i);
113: if (ch == '\r' || (ch == '\n' && (!justWasCR))) {
114: ++out;
115: }
116:
117: justWasCR = (ch == '\r');
118: }
119:
120: return out;
121: }
122:
123: private static void assertEqual(Object one, Object two,
124: CollectionComparer comparer, Stringifier describer,
125: boolean showCollections) {
126: com.tc.util.Assert.assertNotNull(one);
127: com.tc.util.Assert.assertNotNull(two);
128:
129: CollectionMismatch[] mismatches = comparer.getMismatches(one,
130: two);
131: if (mismatches.length > 0) {
132: StringBuffer descrip = new StringBuffer();
133:
134: if (showCollections) {
135: descrip.append("Collections " + describer.toString(one)
136: + " and " + describer.toString(two)
137: + " aren't equal: ");
138: } else {
139: descrip.append("Collections aren't equal:");
140: }
141:
142: for (int i = 0; i < mismatches.length; ++i) {
143: descrip.append(mismatches[i].toString());
144: descrip.append("\n");
145: }
146:
147: Assert.fail(descrip.toString());
148: }
149: }
150:
151: }
|