KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bidimap > AbstractTestBidiMap


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.bidimap;
17
18 import java.util.Collection JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.commons.collections.BidiMap;
25 import org.apache.commons.collections.BulkTest;
26 import org.apache.commons.collections.MapIterator;
27 import org.apache.commons.collections.iterators.AbstractTestMapIterator;
28 import org.apache.commons.collections.map.AbstractTestMap;
29
30 /**
31  * Abstract test class for {@link BidiMap} methods and contracts.
32  *
33  * @version $Revision: 1.9 $ $Date: 2004/02/18 01:20:40 $
34  *
35  * @author Matthew Hawthorne
36  * @author Stephen Colebourne
37  */

38 public abstract class AbstractTestBidiMap extends AbstractTestMap {
39
40     // Test data.
41
private static final Object JavaDoc[][] entriesKV =
42         new Object JavaDoc[][] {
43             new Object JavaDoc[] { "key1", "value1" },
44             new Object JavaDoc[] { "key2", "value2" },
45             new Object JavaDoc[] { "key3", "value3" }
46     };
47     private static final Object JavaDoc[][] entriesVK =
48         new Object JavaDoc[][] {
49             new Object JavaDoc[] { "value1", "key1" },
50             new Object JavaDoc[] { "value2", "key2" },
51             new Object JavaDoc[] { "value3", "key3" }
52     };
53     protected final Object JavaDoc[][] entries;
54
55     public AbstractTestBidiMap(String JavaDoc testName) {
56         super(testName);
57         entries = entriesKV;
58     }
59
60     public AbstractTestBidiMap() {
61         super("Inverse");
62         entries = entriesVK;
63     }
64
65     //-----------------------------------------------------------------------
66
/**
67      * Implement to create an empty <code>BidiMap</code>.
68      *
69      * @return an empty <code>BidiMap</code> implementation.
70      */

71     public abstract BidiMap makeEmptyBidiMap();
72
73     /**
74      * Override to create a full <code>BidiMap</code> other than the default.
75      *
76      * @return a full <code>BidiMap</code> implementation.
77      */

78     public BidiMap makeFullBidiMap() {
79         final BidiMap map = makeEmptyBidiMap();
80         for (int i = 0; i < entries.length; i++) {
81             map.put(entries[i][0], entries[i][1]);
82         }
83         return map;
84     }
85
86     /**
87      * Override to return the empty BidiMap.
88      */

89     public final Map JavaDoc makeEmptyMap() {
90         return makeEmptyBidiMap();
91     }
92
93     /**
94      * Override to indicate to AbstractTestMap this is a BidiMap.
95      */

96     public boolean isAllowDuplicateValues() {
97         return false;
98     }
99     
100     /**
101      * Override as DualHashBidiMap didn't exist until version 3.
102      */

103     public String JavaDoc getCompatibilityVersion() {
104         return "3";
105     }
106
107     // BidiPut
108
//-----------------------------------------------------------------------
109
public void testBidiPut() {
110         if (isPutAddSupported() == false || isPutChangeSupported() == false) return;
111
112         BidiMap map = makeEmptyBidiMap();
113         BidiMap inverse = map.inverseBidiMap();
114         assertEquals(0, map.size());
115         assertEquals(map.size(), inverse.size());
116         
117         map.put("A", "B");
118         assertEquals(1, map.size());
119         assertEquals(map.size(), inverse.size());
120         assertEquals("B", map.get("A"));
121         assertEquals("A", inverse.get("B"));
122         
123         map.put("A", "C");
124         assertEquals(1, map.size());
125         assertEquals(map.size(), inverse.size());
126         assertEquals("C", map.get("A"));
127         assertEquals("A", inverse.get("C"));
128         
129         map.put("B", "C");
130         assertEquals(1, map.size());
131         assertEquals(map.size(), inverse.size());
132         assertEquals("C", map.get("B"));
133         assertEquals("B", inverse.get("C"));
134         
135         map.put("E", "F");
136         assertEquals(2, map.size());
137         assertEquals(map.size(), inverse.size());
138         assertEquals("F", map.get("E"));
139         assertEquals("E", inverse.get("F"));
140     }
141
142     /**
143      * Verifies that {@link #map} is still equal to {@link #confirmed}.
144      * <p>
145      * This implementation checks the inverse map as well.
146      */

147     public void verify() {
148         verifyInverse();
149         super.verify();
150     }
151
152     public void verifyInverse() {
153         assertEquals(map.size(), ((BidiMap) map).inverseBidiMap().size());
154         Map JavaDoc map1 = new HashMap JavaDoc(map);
155         Map JavaDoc map2 = new HashMap JavaDoc(((BidiMap) map).inverseBidiMap());
156         Set JavaDoc keys1 = map1.keySet();
157         Set JavaDoc keys2 = map2.keySet();
158         Collection JavaDoc values1 = map1.values();
159         Collection JavaDoc values2 = map2.values();
160         assertEquals(true, keys1.containsAll(values2));
161         assertEquals(true, values2.containsAll(keys1));
162         assertEquals(true, values1.containsAll(keys2));
163         assertEquals(true, keys2.containsAll(values1));
164     }
165     
166     // testGetKey
167
//-----------------------------------------------------------------------
168
public void testBidiGetKey() {
169         doTestGetKey(makeFullBidiMap(), entries[0][0], entries[0][1]);
170     }
171
172     public void testBidiGetKeyInverse() {
173         doTestGetKey(
174             makeFullBidiMap().inverseBidiMap(),
175             entries[0][1],
176             entries[0][0]);
177     }
178
179     private final void doTestGetKey(BidiMap map, Object JavaDoc key, Object JavaDoc value) {
180         assertEquals("Value not found for key.", value, map.get(key));
181         assertEquals("Key not found for value.", key, map.getKey(value));
182     }
183
184     // testInverse
185
//-----------------------------------------------------------------------
186
public void testBidiInverse() {
187         final BidiMap map = makeFullBidiMap();
188         final BidiMap inverseMap = map.inverseBidiMap();
189
190         assertSame(
191             "Inverse of inverse is not equal to original.",
192             map,
193             inverseMap.inverseBidiMap());
194
195         assertEquals(
196             "Value not found for key.",
197             entries[0][0],
198             inverseMap.get(entries[0][1]));
199
200         assertEquals(
201             "Key not found for value.",
202             entries[0][1],
203             inverseMap.getKey(entries[0][0]));
204     }
205
206     //-----------------------------------------------------------------------
207
public void testBidiModifyEntrySet() {
208         if (isSetValueSupported() == false) return;
209         
210         modifyEntrySet(makeFullBidiMap());
211         modifyEntrySet(makeFullBidiMap().inverseBidiMap());
212     }
213
214     private final void modifyEntrySet(BidiMap map) {
215         // Gets first entry
216
final Map.Entry JavaDoc entry = (Map.Entry JavaDoc)map.entrySet().iterator().next();
217
218         // Gets key and value
219
final Object JavaDoc key = entry.getKey();
220         final Object JavaDoc oldValue = entry.getValue();
221
222         // Sets new value
223
final Object JavaDoc newValue = "newValue";
224         entry.setValue(newValue);
225
226         assertEquals(
227             "Modifying entrySet did not affect underlying Map.",
228             newValue,
229             map.get(key));
230
231         assertNull(
232             "Modifying entrySet did not affect inverse Map.",
233             map.getKey(oldValue));
234     }
235
236     //-----------------------------------------------------------------------
237
public void testBidiClear() {
238         if (isRemoveSupported() == false) {
239             try {
240                 makeFullBidiMap().clear();
241                 fail();
242             } catch(UnsupportedOperationException JavaDoc ex) {}
243             return;
244         }
245
246         BidiMap map = makeFullBidiMap();
247         map.clear();
248         assertTrue("Map was not cleared.", map.isEmpty());
249         assertTrue("Inverse map was not cleared.", map.inverseBidiMap().isEmpty());
250
251         // Tests clear on inverse
252
map = makeFullBidiMap().inverseBidiMap();
253         map.clear();
254         assertTrue("Map was not cleared.", map.isEmpty());
255         assertTrue("Inverse map was not cleared.", map.inverseBidiMap().isEmpty());
256
257     }
258
259     //-----------------------------------------------------------------------
260
public void testBidiRemove() {
261         if (isRemoveSupported() == false) {
262             try {
263                 makeFullBidiMap().remove(entries[0][0]);
264                 fail();
265             } catch(UnsupportedOperationException JavaDoc ex) {}
266             try {
267                 makeFullBidiMap().removeValue(entries[0][1]);
268                 fail();
269             } catch(UnsupportedOperationException JavaDoc ex) {}
270             return;
271         }
272         
273         remove(makeFullBidiMap(), entries[0][0]);
274         remove(makeFullBidiMap().inverseBidiMap(), entries[0][1]);
275
276         removeValue(makeFullBidiMap(), entries[0][1]);
277         removeValue(makeFullBidiMap().inverseBidiMap(), entries[0][0]);
278         
279         assertEquals(null, makeFullBidiMap().removeValue("NotPresent"));
280     }
281
282     private final void remove(BidiMap map, Object JavaDoc key) {
283         final Object JavaDoc value = map.remove(key);
284         assertTrue("Key was not removed.", !map.containsKey(key));
285         assertNull("Value was not removed.", map.getKey(value));
286     }
287
288     private final void removeValue(BidiMap map, Object JavaDoc value) {
289         final Object JavaDoc key = map.removeValue(value);
290         assertTrue("Key was not removed.", !map.containsKey(key));
291         assertNull("Value was not removed.", map.getKey(value));
292     }
293
294     //-----------------------------------------------------------------------
295
public void testBidiKeySetValuesOrder() {
296         resetFull();
297         Iterator JavaDoc keys = map.keySet().iterator();
298         Iterator JavaDoc values = map.values().iterator();
299         for (; keys.hasNext() && values.hasNext();) {
300             Object JavaDoc key = keys.next();
301             Object JavaDoc value = values.next();
302             assertSame(map.get(key), value);
303         }
304         assertEquals(false, keys.hasNext());
305         assertEquals(false, values.hasNext());
306     }
307
308     //-----------------------------------------------------------------------
309
public void testBidiRemoveByKeySet() {
310         if (isRemoveSupported() == false) return;
311         
312         removeByKeySet(makeFullBidiMap(), entries[0][0], entries[0][1]);
313         removeByKeySet(makeFullBidiMap().inverseBidiMap(), entries[0][1], entries[0][0]);
314     }
315
316     private final void removeByKeySet(BidiMap map, Object JavaDoc key, Object JavaDoc value) {
317         map.keySet().remove(key);
318
319         assertTrue("Key was not removed.", !map.containsKey(key));
320         assertTrue("Value was not removed.", !map.containsValue(value));
321
322         assertTrue(
323             "Key was not removed from inverse map.",
324             !map.inverseBidiMap().containsValue(key));
325         assertTrue(
326             "Value was not removed from inverse map.",
327             !map.inverseBidiMap().containsKey(value));
328     }
329
330     //-----------------------------------------------------------------------
331
public void testBidiRemoveByEntrySet() {
332         if (isRemoveSupported() == false) return;
333         
334         removeByEntrySet(makeFullBidiMap(), entries[0][0], entries[0][1]);
335         removeByEntrySet(makeFullBidiMap().inverseBidiMap(), entries[0][1], entries[0][0]);
336     }
337
338     private final void removeByEntrySet(BidiMap map, Object JavaDoc key, Object JavaDoc value) {
339         Map JavaDoc temp = new HashMap JavaDoc();
340         temp.put(key, value);
341         map.entrySet().remove(temp.entrySet().iterator().next());
342
343         assertTrue("Key was not removed.", !map.containsKey(key));
344         assertTrue("Value was not removed.", !map.containsValue(value));
345
346         assertTrue(
347             "Key was not removed from inverse map.",
348             !map.inverseBidiMap().containsValue(key));
349         assertTrue(
350             "Value was not removed from inverse map.",
351             !map.inverseBidiMap().containsKey(value));
352     }
353
354     //-----------------------------------------------------------------------
355
public BulkTest bulkTestMapEntrySet() {
356         return new TestBidiMapEntrySet();
357     }
358
359     public class TestBidiMapEntrySet extends TestMapEntrySet {
360         public TestBidiMapEntrySet() {
361             super();
362         }
363         public void testMapEntrySetIteratorEntrySetValueCrossCheck() {
364             Object JavaDoc key1 = getSampleKeys()[0];
365             Object JavaDoc key2 = getSampleKeys()[1];
366             Object JavaDoc newValue1 = getNewSampleValues()[0];
367             Object JavaDoc newValue2 = getNewSampleValues()[1];
368                 
369             resetFull();
370             // explicitly get entries as sample values/keys are connected for some maps
371
// such as BeanMap
372
Iterator JavaDoc it = TestBidiMapEntrySet.this.collection.iterator();
373             Map.Entry JavaDoc entry1 = getEntry(it, key1);
374             it = TestBidiMapEntrySet.this.collection.iterator();
375             Map.Entry JavaDoc entry2 = getEntry(it, key2);
376             Iterator JavaDoc itConfirmed = TestBidiMapEntrySet.this.confirmed.iterator();
377             Map.Entry JavaDoc entryConfirmed1 = getEntry(itConfirmed, key1);
378             itConfirmed = TestBidiMapEntrySet.this.confirmed.iterator();
379             Map.Entry JavaDoc entryConfirmed2 = getEntry(itConfirmed, key2);
380             TestBidiMapEntrySet.this.verify();
381                 
382             if (isSetValueSupported() == false) {
383                 try {
384                     entry1.setValue(newValue1);
385                 } catch (UnsupportedOperationException JavaDoc ex) {
386                 }
387                 return;
388             }
389
390             // these checked in superclass
391
entry1.setValue(newValue1);
392             entryConfirmed1.setValue(newValue1);
393             entry2.setValue(newValue2);
394             entryConfirmed2.setValue(newValue2);
395             
396             // at this point
397
// key1=newValue1, key2=newValue2
398
try {
399                 entry2.setValue(newValue1); // should remove key1
400
} catch (IllegalArgumentException JavaDoc ex) {
401                 return; // simplest way of dealing with tricky situation
402
}
403             entryConfirmed2.setValue(newValue1);
404             AbstractTestBidiMap.this.confirmed.remove(key1);
405             assertEquals(newValue1, entry2.getValue());
406             assertEquals(true, AbstractTestBidiMap.this.map.containsKey(entry2.getKey()));
407             assertEquals(true, AbstractTestBidiMap.this.map.containsValue(newValue1));
408             assertEquals(newValue1, AbstractTestBidiMap.this.map.get(entry2.getKey()));
409             assertEquals(false, AbstractTestBidiMap.this.map.containsKey(key1));
410             assertEquals(false, AbstractTestBidiMap.this.map.containsValue(newValue2));
411             TestBidiMapEntrySet.this.verify();
412             
413             // check for ConcurrentModification
414
it.next(); // if you fail here, maybe you should be throwing an IAE, see above
415
if (isRemoveSupported()) {
416                 it.remove();
417             }
418         }
419     }
420         
421     public BulkTest bulkTestInverseMap() {
422         return new TestInverseBidiMap(this);
423     }
424
425     public class TestInverseBidiMap extends AbstractTestBidiMap {
426         final AbstractTestBidiMap main;
427         
428         public TestInverseBidiMap(AbstractTestBidiMap main) {
429             super();
430             this.main = main;
431         }
432         public BidiMap makeEmptyBidiMap() {
433             return main.makeEmptyBidiMap().inverseBidiMap();
434         }
435         public BidiMap makeFullBidiMap() {
436             return main.makeFullBidiMap().inverseBidiMap();
437         }
438         public Map JavaDoc makeFullMap() {
439             return ((BidiMap) main.makeFullMap()).inverseBidiMap();
440         }
441         public Object JavaDoc[] getSampleKeys() {
442             return main.getSampleValues();
443         }
444         public Object JavaDoc[] getSampleValues() {
445             return main.getSampleKeys();
446         }
447         
448         public String JavaDoc getCompatibilityVersion() {
449             return main.getCompatibilityVersion();
450         }
451         public boolean isAllowNullKey() {
452             return main.isAllowNullKey();
453         }
454         public boolean isAllowNullValue() {
455             return main.isAllowNullValue();
456         }
457         public boolean isPutAddSupported() {
458             return main.isPutAddSupported();
459         }
460         public boolean isPutChangeSupported() {
461             return main.isPutChangeSupported();
462         }
463         public boolean isSetValueSupported() {
464             return main.isSetValueSupported();
465         }
466         public boolean isRemoveSupported() {
467             return main.isRemoveSupported();
468         }
469
470     }
471     
472     //-----------------------------------------------------------------------
473
public BulkTest bulkTestBidiMapIterator() {
474         return new TestBidiMapIterator();
475     }
476     
477     public class TestBidiMapIterator extends AbstractTestMapIterator {
478         public TestBidiMapIterator() {
479             super("TestBidiMapIterator");
480         }
481         
482         public Object JavaDoc[] addSetValues() {
483             return AbstractTestBidiMap.this.getNewSampleValues();
484         }
485         
486         public boolean supportsRemove() {
487             return AbstractTestBidiMap.this.isRemoveSupported();
488         }
489
490         public boolean supportsSetValue() {
491             return AbstractTestBidiMap.this.isSetValueSupported();
492         }
493
494         public MapIterator makeEmptyMapIterator() {
495             resetEmpty();
496             return ((BidiMap) AbstractTestBidiMap.this.map).mapIterator();
497         }
498
499         public MapIterator makeFullMapIterator() {
500             resetFull();
501             return ((BidiMap) AbstractTestBidiMap.this.map).mapIterator();
502         }
503         
504         public Map JavaDoc getMap() {
505             // assumes makeFullMapIterator() called first
506
return AbstractTestBidiMap.this.map;
507         }
508         
509         public Map JavaDoc getConfirmedMap() {
510             // assumes makeFullMapIterator() called first
511
return AbstractTestBidiMap.this.confirmed;
512         }
513         
514         public void verify() {
515             super.verify();
516             AbstractTestBidiMap.this.verify();
517         }
518     }
519     
520     //-----------------------------------------------------------------------
521
public void testBidiMapIteratorSet() {
522         Object JavaDoc newValue1 = getOtherValues()[0];
523         Object JavaDoc newValue2 = getOtherValues()[1];
524         
525         resetFull();
526         BidiMap bidi = (BidiMap) map;
527         MapIterator it = bidi.mapIterator();
528         assertEquals(true, it.hasNext());
529         Object JavaDoc key1 = it.next();
530         
531         if (isSetValueSupported() == false) {
532             try {
533                 it.setValue(newValue1);
534                 fail();
535             } catch (UnsupportedOperationException JavaDoc ex) {
536             }
537             return;
538         }
539         
540         it.setValue(newValue1);
541         confirmed.put(key1, newValue1);
542         assertSame(key1, it.getKey());
543         assertSame(newValue1, it.getValue());
544         assertEquals(true, bidi.containsKey(key1));
545         assertEquals(true, bidi.containsValue(newValue1));
546         assertEquals(newValue1, bidi.get(key1));
547         verify();
548         
549         it.setValue(newValue1); // same value - should be OK
550
confirmed.put(key1, newValue1);
551         assertSame(key1, it.getKey());
552         assertSame(newValue1, it.getValue());
553         assertEquals(true, bidi.containsKey(key1));
554         assertEquals(true, bidi.containsValue(newValue1));
555         assertEquals(newValue1, bidi.get(key1));
556         verify();
557         
558         Object JavaDoc key2 = it.next();
559         it.setValue(newValue2);
560         confirmed.put(key2, newValue2);
561         assertSame(key2, it.getKey());
562         assertSame(newValue2, it.getValue());
563         assertEquals(true, bidi.containsKey(key2));
564         assertEquals(true, bidi.containsValue(newValue2));
565         assertEquals(newValue2, bidi.get(key2));
566         verify();
567         
568         // at this point
569
// key1=newValue1, key2=newValue2
570
try {
571             it.setValue(newValue1); // should remove key1
572
fail();
573         } catch (IllegalArgumentException JavaDoc ex) {
574             return; // simplest way of dealing with tricky situation
575
}
576         confirmed.put(key2, newValue1);
577         AbstractTestBidiMap.this.confirmed.remove(key1);
578         assertEquals(newValue1, it.getValue());
579         assertEquals(true, bidi.containsKey(it.getKey()));
580         assertEquals(true, bidi.containsValue(newValue1));
581         assertEquals(newValue1, bidi.get(it.getKey()));
582         assertEquals(false, bidi.containsKey(key1));
583         assertEquals(false, bidi.containsValue(newValue2));
584         verify();
585             
586         // check for ConcurrentModification
587
it.next(); // if you fail here, maybe you should be throwing an IAE, see above
588
if (isRemoveSupported()) {
589             it.remove();
590         }
591     }
592
593 }
594
Popular Tags