001: package net.sf.mockcreator.utils;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.HashMap;
006: import java.util.HashSet;
007: import java.util.LinkedList;
008: import java.util.Map;
009: import java.util.Set;
010: import java.util.TreeMap;
011: import java.util.TreeSet;
012: import java.util.Vector;
013:
014: import net.sf.mockcreator.TestCase;
015: import net.sf.mockcreator.utils.CompareByValue;
016:
017: public class RecursiveComparatorCollectionsTest extends TestCase {
018:
019: public RecursiveComparatorCollectionsTest(String name) {
020: super (name);
021: }
022:
023: public void testMap_Empty() {
024: Map map1 = new HashMap();
025: Map map2 = new TreeMap();
026:
027: assertTrue(CompareByValue.equals(map1, map2));
028:
029: Map map3 = new HashMap();
030: map3.put("foo", "bar");
031: assertFalse(CompareByValue.equals(map1, map3));
032: }
033:
034: public void testMap_OneElement_Equal() {
035: Map map1 = new HashMap();
036: Map map2 = new TreeMap();
037:
038: map1.put(new Long(1), "foo");
039: map2.put(new Long(1), "foo");
040:
041: assertTrue(CompareByValue.equals(map1, map2));
042: }
043:
044: public void testMap_OneElement_KeysEqual() {
045: Map map1 = new HashMap();
046: Map map2 = new TreeMap();
047:
048: map1.put(new Long(1), "foo");
049: map2.put(new Long(1), "bar");
050:
051: assertFalse(CompareByValue.equals(map1, map2));
052: }
053:
054: public void testMap_OneElement_ValueEqual() {
055: Map map1 = new HashMap();
056: Map map2 = new TreeMap();
057:
058: map1.put(new Long(1), "foo");
059: map2.put(new Long(2), "foo");
060:
061: assertFalse(CompareByValue.equals(map1, map2));
062: }
063:
064: public void testMap_ThreeElements_OneValueMismatchs() {
065: Map map1 = new HashMap();
066: Map map2 = new TreeMap();
067:
068: map1.put(new Long(1), "foo");
069: map1.put(new Long(2), "bar");
070: map1.put(new Long(3), "baz");
071:
072: map2.put(new Long(1), "foo");
073: map2.put(new Long(2), "bar");
074: map2.put(new Long(3), "zzz");
075:
076: assertFalse(CompareByValue.equals(map1, map2));
077: }
078:
079: public void testMap_ThreeElements_OneKeyMismatchs() {
080: Map map1 = new HashMap();
081: Map map2 = new TreeMap();
082:
083: map1.put(new Long(1), "foo");
084: map1.put(new Long(2), "bar");
085: map1.put(new Long(3), "baz");
086:
087: map2.put(new Long(1), "foo");
088: map2.put(new Long(7), "bar");
089: map2.put(new Long(3), "baz");
090:
091: assertFalse(CompareByValue.equals(map1, map2));
092: }
093:
094: public void testMap_Nulls() {
095: Map map1 = new HashMap();
096: Map map2 = new HashMap();
097:
098: map1.put(null, null);
099: map2.put(null, null);
100:
101: assertTrue(CompareByValue.equals(map1, map2));
102: }
103:
104: public void testSet_Empty() {
105: Set set1 = new HashSet();
106: Set set2 = new TreeSet();
107:
108: assertTrue(CompareByValue.equals(set1, set2));
109:
110: Set set3 = new HashSet();
111: set3.add("foo");
112: assertFalse(CompareByValue.equals(set1, set3));
113: }
114:
115: public void testSet_OneElement_Equal() {
116: Set set1 = new HashSet();
117: Set set2 = new TreeSet();
118:
119: set1.add(new Long(1));
120: set2.add(new Long(1));
121:
122: assertTrue(CompareByValue.equals(set1, set2));
123: }
124:
125: public void testSet_OneElement_Set() {
126: Set set1 = new HashSet();
127: Set set2 = new TreeSet();
128:
129: Set set11 = new HashSet();
130: Set set21 = new TreeSet();
131: set11.add("foo");
132: set21.add("foo");
133:
134: set1.add(set11);
135: set2.add(set21);
136:
137: assertTrue(CompareByValue.compareCollections(set1, set2));
138:
139: set21.add("bar");
140: assertFalse(CompareByValue.compareCollections(set1, set2));
141: }
142:
143: public void testSet_ThreeElements_OneValueMismatchs() {
144: Set set1 = new HashSet();
145: Set set2 = new TreeSet();
146:
147: set1.add("foo");
148: set1.add("bar");
149: set1.add("baz");
150:
151: set2.add("foo");
152: set2.add("bar");
153: set2.add("zzz");
154:
155: assertFalse(CompareByValue.equals(set1, set2));
156: }
157:
158: public void testSet_Nulls() {
159: Set set1 = new HashSet();
160: Set set2 = new HashSet();
161:
162: set1.add(null);
163: set2.add(null);
164:
165: assertTrue(CompareByValue.equals(set1, set2));
166: }
167:
168: public void testCollection_Empty() {
169: Collection col1 = new ArrayList();
170: Collection col2 = new Vector();
171:
172: assertTrue(CompareByValue.equals(col1, col2));
173:
174: Collection col3 = new ArrayList();
175: col3.add("foo");
176: assertFalse(CompareByValue.equals(col1, col3));
177: }
178:
179: public void testCollection_OneElement_Equal() {
180: Collection col1 = new ArrayList();
181: Collection col2 = new Vector();
182:
183: col1.add(new Long(1));
184: col2.add(new Long(1));
185:
186: assertTrue(CompareByValue.equals(col1, col2));
187: }
188:
189: public void testCollection_OneElement_Set() {
190: Collection col1 = new ArrayList();
191: Collection col2 = new Vector();
192:
193: Set set1 = new HashSet();
194: Set set2 = new TreeSet();
195: set1.add("foo");
196: set2.add("foo");
197:
198: col1.add(set1);
199: col2.add(set2);
200:
201: assertTrue(CompareByValue.equals(col1, col2));
202:
203: col2.add(set2);
204: assertFalse(CompareByValue.equals(col1, col2));
205: }
206:
207: public void testCollection_ThreeElements_OneValueMismatchs() {
208: Collection col1 = new LinkedList();
209: Collection col2 = new Vector();
210:
211: col1.add("foo");
212: col1.add("bar");
213: col1.add("baz");
214:
215: col2.add("foo");
216: col2.add("bar");
217: col2.add("zzz");
218:
219: assertFalse(CompareByValue.equals(col1, col2));
220: }
221:
222: public void testCollection_DifferentLength() {
223: Collection col1 = new LinkedList();
224: Collection col2 = new Vector();
225:
226: col1.add("foo");
227: col1.add("bar");
228: col1.add("baz");
229:
230: col2.add("foo");
231: col2.add("bar");
232:
233: assertFalse(CompareByValue.equals(col1, col2));
234: }
235:
236: public void testCollection_Nulls() {
237: Collection col1 = new Vector();
238: Collection col2 = new Vector();
239:
240: col1.add(null);
241: col2.add(null);
242:
243: assertTrue(CompareByValue.equals(col1, col2));
244: }
245:
246: // FIXME: don't know what to do in case of recursion yet
247: // public void test_Recursive() {
248: // Collection map1 = new HashCollection();
249: // Collection map2 = new HashCollection();
250: //
251: // map1.put(map2,map2);
252: // map2.put(map1,map1);
253: //
254: // assertFalse(RecursiveComparator.equals(map1,map2));
255: // }
256: }
|