KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > GenericMapTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest;
6
7 import org.apache.commons.collections.FastHashMap;
8
9 import com.tc.exception.TCNonPortableObjectError;
10 import com.tc.object.config.ConfigVisitor;
11 import com.tc.object.config.DSOClientConfigHelper;
12 import com.tc.object.tx.ReadOnlyException;
13 import com.tc.object.tx.UnlockedSharedObjectException;
14 import com.tc.simulator.app.ApplicationConfig;
15 import com.tc.simulator.app.ErrorContext;
16 import com.tc.simulator.listener.ListenerProvider;
17 import com.tc.util.Assert;
18
19 import gnu.trove.THashMap;
20 import gnu.trove.TObjectFunction;
21 import gnu.trove.TObjectHash;
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.Comparator JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.LinkedHashMap JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Properties JavaDoc;
41 import java.util.Set JavaDoc;
42 import java.util.TreeMap JavaDoc;
43 import java.util.Map.Entry;
44
45 public class GenericMapTestApp extends GenericTestApp {
46
47   private final Map nonSharedArrayMap = new HashMap();
48
49   public GenericMapTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
50     super(appId, cfg, listenerProvider, Map.class);
51   }
52
53   protected Object JavaDoc getTestObject(String JavaDoc test) {
54     List JavaDoc maps = (List JavaDoc) sharedMap.get("maps");
55
56     // This is just to make sure all the expected maps are here.
57
// As new map classes get added to this test, you'll have to adjust this number obviously
58
Assert.assertEquals(24, maps.size());
59
60     return maps.iterator();
61   }
62
63   protected void setupTestObject(String JavaDoc test) {
64     List JavaDoc maps = new ArrayList JavaDoc();
65
66     maps.add(new HashMap());
67     maps.add(new Hashtable JavaDoc());
68     maps.add(new TreeMap JavaDoc(new NullTolerantComparator()));
69     maps.add(new LinkedHashMap JavaDoc());
70     maps.add(new THashMap());
71     maps.add(new FastHashMap());
72     FastHashMap fm = new FastHashMap();
73     fm.setFast(true);
74     maps.add(fm);
75     maps.add(new Properties JavaDoc());
76     maps.add(new MyHashMap(11));
77     maps.add(new MyHashMap(new HashMap()));
78     maps.add(new MyHashMap2());
79     maps.add(new MyHashMap3(0));
80     maps.add(new MyTreeMap(new NullTolerantComparator()));
81     maps.add(new MyTreeMap2(new NullTolerantComparator()));
82     maps.add(new MyHashtable());
83     maps.add(new MyHashtable2());
84     maps.add(new MyLinkedHashMap());
85     maps.add(new MyLinkedHashMap2());
86     maps.add(new MyLinkedHashMap3(true));
87     maps.add(new MyTHashMap());
88     maps.add(new MyFastHashMap());
89     maps.add(new MyProperties());
90     maps.add(new MyProperties2());
91     maps.add(new MyProperties3());
92
93     // maps.add(new IdentityHashMap());
94
// maps.add(new WeakHashMap());
95

96     /*
97      * sharedMap.put("maps", maps); sharedMap.put("arrayforHashMap", new Object[4]); sharedMap.put("arrayforHashtable",
98      * new Object[4]); sharedMap.put("arrayforTreeMap", new Object[4]); sharedMap.put("arrayforTreeMap2", new
99      * Object[4]); sharedMap.put("arrayforTHashMap", new Object[4]); sharedMap.put("arrayforLinkedHashMap", new
100      * Object[4]); sharedMap.put("arrayforFastHashMap", new Object[4]); sharedMap.put("arrayforProperties", new
101      * Object[4]); sharedMap.put("arrayforFastHashMapWithFast", new Object[4]); sharedMap.put("arrayforMyHashMap", new
102      * Object[4]); sharedMap.put("arrayforMyHashMap2", new Object[4]); sharedMap.put("arrayforMyHashMap3", new
103      * Object[4]); sharedMap.put("arrayforMyTreeMap", new Object[4]); sharedMap.put("arrayforMyHashtable", new
104      * Object[4]); sharedMap.put("arrayforMyHashtable2", new Object[4]); sharedMap.put("arrayforMyLinkedHashMap", new
105      * Object[4]); sharedMap.put("arrayforMyLinkedHashMap2", new Object[4]); sharedMap.put("arrayforMyTHashMap", new
106      * Object[4]); sharedMap.put("arrayforMyFastHashMap", new Object[4]); sharedMap.put("arrayforMyProperties", new
107      * Object[4]); sharedMap.put("arrayforMyProperties2", new Object[4]);
108      */

109
110     sharedMap.put("maps", maps);
111     nonSharedArrayMap.put("arrayforHashMap", new Object JavaDoc[4]);
112     nonSharedArrayMap.put("arrayforHashtable", new Object JavaDoc[4]);
113     sharedMap.put("arrayforTreeMap", new Object JavaDoc[4]);
114     sharedMap.put("arrayforTreeMap2", new Object JavaDoc[4]);
115     sharedMap.put("arrayforTHashMap", new Object JavaDoc[4]);
116     nonSharedArrayMap.put("arrayforLinkedHashMap", new Object JavaDoc[4]);
117     nonSharedArrayMap.put("arrayforFastHashMap", new Object JavaDoc[4]);
118     nonSharedArrayMap.put("arrayforProperties", new Object JavaDoc[4]);
119     nonSharedArrayMap.put("arrayforFastHashMapWithFast", new Object JavaDoc[4]);
120     nonSharedArrayMap.put("arrayforMyHashMap", new Object JavaDoc[4]);
121     nonSharedArrayMap.put("arrayforMyHashMap2", new Object JavaDoc[4]);
122     sharedMap.put("arrayforMyTreeMap", new Object JavaDoc[4]);
123     nonSharedArrayMap.put("arrayforMyHashtable", new Object JavaDoc[4]);
124     nonSharedArrayMap.put("arrayforMyHashtable2", new Object JavaDoc[4]);
125     nonSharedArrayMap.put("arrayforMyLinkedHashMap", new Object JavaDoc[4]);
126     nonSharedArrayMap.put("arrayforMyLinkedHashMap2", new Object JavaDoc[4]);
127     nonSharedArrayMap.put("arrayforMyLinkedHashMap3", new Object JavaDoc[4]);
128     sharedMap.put("arrayforMyTHashMap", new Object JavaDoc[4]);
129     nonSharedArrayMap.put("arrayforMyFastHashMap", new Object JavaDoc[4]);
130     nonSharedArrayMap.put("arrayforMyProperties", new Object JavaDoc[4]);
131     nonSharedArrayMap.put("arrayforMyProperties2", new Object JavaDoc[4]);
132     nonSharedArrayMap.put("arrayforMyProperties3", new Object JavaDoc[4]);
133   }
134
135   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
136     String JavaDoc testClass = GenericMapTestApp.class.getName();
137     config.addNewModule("clustered-commons-collections-3.1", "1.0.0");
138     config.getOrCreateSpec(testClass);
139     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
140     config.addWriteAutolock(methodExpression);
141     String JavaDoc readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)";
142     config.addReadAutolock(readOnlyMethodExpression);
143     config.addIncludePattern(testClass + "$*");
144     config.addIncludePattern(Key.class.getName());
145     config.addIncludePattern(NullTolerantComparator.class.getName());
146     config.addIncludePattern(SimpleEntry.class.getName());
147     config.addExcludePattern(MyNonPortableObject.class.getName());
148   }
149
150   void testBasicUnSynchronizedPut(Map map, boolean validate) {
151     if (map instanceof Hashtable JavaDoc) { return; }
152     if (map instanceof FastHashMap) { return; }
153
154     if (validate) {
155       assertEmptyMap(map);
156     } else {
157       try {
158         map.put("January", "Jan");
159         throw new AssertionError JavaDoc("Should have thrown a UnlockedSharedObjectException");
160       } catch (UnlockedSharedObjectException use) {
161         // this is expected.
162
}
163     }
164   }
165
166   void testBasicPut(Map map, boolean validate) {
167     if (validate) {
168       assertSingleMapping(map, "timmy", "teck");
169       if (map instanceof MyHashMap) {
170         Assert.assertEquals("timmy", ((MyHashMap) map).getKey());
171         Assert.assertEquals("teck", ((MyHashMap) map).getValue());
172       } else if (map instanceof MyTreeMap2) {
173         Assert.assertEquals("timmy", ((MyTreeMap2) map).getKey());
174         Assert.assertEquals("teck", ((MyTreeMap2) map).getValue());
175       } else if (map instanceof MyLinkedHashMap2) {
176         Assert.assertEquals("timmy", ((MyLinkedHashMap2) map).getKey());
177         Assert.assertEquals("teck", ((MyLinkedHashMap2) map).getValue());
178       } else if (map instanceof MyProperties) {
179         Assert.assertEquals("timmy", ((MyProperties) map).getKey());
180         Assert.assertEquals("teck", ((MyProperties) map).getValue());
181       }
182     } else {
183       synchronized (map) {
184         Object JavaDoc prev = map.put("timmy", "teck");
185         Assert.assertNull(prev);
186       }
187     }
188   }
189
190   void testBasicPutAll(Map map, boolean validate) {
191     Map toAdd = getOrderSensitiveMappings();
192
193     if (validate) {
194       assertMappings(toAdd, map);
195     } else {
196       synchronized (map) {
197         map.putAll(toAdd);
198       }
199     }
200   }
201
202   void testPutNullKey(Map map, boolean validate) {
203     if (!allowsNull(map)) { return; }
204     if (validate) {
205       assertSingleMapping(map, null, "value");
206     } else {
207       synchronized (map) {
208         Object JavaDoc prev = map.put(null, "value");
209         Assert.assertNull(prev);
210       }
211     }
212   }
213
214   void testReplaceNullKey(Map map, boolean validate) {
215     if (!allowsNull(map)) { return; }
216     if (validate) {
217       assertSingleMapping(map, null, "value2");
218     } else {
219       synchronized (map) {
220         Object JavaDoc prev = map.put(null, "value");
221         Assert.assertNull(prev);
222       }
223
224       synchronized (map) {
225         Object JavaDoc prev = map.put(null, "value2");
226         Assert.assertEquals("value", prev);
227       }
228     }
229   }
230
231   void testBasicReplace(Map map, boolean validate) {
232     if (validate) {
233       assertSingleMapping(map, "key", "value2");
234     } else {
235       synchronized (map) {
236         Object JavaDoc prev = map.put("key", "value");
237         Assert.assertNull(prev);
238       }
239
240       synchronized (map) {
241         Object JavaDoc prev = map.put("key", "value2");
242         Assert.assertEquals("value", prev);
243       }
244     }
245   }
246
247   void testPutNullValue(Map map, boolean validate) {
248     if (!allowsNull(map)) { return; }
249     if (validate) {
250       assertSingleMapping(map, "key", null);
251     } else {
252       synchronized (map) {
253         Object JavaDoc prev = map.put("key", null);
254         Assert.assertNull(prev);
255       }
256     }
257   }
258
259   void testReplaceKeyRetention(Map map, boolean validate) {
260     if (validate) {
261       assertSingleMapping(map, new Key("** doesn't matter **", "key"), "value2");
262
263       Assert.assertEquals(1, map.keySet().size());
264       Key retainedKey = (Key) map.keySet().iterator().next();
265
266       String JavaDoc expect = (map instanceof THashMap) ? "id2" : "id1";
267       Assert.assertEquals(expect, retainedKey.id);
268     } else {
269       synchronized (map) {
270         Object JavaDoc prev = map.put(new Key("id1", "key"), "value");
271         Assert.assertNull(prev);
272       }
273       synchronized (map) {
274         Object JavaDoc prev = map.put(new Key("id2", "key"), "value2");
275         Assert.assertEquals("value", prev);
276       }
277     }
278   }
279
280   void testBasicSetProperty(Map map, boolean validate) {
281     if (!(map instanceof Properties JavaDoc)) { return; }
282
283     if (validate) {
284       assertSingleMapping(map, "timmy", "teck");
285     } else {
286       synchronized (map) {
287         Object JavaDoc previous = ((Properties JavaDoc) map).setProperty("timmy", "teck");
288         Assert.assertNull(previous);
289       }
290     }
291   }
292
293   void testBasicGetProperty(Map map, boolean validate) {
294     if (!(map instanceof Properties JavaDoc)) { return; }
295
296     if (validate) {
297       Assert.assertEquals("value", ((Properties JavaDoc) map).getProperty("key"));
298       Assert.assertEquals("defaultValue", ((Properties JavaDoc) map).getProperty("nonsense", "defaultValue"));
299       Assert.assertEquals("value", ((Properties JavaDoc) map).getProperty("key", "defaultValue"));
300     } else {
301       synchronized (map) {
302         ((Properties JavaDoc) map).setProperty("key", "value");
303       }
304     }
305   }
306
307   void testBasicLoad(Map map, boolean validate) {
308     if (!(map instanceof Properties JavaDoc)) { return; }
309
310     if (validate) {
311       Map expectedMap = new Properties JavaDoc();
312       expectedMap.put("key1", "val1");
313       expectedMap.put("key2", "val2");
314       expectedMap.put("key3", "val3");
315       assertMappings(expectedMap, map);
316     } else {
317       synchronized (map) {
318         Properties JavaDoc data = new Properties JavaDoc();
319         data.setProperty("key1", "val1");
320         data.setProperty("key2", "val2");
321         data.setProperty("key3", "val3");
322         ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
323         try {
324           data.store(outputStream, null);
325         } catch (IOException JavaDoc ioe) {
326           Assert.fail();
327         }
328         ByteArrayInputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(outputStream.toByteArray());
329         try {
330           ((Properties JavaDoc) map).load(inputStream);
331         } catch (IOException JavaDoc ioe) {
332           Assert.fail();
333         }
334       }
335     }
336   }
337
338   /*
339    * Java 1.5 specific API used void testBasicLoadFromXML(Map map, boolean validate) { if (!(map instanceof Properties)) {
340    * return; } if(validate) { Map expectedMap = new Properties(); expectedMap.put("key1", "val1");
341    * expectedMap.put("key2", "val2"); expectedMap.put("key3", "val3"); assertMappings(expectedMap, map); } else {
342    * synchronized (map) { Properties data = new Properties(); data.setProperty("key1", "val1"); data.setProperty("key2",
343    * "val2"); data.setProperty("key3", "val3"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try {
344    * data.storeToXML(outputStream, null); } catch (IOException ioe) { Assert.fail(); } ByteArrayInputStream inputStream =
345    * new ByteArrayInputStream(outputStream.toByteArray()); try { ((Properties)map).loadFromXML(inputStream); } catch
346    * (IOException ioe) { Assert.fail(); } } } }
347    */

