001: /*
002: * Copyright 2004 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.map;
017:
018: import java.util.Map;
019:
020: import junit.framework.Test;
021: import junit.textui.TestRunner;
022:
023: import org.apache.commons.collections.BulkTest;
024: import org.apache.commons.collections.MapIterator;
025: import org.apache.commons.collections.keyvalue.MultiKey;
026:
027: /**
028: * JUnit tests.
029: *
030: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
031: *
032: * @author Stephen Colebourne
033: */
034: public class TestMultiKeyMap extends AbstractTestIterableMap {
035:
036: static final Integer I1 = new Integer(1);
037: static final Integer I2 = new Integer(2);
038: static final Integer I3 = new Integer(3);
039: static final Integer I4 = new Integer(4);
040: static final Integer I5 = new Integer(5);
041: static final Integer I6 = new Integer(6);
042: static final Integer I7 = new Integer(7);
043: static final Integer I8 = new Integer(8);
044:
045: public TestMultiKeyMap(String testName) {
046: super (testName);
047: }
048:
049: public static void main(String[] args) {
050: TestRunner.run(suite());
051: }
052:
053: public static Test suite() {
054: return BulkTest.makeSuite(TestMultiKeyMap.class);
055: }
056:
057: public Map makeEmptyMap() {
058: return new MultiKeyMap();
059: }
060:
061: public Object[] getSampleKeys() {
062: return getMultiKeyKeys();
063: }
064:
065: private MultiKey[] getMultiKeyKeys() {
066: return new MultiKey[] { new MultiKey(I1, I2),
067: new MultiKey(I2, I3), new MultiKey(I3, I4),
068: new MultiKey(I1, I1, I2), new MultiKey(I2, I3, I4),
069: new MultiKey(I3, I7, I6), new MultiKey(I1, I1, I2, I3),
070: new MultiKey(I2, I4, I5, I6),
071: new MultiKey(I3, I6, I7, I8),
072: new MultiKey(I1, I1, I2, I3, I4),
073: new MultiKey(I2, I3, I4, I5, I6),
074: new MultiKey(I3, I5, I6, I7, I8), };
075: }
076:
077: public Object[] getSampleValues() {
078: return new Object[] { "2A", "2B", "2C", "3D", "3E", "3F", "4G",
079: "4H", "4I", "5J", "5K", "5L", };
080: }
081:
082: public Object[] getNewSampleValues() {
083: return new Object[] { "1a", "1b", "1c", "2d", "2e", "2f", "3g",
084: "3h", "3i", "4j", "4k", "4l", };
085: }
086:
087: public Object[] getOtherKeys() {
088: return new Object[] { new MultiKey(I1, I7),
089: new MultiKey(I1, I8), new MultiKey(I2, I4),
090: new MultiKey(I2, I5), };
091: }
092:
093: public boolean isAllowNullKey() {
094: return false;
095: }
096:
097: //-----------------------------------------------------------------------
098: public void testNullHandling() {
099: resetFull();
100: assertEquals(null, map.get(null));
101: assertEquals(false, map.containsKey(null));
102: assertEquals(false, map.containsValue(null));
103: assertEquals(null, map.remove(null));
104: assertEquals(false, map.entrySet().contains(null));
105: assertEquals(false, map.keySet().contains(null));
106: assertEquals(false, map.values().contains(null));
107: try {
108: map.put(null, null);
109: fail();
110: } catch (NullPointerException ex) {
111: }
112: assertEquals(null, map.put(new MultiKey(null, null), null));
113: try {
114: map.put(null, new Object());
115: fail();
116: } catch (NullPointerException ex) {
117: }
118: }
119:
120: //-----------------------------------------------------------------------
121: public void testMultiKeyGet() {
122: resetFull();
123: MultiKeyMap multimap = (MultiKeyMap) map;
124: MultiKey[] keys = getMultiKeyKeys();
125: Object[] values = getSampleValues();
126:
127: for (int i = 0; i < keys.length; i++) {
128: MultiKey key = keys[i];
129: Object value = values[i];
130:
131: switch (key.size()) {
132: case 2:
133: assertEquals(value, multimap.get(key.getKey(0), key
134: .getKey(1)));
135: assertEquals(null, multimap.get(null, key.getKey(1)));
136: assertEquals(null, multimap.get(key.getKey(0), null));
137: assertEquals(null, multimap.get(null, null));
138: assertEquals(null, multimap.get(key.getKey(0), key
139: .getKey(1), null));
140: assertEquals(null, multimap.get(key.getKey(0), key
141: .getKey(1), null, null));
142: assertEquals(null, multimap.get(key.getKey(0), key
143: .getKey(1), null, null, null));
144: break;
145: case 3:
146: assertEquals(value, multimap.get(key.getKey(0), key
147: .getKey(1), key.getKey(2)));
148: assertEquals(null, multimap.get(null, key.getKey(1),
149: key.getKey(2)));
150: assertEquals(null, multimap.get(key.getKey(0), null,
151: key.getKey(2)));
152: assertEquals(null, multimap.get(key.getKey(0), key
153: .getKey(1), null));
154: assertEquals(null, multimap.get(null, null, null));
155: assertEquals(null, multimap.get(key.getKey(0), key
156: .getKey(1), key.getKey(2), null));
157: assertEquals(null, multimap.get(key.getKey(0), key
158: .getKey(1), key.getKey(2), null, null));
159: break;
160: case 4:
161: assertEquals(value, multimap.get(key.getKey(0), key
162: .getKey(1), key.getKey(2), key.getKey(3)));
163: assertEquals(null, multimap.get(null, key.getKey(1),
164: key.getKey(2), key.getKey(3)));
165: assertEquals(null, multimap.get(key.getKey(0), null,
166: key.getKey(2), key.getKey(3)));
167: assertEquals(null, multimap.get(key.getKey(0), key
168: .getKey(1), null, key.getKey(3)));
169: assertEquals(null, multimap.get(key.getKey(0), key
170: .getKey(1), key.getKey(2), null));
171: assertEquals(null, multimap.get(null, null, null, null));
172: assertEquals(null, multimap.get(key.getKey(0), key
173: .getKey(1), key.getKey(2), key.getKey(3), null));
174: break;
175: case 5:
176: assertEquals(value, multimap.get(key.getKey(0), key
177: .getKey(1), key.getKey(2), key.getKey(3), key
178: .getKey(4)));
179: assertEquals(null, multimap.get(null, key.getKey(1),
180: key.getKey(2), key.getKey(3), key.getKey(4)));
181: assertEquals(null, multimap.get(key.getKey(0), null,
182: key.getKey(2), key.getKey(3), key.getKey(4)));
183: assertEquals(null, multimap.get(key.getKey(0), key
184: .getKey(1), null, key.getKey(3), key.getKey(4)));
185: assertEquals(null, multimap.get(key.getKey(0), key
186: .getKey(1), key.getKey(2), null, key.getKey(4)));
187: assertEquals(null, multimap.get(key.getKey(0), key
188: .getKey(1), key.getKey(2), key.getKey(3), null));
189: assertEquals(null, multimap.get(null, null, null, null,
190: null));
191: break;
192: default:
193: fail("Invalid key size");
194: }
195: }
196: }
197:
198: public void testMultiKeyContainsKey() {
199: resetFull();
200: MultiKeyMap multimap = (MultiKeyMap) map;
201: MultiKey[] keys = getMultiKeyKeys();
202: Object[] values = getSampleValues();
203:
204: for (int i = 0; i < keys.length; i++) {
205: MultiKey key = keys[i];
206: Object value = values[i];
207:
208: switch (key.size()) {
209: case 2:
210: assertEquals(true, multimap.containsKey(key.getKey(0),
211: key.getKey(1)));
212: assertEquals(false, multimap.containsKey(null, key
213: .getKey(1)));
214: assertEquals(false, multimap.containsKey(key.getKey(0),
215: null));
216: assertEquals(false, multimap.containsKey(null, null));
217: assertEquals(false, multimap.containsKey(key.getKey(0),
218: key.getKey(1), null));
219: assertEquals(false, multimap.containsKey(key.getKey(0),
220: key.getKey(1), null, null));
221: assertEquals(false, multimap.containsKey(key.getKey(0),
222: key.getKey(1), null, null, null));
223: break;
224: case 3:
225: assertEquals(true, multimap.containsKey(key.getKey(0),
226: key.getKey(1), key.getKey(2)));
227: assertEquals(false, multimap.containsKey(null, key
228: .getKey(1), key.getKey(2)));
229: assertEquals(false, multimap.containsKey(key.getKey(0),
230: null, key.getKey(2)));
231: assertEquals(false, multimap.containsKey(key.getKey(0),
232: key.getKey(1), null));
233: assertEquals(false, multimap.containsKey(null, null,
234: null));
235: assertEquals(false, multimap.containsKey(key.getKey(0),
236: key.getKey(1), key.getKey(2), null));
237: assertEquals(false, multimap.containsKey(key.getKey(0),
238: key.getKey(1), key.getKey(2), null, null));
239: break;
240: case 4:
241: assertEquals(true, multimap.containsKey(key.getKey(0),
242: key.getKey(1), key.getKey(2), key.getKey(3)));
243: assertEquals(false, multimap.containsKey(null, key
244: .getKey(1), key.getKey(2), key.getKey(3)));
245: assertEquals(false, multimap.containsKey(key.getKey(0),
246: null, key.getKey(2), key.getKey(3)));
247: assertEquals(false, multimap.containsKey(key.getKey(0),
248: key.getKey(1), null, key.getKey(3)));
249: assertEquals(false, multimap.containsKey(key.getKey(0),
250: key.getKey(1), key.getKey(2), null));
251: assertEquals(false, multimap.containsKey(null, null,
252: null, null));
253: assertEquals(false, multimap.containsKey(key.getKey(0),
254: key.getKey(1), key.getKey(2), key.getKey(3),
255: null));
256: break;
257: case 5:
258: assertEquals(true, multimap.containsKey(key.getKey(0),
259: key.getKey(1), key.getKey(2), key.getKey(3),
260: key.getKey(4)));
261: assertEquals(false, multimap.containsKey(null, key
262: .getKey(1), key.getKey(2), key.getKey(3), key
263: .getKey(4)));
264: assertEquals(false, multimap.containsKey(key.getKey(0),
265: null, key.getKey(2), key.getKey(3), key
266: .getKey(4)));
267: assertEquals(false, multimap.containsKey(key.getKey(0),
268: key.getKey(1), null, key.getKey(3), key
269: .getKey(4)));
270: assertEquals(false, multimap.containsKey(key.getKey(0),
271: key.getKey(1), key.getKey(2), null, key
272: .getKey(4)));
273: assertEquals(false, multimap.containsKey(key.getKey(0),
274: key.getKey(1), key.getKey(2), key.getKey(3),
275: null));
276: assertEquals(false, multimap.containsKey(null, null,
277: null, null, null));
278: break;
279: default:
280: fail("Invalid key size");
281: }
282: }
283: }
284:
285: public void testMultiKeyPut() {
286: MultiKey[] keys = getMultiKeyKeys();
287: Object[] values = getSampleValues();
288:
289: for (int i = 0; i < keys.length; i++) {
290: MultiKeyMap multimap = new MultiKeyMap();
291:
292: MultiKey key = keys[i];
293: Object value = values[i];
294:
295: switch (key.size()) {
296: case 2:
297: assertEquals(null, multimap.put(key.getKey(0), key
298: .getKey(1), value));
299: assertEquals(1, multimap.size());
300: assertEquals(value, multimap.get(key.getKey(0), key
301: .getKey(1)));
302: assertEquals(true, multimap.containsKey(key.getKey(0),
303: key.getKey(1)));
304: assertEquals(true, multimap.containsKey(new MultiKey(
305: key.getKey(0), key.getKey(1))));
306: assertEquals(value, multimap.put(key.getKey(0), key
307: .getKey(1), null));
308: assertEquals(1, multimap.size());
309: assertEquals(null, multimap.get(key.getKey(0), key
310: .getKey(1)));
311: assertEquals(true, multimap.containsKey(key.getKey(0),
312: key.getKey(1)));
313: break;
314: case 3:
315: assertEquals(null, multimap.put(key.getKey(0), key
316: .getKey(1), key.getKey(2), value));
317: assertEquals(1, multimap.size());
318: assertEquals(value, multimap.get(key.getKey(0), key
319: .getKey(1), key.getKey(2)));
320: assertEquals(true, multimap.containsKey(key.getKey(0),
321: key.getKey(1), key.getKey(2)));
322: assertEquals(true, multimap.containsKey(new MultiKey(
323: key.getKey(0), key.getKey(1), key.getKey(2))));
324: assertEquals(value, multimap.put(key.getKey(0), key
325: .getKey(1), key.getKey(2), null));
326: assertEquals(1, multimap.size());
327: assertEquals(null, multimap.get(key.getKey(0), key
328: .getKey(1), key.getKey(2)));
329: assertEquals(true, multimap.containsKey(key.getKey(0),
330: key.getKey(1), key.getKey(2)));
331: break;
332: case 4:
333: assertEquals(null, multimap
334: .put(key.getKey(0), key.getKey(1), key
335: .getKey(2), key.getKey(3), value));
336: assertEquals(1, multimap.size());
337: assertEquals(value, multimap.get(key.getKey(0), key
338: .getKey(1), key.getKey(2), key.getKey(3)));
339: assertEquals(true, multimap.containsKey(key.getKey(0),
340: key.getKey(1), key.getKey(2), key.getKey(3)));
341: assertEquals(true, multimap.containsKey(new MultiKey(
342: key.getKey(0), key.getKey(1), key.getKey(2),
343: key.getKey(3))));
344: assertEquals(value, multimap.put(key.getKey(0), key
345: .getKey(1), key.getKey(2), key.getKey(3), null));
346: assertEquals(1, multimap.size());
347: assertEquals(null, multimap.get(key.getKey(0), key
348: .getKey(1), key.getKey(2), key.getKey(3)));
349: assertEquals(true, multimap.containsKey(key.getKey(0),
350: key.getKey(1), key.getKey(2), key.getKey(3)));
351: break;
352: case 5:
353: assertEquals(null, multimap.put(key.getKey(0), key
354: .getKey(1), key.getKey(2), key.getKey(3), key
355: .getKey(4), value));
356: assertEquals(1, multimap.size());
357: assertEquals(value, multimap.get(key.getKey(0), key
358: .getKey(1), key.getKey(2), key.getKey(3), key
359: .getKey(4)));
360: assertEquals(true, multimap.containsKey(key.getKey(0),
361: key.getKey(1), key.getKey(2), key.getKey(3),
362: key.getKey(4)));
363: assertEquals(true, multimap.containsKey(new MultiKey(
364: key.getKey(0), key.getKey(1), key.getKey(2),
365: key.getKey(3), key.getKey(4))));
366: assertEquals(value, multimap.put(key.getKey(0), key
367: .getKey(1), key.getKey(2), key.getKey(3), key
368: .getKey(4), null));
369: assertEquals(1, multimap.size());
370: assertEquals(null, multimap.get(key.getKey(0), key
371: .getKey(1), key.getKey(2), key.getKey(3), key
372: .getKey(4)));
373: assertEquals(true, multimap.containsKey(key.getKey(0),
374: key.getKey(1), key.getKey(2), key.getKey(3),
375: key.getKey(4)));
376: break;
377: default:
378: fail("Invalid key size");
379: }
380: }
381: }
382:
383: public void testMultiKeyRemove() {
384: MultiKey[] keys = getMultiKeyKeys();
385: Object[] values = getSampleValues();
386:
387: for (int i = 0; i < keys.length; i++) {
388: resetFull();
389: MultiKeyMap multimap = (MultiKeyMap) map;
390: int size = multimap.size();
391:
392: MultiKey key = keys[i];
393: Object value = values[i];
394:
395: switch (key.size()) {
396: case 2:
397: assertEquals(true, multimap.containsKey(key.getKey(0),
398: key.getKey(1)));
399: assertEquals(value, multimap.remove(key.getKey(0), key
400: .getKey(1)));
401: assertEquals(false, multimap.containsKey(key.getKey(0),
402: key.getKey(1)));
403: assertEquals(size - 1, multimap.size());
404: assertEquals(null, multimap.remove(key.getKey(0), key
405: .getKey(1)));
406: assertEquals(false, multimap.containsKey(key.getKey(0),
407: key.getKey(1)));
408: break;
409: case 3:
410: assertEquals(true, multimap.containsKey(key.getKey(0),
411: key.getKey(1), key.getKey(2)));
412: assertEquals(value, multimap.remove(key.getKey(0), key
413: .getKey(1), key.getKey(2)));
414: assertEquals(false, multimap.containsKey(key.getKey(0),
415: key.getKey(1), key.getKey(2)));
416: assertEquals(size - 1, multimap.size());
417: assertEquals(null, multimap.remove(key.getKey(0), key
418: .getKey(1), key.getKey(2)));
419: assertEquals(false, multimap.containsKey(key.getKey(0),
420: key.getKey(1), key.getKey(2)));
421: break;
422: case 4:
423: assertEquals(true, multimap.containsKey(key.getKey(0),
424: key.getKey(1), key.getKey(2), key.getKey(3)));
425: assertEquals(value, multimap.remove(key.getKey(0), key
426: .getKey(1), key.getKey(2), key.getKey(3)));
427: assertEquals(false, multimap.containsKey(key.getKey(0),
428: key.getKey(1), key.getKey(2), key.getKey(3)));
429: assertEquals(size - 1, multimap.size());
430: assertEquals(null, multimap.remove(key.getKey(0), key
431: .getKey(1), key.getKey(2), key.getKey(3)));
432: assertEquals(false, multimap.containsKey(key.getKey(0),
433: key.getKey(1), key.getKey(2), key.getKey(3)));
434: break;
435: case 5:
436: assertEquals(true, multimap.containsKey(key.getKey(0),
437: key.getKey(1), key.getKey(2), key.getKey(3),
438: key.getKey(4)));
439: assertEquals(value, multimap.remove(key.getKey(0), key
440: .getKey(1), key.getKey(2), key.getKey(3), key
441: .getKey(4)));
442: assertEquals(false, multimap.containsKey(key.getKey(0),
443: key.getKey(1), key.getKey(2), key.getKey(3),
444: key.getKey(4)));
445: assertEquals(size - 1, multimap.size());
446: assertEquals(null, multimap.remove(key.getKey(0), key
447: .getKey(1), key.getKey(2), key.getKey(3), key
448: .getKey(4)));
449: assertEquals(false, multimap.containsKey(key.getKey(0),
450: key.getKey(1), key.getKey(2), key.getKey(3),
451: key.getKey(4)));
452: break;
453: default:
454: fail("Invalid key size");
455: }
456: }
457: }
458:
459: public void testMultiKeyRemoveAll1() {
460: resetFull();
461: MultiKeyMap multimap = (MultiKeyMap) map;
462: assertEquals(12, multimap.size());
463:
464: multimap.removeAll(I1);
465: assertEquals(8, multimap.size());
466: for (MapIterator it = multimap.mapIterator(); it.hasNext();) {
467: MultiKey key = (MultiKey) it.next();
468: assertEquals(false, I1.equals(key.getKey(0)));
469: }
470: }
471:
472: public void testMultiKeyRemoveAll2() {
473: resetFull();
474: MultiKeyMap multimap = (MultiKeyMap) map;
475: assertEquals(12, multimap.size());
476:
477: multimap.removeAll(I2, I3);
478: assertEquals(9, multimap.size());
479: for (MapIterator it = multimap.mapIterator(); it.hasNext();) {
480: MultiKey key = (MultiKey) it.next();
481: assertEquals(false, I2.equals(key.getKey(0))
482: && I3.equals(key.getKey(1)));
483: }
484: }
485:
486: public void testMultiKeyRemoveAll3() {
487: resetFull();
488: MultiKeyMap multimap = (MultiKeyMap) map;
489: assertEquals(12, multimap.size());
490:
491: multimap.removeAll(I1, I1, I2);
492: assertEquals(9, multimap.size());
493: for (MapIterator it = multimap.mapIterator(); it.hasNext();) {
494: MultiKey key = (MultiKey) it.next();
495: assertEquals(false, I1.equals(key.getKey(0))
496: && I1.equals(key.getKey(1))
497: && I2.equals(key.getKey(2)));
498: }
499: }
500:
501: public void testMultiKeyRemoveAll4() {
502: resetFull();
503: MultiKeyMap multimap = (MultiKeyMap) map;
504: assertEquals(12, multimap.size());
505:
506: multimap.removeAll(I1, I1, I2, I3);
507: assertEquals(10, multimap.size());
508: for (MapIterator it = multimap.mapIterator(); it.hasNext();) {
509: MultiKey key = (MultiKey) it.next();
510: assertEquals(false, I1.equals(key.getKey(0))
511: && I1.equals(key.getKey(1))
512: && I2.equals(key.getKey(2)) && key.size() >= 4
513: && I3.equals(key.getKey(3)));
514: }
515: }
516:
517: //-----------------------------------------------------------------------
518: public void testClone() {
519: MultiKeyMap map = new MultiKeyMap();
520: map.put(new MultiKey(I1, I2), "1-2");
521: Map cloned = (Map) map.clone();
522: assertEquals(map.size(), cloned.size());
523: assertSame(map.get(new MultiKey(I1, I2)), cloned
524: .get(new MultiKey(I1, I2)));
525: }
526:
527: //-----------------------------------------------------------------------
528: public void testLRUMultiKeyMap() {
529: MultiKeyMap map = MultiKeyMap.decorate(new LRUMap(2));
530: map.put(I1, I2, "1-2");
531: map.put(I1, I3, "1-3");
532: assertEquals(2, map.size());
533: map.put(I1, I4, "1-4");
534: assertEquals(2, map.size());
535: assertEquals(true, map.containsKey(I1, I3));
536: assertEquals(true, map.containsKey(I1, I4));
537: assertEquals(false, map.containsKey(I1, I2));
538:
539: MultiKeyMap cloned = (MultiKeyMap) map.clone();
540: assertEquals(2, map.size());
541: assertEquals(true, cloned.containsKey(I1, I3));
542: assertEquals(true, cloned.containsKey(I1, I4));
543: assertEquals(false, cloned.containsKey(I1, I2));
544: cloned.put(I1, I5, "1-5");
545: assertEquals(2, cloned.size());
546: assertEquals(true, cloned.containsKey(I1, I4));
547: assertEquals(true, cloned.containsKey(I1, I5));
548: }
549:
550: //-----------------------------------------------------------------------
551: public String getCompatibilityVersion() {
552: return "3.1";
553: }
554:
555: // public void testCreate() throws Exception {
556: // resetEmpty();
557: // writeExternalFormToDisk(
558: // (java.io.Serializable) map,
559: // "D:/dev/collections/data/test/MultiKeyMap.emptyCollection.version3.1.obj");
560: // resetFull();
561: // writeExternalFormToDisk(
562: // (java.io.Serializable) map,
563: // "D:/dev/collections/data/test/MultiKeyMap.fullCollection.version3.1.obj");
564: // }
565: }
|