ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.36
Committed: Sun Jul 21 22:24:18 2013 UTC (10 years, 9 months ago) by dl
Branch: MAIN
Changes since 1.35: +65 -12 lines
Log Message:
Adapt/incorporate JDK8 tests including suggestions by Eric Wang

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.9 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.23 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.14 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11 jsr166 1.25 import java.util.concurrent.ConcurrentHashMap;
12 dl 1.1
13 jsr166 1.16 public class ConcurrentHashMapTest extends JSR166TestCase {
14 dl 1.1 public static void main(String[] args) {
15 jsr166 1.21 junit.textui.TestRunner.run(suite());
16 dl 1.1 }
17     public static Test suite() {
18 jsr166 1.17 return new TestSuite(ConcurrentHashMapTest.class);
19 dl 1.1 }
20    
21 dl 1.5 /**
22 jsr166 1.28 * Returns a new map from Integers 1-5 to Strings "A"-"E".
23 dl 1.5 */
24 dl 1.36 private static ConcurrentHashMap<Integer, String> map5() {
25     ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
26 dl 1.1 assertTrue(map.isEmpty());
27 jsr166 1.17 map.put(one, "A");
28     map.put(two, "B");
29     map.put(three, "C");
30     map.put(four, "D");
31     map.put(five, "E");
32 dl 1.1 assertFalse(map.isEmpty());
33     assertEquals(5, map.size());
34 jsr166 1.17 return map;
35 dl 1.1 }
36    
37 jsr166 1.33 /** Re-implement Integer.compare for old java versions */
38     static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; }
39    
40 jsr166 1.30 // classes for testing Comparable fallbacks
41     static class BI implements Comparable<BI> {
42     private final int value;
43     BI(int value) { this.value = value; }
44     public int compareTo(BI other) {
45 jsr166 1.33 return compare(value, other.value);
46 jsr166 1.30 }
47     public boolean equals(Object x) {
48     return (x instanceof BI) && ((BI)x).value == value;
49     }
50     public int hashCode() { return 42; }
51     }
52     static class CI extends BI { CI(int value) { super(value); } }
53     static class DI extends BI { DI(int value) { super(value); } }
54    
55     static class BS implements Comparable<BS> {
56     private final String value;
57     BS(String value) { this.value = value; }
58     public int compareTo(BS other) {
59     return value.compareTo(other.value);
60     }
61     public boolean equals(Object x) {
62     return (x instanceof BS) && value.equals(((BS)x).value);
63     }
64     public int hashCode() { return 42; }
65     }
66    
67     static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
68     implements Comparable<LexicographicList<E>> {
69     LexicographicList(Collection<E> c) { super(c); }
70     LexicographicList(E e) { super(Collections.singleton(e)); }
71     public int compareTo(LexicographicList<E> other) {
72     int common = Math.min(size(), other.size());
73     int r = 0;
74     for (int i = 0; i < common; i++) {
75     if ((r = get(i).compareTo(other.get(i))) != 0)
76     break;
77     }
78     if (r == 0)
79 jsr166 1.33 r = compare(size(), other.size());
80 jsr166 1.30 return r;
81     }
82     private static final long serialVersionUID = 0;
83     }
84    
85 dl 1.34 static class CollidingObject {
86     final String value;
87     CollidingObject(final String value) { this.value = value; }
88     public int hashCode() { return this.value.hashCode() & 1; }
89     public boolean equals(final Object obj) {
90     return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value);
91     }
92     }
93    
94     static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> {
95     ComparableCollidingObject(final String value) { super(value); }
96     public int compareTo(final ComparableCollidingObject o) {
97     return value.compareTo(o.value);
98     }
99     }
100    
101 jsr166 1.30 /**
102     * Inserted elements that are subclasses of the same Comparable
103     * class are found.
104     */
105     public void testComparableFamily() {
106 jsr166 1.32 ConcurrentHashMap<BI, Boolean> m =
107     new ConcurrentHashMap<BI, Boolean>();
108 jsr166 1.30 for (int i = 0; i < 1000; i++) {
109     assertTrue(m.put(new CI(i), true) == null);
110     }
111     for (int i = 0; i < 1000; i++) {
112     assertTrue(m.containsKey(new CI(i)));
113     assertTrue(m.containsKey(new DI(i)));
114     }
115     }
116    
117     /**
118     * Elements of classes with erased generic type parameters based
119     * on Comparable can be inserted and found.
120     */
121     public void testGenericComparable() {
122 jsr166 1.32 ConcurrentHashMap<Object, Boolean> m =
123     new ConcurrentHashMap<Object, Boolean>();
124 jsr166 1.30 for (int i = 0; i < 1000; i++) {
125     BI bi = new BI(i);
126     BS bs = new BS(String.valueOf(i));
127     LexicographicList<BI> bis = new LexicographicList<BI>(bi);
128     LexicographicList<BS> bss = new LexicographicList<BS>(bs);
129     assertTrue(m.putIfAbsent(bis, true) == null);
130     assertTrue(m.containsKey(bis));
131     if (m.putIfAbsent(bss, true) == null)
132     assertTrue(m.containsKey(bss));
133     assertTrue(m.containsKey(bis));
134     }
135     for (int i = 0; i < 1000; i++) {
136     assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i)))));
137     }
138     }
139    
140     /**
141     * Elements of non-comparable classes equal to those of classes
142     * with erased generic type parameters based on Comparable can be
143     * inserted and found.
144     */
145     public void testGenericComparable2() {
146 jsr166 1.32 ConcurrentHashMap<Object, Boolean> m =
147     new ConcurrentHashMap<Object, Boolean>();
148 jsr166 1.30 for (int i = 0; i < 1000; i++) {
149     m.put(new ArrayList(Collections.singleton(new BI(i))), true);
150     }
151    
152     for (int i = 0; i < 1000; i++) {
153     LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
154     assertTrue(m.containsKey(bis));
155     }
156     }
157    
158 dl 1.1 /**
159 dl 1.34 * Mixtures of instances of comparable and non-comparable classes
160     * can be inserted and found.
161     */
162     public void testMixedComparable() {
163 jsr166 1.35 int size = 10000;
164     ConcurrentHashMap<Object, Object> map =
165 dl 1.34 new ConcurrentHashMap<Object, Object>();
166     Random rng = new Random(1370014958369218000L);
167 jsr166 1.35 for (int i = 0; i < size; i++) {
168 dl 1.34 Object x;
169 jsr166 1.35 switch (rng.nextInt(4)) {
170 dl 1.34 case 0:
171     x = new Object();
172     break;
173     case 1:
174     x = new CollidingObject(Integer.toString(i));
175     break;
176     default:
177     x = new ComparableCollidingObject(Integer.toString(i));
178     }
179     assertNull(map.put(x, x));
180     }
181     int count = 0;
182     for (Object k : map.keySet()) {
183     assertEquals(map.get(k), k);
184     ++count;
185     }
186     assertEquals(count, size);
187     assertEquals(map.size(), size);
188     for (Object k : map.keySet()) {
189     assertEquals(map.put(k, k), k);
190     }
191     }
192    
193     /**
194 jsr166 1.22 * clear removes all pairs
195 dl 1.1 */
196 dl 1.5 public void testClear() {
197 dl 1.1 ConcurrentHashMap map = map5();
198 jsr166 1.17 map.clear();
199 jsr166 1.26 assertEquals(0, map.size());
200 dl 1.1 }
201    
202     /**
203 jsr166 1.22 * Maps with same contents are equal
204 dl 1.5 */
205     public void testEquals() {
206     ConcurrentHashMap map1 = map5();
207     ConcurrentHashMap map2 = map5();
208     assertEquals(map1, map2);
209     assertEquals(map2, map1);
210 jsr166 1.17 map1.clear();
211 dl 1.5 assertFalse(map1.equals(map2));
212     assertFalse(map2.equals(map1));
213     }
214    
215 dl 1.36
216     /**
217     * hashCode() equals sum of each key.hashCode ^ value.hashCode
218     */
219     public void testHashCode() {
220     ConcurrentHashMap<Integer,String> map = map5();
221     int sum = 0;
222     for (Map.Entry<Integer,String> e : map.entrySet())
223     sum += e.getKey().hashCode() ^ e.getValue().hashCode();
224     assertEquals(sum, map.hashCode());
225     }
226    
227 dl 1.5 /**
228 jsr166 1.22 * contains returns true for contained value
229 dl 1.1 */
230 dl 1.5 public void testContains() {
231 dl 1.1 ConcurrentHashMap map = map5();
232 jsr166 1.17 assertTrue(map.contains("A"));
233 dl 1.1 assertFalse(map.contains("Z"));
234     }
235 jsr166 1.14
236 dl 1.1 /**
237 jsr166 1.22 * containsKey returns true for contained key
238 dl 1.1 */
239 dl 1.5 public void testContainsKey() {
240 dl 1.1 ConcurrentHashMap map = map5();
241 jsr166 1.17 assertTrue(map.containsKey(one));
242 dl 1.5 assertFalse(map.containsKey(zero));
243 dl 1.1 }
244    
245     /**
246 jsr166 1.22 * containsValue returns true for held values
247 dl 1.1 */
248 dl 1.5 public void testContainsValue() {
249 dl 1.1 ConcurrentHashMap map = map5();
250 jsr166 1.17 assertTrue(map.containsValue("A"));
251 dl 1.11 assertFalse(map.containsValue("Z"));
252 dl 1.1 }
253    
254     /**
255 jsr166 1.22 * enumeration returns an enumeration containing the correct
256     * elements
257 dl 1.1 */
258 dl 1.5 public void testEnumeration() {
259 dl 1.1 ConcurrentHashMap map = map5();
260 jsr166 1.17 Enumeration e = map.elements();
261     int count = 0;
262     while (e.hasMoreElements()) {
263     count++;
264     e.nextElement();
265     }
266     assertEquals(5, count);
267 dl 1.4 }
268    
269     /**
270 jsr166 1.22 * get returns the correct element at the given key,
271     * or null if not present
272 dl 1.1 */
273 dl 1.5 public void testGet() {
274 dl 1.1 ConcurrentHashMap map = map5();
275 jsr166 1.17 assertEquals("A", (String)map.get(one));
276 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
277 dl 1.5 assertNull(map.get("anything"));
278 dl 1.1 }
279    
280     /**
281 jsr166 1.22 * isEmpty is true of empty map and false for non-empty
282 dl 1.1 */
283 dl 1.5 public void testIsEmpty() {
284 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
285     ConcurrentHashMap map = map5();
286 jsr166 1.17 assertTrue(empty.isEmpty());
287 dl 1.1 assertFalse(map.isEmpty());
288     }
289    
290     /**
291 jsr166 1.22 * keys returns an enumeration containing all the keys from the map
292 dl 1.1 */
293 dl 1.5 public void testKeys() {
294 dl 1.1 ConcurrentHashMap map = map5();
295 jsr166 1.17 Enumeration e = map.keys();
296     int count = 0;
297     while (e.hasMoreElements()) {
298     count++;
299     e.nextElement();
300     }
301     assertEquals(5, count);
302 dl 1.1 }
303    
304     /**
305 jsr166 1.22 * keySet returns a Set containing all the keys
306 dl 1.1 */
307 dl 1.5 public void testKeySet() {
308 dl 1.1 ConcurrentHashMap map = map5();
309 jsr166 1.17 Set s = map.keySet();
310     assertEquals(5, s.size());
311     assertTrue(s.contains(one));
312     assertTrue(s.contains(two));
313     assertTrue(s.contains(three));
314     assertTrue(s.contains(four));
315     assertTrue(s.contains(five));
316 dl 1.1 }
317    
318 dl 1.5 /**
319 jsr166 1.22 * keySet.toArray returns contains all keys
320 dl 1.13 */
321     public void testKeySetToArray() {
322     ConcurrentHashMap map = map5();
323 jsr166 1.17 Set s = map.keySet();
324 dl 1.13 Object[] ar = s.toArray();
325     assertTrue(s.containsAll(Arrays.asList(ar)));
326 jsr166 1.17 assertEquals(5, ar.length);
327 dl 1.13 ar[0] = m10;
328     assertFalse(s.containsAll(Arrays.asList(ar)));
329     }
330    
331     /**
332 jsr166 1.22 * Values.toArray contains all values
333 dl 1.13 */
334     public void testValuesToArray() {
335     ConcurrentHashMap map = map5();
336 jsr166 1.17 Collection v = map.values();
337 dl 1.13 Object[] ar = v.toArray();
338     ArrayList s = new ArrayList(Arrays.asList(ar));
339 jsr166 1.17 assertEquals(5, ar.length);
340     assertTrue(s.contains("A"));
341     assertTrue(s.contains("B"));
342     assertTrue(s.contains("C"));
343     assertTrue(s.contains("D"));
344     assertTrue(s.contains("E"));
345 dl 1.13 }
346    
347     /**
348 jsr166 1.22 * entrySet.toArray contains all entries
349 dl 1.13 */
350     public void testEntrySetToArray() {
351     ConcurrentHashMap map = map5();
352 jsr166 1.17 Set s = map.entrySet();
353 dl 1.13 Object[] ar = s.toArray();
354     assertEquals(5, ar.length);
355     for (int i = 0; i < 5; ++i) {
356     assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
357     assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
358     }
359     }
360    
361     /**
362 dl 1.5 * values collection contains all values
363     */
364     public void testValues() {
365 dl 1.1 ConcurrentHashMap map = map5();
366 jsr166 1.17 Collection s = map.values();
367     assertEquals(5, s.size());
368     assertTrue(s.contains("A"));
369     assertTrue(s.contains("B"));
370     assertTrue(s.contains("C"));
371     assertTrue(s.contains("D"));
372     assertTrue(s.contains("E"));
373 dl 1.1 }
374    
375 dl 1.5 /**
376     * entrySet contains all pairs
377     */
378     public void testEntrySet() {
379 dl 1.1 ConcurrentHashMap map = map5();
380 jsr166 1.17 Set s = map.entrySet();
381     assertEquals(5, s.size());
382 dl 1.1 Iterator it = s.iterator();
383     while (it.hasNext()) {
384     Map.Entry e = (Map.Entry) it.next();
385 jsr166 1.14 assertTrue(
386 dl 1.1 (e.getKey().equals(one) && e.getValue().equals("A")) ||
387     (e.getKey().equals(two) && e.getValue().equals("B")) ||
388     (e.getKey().equals(three) && e.getValue().equals("C")) ||
389     (e.getKey().equals(four) && e.getValue().equals("D")) ||
390     (e.getKey().equals(five) && e.getValue().equals("E")));
391     }
392     }
393    
394     /**
395 jsr166 1.22 * putAll adds all key-value pairs from the given map
396 dl 1.1 */
397 dl 1.5 public void testPutAll() {
398 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
399     ConcurrentHashMap map = map5();
400 jsr166 1.17 empty.putAll(map);
401     assertEquals(5, empty.size());
402     assertTrue(empty.containsKey(one));
403     assertTrue(empty.containsKey(two));
404     assertTrue(empty.containsKey(three));
405     assertTrue(empty.containsKey(four));
406     assertTrue(empty.containsKey(five));
407 dl 1.1 }
408    
409     /**
410 jsr166 1.22 * putIfAbsent works when the given key is not present
411 dl 1.1 */
412 dl 1.5 public void testPutIfAbsent() {
413 dl 1.1 ConcurrentHashMap map = map5();
414 jsr166 1.17 map.putIfAbsent(six, "Z");
415 dl 1.7 assertTrue(map.containsKey(six));
416 dl 1.1 }
417    
418     /**
419 jsr166 1.22 * putIfAbsent does not add the pair if the key is already present
420 dl 1.1 */
421 dl 1.5 public void testPutIfAbsent2() {
422 dl 1.1 ConcurrentHashMap map = map5();
423     assertEquals("A", map.putIfAbsent(one, "Z"));
424     }
425    
426     /**
427 jsr166 1.22 * replace fails when the given key is not present
428 dl 1.7 */
429     public void testReplace() {
430     ConcurrentHashMap map = map5();
431 jsr166 1.17 assertNull(map.replace(six, "Z"));
432 dl 1.7 assertFalse(map.containsKey(six));
433     }
434    
435     /**
436 jsr166 1.22 * replace succeeds if the key is already present
437 dl 1.7 */
438     public void testReplace2() {
439     ConcurrentHashMap map = map5();
440 dl 1.8 assertNotNull(map.replace(one, "Z"));
441 dl 1.7 assertEquals("Z", map.get(one));
442     }
443    
444     /**
445     * replace value fails when the given key not mapped to expected value
446     */
447     public void testReplaceValue() {
448     ConcurrentHashMap map = map5();
449     assertEquals("A", map.get(one));
450 jsr166 1.17 assertFalse(map.replace(one, "Z", "Z"));
451 dl 1.7 assertEquals("A", map.get(one));
452     }
453    
454     /**
455     * replace value succeeds when the given key mapped to expected value
456     */
457     public void testReplaceValue2() {
458     ConcurrentHashMap map = map5();
459     assertEquals("A", map.get(one));
460 jsr166 1.17 assertTrue(map.replace(one, "A", "Z"));
461 dl 1.7 assertEquals("Z", map.get(one));
462     }
463    
464     /**
465 jsr166 1.22 * remove removes the correct key-value pair from the map
466 dl 1.1 */
467 dl 1.5 public void testRemove() {
468 dl 1.1 ConcurrentHashMap map = map5();
469 jsr166 1.17 map.remove(five);
470     assertEquals(4, map.size());
471     assertFalse(map.containsKey(five));
472 dl 1.1 }
473    
474 dl 1.5 /**
475     * remove(key,value) removes only if pair present
476     */
477     public void testRemove2() {
478 dl 1.1 ConcurrentHashMap map = map5();
479 jsr166 1.17 map.remove(five, "E");
480     assertEquals(4, map.size());
481     assertFalse(map.containsKey(five));
482     map.remove(four, "A");
483     assertEquals(4, map.size());
484     assertTrue(map.containsKey(four));
485 dl 1.1 }
486    
487     /**
488 jsr166 1.22 * size returns the correct values
489 dl 1.1 */
490 dl 1.5 public void testSize() {
491 dl 1.1 ConcurrentHashMap map = map5();
492     ConcurrentHashMap empty = new ConcurrentHashMap();
493 jsr166 1.17 assertEquals(0, empty.size());
494     assertEquals(5, map.size());
495 dl 1.1 }
496    
497 dl 1.5 /**
498     * toString contains toString of elements
499     */
500     public void testToString() {
501 dl 1.1 ConcurrentHashMap map = map5();
502     String s = map.toString();
503     for (int i = 1; i <= 5; ++i) {
504 jsr166 1.24 assertTrue(s.contains(String.valueOf(i)));
505 dl 1.1 }
506 jsr166 1.14 }
507 dl 1.1
508     // Exception tests
509 jsr166 1.14
510 dl 1.5 /**
511 dl 1.36 * Cannot create with only negative capacity
512 dl 1.5 */
513     public void testConstructor1() {
514     try {
515 dl 1.36 new ConcurrentHashMap(-1);
516 dl 1.5 shouldThrow();
517 jsr166 1.19 } catch (IllegalArgumentException success) {}
518 dl 1.1 }
519    
520 dl 1.5 /**
521 dl 1.36 * Constructor (initialCapacity, loadFactor) throws
522     * IllegalArgumentException if either argument is negative
523     */
524 dl 1.5 public void testConstructor2() {
525     try {
526 dl 1.36 new ConcurrentHashMap(-1, .75f);
527 dl 1.5 shouldThrow();
528 dl 1.36 } catch (IllegalArgumentException e) {}
529    
530     try {
531     new ConcurrentHashMap(16, -1);
532     shouldThrow();
533     } catch (IllegalArgumentException e) {}
534 dl 1.1 }
535 dl 1.36
536     /**
537     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
538     * throws IllegalArgumentException if any argument is negative
539     */
540     public void testConstructor3() {
541     try {
542     new ConcurrentHashMap(-1, .75f, 1);
543     shouldThrow();
544     } catch (IllegalArgumentException e) {}
545    
546     try {
547     new ConcurrentHashMap(16, -1, 1);
548     shouldThrow();
549     } catch (IllegalArgumentException e) {}
550    
551     try {
552     new ConcurrentHashMap(16, .75f, -1);
553     shouldThrow();
554     } catch (IllegalArgumentException e) {}
555     }
556 dl 1.1
557 dl 1.5 /**
558 dl 1.36 * ConcurrentHashMap(map) throws NullPointerException if the given
559     * map is null
560 dl 1.5 */
561 dl 1.36 public void testConstructor4() {
562 dl 1.5 try {
563 dl 1.36 new ConcurrentHashMap(null);
564 dl 1.5 shouldThrow();
565 dl 1.36 } catch (NullPointerException e) {}
566     }
567    
568     /**
569     * ConcurrentHashMap(map) creates a new map with the same mappings
570     * as the given map
571     */
572     public void testConstructor5() {
573     ConcurrentHashMap map1 = map5();
574     ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
575     assertTrue(map2.equals(map1));
576     map2.put(one, "F");
577     assertFalse(map2.equals(map1));
578 dl 1.1 }
579    
580 dl 1.36
581 dl 1.5 /**
582     * get(null) throws NPE
583     */
584     public void testGet_NullPointerException() {
585     try {
586 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
587     c.get(null);
588 dl 1.5 shouldThrow();
589 jsr166 1.19 } catch (NullPointerException success) {}
590 dl 1.1 }
591    
592 dl 1.5 /**
593     * containsKey(null) throws NPE
594     */
595     public void testContainsKey_NullPointerException() {
596     try {
597 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
598     c.containsKey(null);
599 dl 1.5 shouldThrow();
600 jsr166 1.19 } catch (NullPointerException success) {}
601 dl 1.1 }
602    
603 dl 1.5 /**
604     * containsValue(null) throws NPE
605     */
606     public void testContainsValue_NullPointerException() {
607     try {
608 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
609     c.containsValue(null);
610 dl 1.5 shouldThrow();
611 jsr166 1.19 } catch (NullPointerException success) {}
612 dl 1.1 }
613    
614 dl 1.5 /**
615     * contains(null) throws NPE
616     */
617     public void testContains_NullPointerException() {
618     try {
619 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
620     c.contains(null);
621 dl 1.5 shouldThrow();
622 jsr166 1.19 } catch (NullPointerException success) {}
623 dl 1.1 }
624    
625 dl 1.5 /**
626     * put(null,x) throws NPE
627     */
628     public void testPut1_NullPointerException() {
629     try {
630 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
631     c.put(null, "whatever");
632 dl 1.5 shouldThrow();
633 jsr166 1.19 } catch (NullPointerException success) {}
634 dl 1.1 }
635    
636 dl 1.5 /**
637     * put(x, null) throws NPE
638     */
639     public void testPut2_NullPointerException() {
640     try {
641 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
642     c.put("whatever", null);
643 dl 1.5 shouldThrow();
644 jsr166 1.19 } catch (NullPointerException success) {}
645 dl 1.1 }
646    
647 dl 1.5 /**
648     * putIfAbsent(null, x) throws NPE
649     */
650     public void testPutIfAbsent1_NullPointerException() {
651     try {
652 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
653     c.putIfAbsent(null, "whatever");
654 dl 1.5 shouldThrow();
655 jsr166 1.19 } catch (NullPointerException success) {}
656 dl 1.1 }
657    
658 dl 1.5 /**
659 dl 1.7 * replace(null, x) throws NPE
660     */
661     public void testReplace_NullPointerException() {
662     try {
663     ConcurrentHashMap c = new ConcurrentHashMap(5);
664     c.replace(null, "whatever");
665     shouldThrow();
666 jsr166 1.19 } catch (NullPointerException success) {}
667 dl 1.7 }
668    
669     /**
670     * replace(null, x, y) throws NPE
671     */
672     public void testReplaceValue_NullPointerException() {
673     try {
674     ConcurrentHashMap c = new ConcurrentHashMap(5);
675     c.replace(null, one, "whatever");
676     shouldThrow();
677 jsr166 1.19 } catch (NullPointerException success) {}
678 dl 1.7 }
679    
680     /**
681 dl 1.5 * putIfAbsent(x, null) throws NPE
682     */
683     public void testPutIfAbsent2_NullPointerException() {
684     try {
685 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
686     c.putIfAbsent("whatever", null);
687 dl 1.7 shouldThrow();
688 jsr166 1.19 } catch (NullPointerException success) {}
689 dl 1.7 }
690    
691     /**
692     * replace(x, null) throws NPE
693     */
694     public void testReplace2_NullPointerException() {
695     try {
696     ConcurrentHashMap c = new ConcurrentHashMap(5);
697     c.replace("whatever", null);
698     shouldThrow();
699 jsr166 1.19 } catch (NullPointerException success) {}
700 dl 1.7 }
701    
702     /**
703     * replace(x, null, y) throws NPE
704     */
705     public void testReplaceValue2_NullPointerException() {
706     try {
707     ConcurrentHashMap c = new ConcurrentHashMap(5);
708     c.replace("whatever", null, "A");
709     shouldThrow();
710 jsr166 1.19 } catch (NullPointerException success) {}
711 dl 1.7 }
712    
713     /**
714     * replace(x, y, null) throws NPE
715     */
716     public void testReplaceValue3_NullPointerException() {
717     try {
718     ConcurrentHashMap c = new ConcurrentHashMap(5);
719     c.replace("whatever", one, null);
720 dl 1.5 shouldThrow();
721 jsr166 1.19 } catch (NullPointerException success) {}
722 dl 1.1 }
723    
724 dl 1.5 /**
725     * remove(null) throws NPE
726     */
727     public void testRemove1_NullPointerException() {
728     try {
729 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
730     c.put("sadsdf", "asdads");
731     c.remove(null);
732 dl 1.5 shouldThrow();
733 jsr166 1.19 } catch (NullPointerException success) {}
734 dl 1.1 }
735    
736 dl 1.5 /**
737     * remove(null, x) throws NPE
738     */
739     public void testRemove2_NullPointerException() {
740     try {
741 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
742     c.put("sadsdf", "asdads");
743     c.remove(null, "whatever");
744 dl 1.5 shouldThrow();
745 jsr166 1.19 } catch (NullPointerException success) {}
746 dl 1.1 }
747 dl 1.2
748 dl 1.5 /**
749 dl 1.12 * remove(x, null) returns false
750     */
751     public void testRemove3() {
752 jsr166 1.19 ConcurrentHashMap c = new ConcurrentHashMap(5);
753     c.put("sadsdf", "asdads");
754     assertFalse(c.remove("sadsdf", null));
755 dl 1.12 }
756    
757     /**
758 dl 1.5 * A deserialized map equals original
759     */
760 jsr166 1.18 public void testSerialization() throws Exception {
761 jsr166 1.25 Map x = map5();
762     Map y = serialClone(x);
763 dl 1.2
764 jsr166 1.31 assertNotSame(x, y);
765 jsr166 1.25 assertEquals(x.size(), y.size());
766     assertEquals(x, y);
767     assertEquals(y, x);
768 dl 1.2 }
769 dl 1.6
770     /**
771     * SetValue of an EntrySet entry sets value in the map.
772     */
773     public void testSetValueWriteThrough() {
774 jsr166 1.14 // Adapted from a bug report by Eric Zoerner
775 dl 1.6 ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
776     assertTrue(map.isEmpty());
777     for (int i = 0; i < 20; i++)
778     map.put(new Integer(i), new Integer(i));
779     assertFalse(map.isEmpty());
780     Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
781 dl 1.29 // Unless it happens to be first (in which case remainder of
782     // test is skipped), remove a possibly-colliding key from map
783     // which, under some implementations, may cause entry1 to be
784     // cloned in map
785     if (!entry1.getKey().equals(new Integer(16))) {
786     map.remove(new Integer(16));
787     entry1.setValue("XYZ");
788     assertTrue(map.containsValue("XYZ")); // fails if write-through broken
789     }
790 dl 1.6 }
791 jsr166 1.14
792 dl 1.1 }