348
349   void testKeySetClear(Map map, boolean validate) {
350     if (validate) {
351       assertEmptyMap(map);
352     } else {
353       addMappings(map, 42);
354       synchronized (map) {
355         map.keySet().clear();
356       }
357     }
358   }
359
360   void testKeySetIteratorRemove(Map map, boolean validate) {
361     if (validate) {
362       Map expect = getOrderSensitiveMappings();
363       expect.remove("February");
364       expect.remove("March");
365       assertMappings(expect, map);
366     } else {
367       synchronized (map) {
368         map.putAll(getOrderSensitiveMappings());
369       }
370
371       synchronized (map) {
372         for (Iterator JavaDoc i = map.keySet().iterator(); i.hasNext();) {
373           Object JavaDoc key = i.next();
374           if ("February".equals(key) || "March".equals(key)) {
375             i.remove();
376           }
377         }
378       }
379     }
380   }
381
382   void testKeySetIteratorRemoveNull(Map map, boolean validate) {
383     if (!allowsNull(map)) { return; }
384
385     if (validate) {
386       assertSingleMapping(map, "key1", "value1");
387     } else {
388       synchronized (map) {
389         map.put("key1", "value1");
390         map.put(null, "value for null key");
391       }
392
393       synchronized (map) {
394         for (Iterator JavaDoc i = map.keySet().iterator(); i.hasNext();) {
395           Object JavaDoc key = i.next();
396           if (!"key1".equals(key)) {
397             i.remove();
398           }
399         }
400       }
401     }
402   }
403
404   void testKeySetRemove(Map map, boolean validate) {
405     if (validate) {
406       Map expect = getOrderSensitiveMappings();
407       expect.remove("February");
408       expect.remove("March");
409       assertMappings(expect, map);
410     } else {
411       synchronized (map) {
412         map.putAll(getOrderSensitiveMappings());
413       }
414
415       synchronized (map) {
416         Set JavaDoc keys = map.keySet();
417         boolean removed;
418         removed = keys.remove("February");
419         Assert.assertTrue(removed);
420         removed = keys.remove("March");
421         Assert.assertTrue(removed);
422         removed = keys.remove("key4");
423         Assert.assertFalse(removed);
424       }
425     }
426   }
427
428   void testBasicGet(Map map, boolean validate) {
429     if (validate) {
430       try {
431         if (isAccessOrderedLinkedHashMap(map)) {
432           synchronized (map) {
433             Assert.assertEquals("value", map.get("key"));
434           }
435         } else {
436           Assert.assertEquals("value", map.get("key"));
437         }
438         if (map instanceof MyHashMap) {
439           Assert.assertEquals("value", ((MyHashMap) map).getObject("key"));
440         }
441       } catch (Throwable JavaDoc t) {
442         System.err.println("*******" + Thread.currentThread().getName() + ", map: " + map + "********");
443         throw new RuntimeException JavaDoc(t);
444       }
445     } else {
446       synchronized (map) {
447         map.put("key", "value");
448       }
449     }
450   }
451
452   void testBasicRemove(Map map, boolean validate) {
453     if (validate) {
454       assertEmptyMap(map);
455     } else {
456       synchronized (map) {
457         Object JavaDoc prev = map.put("key", "value");
458         Assert.assertNull(prev);
459       }
460       synchronized (map) {
461         Object JavaDoc prev = map.remove("key");
462         Assert.assertEquals("value", prev);
463       }
464       synchronized (map) {
465         Object JavaDoc prev = map.remove("key");
466         Assert.assertNull(prev);
467       }
468     }
469   }
470
471   void testRemoveNullKey(Map map, boolean validate) {
472     if (!allowsNull(map)) { return; }
473     if (validate) {
474       assertEmptyMap(map);
475     } else {
476       synchronized (map) {
477         map.put(null, "value");
478       }
479       synchronized (map) {
480         Object JavaDoc removed = map.remove(null);
481         Assert.assertEquals("value", removed);
482       }
483     }
484   }
485
486   void testClearNonEmpty(Map map, boolean validate) {
487     if (validate) {
488       assertEmptyMap(map);
489     } else {
490       addMappings(map, 1);
491
492       Assert.assertFalse(map.isEmpty());
493
494       synchronized (map) {
495         map.clear();
496       }
497     }
498   }
499
500   void testClearEmpty(Map map, boolean validate) {
501     if (validate) {
502       assertEmptyMap(map);
503     } else {
504       Assert.assertTrue(map.isEmpty());
505       synchronized (map) {
506         map.clear();
507       }
508     }
509   }
510
511   void testValuesClear(Map map, boolean validate) {
512     if (validate) {
513       assertEmptyMap(map);
514     } else {
515       addMappings(map, 23);
516
517       synchronized (map) {
518         map.values().clear();
519       }
520     }
521   }
522
523   void testValuesRemove(Map map, boolean validate) {
524     if (validate) {
525       Map expect = getOrderSensitiveMappings();
526       expect.remove("March");
527       assertMappings(expect, map);
528     } else {
529       synchronized (map) {
530         map.putAll(getOrderSensitiveMappings());
531       }
532
533       synchronized (map) {
534         map.values().remove("Mar");
535       }
536     }
537   }
538
539   void testValuesDuplicateRemove(Map map, boolean validate) {
540     if (validate) {
541       if (map instanceof THashMap) {
542         // values().remove(Object) on THashMap will remove all mappings for the given value, not just the first
543
assertEmptyMap(map);
544       } else {
545         Object JavaDoc expect = sharedMap.get("expect" + map.getClass().getName());
546         assertSingleMapping(map, expect, "value");
547       }
548     } else {
549       synchronized (map) {
550         map.put("key1", "value");
551         map.put("key2", "value");
552       }
553
554       synchronized (map) {
555         boolean removed = map.values().remove("value");
556         Assert.assertTrue(removed);
557         String JavaDoc expectedKey = map.containsKey("key1") ? "key1" : "key2";
558         sharedMap.put("expect" + map.getClass().getName(), expectedKey);
559       }
560     }
561   }
562
563   void testValuesRemoveNull(Map map, boolean validate) {
564     if (!allowsNull(map)) { return; }
565     if (validate) {
566       assertSingleMapping(map, "key1", "value1");
567     } else {
568       synchronized (map) {
569         map.put("key1", "value1");
570         map.put("key for null value", null);
571         map.put(null, "value for null key");
572       }
573
574       synchronized (map) {
575         map.values().remove(null);
576         map.values().remove("value for null key");
577       }
578     }
579   }
580
581   void testValuesRemoveAll(Map map, boolean validate) {
582     if (validate) {
583       Map expect = getOrderSensitiveMappings();
584       expect.remove("February");
585       expect.remove("March");
586       assertMappings(expect, map);
587     } else {
588       synchronized (map) {
589         map.putAll(getOrderSensitiveMappings());
590       }
591
592       Set JavaDoc toRemove = new HashSet JavaDoc();
593       toRemove.add("Feb");
594       toRemove.add("Mar");
595
596       synchronized (map) {
597         map.values().removeAll(toRemove);
598       }
599     }
600   }
601
602   void testValuesRetainAll(Map map, boolean validate) {
603     if (validate) {
604       assertSingleMapping(map, "March", "Mar");
605     } else {
606       synchronized (map) {
607         map.putAll(getOrderSensitiveMappings());
608       }
609
610       Set JavaDoc toRetain = new HashSet JavaDoc();
611       toRetain.add("Mar");
612
613       synchronized (map) {
614         map.values().retainAll(toRetain);
615       }
616     }
617   }
618
619   void testValuesIteratorRemove(Map map, boolean validate) {
620     if (validate) {
621       Map expect = getOrderSensitiveMappings();
622       expect.remove("March");
623       assertMappings(expect, map);
624     } else {
625       synchronized (map) {
626         map.putAll(getOrderSensitiveMappings());
627       }
628
629       synchronized (map) {
630         for (Iterator JavaDoc i = map.values().iterator(); i.hasNext();) {
631           Object JavaDoc value = i.next();
632           if ("Mar".equals(value)) {
633             i.remove();
634           }
635         }
636       }
637     }
638   }
639
640   void testValuesIteratorRemoveNull(Map map, boolean validate) {
641     if (!allowsNull(map)) { return; }
642     if (validate) {
643       assertSingleMapping(map, "key1", "value1");
644     } else {
645       synchronized (map) {
646         map.put("key1", "value1");
647         map.put(null, "value for null key");
648         map.put("key for null value", null);
649       }
650
651       synchronized (map) {
652         for (Iterator JavaDoc i = map.values().iterator(); i.hasNext();) {
653           Object JavaDoc value = i.next();
654           if (!"value1".equals(value)) {
655             i.remove();
656           }
657         }
658       }
659     }
660   }
661
662   void testValuesIterator(Map map, boolean validate) {
663     if (validate) {
664       int count = 0;
665       for (Iterator JavaDoc i = map.values().iterator(); i.hasNext();) {
666         count++;
667         Assert.assertEquals("teck", i.next());
668       }
669       Assert.assertEquals(1, count);
670     } else {
671       synchronized (map) {
672         map.put("timmy", "teck");
673       }
674     }
675   }
676
677   void testKeySetIterator(Map map, boolean validate) {
678     if (validate) {
679       int count = 0;
680       for (Iterator JavaDoc i = map.keySet().iterator(); i.hasNext();) {
681         count++;
682         Assert.assertEquals("timmy", i.next());
683       }
684       Assert.assertEquals(1, count);
685     } else {
686       synchronized (map) {
687         map.put("timmy", "teck");
688       }
689     }
690   }
691
692   void testEntrySetIterator(Map map, boolean validate) {
693     if (validate) {
694       int count = 0;
695       for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext();) {
696         count++;
697         Map.Entry entry = (Entry) i.next();
698         Assert.assertEquals("timmy", entry.getKey());
699         Assert.assertEquals("teck", entry.getValue());
700       }
701       Assert.assertEquals(1, count);
702     } else {
703       synchronized (map) {
704         map.put("timmy", "teck");
705       }
706     }
707   }
708
709   void testEntrySetAdd(Map map, boolean validate) {
710     // no test for entrySet().add() as HashMap, Hashtable, TreeMap, and THashMap throw
711
// UnsupportedOperationException.
712
}
713
714   void testEntrySetValue(Map map, boolean validate) {
715     if (validate) {
716       assertSingleMapping(map, "key", "value2");
717     } else {
718       synchronized (map) {
719         map.put("key", "value1");
720       }
721
722       synchronized (map) {
723         Set JavaDoc set = map.entrySet();
724         for (Iterator JavaDoc i = set.iterator(); i.hasNext();) {
725           Entry entry = (Entry) i.next();
726           Object JavaDoc prev = entry.setValue("value2");
727           Assert.assertEquals("value1", prev);
728         }
729       }
730     }
731   }
732
733   void testEntrySetValueNull(Map map, boolean validate) {
734     if (!allowsNull(map)) return;
735
736     if (validate) {
737       assertSingleMapping(map, "key", null);
738     } else {
739       synchronized (map) {
740         map.put("key", "value1");
741       }
742
743       synchronized (map) {
744         Set JavaDoc set = map.entrySet();
745         for (Iterator JavaDoc i = set.iterator(); i.hasNext();) {
746           Entry entry = (Entry) i.next();
747           Object JavaDoc prev = entry.setValue(null);
748           Assert.assertEquals("value1", prev);
749         }
750       }
751     }
752   }
753
754   void testEntrySetRemoveNull(Map map, boolean validate) {
755     if (!allowsNull(map)) return;
756
757     if (validate) {
758       assertEmptyMap(map);
759     } else {
760       synchronized (map) {
761         map.put("key", null);
762         map.put(null, "value");
763       }
764
765       addMappings(map, 3);
766
767       synchronized (map) {
768         Set JavaDoc set = map.entrySet();
769         for (Iterator JavaDoc i = set.iterator(); i.hasNext();) {
770           i.next();
771           i.remove();
772         }
773       }
774     }
775   }
776
777   void testEntrySetRemove(Map map, boolean validate) {
778     if (validate) {
779       Map expect = getOrderSensitiveMappings();
780       expect.remove("March");
781       assertMappings(expect, map);
782     } else {
783       synchronized (map) {
784         map.putAll(getOrderSensitiveMappings());
785       }
786
787       synchronized (map) {
788         map.entrySet().remove(new SimpleEntry("March", "Mar"));
789       }
790     }
791   }
792
793   void testEntrySetRemoveAll(Map map, boolean validate) {
794     if (validate) {
795       Map expect = new HashMap();
796       expect.put("die, die", "die, my darling");
797       expect.put("die, die, die", "no die");
798       assertMappings(expect, map);
799     } else {
800       synchronized (map) {
801         map.put("die", "another day");
802         map.put("die, die", "die, my darling");
803         map.put("die, die, die", "no die");
804         map.put("on the last day of your life, don't forget to", "die");
805       }
806
807       Set JavaDoc removeSet = new HashSet JavaDoc(2);
808       removeSet.add(new SimpleEntry("die", "another day"));
809       removeSet.add(new SimpleEntry("on the last day of your life, don't forget to", "die"));
810
811       synchronized (map) {
812         map.entrySet().removeAll(removeSet);
813       }
814     }
815   }
816
817   void testKeySetRemoveAll(Map map, boolean validate) {
818     if (validate) {
819       Map expect = getOrderSensitiveMappings();
820       expect.remove("February");
821       expect.remove("March");
822       assertMappings(expect, map);
823     } else {
824       synchronized (map) {
825         map.putAll(getOrderSensitiveMappings());
826       }
827
828       Set JavaDoc toRemove = new HashSet JavaDoc(2);
829       toRemove.add("February");
830       toRemove.add("March");
831
832       synchronized (map) {
833         map.keySet().removeAll(toRemove);
834       }
835     }
836   }
837
838   void testKeySetRetainAll(Map map, boolean validate) {
839     if (validate) {
840       Map expect = getOrderSensitiveMappings();
841       expect.remove("January");
842       expect.remove("April");
843       assertMappings(expect, map);
844     } else {
845       synchronized (map) {
846         map.putAll(getOrderSensitiveMappings());
847       }
848
849       Set JavaDoc toRetain = new HashSet JavaDoc(2);
850       toRetain.add("March");
851       toRetain.add("February");
852
853       synchronized (map) {
854         map.keySet().retainAll(toRetain);
855       }
856     }
857   }
858
859   void testKeySetRemoveNull(Map map, boolean validate) {
860     if (!allowsNull(map)) { return; }
861
862     if (validate) {
863       assertSingleMapping(map, "key1", "value1");
864     } else {
865       synchronized (map) {
866         map.put("key1", "value1");
867         map.put(null, "value for null key");
868         map.put("key for null value", null);
869       }
870
871       synchronized (map) {
872         map.keySet().remove(null);
873         map.keySet().remove("key for null value");
874       }
875     }
876
877   }
878
879   void testEntrySetRetainAll(Map map, boolean validate) {
880     if (validate) {
881       // NOTE: this test will fail for THashMap if using releases of trove before 1.1b5. See trove bug 1382196
882
// http://sourceforge.net/tracker/index.php?func=detail&aid=1382196&group_id=39235&atid=424682
883
assertSingleMapping(map, "key1", "value1");
884     } else {
885       synchronized (map) {
886         map.put("key1", "value1");
887         map.put("key2", "value2");
888         map.put("key3", "value3");
889       }
890
891       Set JavaDoc retainSet = new HashSet JavaDoc();
892       retainSet.add(new SimpleEntry("key1", "value1"));
893
894       synchronized (map) {
895         map.entrySet().retainAll(retainSet);
896       }
897     }
898   }
899
900   void testEntrySetRetainAll2(Map map, boolean validate) {
901     if (validate) {
902       Map expect = getOrderSensitiveMappings();
903       expect.remove("January");
904       expect.remove("April");
905       assertMappings(expect, map);
906     } else {
907       synchronized (map) {
908         map.putAll(getOrderSensitiveMappings());
909       }
910
911       Set JavaDoc retainSet = new HashSet JavaDoc();
912       retainSet.add(new SimpleEntry("February", "Feb"));
913       retainSet.add(new SimpleEntry("March", "Mar"));
914
915       synchronized (map) {
916         map.entrySet().retainAll(retainSet);
917       }
918     }
919   }
920
921   void testEntrySetClear(Map map, boolean validate) {
922     if (validate) {
923       assertEmptyMap(map);
924     } else {
925       addMappings(map, 3);
926
927       synchronized (map) {
928         map.entrySet().clear();
929       }
930     }
931   }
932
933   void testEntrySetIteratorRemove(Map map, boolean validate) {
934     if (validate) {
935       Map expect = getOrderSensitiveMappings();
936       expect.remove("February");
937       expect.remove("March");
938       assertMappings(expect, map);
939     } else {
940       synchronized (map) {
941         map.putAll(getOrderSensitiveMappings());
942       }
943
944       synchronized (map) {
945         Set JavaDoc set = map.entrySet();
946         for (Iterator JavaDoc i = set.iterator(); i.hasNext();) {
947           Entry entry = (Entry) i.next();
948           Object JavaDoc key = entry.getKey();
949           if ("February".equals(key) || "March".equals(key)) {
950             i.remove();
951           }
952         }
953       }
954     }
955   }
956
957   void testEntrySetIteratorRemoveNull(Map map, boolean validate) {
958     if (!allowsNull(map)) { return; }
959     if (validate) {
960       assertSingleMapping(map, "key1", "value1");
961     } else {
962       synchronized (map) {
963         map.put("key1", "value1");
964         map.put(null, "value for null key");
965         map.put("key for null value", null);
966       }
967
968       synchronized (map) {
969         Set JavaDoc set = map.entrySet();
970         for (Iterator JavaDoc i = set.iterator(); i.hasNext();) {
971           Entry entry = (Entry) i.next();
972           Object JavaDoc key = entry.getKey();
973           if (!"key1".equals(key)) {
974             i.remove();
975           }
976         }
977       }
978     }
979   }
980
981   void testEntrySetToArray(Map map, boolean validate) {
982     Object JavaDoc[] array = getArray(map);
983
984     if (validate) {
985       if (canTestSharedArray(map)) {
986         assertMappingsEqual(array, map);
987       }
988     } else {
989       synchronized (map) {
990         map.putAll(getOrderSensitiveMappings());
991       }
992       synchronized (array) {
993         Object JavaDoc[] returnArray = map.entrySet().toArray(array);
994         Assert.assertTrue(returnArray == array);
995       }
996       assertMappingsEqual(array, map);
997     }
998   }
999
1000  void testKeySetToArray(Map map, boolean validate) {
1001    Object JavaDoc[] array = getArray(map);
1002
1003    if (validate) {
1004      if (canTestSharedArray(map)) {
1005        assertMappingsKeysEqual(array, map.keySet());
1006      }
1007    } else {
1008      synchronized (map) {
1009        map.putAll(getOrderSensitiveMappings());
1010      }
1011      synchronized (array) {
1012        Object JavaDoc[] returnArray = map.keySet().toArray(array);
1013        Assert.assertTrue(returnArray == array);
1014      }
1015      assertMappingsKeysEqual(array, map.keySet());
1016    }
1017  }
1018
1019  void testValuesToArray(Map map, boolean validate) {
1020    Object JavaDoc[] array = getArray(map);
1021
1022    if (validate) {
1023      if (canTestSharedArray(map)) {
1024        assertMappingsKeysEqual(array, map.values());
1025      }
1026    } else {
1027      synchronized (map) {
1028        map.putAll(getOrderSensitiveMappings());
1029      }
1030      synchronized (array) {
1031        Object JavaDoc[] returnArray = map.values().toArray(array);
1032        Assert.assertTrue(returnArray == array);
1033      }
1034      assertMappingsKeysEqual(array, map.values());
1035    }
1036  }
1037
1038  void testFastHashMapSetFast(Map map, boolean validate) {
1039    if (!(map instanceof FastHashMap)) { return; }
1040
1041    FastHashMap fastHashMap = (FastHashMap) map;
1042    if (validate) {
1043      Assert.assertTrue(fastHashMap.getFast());
1044    } else {
1045      synchronized (fastHashMap) {
1046        fastHashMap.setFast(true);
1047      }
1048    }
1049  }
1050
1051  // ReadOnly testing methods.
1052
void testReadOnlyPut(Map map, boolean validate) {
1053    if (map instanceof Hashtable JavaDoc) { return; }
1054    if (map instanceof FastHashMap) { return; }
1055
1056    if (validate) {
1057      assertEmptyMap(map);
1058    } else {
1059      synchronized (map) {
1060        try {
1061          Object JavaDoc prev = map.put("timmy", "teck");
1062          Assert.assertNull(prev);
1063          throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1064        } catch (ReadOnlyException re) {
1065          // expected
1066
}
1067      }
1068    }
1069  }
1070
1071  void testReadOnlyPutAll(Map map, boolean validate) {
1072    if (map instanceof Hashtable JavaDoc) { return; }
1073    if (map instanceof FastHashMap) { return; }
1074
1075    if (validate) {
1076      assertEmptyMap(map);
1077    } else {
1078      synchronized (map) {
1079        try {
1080          Map toAdd = new HashMap();
1081          addMappings(toAdd, 3);
1082          map.putAll(toAdd);
1083          throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1084        } catch (ReadOnlyException t) {
1085          // expected
1086
}
1087      }
1088    }
1089  }
1090
1091  // Setting up for the ReadOnly test for remove.
1092
void testSetUpRemove(Map map, boolean validate) {
1093    if (map instanceof Hashtable JavaDoc) { return; }
1094    if (map instanceof FastHashMap) { return; }
1095
1096    Map toAdd = new HashMap();
1097    addMappings(toAdd, 3);
1098
1099    if (validate) {
1100      assertMappings(toAdd, map);
1101    } else {
1102      synchronized (map) {
1103        map.putAll(toAdd);
1104      }
1105      tryReadOnlyRemove(map);
1106    }
1107
1108  }
1109
1110  // tryReadOnlyRemove() goes hand in hand with testSetUpRemove().
1111
private void tryReadOnlyRemove(Map map) {
1112    synchronized (map) {
1113      try {
1114        map.remove("key2");
1115        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1116      } catch (ReadOnlyException t) {
1117        // expected
1118
}
1119    }
1120  }
1121
1122  // Setting up for the ReadOnly test for clear.
1123
void testSetUpClear(Map map, boolean validate) {
1124    if (map instanceof Hashtable JavaDoc) { return; }
1125    if (map instanceof FastHashMap) { return; }
1126
1127    Map toAdd = new HashMap();
1128    addMappings(toAdd, 3);
1129
1130    if (validate) {
1131      assertMappings(toAdd, map);
1132    } else {
1133      synchronized (map) {
1134        map.putAll(toAdd);
1135      }
1136      tryReadOnlyClear(map);
1137    }
1138
1139  }
1140
1141  // tryReadOnlyClear() goes hand in hand with testSetUpClear().
1142
private void tryReadOnlyClear(Map map) {
1143    synchronized (map) {
1144      try {
1145        map.clear();
1146        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1147      } catch (ReadOnlyException t) {
1148        // expected
1149
}
1150    }
1151  }
1152
1153  // Setting up for the ReadOnly test for entry set clear.
1154
void testSetUpEntrySetClear(Map map, boolean validate) {
1155    if (map instanceof Hashtable JavaDoc) { return; }
1156    if (map instanceof FastHashMap) { return; }
1157
1158    Map toAdd = new HashMap();
1159    addMappings(toAdd, 3);
1160
1161    if (validate) {
1162      assertMappings(toAdd, map);
1163    } else {
1164      synchronized (map) {
1165        map.putAll(toAdd);
1166      }
1167      tryReadOnlyEntrySetClear(map);
1168    }
1169
1170  }
1171
1172  // tryReadOnlyEntrySetClear() goes hand in hand with testSetUpEntrySetClear().
1173
private void tryReadOnlyEntrySetClear(Map map) {
1174    synchronized (map) {
1175      Set JavaDoc entrySet = map.entrySet();
1176      try {
1177        entrySet.clear();
1178        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1179      } catch (ReadOnlyException t) {
1180        // expected
1181
}
1182    }
1183  }
1184
1185  // Setting up for the ReadOnly test for entry set remove.
1186
void testSetUpEntrySetRemove(Map map, boolean validate) {
1187    if (map instanceof Hashtable JavaDoc) { return; }
1188    if (map instanceof FastHashMap) { return; }
1189
1190    Map toAdd = new HashMap();
1191    addMappings(toAdd, 3);
1192
1193    if (validate) {
1194      assertMappings(toAdd, map);
1195    } else {
1196      synchronized (map) {
1197        map.putAll(toAdd);
1198      }
1199      tryReadOnlyEntrySetRemove(map);
1200    }
1201
1202  }
1203
1204  // tryReadOnlyEntrySetRemove() goes hand in hand with testSetUpEntrySetRemove().
1205
private void tryReadOnlyEntrySetRemove(Map map) {
1206    synchronized (map) {
1207      Set JavaDoc entrySet = map.entrySet();
1208      Iterator JavaDoc iterator = entrySet.iterator();
1209      Object JavaDoc o = iterator.next();
1210      try {
1211        entrySet.remove(o);
1212        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1213      } catch (ReadOnlyException t) {
1214        // expected
1215
}
1216    }
1217  }
1218
1219  // Setting up for the ReadOnly test for entry set retainAll.
1220
void testSetUpEntrySetRetainAll(Map map, boolean validate) {
1221    if (map instanceof Hashtable JavaDoc) { return; }
1222    if (map instanceof FastHashMap) { return; }
1223
1224    Map toAdd = new HashMap();
1225    addMappings(toAdd, 3);
1226
1227    if (validate) {
1228      assertMappings(toAdd, map);
1229    } else {
1230      synchronized (map) {
1231        map.putAll(toAdd);
1232      }
1233      tryReadOnlyEntrySetRetainAll(map);
1234    }
1235
1236  }
1237
1238  // tryReadOnlyEntrySetRetainAll() goes hand in hand with testSetUpEntrySetRetainAll().
1239
private void tryReadOnlyEntrySetRetainAll(Map map) {
1240    synchronized (map) {
1241      Set JavaDoc entrySet = map.entrySet();
1242      Object JavaDoc o = entrySet.iterator().next();
1243      Set JavaDoc retainAll = new HashSet JavaDoc();
1244      retainAll.add(o);
1245      try {
1246        entrySet.retainAll(retainAll);
1247        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1248      } catch (ReadOnlyException t) {
1249        // ignore the ReadOnlyException in test
1250
}
1251    }
1252  }
1253
1254  // Setting up for the ReadOnly test for entry set removeAll.
1255
void testSetUpEntrySetRemoveAll(Map map, boolean validate) {
1256    if (map instanceof Hashtable JavaDoc) { return; }
1257    if (map instanceof FastHashMap) { return; }
1258
1259    Map toAdd = new HashMap();
1260    addMappings(toAdd, 3);
1261
1262    if (validate) {
1263      assertMappings(toAdd, map);
1264    } else {
1265      synchronized (map) {
1266        map.putAll(toAdd);
1267      }
1268      tryReadOnlyEntrySetRemoveAll(map);
1269    }
1270
1271  }
1272
1273  // tryReadOnlyEntrySetRemoveAll() goes hand in hand with testSetUpEntrySetRemoveAll().
1274
private void tryReadOnlyEntrySetRemoveAll(Map map) {
1275    synchronized (map) {
1276      Set JavaDoc entrySet = map.entrySet();
1277      Object JavaDoc o = entrySet.iterator().next();
1278      Set JavaDoc removeAll = new HashSet JavaDoc();
1279      removeAll.add(o);
1280      try {
1281        entrySet.removeAll(removeAll);
1282        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1283      } catch (ReadOnlyException t) {
1284        // Expected
1285
}
1286    }
1287  }
1288
1289  // Setting up for the ReadOnly test for entry set iterator remove.
1290
void testSetUpEntrySetIteratorRemove(Map map, boolean validate) {
1291    if (map instanceof Hashtable JavaDoc) { return; }
1292    if (map instanceof FastHashMap) { return; }
1293
1294    Map toAdd = new HashMap();
1295    addMappings(toAdd, 3);
1296
1297    if (validate) {
1298      assertMappings(toAdd, map);
1299    } else {
1300      synchronized (map) {
1301        map.putAll(toAdd);
1302      }
1303      tryReadOnlyEntrySetIteratorRemove(map);
1304    }
1305
1306  }
1307
1308  // tryReadOnlyEntrySetIteratorRemove() goes hand in hand with testSetUpEntrySetIteratorRemove().
1309
private void tryReadOnlyEntrySetIteratorRemove(Map map) {
1310    synchronized (map) {
1311      Iterator JavaDoc iterator = map.entrySet().iterator();
1312      try {
1313        iterator.next();
1314        iterator.remove();
1315        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1316      } catch (ReadOnlyException t) {
1317        // Expected
1318
}
1319    }
1320  }
1321
1322  // Setting up for the ReadOnly test for entry set setValue.
1323
void testSetUpEntrySet(Map map, boolean validate) {
1324    if (map instanceof Hashtable JavaDoc) { return; }
1325    if (map instanceof FastHashMap) { return; }
1326
1327    Map toAdd = new HashMap();
1328    addMappings(toAdd, 3);
1329
1330    if (validate) {
1331      assertMappings(toAdd, map);
1332    } else {
1333      synchronized (map) {
1334        map.putAll(toAdd);
1335      }
1336      tryReadOnlyEntrySet(map);
1337    }
1338
1339  }
1340
1341  // tryReadOnlyEntrySet() goes hand in hand with testSetUpEntrySet().
1342
private void tryReadOnlyEntrySet(Map map) {
1343    synchronized (map) {
1344      Iterator JavaDoc iterator = map.entrySet().iterator();
1345      try {
1346        Entry entry = (Entry) iterator.next();
1347        entry.setValue("modified value");
1348        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1349      } catch (ReadOnlyException t) {
1350        // Expected
1351
}
1352    }
1353  }
1354
1355  // Setting up for the ReadOnly test for key set clear.
1356
void testSetUpKeySetClear(Map map, boolean validate) {
1357    if (map instanceof Hashtable JavaDoc) { return; }
1358    if (map instanceof FastHashMap) { return; }
1359
1360    Map toAdd = new HashMap();
1361    addMappings(toAdd, 3);
1362
1363    if (validate) {
1364      assertMappings(toAdd, map);
1365    } else {
1366      synchronized (map) {
1367        map.putAll(toAdd);
1368      }
1369      tryReadOnlyKeySetClear(map);
1370    }
1371
1372  }
1373
1374  // tryReadOnlyKeySetClear() goes hand in hand with testSetUpKeySetClear().
1375
private void tryReadOnlyKeySetClear(Map map) {
1376    synchronized (map) {
1377      Set JavaDoc keySet = map.keySet();
1378      try {
1379        keySet.clear();
1380        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1381      } catch (ReadOnlyException t) {
1382        // Expected
1383
}
1384    }
1385  }
1386
1387  // Setting up for the ReadOnly test for key set retainAll.
1388
void testSetUpKeySetRetainAll(Map map, boolean validate) {
1389    if (map instanceof Hashtable JavaDoc) { return; }
1390    if (map instanceof FastHashMap) { return; }
1391
1392    Map toAdd = new HashMap();
1393    addMappings(toAdd, 3);
1394
1395    if (validate) {
1396      assertMappings(toAdd, map);
1397    } else {
1398      synchronized (map) {
1399        map.putAll(toAdd);
1400      }
1401      tryReadOnlyKeySetRetainAll(map);
1402    }
1403
1404  }
1405
1406  // tryReadOnlyKeySetRetainAll() goes hand in hand with testSetUpKeySetRetainAll().
1407
private void tryReadOnlyKeySetRetainAll(Map map) {
1408    synchronized (map) {
1409      Set JavaDoc keySet = map.keySet();
1410      Object JavaDoc o = keySet.iterator().next();
1411      Set JavaDoc retainAll = new HashSet JavaDoc();
1412      retainAll.add(o);
1413      try {
1414        keySet.retainAll(retainAll);
1415        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1416      } catch (ReadOnlyException t) {
1417        // Expected
1418
}
1419    }
1420  }
1421
1422  // Setting up for the ReadOnly test for key set removeAll.
1423
void testSetUpKeySetRemoveAll(Map map, boolean validate) {
1424    if (map instanceof Hashtable JavaDoc) { return; }
1425    if (map instanceof FastHashMap) { return; }
1426
1427    Map toAdd = new HashMap();
1428    addMappings(toAdd, 3);
1429
1430    if (validate) {
1431      assertMappings(toAdd, map);
1432    } else {
1433      synchronized (map) {
1434        map.putAll(toAdd);
1435      }
1436      tryReadOnlyKeySetRemoveAll(map);
1437    }
1438
1439  }
1440
1441  // tryReadOnlyKeySetRemoveAll() goes hand in hand with testSetUpKeySetRemoveAll().
1442
private void tryReadOnlyKeySetRemoveAll(Map map) {
1443    synchronized (map) {
1444      Set JavaDoc keySet = map.keySet();
1445      Object JavaDoc o = keySet.iterator().next();
1446      Set JavaDoc removeAll = new HashSet JavaDoc();
1447      removeAll.add(o);
1448      try {
1449        keySet.removeAll(removeAll);
1450        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1451      } catch (ReadOnlyException t) {
1452        // Expected
1453
}
1454    }
1455  }
1456
1457  // Setting up for the ReadOnly test for key set iterator remove.
1458
void testSetUpKeySetIteratorRemove(Map map, boolean validate) {
1459    if (map instanceof Hashtable JavaDoc) { return; }
1460    if (map instanceof FastHashMap) { return; }
1461
1462    Map toAdd = new HashMap();
1463    addMappings(toAdd, 3);
1464
1465    if (validate) {
1466      assertMappings(toAdd, map);
1467    } else {
1468      synchronized (map) {
1469        map.putAll(toAdd);
1470      }
1471      tryReadOnlyKeySetIteratorRemove(map);
1472    }
1473  }
1474
1475  // tryReadOnlyKeySetIteratorRemove() goes hand in hand with testSetUpKeySetIteratorRemove().
1476
private void tryReadOnlyKeySetIteratorRemove(Map map) {
1477    synchronized (map) {
1478      Iterator JavaDoc iterator = map.keySet().iterator();
1479      try {
1480        iterator.next();
1481        iterator.remove();
1482        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1483      } catch (ReadOnlyException t) {
1484        // Expected
1485
}
1486    }
1487  }
1488
1489  // Setting up for the ReadOnly test for values clear.
1490
void testSetUpValuesClear(Map map, boolean validate) {
1491    if (map instanceof Hashtable JavaDoc) { return; }
1492    if (map instanceof FastHashMap) { return; }
1493
1494    Map toAdd = new HashMap();
1495    addMappings(toAdd, 3);
1496
1497    if (validate) {
1498      assertMappings(toAdd, map);
1499    } else {
1500      synchronized (map) {
1501        map.putAll(toAdd);
1502      }
1503      tryReadOnlyValuesClear(map);
1504    }
1505
1506  }
1507
1508  // tryReadOnlyValuesClear() goes hand in hand with testSetUpValuesClear().
1509
private void tryReadOnlyValuesClear(Map map) {
1510    synchronized (map) {
1511      Collection JavaDoc values = map.values();
1512      try {
1513        values.clear();
1514        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1515      } catch (ReadOnlyException t) {
1516        // Expected
1517
}
1518    }
1519  }
1520
1521  // Setting up for the ReadOnly test for values retainAll.
1522
void testSetUpValuesRetainAll(Map map, boolean validate) {
1523    if (map instanceof Hashtable JavaDoc) { return; }
1524    if (map instanceof FastHashMap) { return; }
1525
1526    Map toAdd = new HashMap();
1527    addMappings(toAdd, 3);
1528
1529    if (validate) {
1530      assertMappings(toAdd, map);
1531    } else {
1532      synchronized (map) {
1533        map.putAll(toAdd);
1534      }
1535      tryReadOnlyValuesRetainAll(map);
1536    }
1537
1538  }
1539
1540  // tryReadOnlyKeySetRetainAll() goes hand in hand with testSetUpValuesRetainAll().
1541
private void tryReadOnlyValuesRetainAll(Map map) {
1542    synchronized (map) {
1543      Collection JavaDoc values = map.values();
1544      Object JavaDoc o = values.iterator().next();
1545      Set JavaDoc retainAll = new HashSet JavaDoc();
1546      retainAll.add(o);
1547      try {
1548        values.retainAll(retainAll);
1549        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1550      } catch (ReadOnlyException t) {
1551        // Expected
1552
}
1553    }
1554  }
1555
1556  // Setting up for the ReadOnly test for values removeAll.
1557
void testSetUpValuesRemoveAll(Map map, boolean validate) {
1558    if (map instanceof Hashtable JavaDoc) { return; }
1559    if (map instanceof FastHashMap) { return; }
1560
1561    Map toAdd = new HashMap();
1562    addMappings(toAdd, 3);
1563
1564    if (validate) {
1565      assertMappings(toAdd, map);
1566    } else {
1567      synchronized (map) {
1568        map.putAll(toAdd);
1569      }
1570      tryReadOnlyValuesRemoveAll(map);
1571    }
1572
1573  }
1574
1575  // tryReadOnlyValuesRemoveAll() goes hand in hand with testSetUpValuesRemoveAll().
1576
private void tryReadOnlyValuesRemoveAll(Map map) {
1577    synchronized (map) {
1578      Collection JavaDoc values = map.values();
1579      Object JavaDoc o = values.iterator().next();
1580      Set JavaDoc removeAll = new HashSet JavaDoc();
1581      removeAll.add(o);
1582      try {
1583        values.removeAll(removeAll);
1584        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1585      } catch (ReadOnlyException t) {
1586        // Expected
1587
}
1588    }
1589  }
1590
1591  // Setting up for the ReadOnly test for values iterator remove.
1592
void testSetUpValuesIteratorRemove(Map map, boolean validate) {
1593    if (map instanceof Hashtable JavaDoc) { return; }
1594    if (map instanceof FastHashMap) { return; }
1595
1596    Map toAdd = new HashMap();
1597    addMappings(toAdd, 3);
1598
1599    if (validate) {
1600      assertMappings(toAdd, map);
1601    } else {
1602      synchronized (map) {
1603        map.putAll(toAdd);
1604      }
1605      tryReadOnlyValuesIteratorRemove(map);
1606    }
1607
1608  }
1609
1610  // tryReadOnlyValuesIteratorRemove() goes hand in hand with testSetUpValuesIteratorRemove().
1611
private void tryReadOnlyValuesIteratorRemove(Map map) {
1612    synchronized (map) {
1613      Iterator JavaDoc iterator = map.values().iterator();
1614      try {
1615        iterator.next();
1616        iterator.remove();
1617        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1618      } catch (ReadOnlyException t) {
1619        // Expected
1620
}
1621    }
1622  }
1623
1624  // THashMap specific testing methods.
1625
void testTHashMapRemoveAt(Map map, boolean validate) {
1626    if (!(map instanceof THashMap)) { return; }
1627
1628    Map toAdd = new HashMap();
1629    addMappings(toAdd, 2);
1630    if (validate) {
1631      assertMappings(toAdd, map);
1632    } else {
1633      synchronized (map) {
1634        map.putAll(toAdd);
1635        map.put("key2", "value2");
1636      }
1637      Class JavaDoc mapClass = TObjectHash.class;
1638      Class JavaDoc[] parameterType = new Class JavaDoc[1];
1639
1640      try {
1641        parameterType[0] = Object JavaDoc.class;
1642        Method JavaDoc m = mapClass.getDeclaredMethod("index", parameterType);
1643        m.setAccessible(true); // suppressing java access checking since removeRange is
1644
// a protected method.
1645
Object JavaDoc indexObj = m.invoke(map, new Object JavaDoc[] { "key2" });
1646
1647        synchronized (map) {
1648          parameterType[0] = Integer.TYPE;
1649          m = mapClass.getDeclaredMethod("removeAt", parameterType);
1650          m.setAccessible(true); // suppressing java access checking since removeRange is
1651
// a protected method.
1652

1653          m.invoke(map, new Object JavaDoc[] { indexObj });
1654        }
1655      } catch (NoSuchMethodException JavaDoc e) {
1656        // ignore NoSuchmethodExcpetion in test.
1657
} catch (IllegalArgumentException JavaDoc e) {
1658        // ignore IllegalArgumentException in test.
1659
} catch (IllegalAccessException JavaDoc e) {
1660        // ignore IllegalAccessException in test.
1661
} catch (InvocationTargetException JavaDoc e) {
1662        // ignore InvocationTargetException in test.
1663
}
1664    }
1665  }
1666
1667  void testTHashMapTransformValues(Map map, boolean validate) {
1668    if (!(map instanceof THashMap)) { return; }
1669    THashMap tMap = (THashMap) map;
1670    if (validate) {
1671      for (int i = 0; i < 10; i++) {
1672        Assert.assertEquals(new Integer JavaDoc(i + 1), tMap.get(String.valueOf(i)));
1673      }
1674    } else {
1675      synchronized (tMap) {
1676        for (int i = 0; i < 10; i++) {
1677          tMap.put(String.valueOf(i), new Integer JavaDoc(i));
1678        }
1679      }
1680      synchronized (tMap) {
1681        tMap.transformValues(new MyObjectFunction());
1682      }
1683    }
1684  }
1685
1686  void testPutNonPortableObject(Map map, boolean validate) {
1687    if (!canTestNonPortableObject(map)) { return; }
1688
1689    Map toBeAdded = new HashMap();
1690    toBeAdded.put("First", "First Value");
1691    toBeAdded.put("Second", "Second Value");
1692    if (validate) {
1693      assertMappings(toBeAdded, map);
1694    } else {
1695      synchronized (map) {
1696        map.putAll(toBeAdded);
1697      }
1698      synchronized (map) {
1699        try {
1700          map.put("Non-portable", new MyNonPortableObject());
1701          throw new AssertionError JavaDoc("Should have thrown a TCNonPortableObjectError.");
1702        } catch (TCNonPortableObjectError e) {
1703          //
1704
}
1705      }
1706    }
1707  }
1708
1709  void testLinkedHashMapAccessOrderGet(Map map, boolean validate) {
1710    if (!isAccessOrderedLinkedHashMap(map)) { return; }
1711
1712    if (validate) {
1713      Map toBeExpected = new LinkedHashMap JavaDoc();
1714      toBeExpected.put("First", "First Value");
1715      toBeExpected.put("Third", "Third Value");
1716      toBeExpected.put("Second", "Second Value"); // access order maps put recently accessed items at the end
1717
assertMappings(toBeExpected, map);
1718    } else {
1719      Map toBeAdded = new LinkedHashMap JavaDoc();
1720      toBeAdded.put("First", "First Value");
1721      toBeAdded.put("Second", "Second Value");
1722      toBeAdded.put("Third", "Third Value");
1723      synchronized (map) {
1724        map.putAll(toBeAdded);
1725      }
1726      synchronized (map) {
1727        Assert.assertEquals("Second Value", map.get("Second"));
1728      }
1729    }
1730  }
1731
1732  void testLinkedHashMapInsertionOrderPut(Map map, boolean validate) {
1733    if (!(map instanceof LinkedHashMap JavaDoc)) { return; }
1734
1735    // we only want insertion ordered maps in this test
1736
if (isAccessOrderedLinkedHashMap(map)) { return; }
1737
1738    if (validate) {
1739      Map toBeExpected = new LinkedHashMap JavaDoc();
1740      toBeExpected.put("First", "First Value");
1741      toBeExpected.put("Second", "New Second Value");
1742      toBeExpected.put("Third", "Third Value");
1743      assertMappings(toBeExpected, map);
1744    } else {
1745      Map toBeAdded = new LinkedHashMap JavaDoc();
1746      toBeAdded.put("First", "First Value");
1747      toBeAdded.put("Second", "Second Value");
1748      toBeAdded.put("Third", "Third Value");
1749      synchronized (map) {
1750        map.putAll(toBeAdded);
1751      }
1752      synchronized (map) {
1753        // replacing mapping should not affect order (for insertion order maps)
1754
map.put("Second", "New Second Value");
1755      }
1756    }
1757  }
1758
1759  void testLinkedHashMapInsertionOrderRemovePut(Map map, boolean validate) {
1760    if (!(map instanceof LinkedHashMap JavaDoc)) { return; }
1761
1762    // we only want insertion ordered maps in this test
1763
if (isAccessOrderedLinkedHashMap(map)) { return; }
1764
1765    if (validate) {
1766      Map toBeExpected = new LinkedHashMap JavaDoc();
1767      toBeExpected.put("First", "First Value");
1768      toBeExpected.put("Third", "Third Value");
1769      toBeExpected.put("Second", "New Second Value");
1770      assertMappings(toBeExpected, map);
1771    } else {
1772      Map toBeAdded = new LinkedHashMap JavaDoc();
1773      toBeAdded.put("First", "First Value");
1774      toBeAdded.put("Second", "Second Value");
1775      toBeAdded.put("Third", "Third Value");
1776      synchronized (map) {
1777        map.putAll(toBeAdded);
1778      }
1779      synchronized (map) {
1780        map.remove("Second");
1781        map.put("Second", "New Second Value");
1782      }
1783    }
1784  }
1785
1786  void testLinkedHashMapAccessOrderPut(Map map, boolean validate) {
1787    if (!isAccessOrderedLinkedHashMap(map)) { return; }
1788
1789    if (validate) {
1790      Map toBeExpected = new LinkedHashMap JavaDoc();
1791      toBeExpected.put("First", "First Value");
1792      toBeExpected.put("Third", "Third Value");
1793      toBeExpected.put("Second", "New Second Value"); // access order maps put recently accessed items at the end
1794
assertMappings(toBeExpected, map);
1795    } else {
1796      Map toBeAdded = new LinkedHashMap JavaDoc();
1797      toBeAdded.put("First", "First Value");
1798      toBeAdded.put("Second", "Second Value");
1799      toBeAdded.put("Third", "Third Value");
1800      synchronized (map) {
1801        map.putAll(toBeAdded);
1802      }
1803      synchronized (map) {
1804        // puts count as access on access order linked hash maps
1805
map.put("Second", "New Second Value");
1806      }
1807    }
1808  }
1809
1810  private boolean canTestSharedArray(Map map) {
1811    return (!(map instanceof HashMap) && !(map instanceof LinkedHashMap JavaDoc) && !(map instanceof Hashtable JavaDoc));
1812  }
1813
1814  private boolean canTestNonPortableObject(Map map) {
1815    return ((map instanceof HashMap) || (map instanceof LinkedHashMap JavaDoc) || (map instanceof Hashtable JavaDoc));
1816  }
1817
1818  /**
1819   * Getting the array for HashMap and LinkedHashMap from a non-shared map temporarily until we fixed HashMap and
1820   * LinkedHashMap.
1821   */

