001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.apache.commons.collections.map.AbstractTestMap;
029:
030: /**
031: * Unit Tests for <code>MultiHashMap</code>.
032: *
033: * @version $Revision: 209679 $ $Date: 2005-07-08 01:03:50 +0100 (Fri, 08 Jul 2005) $
034: *
035: * @author Unknown
036: */
037: public class TestMultiHashMap extends AbstractTestMap {
038:
039: public TestMultiHashMap(String testName) {
040: super (testName);
041: }
042:
043: public static Test suite() {
044: return new TestSuite(TestMultiHashMap.class);
045: }
046:
047: public static void main(String args[]) {
048: String[] testCaseName = { TestMultiHashMap.class.getName() };
049: junit.textui.TestRunner.main(testCaseName);
050: }
051:
052: // MutltiHashMap was introduced in Collections 2.x
053: public String getCompatibilityVersion() {
054: return "2";
055: }
056:
057: public Map makeEmptyMap() {
058: return new MultiHashMap();
059: }
060:
061: //----------------------------
062: // Tests
063: //----------------------------
064: public void testPutNGet() {
065: MultiHashMap map = new MultiHashMap();
066: loadMap(map);
067: checkMap(map);
068:
069: assertTrue(map.get(new Integer(99)) == null);
070:
071: map.clear();
072: assertTrue(map.size() == 0);
073: }
074:
075: public void testContainsValue() {
076: MultiHashMap map = new MultiHashMap();
077: loadMap(map);
078:
079: assertTrue(map.containsValue("uno"));
080: assertTrue(map.containsValue("quatro"));
081: assertTrue(map.containsValue("two"));
082:
083: assertTrue(!map.containsValue("uggaBugga"));
084:
085: map.clear();
086: }
087:
088: public void testValues() {
089: MultiHashMap map = new MultiHashMap();
090: loadMap(map);
091:
092: Collection vals = map.values();
093: assertTrue(vals.size() == getFullSize());
094:
095: map.clear();
096: }
097:
098: static private class MapPair {
099: MapPair(int key, String val) {
100: mKey = new Integer(key);
101: mValue = val;
102: }
103:
104: Integer mKey = null;
105: String mValue = null;
106: }
107:
108: static private MapPair[][] sMapPairs = {
109: { new MapPair(0, "zero") },
110: { new MapPair(1, "one"), new MapPair(1, "ONE"),
111: new MapPair(1, "uno") },
112: { new MapPair(2, "two"), new MapPair(2, "two") },
113: { new MapPair(3, "three"), new MapPair(3, "THREE"),
114: new MapPair(3, "tres") },
115: { new MapPair(4, "four"), new MapPair(4, "quatro") } };
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,
133: sMapPairs[jj][ii].mValue);
134: //---------------------------------------------------------
135: }
136: }
137: }
138: assertTrue(map.size() == sMapPairs.length);
139: }
140:
141: private void checkMap(MultiHashMap map) {
142: for (int ii = 0; ii < sMapPairs.length; ii++) {
143: checkKeyList(map, ii);
144: }
145: }
146:
147: private void checkKeyList(MultiHashMap map, int index) {
148: assertTrue(index < sMapPairs.length);
149: Integer key = sMapPairs[index][0].mKey;
150:
151: Object obj = map.get(key);
152: //--------------------------
153:
154: assertTrue(obj != null);
155: assertTrue(obj instanceof Collection);
156: Collection keyList = (Collection) obj;
157:
158: assertTrue(keyList.size() == sMapPairs[index].length);
159: Iterator iter = keyList.iterator();
160: while (iter.hasNext()) {
161: Object oval = iter.next();
162: assertTrue(oval != null);
163: assertTrue(oval instanceof String);
164: String val = (String) oval;
165: boolean foundIt = false;
166: for (int ii = 0; ii < sMapPairs[index].length; ii++) {
167: if (val.equals(sMapPairs[index][ii].mValue)) {
168: foundIt = true;
169: }
170: }
171: assertTrue(foundIt);
172: }
173: }
174:
175: public int getFullSize() {
176: int len = 0;
177: for (int ii = 0; ii < sMapPairs.length; ii++) {
178: len += sMapPairs[ii].length;
179: }
180: return len;
181: }
182:
183: public void testEntrySetIterator() {
184: }
185:
186: public void testEntrySetContainsProperMappings() {
187: }
188:
189: public void testEntrySetIteratorHasProperMappings() {
190: // override and ignore test -- it will fail when verifying the iterator for
191: // the set contains the right value -- we're not returning the value, we're
192: // returning a collection.
193: // TODO: re-implement this test to ensure the values of the iterator match
194: // the proper collection rather than the value the superclass is checking
195: // for.
196: return;
197: }
198:
199: // Next methods are overriden because MultiHashMap values are always a
200: // collection, and deviate from the Map contract because of this.
201:
202: // TODO: implement the tests to ensure that Map.get(Object) returns the
203: // appropriate collection of values
204:
205: public void testMapGet() {
206: }
207:
208: public void testMapPut() {
209: }
210:
211: public void testMapPutAll() {
212: }
213:
214: public void testMapRemove() {
215: }
216:
217: public void testMapEquals() {
218: MultiHashMap one = new MultiHashMap();
219: Integer value = new Integer(1);
220: one.put("One", value);
221: one.remove("One", value);
222:
223: MultiHashMap two = new MultiHashMap();
224: assertEquals(two, one);
225: }
226:
227: public void testMapHashCode() {
228: }
229:
230: // The verification for the map and its entry set must also be overridden
231: // because the values are not going to be the same as the values in the
232: // confirmed map (they're going to be collections of values instead).
233: public void verifyMap() {
234: // TODO: implement test to ensure that map is the same as confirmed if
235: // its values were converted into collections.
236: }
237:
238: public void verifyEntrySet() {
239: // TODO: implement test to ensure that each entry is the same as one in
240: // the confirmed map, but with the value wrapped in a collection.
241: }
242:
243: // The verification method must be overridden because MultiHashMap's
244: // values() is not properly backed by the map (Bug 9573).
245:
246: public void verifyValues() {
247: // update the values view to the latest version, then proceed to verify
248: // as usual.
249: values = map.values();
250: super .verifyValues();
251: }
252:
253: //-----------------------------------------------------------------------
254: public void testGetCollection() {
255: MultiHashMap map = new MultiHashMap();
256: map.put("A", "AA");
257: assertSame(map.get("A"), map.getCollection("A"));
258: }
259:
260: public void testTotalSize() {
261: MultiHashMap map = new MultiHashMap();
262: assertEquals(0, map.totalSize());
263: map.put("A", "AA");
264: assertEquals(1, map.totalSize());
265: map.put("B", "BA");
266: assertEquals(2, map.totalSize());
267: map.put("B", "BB");
268: assertEquals(3, map.totalSize());
269: map.put("B", "BC");
270: assertEquals(4, map.totalSize());
271: map.remove("A");
272: assertEquals(3, map.totalSize());
273: map.remove("B", "BC");
274: assertEquals(2, map.totalSize());
275: }
276:
277: public void testSize_Key() {
278: MultiHashMap map = new MultiHashMap();
279: assertEquals(0, map.size("A"));
280: assertEquals(0, map.size("B"));
281: map.put("A", "AA");
282: assertEquals(1, map.size("A"));
283: assertEquals(0, map.size("B"));
284: map.put("B", "BA");
285: assertEquals(1, map.size("A"));
286: assertEquals(1, map.size("B"));
287: map.put("B", "BB");
288: assertEquals(1, map.size("A"));
289: assertEquals(2, map.size("B"));
290: map.put("B", "BC");
291: assertEquals(1, map.size("A"));
292: assertEquals(3, map.size("B"));
293: map.remove("A");
294: assertEquals(0, map.size("A"));
295: assertEquals(3, map.size("B"));
296: map.remove("B", "BC");
297: assertEquals(0, map.size("A"));
298: assertEquals(2, map.size("B"));
299: }
300:
301: public void testIterator_Key() {
302: MultiHashMap map = new MultiHashMap();
303: assertEquals(false, map.iterator("A").hasNext());
304: map.put("A", "AA");
305: Iterator it = map.iterator("A");
306: assertEquals(true, it.hasNext());
307: it.next();
308: assertEquals(false, it.hasNext());
309: }
310:
311: public void testContainsValue_Key() {
312: MultiHashMap map = new MultiHashMap();
313: assertEquals(false, map.containsValue("A", "AA"));
314: assertEquals(false, map.containsValue("B", "BB"));
315: map.put("A", "AA");
316: assertEquals(true, map.containsValue("A", "AA"));
317: assertEquals(false, map.containsValue("A", "AB"));
318: }
319:
320: public void testPutAll_Map1() {
321: MultiMap original = new MultiHashMap();
322: original.put("key", "object1");
323: original.put("key", "object2");
324:
325: MultiHashMap test = new MultiHashMap();
326: test.put("keyA", "objectA");
327: test.put("key", "object0");
328: test.putAll(original);
329:
330: assertEquals(2, test.size());
331: assertEquals(4, test.totalSize());
332: assertEquals(1, test.getCollection("keyA").size());
333: assertEquals(3, test.getCollection("key").size());
334: assertEquals(true, test.containsValue("objectA"));
335: assertEquals(true, test.containsValue("object0"));
336: assertEquals(true, test.containsValue("object1"));
337: assertEquals(true, test.containsValue("object2"));
338: }
339:
340: public void testPutAll_Map2() {
341: Map original = new HashMap();
342: original.put("keyX", "object1");
343: original.put("keyY", "object2");
344:
345: MultiHashMap test = new MultiHashMap();
346: test.put("keyA", "objectA");
347: test.put("keyX", "object0");
348: test.putAll(original);
349:
350: assertEquals(3, test.size());
351: assertEquals(4, test.totalSize());
352: assertEquals(1, test.getCollection("keyA").size());
353: assertEquals(2, test.getCollection("keyX").size());
354: assertEquals(1, test.getCollection("keyY").size());
355: assertEquals(true, test.containsValue("objectA"));
356: assertEquals(true, test.containsValue("object0"));
357: assertEquals(true, test.containsValue("object1"));
358: assertEquals(true, test.containsValue("object2"));
359: }
360:
361: public void testPutAll_KeyCollection() {
362: MultiHashMap map = new MultiHashMap();
363: Collection coll = Arrays.asList(new Object[] { "X", "Y", "Z" });
364:
365: assertEquals(true, map.putAll("A", coll));
366: assertEquals(3, map.size("A"));
367: assertEquals(true, map.containsValue("A", "X"));
368: assertEquals(true, map.containsValue("A", "Y"));
369: assertEquals(true, map.containsValue("A", "Z"));
370:
371: assertEquals(false, map.putAll("A", null));
372: assertEquals(3, map.size("A"));
373: assertEquals(true, map.containsValue("A", "X"));
374: assertEquals(true, map.containsValue("A", "Y"));
375: assertEquals(true, map.containsValue("A", "Z"));
376:
377: assertEquals(false, map.putAll("A", new ArrayList()));
378: assertEquals(3, map.size("A"));
379: assertEquals(true, map.containsValue("A", "X"));
380: assertEquals(true, map.containsValue("A", "Y"));
381: assertEquals(true, map.containsValue("A", "Z"));
382:
383: coll = Arrays.asList(new Object[] { "M" });
384: assertEquals(true, map.putAll("A", coll));
385: assertEquals(4, map.size("A"));
386: assertEquals(true, map.containsValue("A", "X"));
387: assertEquals(true, map.containsValue("A", "Y"));
388: assertEquals(true, map.containsValue("A", "Z"));
389: assertEquals(true, map.containsValue("A", "M"));
390: }
391:
392: public void testClone() {
393: MultiHashMap map = new MultiHashMap();
394: map.put("A", "1");
395: map.put("A", "2");
396: Collection coll = (Collection) map.get("A");
397: assertEquals(1, map.size());
398: assertEquals(2, coll.size());
399:
400: MultiHashMap cloned = (MultiHashMap) map.clone();
401: Collection clonedColl = (Collection) cloned.get("A");
402: assertNotSame(map, cloned);
403: assertNotSame(coll, clonedColl);
404: assertEquals(1, map.size());
405: assertEquals(2, coll.size());
406: assertEquals(1, cloned.size());
407: assertEquals(2, clonedColl.size());
408: map.put("A", "3");
409: assertEquals(1, map.size());
410: assertEquals(3, coll.size());
411: assertEquals(1, cloned.size());
412: assertEquals(2, clonedColl.size());
413: }
414:
415: public void testConstructorCopy1() {
416: MultiHashMap map = new MultiHashMap();
417: map.put("A", "1");
418: map.put("A", "2");
419: Collection coll = (Collection) map.get("A");
420: assertEquals(1, map.size());
421: assertEquals(2, coll.size());
422:
423: MultiHashMap newMap = new MultiHashMap(map);
424: Collection newColl = (Collection) newMap.get("A");
425: assertNotSame(map, newMap);
426: assertNotSame(coll, newColl);
427: assertEquals(1, map.size());
428: assertEquals(2, coll.size());
429: assertEquals(1, newMap.size());
430: assertEquals(2, newColl.size());
431:
432: map.put("A", "3");
433: assertEquals(1, map.size());
434: assertEquals(3, coll.size());
435: assertEquals(1, newMap.size());
436: assertEquals(2, newColl.size());
437: }
438:
439: public void testConstructorCopy2() {
440: Map map = new HashMap();
441: map.put("A", "1");
442: map.put("B", "2");
443: assertEquals(2, map.size());
444:
445: MultiHashMap newMap = new MultiHashMap(map);
446: Collection newColl = (Collection) newMap.get("A");
447: assertNotSame(map, newMap);
448: assertEquals(2, map.size());
449: assertEquals(2, newMap.size());
450: assertEquals(1, newColl.size());
451:
452: map.put("A", "3");
453: assertEquals(2, map.size());
454: assertEquals(2, newMap.size());
455: assertEquals(1, newColl.size());
456:
457: map.put("C", "4");
458: assertEquals(3, map.size());
459: assertEquals(2, newMap.size());
460: assertEquals(1, newColl.size());
461: }
462:
463: public void testRemove_KeyItem() {
464: MultiHashMap map = new MultiHashMap();
465: map.put("A", "AA");
466: map.put("A", "AB");
467: map.put("A", "AC");
468: assertEquals(null, map.remove("C", "CA"));
469: assertEquals(null, map.remove("A", "AD"));
470: assertEquals("AC", map.remove("A", "AC"));
471: assertEquals("AB", map.remove("A", "AB"));
472: assertEquals("AA", map.remove("A", "AA"));
473: assertEquals(new MultiHashMap(), map);
474: }
475:
476: }
|