KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ConcurrentHashMapTestApp


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

4 package com.tctest;
5
6 import com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.object.config.TransparencyClassSpec;
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.listener.ListenerProvider;
11 import com.tc.util.Assert;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.Map.Entry;
21 import java.util.concurrent.ConcurrentHashMap JavaDoc;
22
23 public class ConcurrentHashMapTestApp extends GenericTestApp {
24
25   private final DataKey[] keyRoots = new DataKey[]{ new DataKey(1), new DataKey(2), new DataKey(3), new DataKey(4)};
26   private final DataValue[] valueRoots = new DataValue[]{ new DataValue(10), new DataValue(20), new DataValue(30), new DataValue(40) };
27   
28   private final HashKey[] hashKeys = new HashKey[]{ new HashKey(1), new HashKey(2), new HashKey(3), new HashKey(4)};
29   private final HashValue[] hashValues = new HashValue[]{ new HashValue(10), new HashValue(20), new HashValue(30), new HashValue(40) };
30   
31   public ConcurrentHashMapTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
32     super(appId, cfg, listenerProvider, ConcurrentHashMap JavaDoc.class);
33   }
34
35   protected Object JavaDoc getTestObject(String JavaDoc test) {
36     return sharedMap.get("map");
37   }
38
39   protected void setupTestObject(String JavaDoc test) {
40     List JavaDoc listOfMaps = new ArrayList JavaDoc();
41     listOfMaps.add(new ConcurrentHashMap JavaDoc());
42     sharedMap.put("maps", listOfMaps);
43     sharedMap.put("map", new ConcurrentHashMap JavaDoc());
44     sharedMap.put("arrayforConcurrentHashMap", new Object JavaDoc[4]);
45     sharedMap.put("arrayforConcurrentHashMapWithHashKeys", new Object JavaDoc[4]);
46   }
47   
48   void testPut1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
49     if (validate) {
50       Assert.assertFalse(map.isEmpty());
51       Assert.assertEquals(1, map.size());
52       Assert.assertEquals(20, ((DataValue) (map.get(keyRoots[0]))).getInt());
53     } else {
54       DataValue value1 = new DataValue(10);
55       DataValue value2 = new DataValue(20);
56       Object JavaDoc o = map.put(keyRoots[0], value1);
57       Assert.assertNull(o);
58
59       o = map.put(keyRoots[0], value2);
60       Assert.assertTrue(o == value1);
61     }
62   }
63   
64   void testPut2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
65     if (validate) {
66       Assert.assertFalse(map.isEmpty());
67       Assert.assertEquals(1, map.size());
68       Assert.assertEquals(hashValues[1], map.get(hashKeys[0]));
69     } else {
70       Object JavaDoc o = map.put(hashKeys[0], hashValues[0]);
71       Assert.assertNull(o);
72
73       o = map.put(hashKeys[0], hashValues[1]);
74       Assert.assertTrue(o == hashValues[0]);
75     }
76   }
77
78   void testPutIfAbsent(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
79     if (validate) {
80       Assert.assertFalse(map.isEmpty());
81       Assert.assertEquals(1, map.size());
82       Assert.assertEquals(10, ((DataValue) (map.get(keyRoots[0]))).getInt());
83     } else {
84       DataValue value1 = new DataValue(10);
85       DataValue value2 = new DataValue(20);
86       Object JavaDoc o = map.put(keyRoots[0], value1);
87
88       o = map.putIfAbsent(keyRoots[0], value2);
89       Assert.assertTrue(o == value1);
90     }
91   }
92
93   void testPutIfAbsent2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
94     if (validate) {
95       Assert.assertFalse(map.isEmpty());
96       Assert.assertEquals(1, map.size());
97       Assert.assertEquals(hashValues[0], map.get(hashKeys[0]));
98     } else {
99       Object JavaDoc o = map.put(hashKeys[0], hashValues[0]);
100
101       o = map.putIfAbsent(hashKeys[0], hashValues[1]);
102       Assert.assertTrue(o == hashValues[0]);
103     }
104   }
105   
106   void testPutAll1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
107     Map JavaDoc toPut = new HashMap JavaDoc();
108     toPut.put(keyRoots[0], valueRoots[0]);
109     toPut.put(keyRoots[1], valueRoots[1]);
110     toPut.put(keyRoots[2], valueRoots[2]);
111     toPut.put(keyRoots[3], valueRoots[3]);
112
113     if (validate) {
114       assertMappingsEqual(toPut, map);
115     } else {
116       map.putAll(toPut);
117     }
118   }
119   
120   void testPutAll2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
121     Map JavaDoc toPut = new HashMap JavaDoc();
122     toPut.put(hashKeys[0], hashValues[0]);
123     toPut.put(hashKeys[1], hashValues[1]);
124     toPut.put(hashKeys[2], hashValues[2]);
125     toPut.put(hashKeys[3], hashValues[3]);
126
127     if (validate) {
128       assertMappingsHashEqual(toPut, map);
129     } else {
130       map.putAll(toPut);
131     }
132   }
133
134   void testRemove1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
135     Map JavaDoc toPut = new HashMap JavaDoc();
136     toPut.put(keyRoots[0], valueRoots[0]);
137     toPut.put(keyRoots[1], valueRoots[1]);
138     toPut.put(keyRoots[2], valueRoots[2]);
139     toPut.put(keyRoots[3], valueRoots[3]);
140
141     if (validate) {
142       toPut.remove(keyRoots[1]);
143       assertMappingsEqual(toPut, map);
144     } else {
145       map.putAll(toPut);
146       map.remove(keyRoots[1]);
147     }
148   }
149
150   void testHashRemove1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
151     Map JavaDoc toPut = new HashMap JavaDoc();
152     toPut.put(hashKeys[0], hashValues[0]);
153     toPut.put(hashKeys[1], hashValues[1]);
154     toPut.put(hashKeys[2], hashValues[2]);
155     toPut.put(hashKeys[3], hashValues[3]);
156
157     if (validate) {
158       toPut.remove(hashKeys[1]);
159       assertMappingsHashEqual(toPut, map);
160     } else {
161       map.putAll(toPut);
162       map.remove(hashKeys[1]);
163     }
164   }
165
166   void testRemove2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
167     Map JavaDoc toPut = new HashMap JavaDoc();
168     toPut.put(keyRoots[0], valueRoots[0]);
169     toPut.put(keyRoots[1], valueRoots[1]);
170     toPut.put(keyRoots[2], valueRoots[2]);
171     toPut.put(keyRoots[3], valueRoots[3]);
172
173     if (validate) {
174       assertMappingsEqual(toPut, map);
175     } else {
176       map.putAll(toPut);
177       map.remove(keyRoots[1], new DataValue(30));
178     }
179   }
180   
181   void testHashRemove2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
182     Map JavaDoc toPut = new HashMap JavaDoc();
183     toPut.put(hashKeys[0], hashValues[0]);
184     toPut.put(hashKeys[1], hashValues[1]);
185     toPut.put(hashKeys[2], hashValues[2]);
186     toPut.put(hashKeys[3], hashValues[3]);
187
188     if (validate) {
189       assertMappingsHashEqual(toPut, map);
190     } else {
191       map.putAll(toPut);
192       map.remove(hashKeys[1], new HashValue(30));
193     }
194   }
195   
196   void testRemove3(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
197     Map JavaDoc toPut = new HashMap JavaDoc();
198     toPut.put(keyRoots[0], valueRoots[0]);
199     toPut.put(keyRoots[1], valueRoots[1]);
200     toPut.put(keyRoots[2], valueRoots[2]);
201     toPut.put(keyRoots[3], valueRoots[3]);
202
203     if (validate) {
204       toPut.remove(keyRoots[3]);
205       assertMappingsEqual(toPut, map);
206     } else {
207       map.putAll(toPut);
208       map.remove(keyRoots[3], valueRoots[3]);
209     }
210   }
211   
212   void testHashRemove3(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
213     Map JavaDoc toPut = new HashMap JavaDoc();
214     toPut.put(hashKeys[0], hashValues[0]);
215     toPut.put(hashKeys[1], hashValues[1]);
216     toPut.put(hashKeys[2], hashValues[2]);
217     toPut.put(hashKeys[3], hashValues[3]);
218
219     if (validate) {
220       toPut.remove(hashKeys[3]);
221       assertMappingsHashEqual(toPut, map);
222     } else {
223       map.putAll(toPut);
224       //map.remove(hashKeys[3], new HashValue(40));
225
map.remove(hashKeys[3], hashValues[3]);
226     }
227   }
228   
229   void testReplace1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
230     if (validate) {
231       Assert.assertEquals(10, ((DataValue) map.get(keyRoots[0])).getInt());
232     } else {
233       DataValue value1 = new DataValue(10);
234       Object JavaDoc o = map.put(keyRoots[0], value1);
235       Assert.assertNull(o);
236       o = map.replace(new DataKey(1), new DataValue(20));
237       Assert.assertNull(o);
238     }
239   }
240   
241   void testHashReplace1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
242     if (validate) {
243       assertSingleHashMapping(hashKeys[0], hashValues[0], map);
244     } else {
245       Object JavaDoc o = map.put(hashKeys[0], hashValues[0]);
246       Assert.assertNull(o);
247       o = map.replace(hashKeys[1], hashValues[1]);
248       Assert.assertNull(o);
249     }
250   }
251
252   void testReplace2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
253     if (validate) {
254       Assert.assertEquals(20, ((DataValue) map.get(keyRoots[0])).getInt());
255     } else {
256       DataValue value1 = new DataValue(10);
257       Object JavaDoc o = map.put(keyRoots[0], value1);
258       Assert.assertNull(o);
259       o = map.replace(keyRoots[0], new DataValue(20));
260       Assert.assertEquals(10, ((DataValue) o).getInt());
261     }
262   }
263
264   void testHashReplace2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
265     if (validate) {
266       assertSingleHashMapping(hashKeys[0], hashValues[1], map);
267     } else {
268       Object JavaDoc o = map.put(hashKeys[0], hashValues[0]);
269       Assert.assertNull(o);
270       Object JavaDoc o2 = new HashKey(1);
271       o = map.replace(o2, new HashValue(20));
272       Assert.assertEquals(o, hashValues[0]);
273     }
274   }
275   
276   void testReplaceIfValueEqual1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
277     if (validate) {
278       Assert.assertEquals(valueRoots[0], map.get(keyRoots[0]));
279     } else {
280       Object JavaDoc o = map.put(keyRoots[0], valueRoots[0]);
281       Assert.assertNull(o);
282       boolean returnValue = map.replace(keyRoots[0], new DataValue(10), new DataValue(20));
283       Assert.assertFalse(returnValue);
284     }
285   }
286   
287   void testHashReplaceIfValueEqual1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
288     if (validate) {
289       Assert.assertEquals(hashValues[0], map.get(hashKeys[0]));
290     } else {
291       Object JavaDoc o = map.put(hashKeys[0], hashValues[0]);
292       Assert.assertNull(o);
293       boolean returnValue = map.replace(new HashKey(1), new HashValue(15), new DataValue(20));
294       Assert.assertFalse(returnValue);
295     }
296   }
297   
298   void testReplaceIfValueEqual2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
299     if (validate) {
300       Assert.assertEquals(20, ((DataValue) map.get(keyRoots[0])).getInt());
301     } else {
302       Object JavaDoc o = map.put(keyRoots[0], valueRoots[0]);
303       Assert.assertNull(o);
304       boolean returnValue = map.replace(keyRoots[0], valueRoots[0], new DataValue(20));
305       Assert.assertTrue(returnValue);
306     }
307   }
308   
309   void testHashReplaceIfValueEqual2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
310     if (validate) {
311       Assert.assertEquals(hashValues[1], map.get(hashKeys[0]));
312     } else {
313       Object JavaDoc o = map.put(hashKeys[0], hashValues[0]);
314       Assert.assertNull(o);
315       boolean returnValue = map.replace(new HashKey(1), new HashValue(10), new HashValue(20));
316       Assert.assertTrue(returnValue);
317     }
318   }
319
320   void testContains1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
321     if (validate) {
322       Assert.assertTrue(map.containsKey(keyRoots[0]));
323       Assert.assertFalse(map.containsKey(new DataKey(1)));
324
325       Assert.assertTrue(map.containsValue(valueRoots[0]));
326       Assert.assertFalse(map.containsValue(new DataValue(10)));
327
328       Assert.assertTrue(map.contains(valueRoots[0]));
329       Assert.assertFalse(map.contains(new DataValue(10)));
330     } else {
331       map.put(keyRoots[0], valueRoots[0]);
332     }
333   }
334   
335   void testContains2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
336     if (validate) {
337       Assert.assertTrue(map.containsKey(hashKeys[0]));
338       Assert.assertTrue(map.containsKey(new HashKey(1)));
339
340       Assert.assertTrue(map.containsValue(hashValues[0]));
341       Assert.assertTrue(map.containsValue(new HashValue(10)));
342
343       Assert.assertTrue(map.contains(hashValues[0]));
344       Assert.assertTrue(map.contains(new HashValue(10)));
345     } else {
346       map.put(hashKeys[0], hashValues[0]);
347     }
348   }
349   
350   void testEntrySetClear(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
351     Map JavaDoc toPut = new HashMap JavaDoc();
352     DataKey key1 = new DataKey(1);
353     DataKey key2 = new DataKey(2);
354     DataKey key3 = new DataKey(3);
355
356     DataValue value1 = new DataValue(10);
357     DataValue value2 = new DataValue(20);
358     DataValue value3 = new DataValue(30);
359     toPut.put(key1, value1);
360     toPut.put(key2, value2);
361     toPut.put(key3, value3);
362     if (validate) {
363       Assert.assertEquals(0, map.size());
364     } else {
365       map.putAll(toPut);
366       
367       map.entrySet().clear();
368     }
369   }
370   
371   void testEntrySetContains1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
372     SimpleEntry entry = new SimpleEntry(keyRoots[0], valueRoots[0]);
373     if (validate) {
374       Assert.assertTrue(map.entrySet().contains(entry));
375     } else {
376       map.put(keyRoots[0], valueRoots[0]);
377     }
378   }
379   
380   void testEntrySetContains2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
381     SimpleEntry entry = new SimpleEntry(new HashKey(1), new HashValue(10));
382     if (validate) {
383       Assert.assertTrue(map.entrySet().contains(entry));
384     } else {
385       map.put(hashKeys[0], hashValues[0]);
386     }
387   }
388   
389   void testEntrySetContainsAll1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
390     Map JavaDoc toPut = new HashMap JavaDoc();
391     toPut.put(keyRoots[0], valueRoots[0]);
392     toPut.put(keyRoots[1], valueRoots[1]);
393     toPut.put(keyRoots[2], valueRoots[2]);
394     toPut.put(keyRoots[3], valueRoots[3]);
395  
396     if (validate) {
397       SimpleEntry entry1 = new SimpleEntry(keyRoots[1], valueRoots[1]);
398       SimpleEntry entry2 = new SimpleEntry(keyRoots[2], valueRoots[2]);
399       List JavaDoc containsList = new ArrayList JavaDoc(2);
400       containsList.add(entry1);
401       containsList.add(entry2);
402       Assert.assertTrue(map.entrySet().containsAll(containsList));
403     } else {
404       map.putAll(toPut);
405     }
406   }
407   
408   void testEntrySetContainsAll2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
409     Map JavaDoc toPut = new HashMap JavaDoc();
410     toPut.put(hashKeys[0], hashValues[0]);
411     toPut.put(hashKeys[1], hashValues[1]);
412     toPut.put(hashKeys[2], hashValues[2]);
413     toPut.put(hashKeys[3], hashValues[3]);
414  
415     if (validate) {
416       SimpleEntry entry1 = new SimpleEntry(hashKeys[1], hashValues[1]);
417       SimpleEntry entry2 = new SimpleEntry(new HashKey(3), new HashValue(30));
418       List JavaDoc containsList = new ArrayList JavaDoc(2);
419       containsList.add(entry1);
420       containsList.add(entry2);
421       Assert.assertTrue(map.entrySet().containsAll(containsList));
422     } else {
423       map.putAll(toPut);
424     }
425   }
426   
427   void testEntrySetRetainAll1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
428     Map JavaDoc toPut = new HashMap JavaDoc();
429     toPut.put(keyRoots[0], valueRoots[0]);
430     toPut.put(keyRoots[1], valueRoots[1]);
431     toPut.put(keyRoots[2], valueRoots[2]);
432     toPut.put(keyRoots[3], valueRoots[3]);
433     if (validate) {
434       toPut.remove(keyRoots[0]);
435       toPut.remove(keyRoots[3]);
436       assertMappingsEqual(toPut, map);
437     } else {
438       map.putAll(toPut);
439       SimpleEntry entry1 = new SimpleEntry(keyRoots[1], valueRoots[1]);
440       SimpleEntry entry2 = new SimpleEntry(keyRoots[2], valueRoots[2]);
441       List JavaDoc containsList = new ArrayList JavaDoc(2);
442       containsList.add(entry1);
443       containsList.add(entry2);
444       map.entrySet().retainAll(containsList);
445     }
446   }
447   
448   void testEntrySetRetainAll2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
449     Map JavaDoc toPut = new HashMap JavaDoc();
450     toPut.put(hashKeys[0], hashValues[0]);
451     toPut.put(hashKeys[1], hashValues[1]);
452     toPut.put(hashKeys[2], hashValues[2]);
453     toPut.put(hashKeys[3], hashValues[3]);
454     
455     if (validate) {
456       toPut.remove(hashKeys[0]);
457       toPut.remove(hashKeys[3]);
458       assertMappingsHashEqual(toPut, map);
459     } else {
460       map.putAll(toPut);
461       SimpleEntry entry1 = new SimpleEntry(hashKeys[1], hashValues[1]);
462       SimpleEntry entry2 = new SimpleEntry(new HashKey(3), new HashValue(30));
463       List JavaDoc containsList = new ArrayList JavaDoc(2);
464       containsList.add(entry1);
465       containsList.add(entry2);
466       map.entrySet().retainAll(containsList);
467     }
468   }
469   
470   void testEntrySetRemove1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
471     if (validate) {
472       Assert.assertEquals(0, map.size());
473     } else {
474       map.put(keyRoots[0], valueRoots[0]);
475       SimpleEntry entry = new SimpleEntry(keyRoots[0], valueRoots[0]);
476       map.entrySet().remove(entry);
477     }
478   }
479   
480   void testEntrySetRemove2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
481     if (validate) {
482       Assert.assertEquals(0, map.size());
483     } else {
484       map.put(hashKeys[0], hashValues[0]);
485       SimpleEntry entry = new SimpleEntry(hashKeys[0], hashValues[0]);
486       map.entrySet().remove(entry);
487     }
488   }
489   
490   void testEntrySetRemoveAll1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
491     Map JavaDoc toPut = new HashMap JavaDoc();
492     toPut.put(keyRoots[0], valueRoots[0]);
493     toPut.put(keyRoots[1], valueRoots[1]);
494     toPut.put(keyRoots[2], valueRoots[2]);
495     toPut.put(keyRoots[3], valueRoots[3]);
496     if (validate) {
497       toPut.remove(keyRoots[1]);
498       toPut.remove(keyRoots[2]);
499       assertMappingsEqual(toPut, map);
500     } else {
501       map.putAll(toPut);
502       
503       SimpleEntry entry1 = new SimpleEntry(keyRoots[1], valueRoots[1]);
504       SimpleEntry entry2 = new SimpleEntry(keyRoots[2], valueRoots[2]);
505       List JavaDoc toRemove = new ArrayList JavaDoc(2);
506       toRemove.add(entry1);
507       toRemove.add(entry2);
508       map.entrySet().removeAll(toRemove);
509     }
510   }
511   
512   void testEntrySetRemoveAll2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
513     Map JavaDoc toPut = new HashMap JavaDoc();
514     toPut.put(hashKeys[0], hashValues[0]);
515     toPut.put(hashKeys[1], hashValues[1]);
516     toPut.put(hashKeys[2], hashValues[2]);
517     toPut.put(hashKeys[3], hashValues[3]);
518     if (validate) {
519       toPut.remove(hashKeys[1]);
520       toPut.remove(hashKeys[2]);
521       assertMappingsHashEqual(toPut, map);
522     } else {
523       map.putAll(toPut);
524       
525       SimpleEntry entry1 = new SimpleEntry(hashKeys[1], hashValues[1]);
526       SimpleEntry entry2 = new SimpleEntry(new HashKey(3), new HashValue(30));
527       List JavaDoc toRemove = new ArrayList JavaDoc(2);
528       toRemove.add(entry1);
529       toRemove.add(entry2);
530       map.entrySet().removeAll(toRemove);
531     }
532   }
533   
534   void testEntrySetSize1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
535     if (validate) {
536       Assert.assertEquals(1, map.entrySet().size());
537     } else {
538       map.put(keyRoots[0], valueRoots[0]);
539     }
540   }
541   
542   void testEntrySetSize2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
543     if (validate) {
544       Assert.assertEquals(1, map.entrySet().size());
545     } else {
546       map.put(hashKeys[0], hashValues[0]);
547     }
548   }
549   
550   void testEntrySetSetValue1(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
551     Map JavaDoc toPut = new HashMap JavaDoc();
552     toPut.put(keyRoots[0], valueRoots[0]);
553     toPut.put(keyRoots[1], valueRoots[1]);
554     toPut.put(keyRoots[2], valueRoots[2]);
555     toPut.put(keyRoots[3], valueRoots[3]);
556     if (validate) {
557       Assert.assertEquals(15, ((DataValue)map.get(keyRoots[1])).getInt());
558     } else {
559       map.putAll(toPut);
560       for (Iterator JavaDoc i=map.entrySet().iterator(); i.hasNext(); ) {
561         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
562         if (((DataKey)entry.getKey()).getInt() == 2) {
563           entry.setValue(new DataValue(15));
564         }
565       }
566     }
567   }
568   
569   void testEntrySetSetValue2(ConcurrentHashMap JavaDoc map, boolean validate) throws Exception JavaDoc {
570     Map JavaDoc toPut = new HashMap JavaDoc();
571     toPut.put(hashKeys[0], hashValues[0]);
572     toPut.put(hashKeys[1], hashValues[1]);
573     toPut.put(hashKeys[2], hashValues[2]);
574     toPut.put(hashKeys[3], hashValues[3]);
575     if (validate) {
576       Assert.assertEquals(new HashValue(15), map.get(hashKeys[1]));
577     } else {
578       map.putAll(toPut);
579       for (Iterator JavaDoc i=map.entrySet().iterator(); i.hasNext(); ) {
580         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
581         if (((HashKey)entry.getKey()).getInt() == 2) {
582           entry.setValue(new HashValue(15));
583         }
584       }
585     }
586   }
587   
588   void testEntrySetIteratorRemove1(ConcurrentHashMap JavaDoc map, boolean validate) {
589     Map JavaDoc toPut = new HashMap JavaDoc();
590     toPut.put(keyRoots[0], valueRoots[0]);
591     toPut.put(keyRoots[1], valueRoots[1]);
592     toPut.put(keyRoots[2], valueRoots[2]);
593     toPut.put(keyRoots[3], valueRoots[3]);
594     if (validate) {
595       toPut.remove(keyRoots[1]);
596       assertMappingsEqual(toPut, map);
597     } else {
598       map.putAll(toPut);
599       assertMappingsEqual(toPut, map);
600       
601       for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext(); ) {
602         Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
603         if (e.getKey().equals(keyRoots[1])) {
604           i.remove();
605           break;
606         }
607       }
608     }
609   }
610   
611   void testEntrySetIteratorRemove2(ConcurrentHashMap JavaDoc map, boolean validate) {
612     Map JavaDoc toPut = new HashMap JavaDoc();
613     toPut.put(hashKeys[0], hashValues[0]);
614     toPut.put(hashKeys[1], hashValues[1]);
615     toPut.put(hashKeys[2], hashValues[2]);
616     toPut.put(hashKeys[3], hashValues[3]);
617     if (validate) {
618       toPut.remove(hashKeys[1]);
619       assertMappingsHashEqual(toPut, map);
620     } else {
621       map.putAll(toPut);
622       assertMappingsHashEqual(toPut, map);
623       
624       for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext(); ) {
625         Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
626         if (e.getKey().equals(hashKeys[1])) {
627           i.remove();
628           break;
629         }
630       }
631     }
632   }
633   
634   void testEntrySetToArray1(ConcurrentHashMap JavaDoc map, boolean validate) {
635     Map JavaDoc toPut = new HashMap JavaDoc();
636     toPut.put(keyRoots[0], valueRoots[0]);
637     toPut.put(keyRoots[1], valueRoots[1]);
638     toPut.put(keyRoots[2], valueRoots[2]);
639     toPut.put(keyRoots[3], valueRoots[3]);
640     Object JavaDoc[] array = getArray(map, false);
641
642     if (validate) {
643       assertMappingsEqual(array, map);
644     } else {
645       map.putAll(toPut);
646       synchronized (array) {
647         Object JavaDoc[] returnArray = map.entrySet().toArray(array);
648         Assert.assertTrue(returnArray == array);
649       }
650     }
651   }
652   
653   void testEntrySetToArray2(ConcurrentHashMap JavaDoc map, boolean validate) {
654     Map JavaDoc toPut = new HashMap JavaDoc();
655     toPut.put(hashKeys[0], hashValues[0]);
656     toPut.put(hashKeys[1], hashValues[1]);
657     toPut.put(hashKeys[2], hashValues[2]);
658     toPut.put(hashKeys[3], hashValues[3]);
659     Object JavaDoc[] array = getArray(map, true);
660
661     if (validate) {
662       assertMappingsEqual(array, map);
663     } else {
664       map.putAll(toPut);
665       synchronized (array) {
666         Object JavaDoc[] returnArray = map.entrySet().toArray(array);
667         Assert.assertTrue(returnArray == array);
668       }
669     }
670   }
671   
672   void testValuesClear1(ConcurrentHashMap JavaDoc map, boolean validate) {
673     Map JavaDoc toPut = new HashMap JavaDoc();
674     toPut.put(keyRoots[0], valueRoots[0]);
675     toPut.put(keyRoots[1], valueRoots[1]);
676     toPut.put(keyRoots[2], valueRoots[2]);
677     toPut.put(keyRoots[3], valueRoots[3]);
678
679     if (validate) {
680       Assert.assertEquals(0, map.size());
681     } else {
682       map.putAll(toPut);
683       map.values().clear();
684     }
685   }
686   
687   void testValuesClear2(ConcurrentHashMap JavaDoc map, boolean validate) {
688     Map JavaDoc toPut = new HashMap JavaDoc();
689     toPut.put(hashKeys[0], hashValues[0]);
690     toPut.put(hashKeys[1], hashValues[1]);
691     toPut.put(hashKeys[2], hashValues[2]);
692     toPut.put(hashKeys[3], hashValues[3]);
693
694     if (validate) {
695       Assert.assertEquals(0, map.size());
696     } else {
697       map.putAll(toPut);
698       map.values().clear();
699     }
700   }
701   
702   void testValuesContains1(ConcurrentHashMap JavaDoc map, boolean validate) {
703     Map JavaDoc toPut = new HashMap JavaDoc();
704     toPut.put(keyRoots[0], valueRoots[0]);
705     toPut.put(keyRoots[1], valueRoots[1]);
706     toPut.put(keyRoots[2], valueRoots[2]);
707     toPut.put(keyRoots[3], valueRoots[3]);
708
709     if (validate) {
710       Assert.assertTrue(map.values().contains(valueRoots[2]));
711     } else {
712       map.putAll(toPut);
713     }
714   }
715   
716   void testValuesContains2(ConcurrentHashMap JavaDoc map, boolean validate) {
717     Map JavaDoc toPut = new HashMap JavaDoc();
718     toPut.put(hashKeys[0], hashValues[0]);
719     toPut.put(hashKeys[1], hashValues[1]);
720     toPut.put(hashKeys[2], hashValues[2]);
721     toPut.put(hashKeys[3], hashValues[3]);
722
723     if (validate) {
724       Assert.assertTrue(map.values().contains(new HashValue(20)));
725     } else {
726       map.putAll(toPut);
727     }
728   }
729   
730   void testValuesContainsAll1(ConcurrentHashMap JavaDoc map, boolean validate) {
731     Map JavaDoc toPut = new HashMap JavaDoc();
732     toPut.put(keyRoots[0], valueRoots[0]);
733     toPut.put(keyRoots[1], valueRoots[1]);
734     toPut.put(keyRoots[2], valueRoots[2]);
735     toPut.put(keyRoots[3], valueRoots[3]);
736
737     if (validate) {
738       Assert.assertTrue(map.values().containsAll(toPut.values()));
739     } else {
740       map.putAll(toPut);
741     }
742   }
743   
744   void testValuesContainsAll2(ConcurrentHashMap JavaDoc map, boolean validate) {
745     Map JavaDoc toPut = new HashMap JavaDoc();
746     toPut.put(hashKeys[0], hashValues[0]);
747     toPut.put(hashKeys[1], hashValues[1]);
748     toPut.put(hashKeys[2], hashValues[2]);
749     toPut.put(hashKeys[3], hashValues[3]);
750
751     if (validate) {
752       Assert.assertTrue(map.values().containsAll(toPut.values()));
753     } else {
754       map.putAll(toPut);
755     }
756   }
757   
758   void testValuesRemove1(ConcurrentHashMap JavaDoc map, boolean validate) {
759     Map JavaDoc toPut = new HashMap JavaDoc();
760     toPut.put(keyRoots[0], valueRoots[0]);
761     toPut.put(keyRoots[1], valueRoots[1]);
762     toPut.put(keyRoots[2], valueRoots[2]);
763     toPut.put(keyRoots[3], valueRoots[3]);
764
765     if (validate) {
766       toPut.remove(keyRoots[1]);
767       assertMappingsEqual(toPut, map);
768     } else {
769       map.putAll(toPut);
770       map.values().remove(valueRoots[1]);
771     }
772   }
773   
774   void testValuesRemove2(ConcurrentHashMap JavaDoc map, boolean validate) {
775     Map JavaDoc toPut = new HashMap JavaDoc();
776     toPut.put(hashKeys[0], hashValues[0]);
777     toPut.put(hashKeys[1], hashValues[1]);
778     toPut.put(hashKeys[2], hashValues[2]);
779     toPut.put(hashKeys[3], hashValues[3]);
780
781     if (validate) {
782       toPut.remove(hashKeys[1]);
783       assertMappingsHashEqual(toPut, map);
784     } else {
785       map.putAll(toPut);
786       map.values().remove(new HashValue(20));
787     }
788   }
789   
790   void testValuesRemoveAll1(ConcurrentHashMap JavaDoc map, boolean validate) {
791     Map JavaDoc toPut = new HashMap JavaDoc();
792     toPut.put(keyRoots[0], valueRoots[0]);
793     toPut.put(keyRoots[1], valueRoots[1]);
794     toPut.put(keyRoots[2], valueRoots[2]);
795     toPut.put(keyRoots[3], valueRoots[3]);
796
797     if (validate) {
798       List JavaDoc expect = new ArrayList JavaDoc();
799       expect.add(valueRoots[0]);
800       expect.add(valueRoots[2]);
801       assertCollectionsEqual(expect, map.values());
802     } else {
803       map.putAll(toPut);
804       List JavaDoc toRemove = new ArrayList JavaDoc(2);
805       toRemove.add(valueRoots[1]);
806       toRemove.add(valueRoots[3]);
807       map.values().removeAll(toRemove);
808     }
809   }
810   
811   void testValuesRemoveAll2(ConcurrentHashMap JavaDoc map, boolean validate) {
812     Map JavaDoc toPut = new HashMap JavaDoc();
813     toPut.put(hashKeys[0], hashValues[0]);
814     toPut.put(hashKeys[1], hashValues[1]);
815     toPut.put(hashKeys[2], hashValues[2]);
816     toPut.put(hashKeys[3], hashValues[3]);
817
818     if (validate) {
819       List JavaDoc expect = new ArrayList JavaDoc();
820       expect.add(hashValues[0]);
821       expect.add(hashValues[2]);
822       assertCollectionsEqual(expect, map.values());
823     } else {
824       map.putAll(toPut);
825       List JavaDoc toRemove = new ArrayList JavaDoc(2);
826       toRemove.add(new HashValue(20));
827       toRemove.add(new HashValue(40));
828       map.values().removeAll(toRemove);
829     }
830   }
831   
832   void testValuesRetainAll1(ConcurrentHashMap JavaDoc map, boolean validate) {
833     Map JavaDoc toPut = new HashMap JavaDoc();
834     toPut.put(keyRoots[0], valueRoots[0]);
835     toPut.put(keyRoots[1], valueRoots[1]);
836     toPut.put(keyRoots[2], valueRoots[2]);
837     toPut.put(keyRoots[3], valueRoots[3]);
838
839     if (validate) {
840       List JavaDoc expect = new ArrayList JavaDoc();
841       expect.add(valueRoots[1]);
842       expect.add(valueRoots[3]);
843       assertCollectionsEqual(expect, map.values());
844     } else {
845       map.putAll(toPut);
846       List JavaDoc toRetain = new ArrayList JavaDoc(2);
847       toRetain.add(valueRoots[1]);
848       toRetain.add(valueRoots[3]);
849       map.values().retainAll(toRetain);
850     }
851   }
852   
853   void testValuesRetainAll2(ConcurrentHashMap JavaDoc map, boolean validate) {
854     Map JavaDoc toPut = new HashMap JavaDoc();
855     toPut.put(hashKeys[0], hashValues[0]);
856     toPut.put(hashKeys[1], hashValues[1]);
857     toPut.put(hashKeys[2], hashValues[2]);
858     toPut.put(hashKeys[3], hashValues[3]);
859
860     if (validate) {
861       List JavaDoc expect = new ArrayList JavaDoc();
862       expect.add(hashValues[1]);
863       expect.add(hashValues[3]);
864       assertCollectionsEqual(expect, map.values());
865     } else {
866       map.putAll(toPut);
867       List JavaDoc toRetain = new ArrayList JavaDoc(2);
868       toRetain.add(new HashValue(20));
869       toRetain.add(new HashValue(40));
870       map.values().retainAll(toRetain);
871     }
872   }
873   
874   void testValuesToArray1(ConcurrentHashMap JavaDoc map, boolean validate) {
875     Map JavaDoc toPut = new HashMap JavaDoc();
876     toPut.put(keyRoots[0], valueRoots[0]);
877     toPut.put(keyRoots[1], valueRoots[1]);
878     toPut.put(keyRoots[2], valueRoots[2]);
879     toPut.put(keyRoots[3], valueRoots[3]);
880     Object JavaDoc[] array = getArray(map, false);
881
882     if (validate) {
883       assertCollectionsEqual(array, map.values());
884     } else {
885       map.putAll(toPut);
886       synchronized (array) {
887         Object JavaDoc[] returnArray = map.values().toArray(array);
888         Assert.assertTrue(returnArray == array);
889       }
890     }
891   }
892   
893   void testValuesToArray2(ConcurrentHashMap JavaDoc map, boolean validate) {
894     Map JavaDoc toPut = new HashMap JavaDoc();
895     toPut.put(hashKeys[0], hashValues[0]);
896     toPut.put(hashKeys[1], hashValues[1]);
897     toPut.put(hashKeys[2], hashValues[2]);
898     toPut.put(hashKeys[3], hashValues[3]);
899     Object JavaDoc[] array = getArray(map, false);
900
901     if (validate) {
902       assertCollectionsEqual(array, map.values());
903     } else {
904       map.putAll(toPut);
905       synchronized (array) {
906         Object JavaDoc[] returnArray = map.values().toArray(array);
907         Assert.assertTrue(returnArray == array);
908       }
909     }
910   }
911   
912   void testValuesIteratorRemove1(ConcurrentHashMap JavaDoc map, boolean validate) {
913     Map JavaDoc toPut = new HashMap JavaDoc();
914     toPut.put(keyRoots[0], valueRoots[0]);
915     toPut.put(keyRoots[1], valueRoots[1]);
916     toPut.put(keyRoots[2], valueRoots[2]);
917     toPut.put(keyRoots[3], valueRoots[3]);
918
919     if (validate) {
920       List JavaDoc expect = new ArrayList JavaDoc();
921       expect.add(valueRoots[0]);
922       expect.add(valueRoots[2]);
923       expect.add(valueRoots[3]);
924       assertCollectionsEqual(expect, map.values());
925     } else {
926       map.putAll(toPut);
927       for (Iterator JavaDoc i=map.values().iterator(); i.hasNext(); ) {
928         DataValue value = (DataValue)i.next();
929         if (value.getInt() == 20) {
930           i.remove();
931         }
932       }
933     }
934   }
935   
936   void testValuesIteratorRemove2(ConcurrentHashMap JavaDoc map, boolean validate) {
937     Map JavaDoc toPut = new HashMap JavaDoc();
938     toPut.put(hashKeys[0], hashValues[0]);
939     toPut.put(hashKeys[1], hashValues[1]);
940     toPut.put(hashKeys[2], hashValues[2]);
941     toPut.put(hashKeys[3], hashValues[3]);
942
943     if (validate) {
944       List JavaDoc expect = new ArrayList JavaDoc();
945       expect.add(hashValues[0]);
946       expect.add(hashValues[2]);
947       expect.add(hashValues[3]);
948       assertCollectionsEqual(expect, map.values());
949     } else {
950       map.putAll(toPut);
951       for (Iterator JavaDoc i=map.values().iterator(); i.hasNext(); ) {
952         Object JavaDoc value = i.next();
953         if (value.equals(new HashValue(20))) {
954           i.remove();
955         }
956       }
957     }
958   }
959   
960   void testKeySetClear1(ConcurrentHashMap JavaDoc map, boolean validate) {
961     Map JavaDoc toPut = new HashMap JavaDoc();
962     toPut.put(keyRoots[0], valueRoots[0]);
963     toPut.put(keyRoots[1], valueRoots[1]);
964     toPut.put(keyRoots[2], valueRoots[2]);
965     toPut.put(keyRoots[3], valueRoots[3]);
966
967     if (validate) {
968       Assert.assertEquals(0, map.size());
969     } else {
970       map.putAll(toPut);
971       map.keySet().clear();
972     }
973   }
974   
975   void testKeySetClear2(ConcurrentHashMap JavaDoc map, boolean validate) {
976     Map JavaDoc toPut = new HashMap JavaDoc();
977     toPut.put(hashKeys[0], hashValues[0]);
978     toPut.put(hashKeys[1], hashValues[1]);
979     toPut.put(hashKeys[2], hashValues[2]);
980     toPut.put(hashKeys[3], hashValues[3]);
981
982     if (validate) {
983       Assert.assertEquals(0, map.size());
984     } else {
985       map.putAll(toPut);
986       map.keySet().clear();
987     }
988   }
989   
990   void testKeySetContains1(ConcurrentHashMap JavaDoc map, boolean validate) {
991     Map JavaDoc toPut = new HashMap JavaDoc();
992     toPut.put(keyRoots[0], valueRoots[0]);
993     toPut.put(keyRoots[1], valueRoots[1]);
994     toPut.put(keyRoots[2], valueRoots[2]);
995     toPut.put(keyRoots[3], valueRoots[3]);
996
997     if (validate) {
998       Assert.assertTrue(map.keySet().contains(keyRoots[2]));
999     } else {
1000      map.putAll(toPut);
1001    }
1002  }
1003  
1004  void testKeySetContains2(ConcurrentHashMap JavaDoc map, boolean validate) {
1005    Map JavaDoc toPut = new HashMap JavaDoc();
1006    toPut.put(hashKeys[0], hashValues[0]);
1007    toPut.put(hashKeys[1], hashValues[1]);
1008    toPut.put(hashKeys[2], hashValues[2]);
1009    toPut.put(hashKeys[3], hashValues[3]);
1010
1011    if (validate) {
1012      Assert.assertTrue(map.keySet().contains(new HashKey(2)));
1013    } else {
1014      map.putAll(toPut);
1015    }
1016  }
1017  
1018  void testKeySetContainsAll1(ConcurrentHashMap JavaDoc map, boolean validate) {
1019    Map JavaDoc toPut = new HashMap JavaDoc();
1020    toPut.put(keyRoots[0], valueRoots[0]);
1021    toPut.put(keyRoots[1], valueRoots[1]);
1022    toPut.put(keyRoots[2], valueRoots[2]);
1023    toPut.put(keyRoots[3], valueRoots[3]);
1024
1025    if (validate) {
1026      Assert.assertTrue(map.keySet().containsAll(toPut.keySet()));
1027    } else {
1028      map.putAll(toPut);
1029    }
1030  }
1031  
1032  void testKeySetContainsAll2(ConcurrentHashMap JavaDoc map, boolean validate) {
1033    Map JavaDoc toPut = new HashMap JavaDoc();
1034    toPut.put(hashKeys[0], hashValues[0]);
1035    toPut.put(hashKeys[1], hashValues[1]);
1036    toPut.put(hashKeys[2], hashValues[2]);
1037    toPut.put(hashKeys[3], hashValues[3]);
1038
1039    if (validate) {
1040      Assert.assertTrue(map.keySet().containsAll(toPut.keySet()));
1041    } else {
1042      map.putAll(toPut);
1043    }
1044  }
1045  
1046  void testKeySetRemove1(ConcurrentHashMap JavaDoc map, boolean validate) {
1047    Map JavaDoc toPut = new HashMap JavaDoc();
1048    toPut.put(keyRoots[0], valueRoots[0]);
1049    toPut.put(keyRoots[1], valueRoots[1]);
1050    toPut.put(keyRoots[2], valueRoots[2]);
1051    toPut.put(keyRoots[3], valueRoots[3]);
1052
1053    if (validate) {
1054      toPut.remove(keyRoots[1]);
1055      assertMappingsEqual(toPut, map);
1056    } else {
1057      map.putAll(toPut);
1058      map.keySet().remove(keyRoots[1]);
1059    }
1060  }
1061  
1062  void testKeySetRemove2(ConcurrentHashMap JavaDoc map, boolean validate) {
1063    Map JavaDoc toPut = new HashMap JavaDoc();
1064    toPut.put(hashKeys[0], hashValues[0]);
1065    toPut.put(hashKeys[1], hashValues[1]);
1066    toPut.put(hashKeys[2], hashValues[2]);
1067    toPut.put(hashKeys[3], hashValues[3]);
1068
1069    if (validate) {
1070      toPut.remove(hashKeys[1]);
1071      assertMappingsHashEqual(toPut, map);
1072    } else {
1073      map.putAll(toPut);
1074      map.keySet().remove(new HashKey(2));
1075    }
1076  }
1077  
1078  void testKeySetRemoveAll1(ConcurrentHashMap JavaDoc map, boolean validate) {
1079    Map JavaDoc toPut = new HashMap JavaDoc();
1080    toPut.put(keyRoots[0], valueRoots[0]);
1081    toPut.put(keyRoots[1], valueRoots[1]);
1082    toPut.put(keyRoots[2], valueRoots[2]);
1083    toPut.put(keyRoots[3], valueRoots[3]);
1084
1085    if (validate) {
1086      List JavaDoc expect = new ArrayList JavaDoc();
1087      expect.add(keyRoots[0]);
1088      expect.add(keyRoots[2]);
1089      assertCollectionsEqual(expect, map.keySet());
1090    } else {
1091      map.putAll(toPut);
1092      List JavaDoc toRemove = new ArrayList JavaDoc(2);
1093      toRemove.add(keyRoots[1]);
1094      toRemove.add(keyRoots[3]);
1095      map.keySet().removeAll(toRemove);
1096    }
1097  }
1098  
1099  void testKeySetRemoveAll2(ConcurrentHashMap JavaDoc map, boolean validate) {
1100    Map JavaDoc toPut = new HashMap JavaDoc();
1101    toPut.put(hashKeys[0], hashValues[0]);
1102    toPut.put(hashKeys[1], hashValues[1]);
1103    toPut.put(hashKeys[2], hashValues[2]);
1104    toPut.put(hashKeys[3], hashValues[3]);
1105
1106    if (validate) {
1107      List JavaDoc expect = new ArrayList JavaDoc();
1108      expect.add(hashKeys[0]);
1109      expect.add(hashKeys[2]);
1110      assertCollectionsEqual(expect, map.keySet());
1111    } else {
1112      map.putAll(toPut);
1113      List JavaDoc toRemove = new ArrayList JavaDoc(2);
1114      toRemove.add(new HashKey(2));
1115      toRemove.add(new HashKey(4));
1116      map.keySet().removeAll(toRemove);
1117    }
1118  }
1119  
1120  void testKeySetRetainAll1(ConcurrentHashMap JavaDoc map, boolean validate) {
1121    Map JavaDoc toPut = new HashMap JavaDoc();
1122    toPut.put(keyRoots[0], valueRoots[0]);
1123    toPut.put(keyRoots[1], valueRoots[1]);
1124    toPut.put(keyRoots[2], valueRoots[2]);
1125    toPut.put(keyRoots[3], valueRoots[3]);
1126
1127    if (validate) {
1128      List JavaDoc expect = new ArrayList JavaDoc();
1129      expect.add(keyRoots[1]);
1130      expect.add(keyRoots[3]);
1131      assertCollectionsEqual(expect, map.keySet());
1132    } else {
1133      map.putAll(toPut);
1134      List JavaDoc toRetain = new ArrayList JavaDoc(2);
1135      toRetain.add(keyRoots[1]);
1136      toRetain.add(keyRoots[3]);
1137      map.keySet().retainAll(toRetain);
1138    }
1139  }
1140  
1141  void testKeySetRetainAll2(ConcurrentHashMap JavaDoc map, boolean validate) {
1142    Map JavaDoc toPut = new HashMap JavaDoc();
1143    toPut.put(hashKeys[0], hashValues[0]);
1144    toPut.put(hashKeys[1], hashValues[1]);
1145    toPut.put(hashKeys[2], hashValues[2]);
1146    toPut.put(hashKeys[3], hashValues[3]);
1147
1148    if (validate) {
1149      List JavaDoc expect = new ArrayList JavaDoc();
1150      expect.add(hashKeys[1]);
1151      expect.add(hashKeys[3]);
1152      assertCollectionsEqual(expect, map.keySet());
1153    } else {
1154      map.putAll(toPut);
1155      List JavaDoc toRetain = new ArrayList JavaDoc(2);
1156      toRetain.add(hashKeys[1]);
1157      toRetain.add(hashKeys[3]);
1158      map.keySet().retainAll(toRetain);
1159    }
1160  }
1161  
1162  void testKeySetToArray1(ConcurrentHashMap JavaDoc map, boolean validate) {
1163    Map JavaDoc toPut = new HashMap JavaDoc();
1164    toPut.put(keyRoots[0], valueRoots[0]);
1165    toPut.put(keyRoots[1], valueRoots[1]);
1166    toPut.put(keyRoots[2], valueRoots[2]);
1167    toPut.put(keyRoots[3], valueRoots[3]);
1168    Object JavaDoc[] array = getArray(map, false);
1169
1170    if (validate) {
1171      assertCollectionsEqual(array, map.keySet());
1172    } else {
1173      map.putAll(toPut);
1174      synchronized (array) {
1175        Object JavaDoc[] returnArray = map.keySet().toArray(array);
1176        Assert.assertTrue(returnArray == array);
1177      }
1178    }
1179  }
1180  
1181  void testKeySetToArray2(ConcurrentHashMap JavaDoc map, boolean validate) {
1182    Map JavaDoc toPut = new HashMap JavaDoc();
1183    toPut.put(hashKeys[0], hashValues[0]);
1184    toPut.put(hashKeys[1], hashValues[1]);
1185    toPut.put(hashKeys[2], hashValues[2]);
1186    toPut.put(hashKeys[3], hashValues[3]);
1187    Object JavaDoc[] array = getArray(map, false);
1188
1189    if (validate) {
1190      assertCollectionsEqual(array, map.keySet());
1191    } else {
1192      map.putAll(toPut);
1193      synchronized (array) {
1194        Object JavaDoc[] returnArray = map.keySet().toArray(array);
1195        Assert.assertTrue(returnArray == array);
1196      }
1197    }
1198  }
1199  
1200  void testKeySetIteratorRemove1(ConcurrentHashMap JavaDoc map, boolean validate) {
1201    Map JavaDoc toPut = new HashMap JavaDoc();
1202    toPut.put(keyRoots[0], valueRoots[0]);
1203    toPut.put(keyRoots[1], valueRoots[1]);
1204    toPut.put(keyRoots[2], valueRoots[2]);
1205    toPut.put(keyRoots[3], valueRoots[3]);
1206
1207    if (validate) {
1208      List JavaDoc expect = new ArrayList JavaDoc();
1209      expect.add(keyRoots[0]);
1210      expect.add(keyRoots[2]);
1211      expect.add(keyRoots[3]);
1212      assertCollectionsEqual(expect, map.keySet());
1213    } else {
1214      map.putAll(toPut);
1215      for (Iterator JavaDoc i=map.keySet().iterator(); i.hasNext(); ) {
1216        DataKey key = (DataKey)i.next();
1217        if (key.getInt() == 2) {
1218          i.remove();
1219        }
1220      }
1221    }
1222  }
1223  
1224  void testKeySetIteratorRemove2(ConcurrentHashMap JavaDoc map, boolean validate) {
1225    Map JavaDoc toPut = new HashMap JavaDoc();
1226    toPut.put(hashKeys[0], hashValues[0]);
1227    toPut.put(hashKeys[1], hashValues[1]);
1228    toPut.put(hashKeys[2], hashValues[2]);
1229    toPut.put(hashKeys[3], hashValues[3]);
1230
1231    if (validate) {
1232      List JavaDoc expect = new ArrayList JavaDoc();
1233      expect.add(hashKeys[0]);
1234      expect.add(hashKeys[2]);
1235      expect.add(hashKeys[3]);
1236      assertCollectionsEqual(expect, map.keySet());
1237    } else {
1238      map.putAll(toPut);
1239      for (Iterator JavaDoc i=map.keySet().iterator(); i.hasNext(); ) {
1240        Object JavaDoc key = i.next();
1241        if (key.equals(new HashKey(2))) {
1242          i.remove();
1243        }
1244      }
1245    }
1246  }
1247
1248  void assertSingleHashMapping(Object JavaDoc expectedKey, Object JavaDoc expectedValue, Map JavaDoc map) {
1249    Assert.assertEquals(1, map.size());
1250    Assert.assertEquals(expectedValue, map.get(expectedKey));
1251  }
1252  
1253  void assertMappingsEqual(Object JavaDoc[] expect, Map JavaDoc map) {
1254    Assert.assertEquals(expect.length, map.size());
1255    for (int i = 0; i < expect.length; i++) {
1256      Entry entry = (Entry) expect[i];
1257      Object JavaDoc val = map.get(entry.getKey());
1258      Assert.assertEquals(entry.getValue(), val);
1259    }
1260  }
1261  
1262  void assertCollectionsEqual(Object JavaDoc[] expect, Collection JavaDoc collection) {
1263    Assert.assertEquals(expect.length, collection.size());
1264    for (int i = 0; i < expect.length; i++) {
1265      Assert.assertTrue(collection.contains(expect[i]));
1266    }
1267  }
1268  
1269  void assertCollectionsEqual(Collection JavaDoc expect, Collection JavaDoc collection) {
1270    Assert.assertEquals(expect.size(), collection.size());
1271    for (Iterator JavaDoc i=expect.iterator(); i.hasNext(); ) {
1272      Assert.assertTrue(collection.contains(i.next()));
1273    }
1274  }
1275  
1276  void assertMappingsHashEqual(Map JavaDoc expect, Map JavaDoc actual) {
1277    Assert.assertEquals(expect.size(), actual.size());
1278
1279    Set JavaDoc expectEntries = expect.entrySet();
1280    Set JavaDoc actualEntries = actual.entrySet();
1281
1282    for (Iterator JavaDoc i = expectEntries.iterator(); i.hasNext();) {
1283      Entry entry = (Entry) i.next();
1284      Assert.assertEquals(entry.getValue(), actual.get(entry.getKey()));
1285    }
1286
1287    for (Iterator JavaDoc i = actualEntries.iterator(); i.hasNext();) {
1288      Entry entry = (Entry) i.next();
1289      Assert.assertEquals(entry.getValue(), expect.get(entry.getKey()));
1290    }
1291  }
1292  
1293  void assertMappingsEqual(Map JavaDoc expect, Map JavaDoc actual) {
1294    Assert.assertEquals(expect.size(), actual.size());
1295
1296    Set JavaDoc expectEntries = expect.entrySet();
1297    Set JavaDoc actualEntries = actual.entrySet();
1298
1299    for (Iterator JavaDoc i = expectEntries.iterator(); i.hasNext();) {
1300      Entry entry = (Entry) i.next();
1301      Assert.assertEquals(((DataValue)entry.getValue()).getInt(), ((DataValue)actual.get(entry.getKey())).getInt());
1302    }
1303
1304    for (Iterator JavaDoc i = actualEntries.iterator(); i.hasNext();) {
1305      Entry entry = (Entry) i.next();
1306      Assert.assertEquals(((DataValue)entry.getValue()).getInt(), ((DataValue)expect.get(entry.getKey())).getInt());
1307    }
1308  }
1309  
1310  private Object JavaDoc[] getArray(Map JavaDoc map, boolean hashKey) {
1311    if (!hashKey) {
1312      return (Object JavaDoc[]) sharedMap.get("arrayforConcurrentHashMap");
1313    } else {
1314      return (Object JavaDoc[]) sharedMap.get("arrayforConcurrentHashMapWithHashKeys");
1315    }
1316  }
1317
1318  public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
1319    String JavaDoc testClass = ConcurrentHashMapTestApp.class.getName();
1320    String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
1321    config.addWriteAutolock(methodExpression);
1322    TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
1323    config.addIncludePattern(testClass + "$*");
1324    spec.addRoot("keyRoots", "keyRoots");
1325    spec.addRoot("valueRoots", "valueRoots");
1326    spec.addRoot("hashKeys", "hashKeys");
1327    spec.addRoot("hashValues", "hashValues");
1328  }
1329  
1330  private static class SimpleEntry implements Map.Entry JavaDoc {
1331
1332    private final Object JavaDoc key;
1333    private Object JavaDoc value;
1334
1335    public SimpleEntry(Object JavaDoc key, Object JavaDoc value) {
1336      this.key = key;
1337      this.value = value;
1338    }
1339
1340    public SimpleEntry(Map.Entry JavaDoc e) {
1341      this.key = e.getKey();
1342      this.value = e.getValue();
1343    }
1344
1345    public Object JavaDoc getKey() {
1346      return key;
1347    }
1348
1349    public Object JavaDoc getValue() {
1350      return value;
1351    }
1352
1353    public Object JavaDoc setValue(Object JavaDoc value) {
1354      Object JavaDoc oldValue = this.value;
1355      this.value = value;
1356      return oldValue;
1357    }
1358
1359    public boolean equals(Object JavaDoc o) {
1360      if (!(o instanceof Map.Entry JavaDoc)) return false;
1361      Map.Entry JavaDoc e = (Map.Entry JavaDoc) o;
1362      return eq(key, e.getKey()) && eq(value, e.getValue());
1363    }
1364
1365    public int hashCode() {
1366      return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
1367    }
1368
1369    public String JavaDoc toString() {
1370      return key + "=" + value;
1371    }
1372
1373    private static boolean eq(Object JavaDoc o1, Object JavaDoc o2) {
1374      return (o1 == null ? o2 == null : o1.equals(o2));
1375    }
1376  }
1377
1378  private static class DataKey {
1379    private int i;
1380
1381    public DataKey(int i) {
1382      super();
1383      this.i = i;
1384    }
1385
1386    public int getInt() {
1387      return this.i;
1388    }
1389  }
1390
1391  private static class DataValue {
1392    private int i;
1393
1394    public DataValue(int i) {
1395      super();
1396      this.i = i;
1397    }
1398
1399    public int getInt() {
1400      return this.i;
1401    }
1402  }
1403  
1404  private static class HashKey {
1405    private int i;
1406
1407    public HashKey(int i) {
1408      super();
1409      this.i = i;
1410    }
1411
1412    public int getInt() {
1413      return this.i;
1414    }
1415    
1416    public int hashCode() {
1417      return i;
1418    }
1419    
1420    public boolean equals(Object JavaDoc obj) {
1421      if (obj == null) return false;
1422      if (! (obj instanceof HashKey)) return false;
1423      return ((HashKey)obj).i == i;
1424    }
1425  }
1426
1427  private static class HashValue {
1428    private int i;
1429
1430    public HashValue(int i) {
1431      super();
1432      this.i = i;
1433    }
1434
1435    public int getInt() {
1436      return this.i;
1437    }
1438    
1439    public int hashCode() {
1440      return i;
1441    }
1442    
1443    public boolean equals(Object JavaDoc obj) {
1444      if (obj == null) return false;
1445      if (! (obj instanceof HashValue)) return false;
1446      return ((HashValue)obj).i == i;
1447    }
1448    
1449    public String JavaDoc toString() {
1450      return super.toString() + ", i: " + i;
1451    }
1452  }
1453  
1454}
1455
Popular Tags