1822  private Object JavaDoc getMySubclassArray(Map map) {
1823    if (map instanceof MyLinkedHashMap3) { return nonSharedArrayMap.get("arrayforMyLinkedHashMap3"); }
1824    if (map instanceof MyLinkedHashMap2) { return nonSharedArrayMap.get("arrayforMyLinkedHashMap2"); }
1825    if (map instanceof MyLinkedHashMap) { return nonSharedArrayMap.get("arrayforMyLinkedHashMap"); }
1826    if (map instanceof MyHashMap2) { return nonSharedArrayMap.get("arrayforMyHashMap2"); }
1827    if (map instanceof MyHashMap) { return nonSharedArrayMap.get("arrayforMyHashMap"); }
1828    if (map instanceof MyHashMap3) { return sharedMap.get("arrayforMyHashMap3"); }
1829    if (map instanceof MyTreeMap) { return sharedMap.get("arrayforMyTreeMap"); }
1830    if (map instanceof MyTreeMap2) { return sharedMap.get("arrayforMyTreeMap"); }
1831    if (map instanceof MyHashtable2) { return nonSharedArrayMap.get("arrayforMyHashtable2"); }
1832    if (map instanceof MyHashtable) { return nonSharedArrayMap.get("arrayforMyHashtable"); }
1833    if (map instanceof MyTHashMap) { return sharedMap.get("arrayforMyTHashMap"); }
1834    if (map instanceof MyFastHashMap) { return nonSharedArrayMap.get("arrayforMyFastHashMap"); }
1835    if (map instanceof MyProperties2) { return nonSharedArrayMap.get("arrayforMyProperties2"); }
1836    if (map instanceof MyProperties3) { return nonSharedArrayMap.get("arrayforMyProperties3"); }
1837    if (map instanceof MyProperties) { return nonSharedArrayMap.get("arrayforMyProperties"); }
1838    return null;
1839  }
1840
1841  /**
1842   * Getting the array for HashMap and LinkedHashMap from a non-shared map temporarily until we fixed HashMap and
1843   * LinkedHashMap.
1844   */

