KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestMultiHashMap


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27
28 import org.apache.commons.collections.map.AbstractTestMap;
29
30 /**
31  * Unit Tests for <code>MultiHashMap</code>.
32  *
33  * @version $Revision: 1.20 $ $Date: 2004/06/09 22:11:54 $
34  *
35  * @author Unknown
36  */

37 public class TestMultiHashMap extends AbstractTestMap {
38
39     public TestMultiHashMap(String JavaDoc testName) {
40         super(testName);
41     }
42
43     public static Test suite() {
44         return new TestSuite(TestMultiHashMap.class);
45     }
46
47     public static void main(String JavaDoc args[]) {
48         String JavaDoc[] testCaseName = { TestMultiHashMap.class.getName()};
49         junit.textui.TestRunner.main(testCaseName);
50     }
51
52     // MutltiHashMap was introduced in Collections 2.x
53
public String JavaDoc getCompatibilityVersion() {
54         return "2";
55     }
56
57     public Map JavaDoc makeEmptyMap() {
58         return new MultiHashMap();
59     }
60     
61     //----------------------------
62
// Tests
63
//----------------------------
64
public void testPutNGet() {
65         MultiHashMap map = new MultiHashMap();
66         loadMap(map);
67         checkMap(map);
68
69         assertTrue(map.get(new Integer JavaDoc(99)) == null);
70
71         map.clear();
72         assertTrue(map.size() == 0);
73     }
74
75     public void testContainsValue() {
76         MultiHashMap map = new MultiHashMap();
77         loadMap(map);
78
79         assertTrue(map.containsValue("uno"));
80         assertTrue(map.containsValue("quatro"));
81         assertTrue(map.containsValue("two"));
82
83         assertTrue(!map.containsValue("uggaBugga"));
84
85         map.clear();
86     }
87     
88     public void testValues() {
89         MultiHashMap map = new MultiHashMap();
90         loadMap(map);
91
92         Collection JavaDoc vals = map.values();
93         assertTrue(vals.size() == getFullSize());
94
95         map.clear();
96     }
97
98     static private class MapPair {
99         MapPair(int key, String JavaDoc val) {
100             mKey = new Integer JavaDoc(key);
101             mValue = val;
102         }
103
104         Integer JavaDoc mKey = null;
105         String JavaDoc mValue = null;
106     }
107     
108     static private MapPair[][] sMapPairs =
109     {
110         {new MapPair(0,"zero")},
111         {new MapPair(1,"one"), new MapPair(1,"ONE"), new MapPair(1,"uno")},
112         {new MapPair(2,"two"), new MapPair(2,"two") },
113         {new MapPair(3,"three"), new MapPair(3,"THREE"), new MapPair(3,"tres")},
114         {new MapPair(4,"four"), new MapPair(4,"quatro")}
115     };
116     
117     private void loadMap(MultiHashMap map) {
118         // Set up so that we load the keys "randomly"
119
// (i.e. we don't want to load int row-order, so that all like keys
120
// load together. We want to mix it up...)
121

122         int numRows = sMapPairs.length;
123         int maxCols = 0;
124         for (int ii = 0; ii < sMapPairs.length; ii++) {
125             if (sMapPairs[ii].length > maxCols) {
126                 maxCols = sMapPairs[ii].length;
127             }
128         }
129         for (int ii = 0; ii < maxCols; ii++) {
130             for (int jj = 0; jj < numRows; jj++) {
131                 if (ii < sMapPairs[jj].length) {
132                     map.put(sMapPairs[jj][ii].mKey, sMapPairs[jj][ii].mValue);
133                     //---------------------------------------------------------
134
}
135             }
136         }
137         assertTrue(map.size() == sMapPairs.length);
138     }
139     
140     private void checkMap(MultiHashMap map) {
141         for (int ii = 0; ii < sMapPairs.length; ii++) {
142             checkKeyList(map, ii);
143         }
144     }
145
146     private void checkKeyList(MultiHashMap map, int index) {
147         assertTrue(index < sMapPairs.length);
148         Integer JavaDoc key = sMapPairs[index][0].mKey;
149
150         Object JavaDoc obj = map.get(key);
151         //--------------------------
152

153         assertTrue(obj != null);
154         assertTrue(obj instanceof Collection JavaDoc);
155         Collection JavaDoc keyList = (Collection JavaDoc) obj;
156
157         assertTrue(keyList.size() == sMapPairs[index].length);
158         Iterator JavaDoc iter = keyList.iterator();
159         while (iter.hasNext()) {
160             Object JavaDoc oval = iter.next();
161             assertTrue(oval != null);
162             assertTrue(oval instanceof String JavaDoc);
163             String JavaDoc val = (String JavaDoc) oval;
164             boolean foundIt = false;
165             for (int ii = 0; ii < sMapPairs[index].length; ii++) {
166                 if (val.equals(sMapPairs[index][ii].mValue)) {
167                     foundIt = true;
168                 }
169             }
170             assertTrue(foundIt);
171         }
172     }
173     
174     public int getFullSize() {
175         int len = 0;
176         for (int ii = 0; ii < sMapPairs.length; ii++) {
177             len += sMapPairs[ii].length;
178         }
179         return len;
180     }
181     
182
183     public void testEntrySetIterator() {
184     }
185     public void testEntrySetContainsProperMappings() {
186     }
187     public void testEntrySetIteratorHasProperMappings() {
188         // override and ignore test -- it will fail when verifying the iterator for
189
// the set contains the right value -- we're not returning the value, we're
190
// returning a collection.
191
// TODO: re-implement this test to ensure the values of the iterator match
192
// the proper collection rather than the value the superclass is checking
193
// for.
194
return;
195     }
196
197     // Next methods are overriden because MultiHashMap values are always a
198
// collection, and deviate from the Map contract because of this.
199

200     // TODO: implement the tests to ensure that Map.get(Object) returns the
201
// appropriate collection of values
202

203     public void testMapGet() {
204     }
205
206     public void testMapPut() {
207     }
208
209     public void testMapPutAll() {
210     }
211
212     public void testMapRemove() {
213     }
214
215     public void testMapEquals() {
216         MultiHashMap one = new MultiHashMap();
217         Integer JavaDoc value = new Integer JavaDoc(1);
218         one.put("One", value);
219         one.remove("One", value);
220         
221         MultiHashMap two = new MultiHashMap();
222         assertEquals(two, one);
223     }
224
225     public void testMapHashCode() {
226     }
227
228     // The verification for the map and its entry set must also be overridden
229
// because the values are not going to be the same as the values in the
230
// confirmed map (they're going to be collections of values instead).
231
public void verifyMap() {
232         // TODO: implement test to ensure that map is the same as confirmed if
233
// its values were converted into collections.
234
}
235
236     public void verifyEntrySet() {
237         // TODO: implement test to ensure that each entry is the same as one in
238
// the confirmed map, but with the value wrapped in a collection.
239
}
240
241     // The verification method must be overridden because MultiHashMap's
242
// values() is not properly backed by the map (Bug 9573).
243

244     public void verifyValues() {
245         // update the values view to the latest version, then proceed to verify
246
// as usual.
247
values = map.values();
248         super.verifyValues();
249     }
250     
251     //-----------------------------------------------------------------------
252
public void testGetCollection() {
253         MultiHashMap map = new MultiHashMap();
254         map.put("A", "AA");
255         assertSame(map.get("A"), map.getCollection("A"));
256     }
257     
258     public void testTotalSize() {
259         MultiHashMap map = new MultiHashMap();
260         assertEquals(0, map.totalSize());
261         map.put("A", "AA");
262         assertEquals(1, map.totalSize());
263         map.put("B", "BA");
264         assertEquals(2, map.totalSize());
265         map.put("B", "BB");
266         assertEquals(3, map.totalSize());
267         map.put("B", "BC");
268         assertEquals(4, map.totalSize());
269         map.remove("A");
270         assertEquals(3, map.totalSize());
271         map.remove("B", "BC");
272         assertEquals(2, map.totalSize());
273     }
274     
275     public void testSize_Key() {
276         MultiHashMap map = new MultiHashMap();
277         assertEquals(0, map.size("A"));
278         assertEquals(0, map.size("B"));
279         map.put("A", "AA");
280         assertEquals(1, map.size("A"));
281         assertEquals(0, map.size("B"));
282         map.put("B", "BA");
283         assertEquals(1, map.size("A"));
284         assertEquals(1, map.size("B"));
285         map.put("B", "BB");
286         assertEquals(1, map.size("A"));
287         assertEquals(2, map.size("B"));
288         map.put("B", "BC");
289         assertEquals(1, map.size("A"));
290         assertEquals(3, map.size("B"));
291         map.remove("A");
292         assertEquals(0, map.size("A"));
293         assertEquals(3, map.size("B"));
294         map.remove("B", "BC");
295         assertEquals(0, map.size("A"));
296         assertEquals(2, map.size("B"));
297     }
298     
299     public void testIterator_Key() {
300         MultiHashMap map = new MultiHashMap();
301         assertEquals(false, map.iterator("A").hasNext());
302         map.put("A", "AA");
303         Iterator JavaDoc it = map.iterator("A");
304         assertEquals(true, it.hasNext());
305         it.next();
306         assertEquals(false, it.hasNext());
307     }
308     
309     public void testContainsValue_Key() {
310         MultiHashMap map = new MultiHashMap();
311         assertEquals(false, map.containsValue("A", "AA"));
312         assertEquals(false, map.containsValue("B", "BB"));
313         map.put("A", "AA");
314         assertEquals(true, map.containsValue("A", "AA"));
315         assertEquals(false, map.containsValue("A", "AB"));
316     }
317     
318     public void testPutAll_KeyCollection() {
319         MultiHashMap map = new MultiHashMap();
320         Collection JavaDoc coll = Arrays.asList(new Object JavaDoc[] {"X", "Y", "Z"});
321         
322         assertEquals(true, map.putAll("A", coll));
323         assertEquals(3, map.size("A"));
324         assertEquals(true, map.containsValue("A", "X"));
325         assertEquals(true, map.containsValue("A", "Y"));
326         assertEquals(true, map.containsValue("A", "Z"));
327         
328         assertEquals(false, map.putAll("A", null));
329         assertEquals(3, map.size("A"));
330         assertEquals(true, map.containsValue("A", "X"));
331         assertEquals(true, map.containsValue("A", "Y"));
332         assertEquals(true, map.containsValue("A", "Z"));
333         
334         assertEquals(false, map.putAll("A", new ArrayList JavaDoc()));
335         assertEquals(3, map.size("A"));
336         assertEquals(true, map.containsValue("A", "X"));
337         assertEquals(true, map.containsValue("A", "Y"));
338         assertEquals(true, map.containsValue("A", "Z"));
339         
340         coll = Arrays.asList(new Object JavaDoc[] {"M"});
341         assertEquals(true, map.putAll("A", coll));
342         assertEquals(4, map.size("A"));
343         assertEquals(true, map.containsValue("A", "X"));
344         assertEquals(true, map.containsValue("A", "Y"));
345         assertEquals(true, map.containsValue("A", "Z"));
346         assertEquals(true, map.containsValue("A", "M"));
347     }
348
349     public void testClone() {
350         MultiHashMap map = new MultiHashMap();
351         map.put("A", "1");
352         map.put("A", "2");
353         Collection JavaDoc coll = (Collection JavaDoc) map.get("A");
354         assertEquals(1, map.size());
355         assertEquals(2, coll.size());
356         
357         MultiHashMap cloned = (MultiHashMap) map.clone();
358         Collection JavaDoc clonedColl = (Collection JavaDoc) cloned.get("A");
359         assertNotSame(map, cloned);
360         assertNotSame(coll, clonedColl);
361         assertEquals(1, map.size());
362         assertEquals(2, coll.size());
363         assertEquals(1, cloned.size());
364         assertEquals(2, clonedColl.size());
365         map.put("A", "3");
366         assertEquals(1, map.size());
367         assertEquals(3, coll.size());
368         assertEquals(1, cloned.size());
369         assertEquals(2, clonedColl.size());
370     }
371
372     public void testConstructorCopy1() {
373         MultiHashMap map = new MultiHashMap();
374         map.put("A", "1");
375         map.put("A", "2");
376         Collection JavaDoc coll = (Collection JavaDoc) map.get("A");
377         assertEquals(1, map.size());
378         assertEquals(2, coll.size());
379         
380         MultiHashMap newMap = new MultiHashMap(map);
381         Collection JavaDoc newColl = (Collection JavaDoc) newMap.get("A");
382         assertNotSame(map, newMap);
383         assertNotSame(coll, newColl);
384         assertEquals(1, map.size());
385         assertEquals(2, coll.size());
386         assertEquals(1, newMap.size());
387         assertEquals(2, newColl.size());
388         
389         map.put("A", "3");
390         assertEquals(1, map.size());
391         assertEquals(3, coll.size());
392         assertEquals(1, newMap.size());
393         assertEquals(2, newColl.size());
394     }
395
396     public void testConstructorCopy2() {
397         Map JavaDoc map = new HashMap JavaDoc();
398         map.put("A", "1");
399         map.put("B", "2");
400         assertEquals(2, map.size());
401         
402         MultiHashMap newMap = new MultiHashMap(map);
403         Collection JavaDoc newColl = (Collection JavaDoc) newMap.get("A");
404         assertNotSame(map, newMap);
405         assertEquals(2, map.size());
406         assertEquals(2, newMap.size());
407         assertEquals(1, newColl.size());
408         
409         map.put("A", "3");
410         assertEquals(2, map.size());
411         assertEquals(2, newMap.size());
412         assertEquals(1, newColl.size());
413         
414         map.put("C", "4");
415         assertEquals(3, map.size());
416         assertEquals(2, newMap.size());
417         assertEquals(1, newColl.size());
418     }
419
420 }
421
Popular Tags