KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
14  * A set of primitives for comparing collections in tests.
15  */

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 JavaDoc one, Object JavaDoc two) {
25     assertEqualUnorderedUncounted(one, two, STANDARD_STRINGIFIER);
26   }
27
28   public static void assertEqualUnorderedUncounted(Object JavaDoc one, Object JavaDoc two, Stringifier stringifier) {
29     assertEqualUnorderedUncounted(one, two, STANDARD_COMPARATOR, stringifier);
30   }
31
32   public static void assertEqualUnorderedUncounted(Object JavaDoc one, Object JavaDoc two, EqualityComparator comparator) {
33     assertEqualUnorderedUncounted(one, two, comparator, STANDARD_STRINGIFIER);
34   }
35
36   public static void assertEqualUnorderedUncounted(Object JavaDoc one, Object JavaDoc two, EqualityComparator comparator,
37                                                    Stringifier stringifier) {
38     assertEqual(one, two, new UnorderedUncountedCollectionComparer(comparator, stringifier), stringifier);
39   }
40
41   public static void assertEqualUnordered(Object JavaDoc one, Object JavaDoc two) {
42     assertEqualUnordered(one, two, STANDARD_STRINGIFIER);
43   }
44
45   public static void assertEqualUnordered(Object JavaDoc one, Object JavaDoc two, Stringifier stringifier) {
46     assertEqualUnordered(one, two, STANDARD_COMPARATOR, stringifier);
47   }
48
49   public static void assertEqualUnordered(Object JavaDoc one, Object JavaDoc two, EqualityComparator comparator) {
50     assertEqualUnordered(one, two, comparator, STANDARD_STRINGIFIER);
51   }
52
53   public static void assertEqualUnordered(Object JavaDoc one, Object JavaDoc two, EqualityComparator comparator, Stringifier stringifier) {
54     assertEqual(one, two, new UnorderedCollectionComparer(comparator, stringifier), stringifier);
55   }
56
57   public static void assertEqualOrdered(Object JavaDoc one, Object JavaDoc two) {
58     assertEqualOrdered(one, two, STANDARD_STRINGIFIER);
59   }
60
61   public static void assertEqualOrdered(Object JavaDoc one, Object JavaDoc two, Stringifier stringifier) {
62     assertEqualOrdered(one, two, STANDARD_COMPARATOR, stringifier);
63   }
64
65   public static void assertEqualOrdered(Object JavaDoc one, Object JavaDoc two, EqualityComparator comparator) {
66     assertEqualOrdered(one, two, comparator, STANDARD_STRINGIFIER);
67   }
68
69   public static void assertEqualOrdered(Object JavaDoc one, Object JavaDoc two, EqualityComparator comparator, Stringifier stringifier) {
70     assertEqual(one, two, new OrderedCollectionComparer(comparator, stringifier), stringifier);
71   }
72
73   private static void assertEqual(Object JavaDoc one, Object JavaDoc two, CollectionComparer comparer, Stringifier describer) {
74     com.tc.util.Assert.assertNotNull(one);
75     com.tc.util.Assert.assertNotNull(two);
76
77     String JavaDoc oneString = describer.toString(one);
78     String JavaDoc 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 JavaDoc 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 JavaDoc one, Object JavaDoc 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 JavaDoc descrip = new StringBuffer JavaDoc();
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