1845  private Object JavaDoc[] getArray(Map map) {
1846    Object JavaDoc o = getMySubclassArray(map);
1847    if (o != null) { return (Object JavaDoc[]) o; }
1848
1849    if (map instanceof Properties JavaDoc) { return (Object JavaDoc[]) nonSharedArrayMap.get("arrayforProperties"); }
1850    if (map instanceof FastHashMap) {
1851      if (((FastHashMap) map).getFast()) {
1852        return (Object JavaDoc[]) nonSharedArrayMap.get("arrayforFastHashMapWithFast");
1853      } else {
1854        return (Object JavaDoc[]) nonSharedArrayMap.get("arrayforFastHashMap");
1855      }
1856    } else if (map instanceof LinkedHashMap JavaDoc) {
1857      return (Object JavaDoc[]) nonSharedArrayMap.get("arrayforLinkedHashMap");
1858    } else if (map instanceof HashMap) {
1859      return (Object JavaDoc[]) nonSharedArrayMap.get("arrayforHashMap");
1860    } else if (map instanceof Hashtable JavaDoc) {
1861      return (Object JavaDoc[]) nonSharedArrayMap.get("arrayforHashtable");
1862    } else if (map instanceof TreeMap JavaDoc) {
1863      return (Object JavaDoc[]) sharedMap.get("arrayforTreeMap");
1864    } else if (map instanceof THashMap) { return (Object JavaDoc[]) sharedMap.get("arrayforTHashMap"); }
1865    return null;
1866  }
1867
1868  void assertMappingsKeysEqual(Object JavaDoc[] expect, Collection JavaDoc collection) {
1869    Assert.assertEquals(expect.length, collection.size());
1870    for (int i = 0; i < expect.length; i++) {
1871      Assert.assertTrue(collection.contains(expect[i]));
1872    }
1873  }
1874
1875  void assertMappings(Map expect, Map actual) {
1876    Assert.assertEquals(expect.size(), actual.size());
1877
1878    Set JavaDoc expectEntries = expect.entrySet();
1879    Set JavaDoc actualEntries = actual.entrySet();
1880    if (actual instanceof LinkedHashMap JavaDoc) {
1881      for (Iterator JavaDoc iExpect = expectEntries.iterator(), iActual = actualEntries.iterator(); iExpect.hasNext();) {
1882        Assert.assertEquals(iExpect.next(), iActual.next());
1883      }
1884    }
1885
1886    for (Iterator JavaDoc i = actualEntries.iterator(); i.hasNext();) {
1887      Entry entry = (Entry) i.next();
1888      Assert.assertEquals(entry.getValue(), expect.get(entry.getKey()));
1889    }
1890  }
1891
1892  void assertMappingsEqual(Object JavaDoc[] expect, Map map) {
1893    Assert.assertEquals(expect.length, map.size());
1894
1895    if (map instanceof LinkedHashMap JavaDoc) {
1896      Set JavaDoc entries = map.entrySet();
1897      int i = 0;
1898      for (Iterator JavaDoc iActual = entries.iterator(); iActual.hasNext();) {
1899        Assert.assertEquals(expect[i++], iActual.next());
1900      }
1901    }
1902
1903    for (int i = 0; i < expect.length; i++) {
1904      Entry entry = (Entry) expect[i];
1905      if (isAccessOrderedLinkedHashMap(map)) {
1906        synchronized (map) {
1907          Assert.assertEquals(entry.getValue(), map.get(entry.getKey()));
1908        }
1909      } else {
1910        Assert.assertEquals(entry.getValue(), map.get(entry.getKey()));
1911      }
1912    }
1913  }
1914
1915  void assertSingleMapping(Map map, final Object JavaDoc key, final Object JavaDoc value) {
1916    try {
1917      Assert.assertFalse(map.isEmpty());
1918      Assert.assertEquals(1, map.size());
1919      Assert.assertEquals(1, map.entrySet().size());
1920      Assert.assertEquals(1, map.values().size());
1921      Assert.assertEquals(1, map.keySet().size());
1922
1923      if (isAccessOrderedLinkedHashMap(map)) {
1924        // MyLinkedHashMap3 has accessOrder set to true; therefore, get() method
1925
// will mutate internal state and thus, require synchronized.
1926
synchronized (map) {
1927          Assert.assertEquals(value, map.get(key));
1928        }
1929      } else {
1930        Assert.assertEquals(value, map.get(key));
1931      }
1932      Assert.assertTrue(map.containsKey(key));
1933      Assert.assertTrue(map.containsValue(value));
1934
1935      Set JavaDoc entries = map.entrySet();
1936      for (Iterator JavaDoc i = entries.iterator(); i.hasNext();) {
1937        Entry entry = (Entry) i.next();
1938        Assert.assertEquals(key, entry.getKey());
1939        Assert.assertEquals(value, entry.getValue());
1940      }
1941
1942      for (Iterator JavaDoc i = map.values().iterator(); i.hasNext();) {
1943        Object JavaDoc o = i.next();
1944        Assert.assertEquals(value, o);
1945      }
1946
1947      for (Iterator JavaDoc i = map.keySet().iterator(); i.hasNext();) {
1948        Object JavaDoc o = i.next();
1949        Assert.assertEquals(key, o);
1950      }
1951    } catch (Throwable JavaDoc t) {
1952      notifyError(new ErrorContext(map.getClass().getName(), t));
1953    }
1954  }
1955
1956  void assertEmptyMap(Map map) {
1957    try {
1958      Assert.assertTrue(map.isEmpty());
1959      Assert.assertEquals(0, map.size());
1960      Assert.assertEquals(0, map.entrySet().size());
1961      Assert.assertEquals(0, map.values().size());
1962      Assert.assertEquals(0, map.keySet().size());
1963      Assert.assertEquals(Collections.EMPTY_MAP, map);
1964    } catch (Throwable JavaDoc t) {
1965      notifyError(new ErrorContext(map.getClass().getName(), t));
1966    }
1967  }
1968
1969  private void addMappings(Map map, int count) {
1970    synchronized (map) {
1971      for (int i = 0; i < count; i++) {
1972        map.put("key" + i, "value" + i);
1973      }
1974    }
1975  }
1976
1977  private Map getOrderSensitiveMappings() {
1978    Map map = null;
1979    map = new HashMap();
1980    map.put("January", "Jan");
1981    map.put("February", "Feb");
1982    map.put("March", "Mar");
1983    map.put("April", "Apr");
1984    return map;
1985  }
1986
1987  private boolean isAccessOrderedLinkedHashMap(Map map) {
1988    if (map instanceof LinkedHashMap JavaDoc) {
1989      try {
1990        Field JavaDoc f = LinkedHashMap JavaDoc.class.getDeclaredField("accessOrder");
1991        f.setAccessible(true);
1992        return ((Boolean JavaDoc) f.get(map)).booleanValue();
1993      } catch (Throwable JavaDoc t) {
1994        throw new RuntimeException JavaDoc(t);
1995      }
1996    }
1997
1998    return false;
1999  }
2000
2001  private static boolean allowsNull(Map map) {
2002    return !(map instanceof Hashtable JavaDoc);
2003  }
2004
2005  // Used to determine if a specific key instance is retained or not
2006
private static class Key implements Comparable JavaDoc {
2007    private final String JavaDoc id;
2008    private final String JavaDoc equals;
2009
2010    Key(String JavaDoc id, String JavaDoc equals) {
2011      this.id = id;
2012      this.equals = equals;
2013    }
2014
2015    public int hashCode() {
2016      return equals == null ? 0 : equals.hashCode();
2017    }
2018
2019    public boolean equals(Object JavaDoc o) {
2020      if (!(o instanceof Key)) { return false; }
2021      Key other = (Key) o;
2022      if (this.equals == null && other.equals == null) { return true; }
2023      if (this.equals == null && other.equals != null) { return false; }
2024      if (other.equals == null && this.equals != null) { return false; }
2025      return this.equals.equals(other.equals);
2026    }
2027
2028    public int compareTo(Object JavaDoc o) {
2029      Key cmp = (Key) o;
2030      return equals.compareTo(cmp.equals);
2031    }
2032
2033  }
2034
2035  private static class NullTolerantComparator implements Comparator JavaDoc {
2036
2037    public int compare(Object JavaDoc o1, Object JavaDoc o2) {
2038      if (o1 == null && o2 == null) { return 0; }
2039      if (o1 == null && o2 != null) { return -1; }
2040      if (o1 != null && o2 == null) { return 1; }
2041      return ((Comparable JavaDoc) o1).compareTo(o2);
2042    }
2043  }
2044
2045  private static class SimpleEntry implements Map.Entry {
2046    // NOTE: this class more or less copied from Sun's source to java/util/AbstractMap
2047

2048    private final Object JavaDoc key;
2049    private Object JavaDoc value;
2050
2051    public SimpleEntry(Object JavaDoc key, Object JavaDoc value) {
2052      this.key = key;
2053      this.value = value;
2054    }
2055
2056    public SimpleEntry(Map.Entry e) {
2057      this.key = e.getKey();
2058      this.value = e.getValue();
2059    }
2060
2061    public Object JavaDoc getKey() {
2062      return key;
2063    }
2064
2065    public Object JavaDoc getValue() {
2066      return value;
2067    }
2068
2069    public Object JavaDoc setValue(Object JavaDoc value) {
2070      Object JavaDoc oldValue = this.value;
2071      this.value = value;
2072      return oldValue;
2073    }
2074
2075    public boolean equals(Object JavaDoc o) {
2076      if (!(o instanceof Map.Entry)) return false;
2077      Map.Entry e = (Map.Entry) o;
2078      return eq(key, e.getKey()) && eq(value, e.getValue());
2079    }
2080
2081    public int hashCode() {
2082      return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
2083    }
2084
2085    public String JavaDoc toString() {
2086      return key + "=" + value;
2087    }
2088
2089    private static boolean eq(Object JavaDoc o1, Object JavaDoc o2) {
2090      return (o1 == null ? o2 == null : o1.equals(o2));
2091    }
2092  }
2093
2094  private static class MyHashMap extends HashMap {
2095    protected Object JavaDoc key;
2096    protected Object JavaDoc value;
2097
2098    public MyHashMap(int k) {
2099      super(11);
2100    }
2101
2102    public MyHashMap(Map map) {
2103      putAll(map);
2104    }
2105
2106    public Object JavaDoc put(Object JavaDoc arg0, Object JavaDoc arg1) {
2107      this.key = arg0;
2108      this.value = arg1;
2109      return super.put(arg0, arg1);
2110    }
2111
2112    public Object JavaDoc getKey() {
2113      return key;
2114    }
2115
2116    public Object JavaDoc getValue() {
2117      return value;
2118    }
2119
2120    public Object JavaDoc getObject(Object JavaDoc arg) {
2121      return super.get(arg);
2122    }
2123
2124    protected void testProtected() {
2125      // do nothing
2126
}
2127
2128    private void readObject(java.io.ObjectInputStream JavaDoc s) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
2129      s.defaultReadObject();
2130    }
2131
2132    private void writeObject(java.io.ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
2133      s.defaultWriteObject();
2134    }
2135  }
2136
2137  private static class MyHashMap2 extends MyHashMap {
2138    public MyHashMap2() {
2139      super(12);
2140    }
2141  }
2142
2143  private static class MyTreeMap extends TreeMap JavaDoc {
2144    public MyTreeMap() {
2145      super();
2146    }
2147
2148    public MyTreeMap(Comparator JavaDoc comparator) {
2149      super(comparator);
2150    }
2151  }
2152
2153  private static class MyHashMap3 extends HashMap {
2154    private int i;
2155
2156    public MyHashMap3(int i) {
2157      super();
2158      this.i = i;
2159    }
2160
2161    public int getI() {
2162      // this method here to silence compiler warning, no other reason
2163
return i;
2164    }
2165  }
2166
2167  private static class MyTreeMap2 extends MyTreeMap {
2168    private Object JavaDoc key;
2169    private Object JavaDoc value;
2170
2171    public MyTreeMap2(Comparator JavaDoc comparator) {
2172      super(comparator);
2173    }
2174
2175    public Object JavaDoc put(Object JavaDoc arg0, Object JavaDoc arg1) {
2176      this.key = arg0;
2177      this.value = arg1;
2178      return super.put(arg0, arg1);
2179    }
2180
2181    public Object JavaDoc getKey() {
2182      return key;
2183    }
2184
2185    public Object JavaDoc getValue() {
2186      return value;
2187    }
2188  }
2189
2190  private static class MyHashtable extends Hashtable JavaDoc {
2191    public MyHashtable() {
2192      super();
2193    }
2194
2195  }
2196
2197  private static class MyHashtable2 extends MyHashtable {
2198    public MyHashtable2() {
2199      super();
2200    }
2201  }
2202
2203  private static class MyLinkedHashMap extends LinkedHashMap JavaDoc {
2204    public MyLinkedHashMap() {
2205      super();
2206    }
2207  }
2208
2209  private static class MyLinkedHashMap2 extends MyLinkedHashMap {
2210    private Object JavaDoc key;
2211    private Object JavaDoc value;
2212
2213    public MyLinkedHashMap2() {
2214      super();
2215    }
2216
2217    public Object JavaDoc put(Object JavaDoc arg0, Object JavaDoc arg1) {
2218      this.key = arg0;
2219      this.value = arg1;
2220      return super.put(arg0, arg1);
2221    }
2222
2223    public Object JavaDoc getKey() {
2224      return key;
2225    }
2226
2227    public Object JavaDoc getValue() {
2228      return value;
2229    }
2230  }
2231
2232  private static class MyLinkedHashMap3 extends LinkedHashMap JavaDoc {
2233    public MyLinkedHashMap3(boolean accessOrder) {
2234      super(10, 0.75f, accessOrder);
2235    }
2236  }
2237
2238  private static class MyTHashMap extends THashMap {
2239    public MyTHashMap() {
2240      super();
2241    }
2242  }
2243
2244  private static class MyFastHashMap extends FastHashMap {
2245    public MyFastHashMap() {
2246      super();
2247    }
2248  }
2249
2250  private static class MyProperties extends Properties JavaDoc {
2251    private Object JavaDoc key;
2252    private Object JavaDoc value;
2253
2254    public MyProperties() {
2255      super();
2256    }
2257
2258    public Object JavaDoc put(Object JavaDoc arg0, Object JavaDoc arg1) {
2259      this.key = arg0;
2260      this.value = arg1;
2261      return super.put(arg0, arg1);
2262    }
2263
2264    public Object JavaDoc getKey() {
2265      return key;
2266    }
2267
2268    public Object JavaDoc getValue() {
2269      return value;
2270    }
2271  }
2272
2273  private static class MyProperties2 extends MyProperties {
2274    private Object JavaDoc lastGetKey;
2275
2276    public MyProperties2() {
2277      super();
2278    }
2279
2280    public synchronized Object JavaDoc get(Object JavaDoc key) {
2281      this.lastGetKey = key;
2282      return super.get(key);
2283    }
2284
2285    public Object JavaDoc getLastGetKey() {
2286      return lastGetKey;
2287    }
2288  }
2289  
2290  private static class MyProperties3 extends Properties JavaDoc {
2291    public MyProperties3() {
2292      super();
2293    }
2294    
2295    public void setDefault(Properties JavaDoc newDefaults) {
2296      defaults = newDefaults;
2297    }
2298
2299    public Object JavaDoc put(Object JavaDoc arg0, Object JavaDoc arg1) {
2300      return super.put(arg0, arg1);
2301    }
2302  }
2303
2304  private static class MyObjectFunction implements TObjectFunction {
2305
2306    public Object JavaDoc execute(Object JavaDoc value) {
2307      return new Integer JavaDoc(((Integer JavaDoc) value).intValue() + 1);
2308    }
2309
2310  }
2311
2312  private static class MyNonPortableObject {
2313    public MyNonPortableObject() {
2314      super();
2315    }
2316  }
2317
2318}
2319
Popular Tags