KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > TestMultiKeyMap


1 /*
2  * Copyright 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.map;
17
18 import java.util.Map JavaDoc;
19
20 import junit.framework.Test;
21 import junit.textui.TestRunner;
22
23 import org.apache.commons.collections.BulkTest;
24 import org.apache.commons.collections.MapIterator;
25 import org.apache.commons.collections.keyvalue.MultiKey;
26
27 /**
28  * JUnit tests.
29  *
30  * @version $Revision: 1.2 $ $Date: 2004/04/30 23:51:36 $
31  *
32  * @author Stephen Colebourne
33  */

34 public class TestMultiKeyMap extends AbstractTestIterableMap {
35     
36     static final Integer JavaDoc I1 = new Integer JavaDoc(1);
37     static final Integer JavaDoc I2 = new Integer JavaDoc(2);
38     static final Integer JavaDoc I3 = new Integer JavaDoc(3);
39     static final Integer JavaDoc I4 = new Integer JavaDoc(4);
40     static final Integer JavaDoc I5 = new Integer JavaDoc(5);
41     static final Integer JavaDoc I6 = new Integer JavaDoc(6);
42     static final Integer JavaDoc I7 = new Integer JavaDoc(7);
43     static final Integer JavaDoc I8 = new Integer JavaDoc(8);
44
45     public TestMultiKeyMap(String JavaDoc testName) {
46         super(testName);
47     }
48
49     public static void main(String JavaDoc[] args) {
50         TestRunner.run(suite());
51     }
52
53     public static Test suite() {
54         return BulkTest.makeSuite(TestMultiKeyMap.class);
55     }
56
57     public Map JavaDoc makeEmptyMap() {
58         return new MultiKeyMap();
59     }
60
61     public Object JavaDoc[] getSampleKeys() {
62         return getMultiKeyKeys();
63     }
64
65     private MultiKey[] getMultiKeyKeys() {
66         return new MultiKey[] {
67             new MultiKey(I1, I2),
68             new MultiKey(I2, I3),
69             new MultiKey(I3, I4),
70             new MultiKey(I1, I1, I2),
71             new MultiKey(I2, I3, I4),
72             new MultiKey(I3, I7, I6),
73             new MultiKey(I1, I1, I2, I3),
74             new MultiKey(I2, I4, I5, I6),
75             new MultiKey(I3, I6, I7, I8),
76             new MultiKey(I1, I1, I2, I3, I4),
77             new MultiKey(I2, I3, I4, I5, I6),
78             new MultiKey(I3, I5, I6, I7, I8),
79         };
80     }
81
82     public Object JavaDoc[] getSampleValues() {
83         return new Object JavaDoc[] {
84             "2A", "2B", "2C",
85             "3D", "3E", "3F",
86             "4G", "4H", "4I",
87             "5J", "5K", "5L",
88         };
89     }
90
91     public Object JavaDoc[] getNewSampleValues() {
92         return new Object JavaDoc[] {
93             "1a", "1b", "1c",
94             "2d", "2e", "2f",
95             "3g", "3h", "3i",
96             "4j", "4k", "4l",
97         };
98     }
99
100     public Object JavaDoc[] getOtherKeys() {
101         return new Object JavaDoc[] {
102             new MultiKey(I1, I7),
103             new MultiKey(I1, I8),
104             new MultiKey(I2, I4),
105             new MultiKey(I2, I5),
106         };
107     }
108     
109     public boolean isAllowNullKey() {
110         return false;
111     }
112
113     //-----------------------------------------------------------------------
114
public void testNullHandling() {
115         resetFull();
116         assertEquals(null, map.get(null));
117         assertEquals(false, map.containsKey(null));
118         assertEquals(false, map.containsValue(null));
119         assertEquals(null, map.remove(null));
120         assertEquals(false, map.entrySet().contains(null));
121         assertEquals(false, map.keySet().contains(null));
122         assertEquals(false, map.values().contains(null));
123         try {
124             map.put(null, null);
125             fail();
126         } catch (NullPointerException JavaDoc ex) {}
127         assertEquals(null, map.put(new MultiKey(null, null), null));
128         try {
129             map.put(null, new Object JavaDoc());
130             fail();
131         } catch (NullPointerException JavaDoc ex) {}
132     }
133
134     //-----------------------------------------------------------------------
135
public void testMultiKeyGet() {
136         resetFull();
137         MultiKeyMap multimap = (MultiKeyMap) map;
138         MultiKey[] keys = getMultiKeyKeys();
139         Object JavaDoc[] values = getSampleValues();
140         
141         for (int i = 0; i < keys.length; i++) {
142             MultiKey key = keys[i];
143             Object JavaDoc value = values[i];
144             
145             switch (key.size()) {
146                 case 2:
147                 assertEquals(value, multimap.get(key.getKey(0), key.getKey(1)));
148                 assertEquals(null, multimap.get(null, key.getKey(1)));
149                 assertEquals(null, multimap.get(key.getKey(0), null));
150                 assertEquals(null, multimap.get(null, null));
151                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null));
152                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null, null));
153                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null, null, null));
154                 break;
155                 case 3:
156                 assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2)));
157                 assertEquals(null, multimap.get(null, key.getKey(1), key.getKey(2)));
158                 assertEquals(null, multimap.get(key.getKey(0), null, key.getKey(2)));
159                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null));
160                 assertEquals(null, multimap.get(null, null, null));
161                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), null));
162                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), null, null));
163                 break;
164                 case 4:
165                 assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
166                 assertEquals(null, multimap.get(null, key.getKey(1), key.getKey(2), key.getKey(3)));
167                 assertEquals(null, multimap.get(key.getKey(0), null, key.getKey(2), key.getKey(3)));
168                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null, key.getKey(3)));
169                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), null));
170                 assertEquals(null, multimap.get(null, null, null, null));
171                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), null));
172                 break;
173                 case 5:
174                 assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
175                 assertEquals(null, multimap.get(null, key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
176                 assertEquals(null, multimap.get(key.getKey(0), null, key.getKey(2), key.getKey(3), key.getKey(4)));
177                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null, key.getKey(3), key.getKey(4)));
178                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), null, key.getKey(4)));
179                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), null));
180                 assertEquals(null, multimap.get(null, null, null, null, null));
181                 break;
182                 default:
183                 fail("Invalid key size");
184             }
185         }
186     }
187     
188     public void testMultiKeyContainsKey() {
189         resetFull();
190         MultiKeyMap multimap = (MultiKeyMap) map;
191         MultiKey[] keys = getMultiKeyKeys();
192         Object JavaDoc[] values = getSampleValues();
193         
194         for (int i = 0; i < keys.length; i++) {
195             MultiKey key = keys[i];
196             Object JavaDoc value = values[i];
197             
198             switch (key.size()) {
199                 case 2:
200                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1)));
201                 assertEquals(false, multimap.containsKey(null, key.getKey(1)));
202                 assertEquals(false, multimap.containsKey(key.getKey(0), null));
203                 assertEquals(false, multimap.containsKey(null, null));
204                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null));
205                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null, null));
206                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null, null, null));
207                 break;
208                 case 3:
209                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
210                 assertEquals(false, multimap.containsKey(null, key.getKey(1), key.getKey(2)));
211                 assertEquals(false, multimap.containsKey(key.getKey(0), null, key.getKey(2)));
212                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null));
213                 assertEquals(false, multimap.containsKey(null, null, null));
214                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), null));
215                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), null, null));
216                 break;
217                 case 4:
218                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
219                 assertEquals(false, multimap.containsKey(null, key.getKey(1), key.getKey(2), key.getKey(3)));
220                 assertEquals(false, multimap.containsKey(key.getKey(0), null, key.getKey(2), key.getKey(3)));
221                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null, key.getKey(3)));
222                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), null));
223                 assertEquals(false, multimap.containsKey(null, null, null, null));
224                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), null));
225                 break;
226                 case 5:
227                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
228                 assertEquals(false, multimap.containsKey(null, key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
229                 assertEquals(false, multimap.containsKey(key.getKey(0), null, key.getKey(2), key.getKey(3), key.getKey(4)));
230                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null, key.getKey(3), key.getKey(4)));
231                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), null, key.getKey(4)));
232                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), null));
233                 assertEquals(false, multimap.containsKey(null, null, null, null, null));
234                 break;
235                 default:
236                 fail("Invalid key size");
237             }
238         }
239     }
240     
241     public void testMultiKeyPut() {
242         MultiKey[] keys = getMultiKeyKeys();
243         Object JavaDoc[] values = getSampleValues();
244         
245         for (int i = 0; i < keys.length; i++) {
246             MultiKeyMap multimap = new MultiKeyMap();
247             
248             MultiKey key = keys[i];
249             Object JavaDoc value = values[i];
250             
251             switch (key.size()) {
252                 case 2:
253                 assertEquals(null, multimap.put(key.getKey(0), key.getKey(1), value));
254                 assertEquals(1, multimap.size());
255                 assertEquals(value, multimap.get(key.getKey(0), key.getKey(1)));
256                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1)));
257                 assertEquals(true, multimap.containsKey(new MultiKey(key.getKey(0), key.getKey(1))));
258                 assertEquals(value, multimap.put(key.getKey(0), key.getKey(1), null));
259                 assertEquals(1, multimap.size());
260                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1)));
261                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1)));
262                 break;
263                 case 3:
264                 assertEquals(null, multimap.put(key.getKey(0), key.getKey(1), key.getKey(2), value));
265                 assertEquals(1, multimap.size());
266                 assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2)));
267                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
268                 assertEquals(true, multimap.containsKey(new MultiKey(key.getKey(0), key.getKey(1), key.getKey(2))));
269                 assertEquals(value, multimap.put(key.getKey(0), key.getKey(1), key.getKey(2), null));
270                 assertEquals(1, multimap.size());
271                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2)));
272                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
273                 break;
274                 case 4:
275                 assertEquals(null, multimap.put(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), value));
276                 assertEquals(1, multimap.size());
277                 assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
278                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
279                 assertEquals(true, multimap.containsKey(new MultiKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3))));
280                 assertEquals(value, multimap.put(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), null));
281                 assertEquals(1, multimap.size());
282                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
283                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
284                 break;
285                 case 5:
286                 assertEquals(null, multimap.put(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4), value));
287                 assertEquals(1, multimap.size());
288                 assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
289                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
290                 assertEquals(true, multimap.containsKey(new MultiKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4))));
291                 assertEquals(value, multimap.put(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4), null));
292                 assertEquals(1, multimap.size());
293                 assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
294                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
295                 break;
296                 default:
297                 fail("Invalid key size");
298             }
299         }
300     }
301     
302     public void testMultiKeyRemove() {
303         MultiKey[] keys = getMultiKeyKeys();
304         Object JavaDoc[] values = getSampleValues();
305         
306         for (int i = 0; i < keys.length; i++) {
307             resetFull();
308             MultiKeyMap multimap = (MultiKeyMap) map;
309             int size = multimap.size();
310             
311             MultiKey key = keys[i];
312             Object JavaDoc value = values[i];
313             
314             switch (key.size()) {
315                 case 2:
316                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1)));
317                 assertEquals(value, multimap.remove(key.getKey(0), key.getKey(1)));
318                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1)));
319                 assertEquals(size - 1, multimap.size());
320                 assertEquals(null, multimap.remove(key.getKey(0), key.getKey(1)));
321                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1)));
322                 break;
323                 case 3:
324                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
325                 assertEquals(value, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2)));
326                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
327                 assertEquals(size - 1, multimap.size());
328                 assertEquals(null, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2)));
329                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
330                 break;
331                 case 4:
332                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
333                 assertEquals(value, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
334                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
335                 assertEquals(size - 1, multimap.size());
336                 assertEquals(null, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
337                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
338                 break;
339                 case 5:
340                 assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
341                 assertEquals(value, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
342                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
343                 assertEquals(size - 1, multimap.size());
344                 assertEquals(null, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
345                 assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
346                 break;
347                 default:
348                 fail("Invalid key size");
349             }
350         }
351     }
352     
353     public void testMultiKeyRemoveAll1() {
354         resetFull();
355         MultiKeyMap multimap = (MultiKeyMap) map;
356         assertEquals(12, multimap.size());
357         
358         multimap.removeAll(I1);
359         assertEquals(8, multimap.size());
360         for (MapIterator it = multimap.mapIterator(); it.hasNext();) {
361             MultiKey key = (MultiKey) it.next();
362             assertEquals(false, I1.equals(key.getKey(0)));
363         }
364     }
365     
366     public void testMultiKeyRemoveAll2() {
367         resetFull();
368         MultiKeyMap multimap = (MultiKeyMap) map;
369         assertEquals(12, multimap.size());
370         
371         multimap.removeAll(I2, I3);
372         assertEquals(9, multimap.size());
373         for (MapIterator it = multimap.mapIterator(); it.hasNext();) {
374             MultiKey key = (MultiKey) it.next();
375             assertEquals(false, I2.equals(key.getKey(0)) && I3.equals(key.getKey(1)));
376         }
377     }
378     
379     public void testMultiKeyRemoveAll3() {
380         resetFull();
381         MultiKeyMap multimap = (MultiKeyMap) map;
382         assertEquals(12, multimap.size());
383         
384         multimap.removeAll(I1, I1, I2);
385         assertEquals(9, multimap.size());
386         for (MapIterator it = multimap.mapIterator(); it.hasNext();) {
387             MultiKey key = (MultiKey) it.next();
388             assertEquals(false, I1.equals(key.getKey(0)) && I1.equals(key.getKey(1)) && I2.equals(key.getKey(2)));
389         }
390     }
391     
392     public void testMultiKeyRemoveAll4() {
393         resetFull();
394         MultiKeyMap multimap = (MultiKeyMap) map;
395         assertEquals(12, multimap.size());
396         
397         multimap.removeAll(I1, I1, I2, I3);
398         assertEquals(10, multimap.size());
399         for (MapIterator it = multimap.mapIterator(); it.hasNext();) {
400             MultiKey key = (MultiKey) it.next();
401             assertEquals(false, I1.equals(key.getKey(0)) && I1.equals(key.getKey(1)) && I2.equals(key.getKey(2)) && key.size() >= 4 && I3.equals(key.getKey(3)));
402         }
403     }
404     
405     //-----------------------------------------------------------------------
406
public void testClone() {
407         MultiKeyMap map = new MultiKeyMap();
408         map.put(new MultiKey(I1, I2), "1-2");
409         Map JavaDoc cloned = (Map JavaDoc) map.clone();
410         assertEquals(map.size(), cloned.size());
411         assertSame(map.get(new MultiKey(I1, I2)), cloned.get(new MultiKey(I1, I2)));
412     }
413
414     //-----------------------------------------------------------------------
415
public void testLRUMultiKeyMap() {
416         MultiKeyMap map = MultiKeyMap.decorate(new LRUMap(2));
417         map.put(I1, I2, "1-2");
418         map.put(I1, I3, "1-3");
419         assertEquals(2, map.size());
420         map.put(I1, I4, "1-4");
421         assertEquals(2, map.size());
422         assertEquals(true, map.containsKey(I1, I3));
423         assertEquals(true, map.containsKey(I1, I4));
424         assertEquals(false, map.containsKey(I1, I2));
425         
426         MultiKeyMap cloned = (MultiKeyMap) map.clone();
427         assertEquals(2, map.size());
428         assertEquals(true, cloned.containsKey(I1, I3));
429         assertEquals(true, cloned.containsKey(I1, I4));
430         assertEquals(false, cloned.containsKey(I1, I2));
431         cloned.put(I1, I5, "1-5");
432         assertEquals(2, cloned.size());
433         assertEquals(true, cloned.containsKey(I1, I4));
434         assertEquals(true, cloned.containsKey(I1, I5));
435     }
436
437     //-----------------------------------------------------------------------
438
public String JavaDoc getCompatibilityVersion() {
439         return "3.1";
440     }
441
442 // public void testCreate() throws Exception {
443
// resetEmpty();
444
// writeExternalFormToDisk(
445
// (java.io.Serializable) map,
446
// "D:/dev/collections/data/test/MultiKeyMap.emptyCollection.version3.1.obj");
447
// resetFull();
448
// writeExternalFormToDisk(
449
// (java.io.Serializable) map,
450
// "D:/dev/collections/data/test/MultiKeyMap.fullCollection.version3.1.obj");
451
// }
452
}
453
Popular Tags