ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.38
Committed: Tue Sep 24 17:03:14 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +2 -2 lines
Log Message:
testMixedComparable: reduce run time

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.38 int size = 1200; // makes measured test run time -> 35ms
164 jsr166 1.35 ConcurrentHashMap<Object, Object> map =
165 dl 1.34 new ConcurrentHashMap<Object, Object>();
166 jsr166 1.38 Random rng = new Random();
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 jsr166 1.37 * hashCode() equals sum of each key.hashCode ^ value.hashCode
217     */
218 dl 1.36 public void testHashCode() {
219     ConcurrentHashMap<Integer,String> map = map5();
220     int sum = 0;
221     for (Map.Entry<Integer,String> e : map.entrySet())
222     sum += e.getKey().hashCode() ^ e.getValue().hashCode();
223     assertEquals(sum, map.hashCode());
224     }
225    
226 dl 1.5 /**
227 jsr166 1.22 * contains returns true for contained value
228 dl 1.1 */
229 dl 1.5 public void testContains() {
230 dl 1.1 ConcurrentHashMap map = map5();
231 jsr166 1.17 assertTrue(map.contains("A"));
232 dl 1.1 assertFalse(map.contains("Z"));
233     }
234 jsr166 1.14
235 dl 1.1 /**
236 jsr166 1.22 * containsKey returns true for contained key
237 dl 1.1 */
238 dl 1.5 public void testContainsKey() {
239 dl 1.1 ConcurrentHashMap map = map5();
240 jsr166 1.17 assertTrue(map.containsKey(one));
241 dl 1.5 assertFalse(map.containsKey(zero));
242 dl 1.1 }
243    
244     /**
245 jsr166 1.22 * containsValue returns true for held values
246 dl 1.1 */
247 dl 1.5 public void testContainsValue() {
248 dl 1.1 ConcurrentHashMap map = map5();
249 jsr166 1.17 assertTrue(map.containsValue("A"));
250 dl 1.11 assertFalse(map.containsValue("Z"));
251 dl 1.1 }
252    
253     /**
254 jsr166 1.22 * enumeration returns an enumeration containing the correct
255     * elements
256 dl 1.1 */
257 dl 1.5 public void testEnumeration() {
258 dl 1.1 ConcurrentHashMap map = map5();
259 jsr166 1.17 Enumeration e = map.elements();
260     int count = 0;
261     while (e.hasMoreElements()) {
262     count++;
263     e.nextElement();
264     }
265     assertEquals(5, count);
266 dl 1.4 }
267    
268     /**
269 jsr166 1.22 * get returns the correct element at the given key,
270     * or null if not present
271 dl 1.1 */
272 dl 1.5 public void testGet() {
273 dl 1.1 ConcurrentHashMap map = map5();
274 jsr166 1.17 assertEquals("A", (String)map.get(one));
275 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
276 dl 1.5 assertNull(map.get("anything"));
277 dl 1.1 }
278    
279     /**
280 jsr166 1.22 * isEmpty is true of empty map and false for non-empty
281 dl 1.1 */
282 dl 1.5 public void testIsEmpty() {
283 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
284     ConcurrentHashMap map = map5();
285 jsr166 1.17 assertTrue(empty.isEmpty());
286 dl 1.1 assertFalse(map.isEmpty());
287     }
288    
289     /**
290 jsr166 1.22 * keys returns an enumeration containing all the keys from the map
291 dl 1.1 */
292 dl 1.5 public void testKeys() {
293 dl 1.1 ConcurrentHashMap map = map5();
294 jsr166 1.17 Enumeration e = map.keys();
295     int count = 0;
296     while (e.hasMoreElements()) {
297     count++;
298     e.nextElement();
299     }
300     assertEquals(5, count);
301 dl 1.1 }
302    
303     /**
304 jsr166 1.22 * keySet returns a Set containing all the keys
305 dl 1.1 */
306 dl 1.5 public void testKeySet() {
307 dl 1.1 ConcurrentHashMap map = map5();
308 jsr166 1.17 Set s = map.keySet();
309     assertEquals(5, s.size());
310     assertTrue(s.contains(one));
311     assertTrue(s.contains(two));
312     assertTrue(s.contains(three));
313     assertTrue(s.contains(four));
314     assertTrue(s.contains(five));
315 dl 1.1 }
316    
317 dl 1.5 /**
318 jsr166 1.22 * keySet.toArray returns contains all keys
319 dl 1.13 */
320     public void testKeySetToArray() {
321     ConcurrentHashMap map = map5();
322 jsr166 1.17 Set s = map.keySet();
323 dl 1.13 Object[] ar = s.toArray();
324     assertTrue(s.containsAll(Arrays.asList(ar)));
325 jsr166 1.17 assertEquals(5, ar.length);
326 dl 1.13 ar[0] = m10;
327     assertFalse(s.containsAll(Arrays.asList(ar)));
328     }
329    
330     /**
331 jsr166 1.22 * Values.toArray contains all values
332 dl 1.13 */
333     public void testValuesToArray() {
334     ConcurrentHashMap map = map5();
335 jsr166 1.17 Collection v = map.values();
336 dl 1.13 Object[] ar = v.toArray();
337     ArrayList s = new ArrayList(Arrays.asList(ar));
338 jsr166 1.17 assertEquals(5, ar.length);
339     assertTrue(s.contains("A"));
340     assertTrue(s.contains("B"));
341     assertTrue(s.contains("C"));
342     assertTrue(s.contains("D"));
343     assertTrue(s.contains("E"));
344 dl 1.13 }
345    
346     /**
347 jsr166 1.22 * entrySet.toArray contains all entries
348 dl 1.13 */
349     public void testEntrySetToArray() {
350     ConcurrentHashMap map = map5();
351 jsr166 1.17 Set s = map.entrySet();
352 dl 1.13 Object[] ar = s.toArray();
353     assertEquals(5, ar.length);
354     for (int i = 0; i < 5; ++i) {
355     assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
356     assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
357     }
358     }
359    
360     /**
361 dl 1.5 * values collection contains all values
362     */
363     public void testValues() {
364 dl 1.1 ConcurrentHashMap map = map5();
365 jsr166 1.17 Collection s = map.values();
366     assertEquals(5, s.size());
367     assertTrue(s.contains("A"));
368     assertTrue(s.contains("B"));
369     assertTrue(s.contains("C"));
370     assertTrue(s.contains("D"));
371     assertTrue(s.contains("E"));
372 dl 1.1 }
373    
374 dl 1.5 /**
375     * entrySet contains all pairs
376     */
377     public void testEntrySet() {
378 dl 1.1 ConcurrentHashMap map = map5();
379 jsr166 1.17 Set s = map.entrySet();
380     assertEquals(5, s.size());
381 dl 1.1 Iterator it = s.iterator();
382     while (it.hasNext()) {
383     Map.Entry e = (Map.Entry) it.next();
384 jsr166 1.14 assertTrue(
385 dl 1.1 (e.getKey().equals(one) && e.getValue().equals("A")) ||
386     (e.getKey().equals(two) && e.getValue().equals("B")) ||
387     (e.getKey().equals(three) && e.getValue().equals("C")) ||
388     (e.getKey().equals(four) && e.getValue().equals("D")) ||
389     (e.getKey().equals(five) && e.getValue().equals("E")));
390     }
391     }
392    
393     /**
394 jsr166 1.22 * putAll adds all key-value pairs from the given map
395 dl 1.1 */
396 dl 1.5 public void testPutAll() {
397 dl 1.1 ConcurrentHashMap empty = new ConcurrentHashMap();
398     ConcurrentHashMap map = map5();
399 jsr166 1.17 empty.putAll(map);
400     assertEquals(5, empty.size());
401     assertTrue(empty.containsKey(one));
402     assertTrue(empty.containsKey(two));
403     assertTrue(empty.containsKey(three));
404     assertTrue(empty.containsKey(four));
405     assertTrue(empty.containsKey(five));
406 dl 1.1 }
407    
408     /**
409 jsr166 1.22 * putIfAbsent works when the given key is not present
410 dl 1.1 */
411 dl 1.5 public void testPutIfAbsent() {
412 dl 1.1 ConcurrentHashMap map = map5();
413 jsr166 1.17 map.putIfAbsent(six, "Z");
414 dl 1.7 assertTrue(map.containsKey(six));
415 dl 1.1 }
416    
417     /**
418 jsr166 1.22 * putIfAbsent does not add the pair if the key is already present
419 dl 1.1 */
420 dl 1.5 public void testPutIfAbsent2() {
421 dl 1.1 ConcurrentHashMap map = map5();
422     assertEquals("A", map.putIfAbsent(one, "Z"));
423     }
424    
425     /**
426 jsr166 1.22 * replace fails when the given key is not present
427 dl 1.7 */
428     public void testReplace() {
429     ConcurrentHashMap map = map5();
430 jsr166 1.17 assertNull(map.replace(six, "Z"));
431 dl 1.7 assertFalse(map.containsKey(six));
432     }
433    
434     /**
435 jsr166 1.22 * replace succeeds if the key is already present
436 dl 1.7 */
437     public void testReplace2() {
438     ConcurrentHashMap map = map5();
439 dl 1.8 assertNotNull(map.replace(one, "Z"));
440 dl 1.7 assertEquals("Z", map.get(one));
441     }
442    
443     /**
444     * replace value fails when the given key not mapped to expected value
445     */
446     public void testReplaceValue() {
447     ConcurrentHashMap map = map5();
448     assertEquals("A", map.get(one));
449 jsr166 1.17 assertFalse(map.replace(one, "Z", "Z"));
450 dl 1.7 assertEquals("A", map.get(one));
451     }
452    
453     /**
454     * replace value succeeds when the given key mapped to expected value
455     */
456     public void testReplaceValue2() {
457     ConcurrentHashMap map = map5();
458     assertEquals("A", map.get(one));
459 jsr166 1.17 assertTrue(map.replace(one, "A", "Z"));
460 dl 1.7 assertEquals("Z", map.get(one));
461     }
462    
463     /**
464 jsr166 1.22 * remove removes the correct key-value pair from the map
465 dl 1.1 */
466 dl 1.5 public void testRemove() {
467 dl 1.1 ConcurrentHashMap map = map5();
468 jsr166 1.17 map.remove(five);
469     assertEquals(4, map.size());
470     assertFalse(map.containsKey(five));
471 dl 1.1 }
472    
473 dl 1.5 /**
474     * remove(key,value) removes only if pair present
475     */
476     public void testRemove2() {
477 dl 1.1 ConcurrentHashMap map = map5();
478 jsr166 1.17 map.remove(five, "E");
479     assertEquals(4, map.size());
480     assertFalse(map.containsKey(five));
481     map.remove(four, "A");
482     assertEquals(4, map.size());
483     assertTrue(map.containsKey(four));
484 dl 1.1 }
485    
486     /**
487 jsr166 1.22 * size returns the correct values
488 dl 1.1 */
489 dl 1.5 public void testSize() {
490 dl 1.1 ConcurrentHashMap map = map5();
491     ConcurrentHashMap empty = new ConcurrentHashMap();
492 jsr166 1.17 assertEquals(0, empty.size());
493     assertEquals(5, map.size());
494 dl 1.1 }
495    
496 dl 1.5 /**
497     * toString contains toString of elements
498     */
499     public void testToString() {
500 dl 1.1 ConcurrentHashMap map = map5();
501     String s = map.toString();
502     for (int i = 1; i <= 5; ++i) {
503 jsr166 1.24 assertTrue(s.contains(String.valueOf(i)));
504 dl 1.1 }
505 jsr166 1.14 }
506 dl 1.1
507     // Exception tests
508 jsr166 1.14
509 dl 1.5 /**
510 dl 1.36 * Cannot create with only negative capacity
511 dl 1.5 */
512     public void testConstructor1() {
513     try {
514 dl 1.36 new ConcurrentHashMap(-1);
515 dl 1.5 shouldThrow();
516 jsr166 1.19 } catch (IllegalArgumentException success) {}
517 dl 1.1 }
518    
519 dl 1.5 /**
520 dl 1.36 * Constructor (initialCapacity, loadFactor) throws
521     * IllegalArgumentException if either argument is negative
522     */
523 dl 1.5 public void testConstructor2() {
524     try {
525 dl 1.36 new ConcurrentHashMap(-1, .75f);
526 dl 1.5 shouldThrow();
527 jsr166 1.37 } catch (IllegalArgumentException e) {}
528    
529 dl 1.36 try {
530     new ConcurrentHashMap(16, -1);
531     shouldThrow();
532 jsr166 1.37 } catch (IllegalArgumentException e) {}
533 dl 1.1 }
534 jsr166 1.37
535     /**
536     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
537     * throws IllegalArgumentException if any argument is negative
538     */
539 dl 1.36 public void testConstructor3() {
540 jsr166 1.37 try {
541     new ConcurrentHashMap(-1, .75f, 1);
542     shouldThrow();
543     } catch (IllegalArgumentException e) {}
544    
545     try {
546     new ConcurrentHashMap(16, -1, 1);
547     shouldThrow();
548     } catch (IllegalArgumentException e) {}
549    
550     try {
551     new ConcurrentHashMap(16, .75f, -1);
552     shouldThrow();
553     } catch (IllegalArgumentException e) {}
554     }
555 dl 1.1
556 dl 1.5 /**
557 dl 1.36 * ConcurrentHashMap(map) throws NullPointerException if the given
558     * map is null
559 dl 1.5 */
560 dl 1.36 public void testConstructor4() {
561 dl 1.5 try {
562 dl 1.36 new ConcurrentHashMap(null);
563 dl 1.5 shouldThrow();
564 dl 1.36 } catch (NullPointerException e) {}
565     }
566    
567     /**
568     * ConcurrentHashMap(map) creates a new map with the same mappings
569     * as the given map
570     */
571     public void testConstructor5() {
572     ConcurrentHashMap map1 = map5();
573     ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
574     assertTrue(map2.equals(map1));
575     map2.put(one, "F");
576     assertFalse(map2.equals(map1));
577 dl 1.1 }
578    
579 dl 1.36
580 dl 1.5 /**
581     * get(null) throws NPE
582     */
583     public void testGet_NullPointerException() {
584     try {
585 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
586     c.get(null);
587 dl 1.5 shouldThrow();
588 jsr166 1.19 } catch (NullPointerException success) {}
589 dl 1.1 }
590    
591 dl 1.5 /**
592     * containsKey(null) throws NPE
593     */
594     public void testContainsKey_NullPointerException() {
595     try {
596 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
597     c.containsKey(null);
598 dl 1.5 shouldThrow();
599 jsr166 1.19 } catch (NullPointerException success) {}
600 dl 1.1 }
601    
602 dl 1.5 /**
603     * containsValue(null) throws NPE
604     */
605     public void testContainsValue_NullPointerException() {
606     try {
607 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
608     c.containsValue(null);
609 dl 1.5 shouldThrow();
610 jsr166 1.19 } catch (NullPointerException success) {}
611 dl 1.1 }
612    
613 dl 1.5 /**
614     * contains(null) throws NPE
615     */
616     public void testContains_NullPointerException() {
617     try {
618 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
619     c.contains(null);
620 dl 1.5 shouldThrow();
621 jsr166 1.19 } catch (NullPointerException success) {}
622 dl 1.1 }
623    
624 dl 1.5 /**
625     * put(null,x) throws NPE
626     */
627     public void testPut1_NullPointerException() {
628     try {
629 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
630     c.put(null, "whatever");
631 dl 1.5 shouldThrow();
632 jsr166 1.19 } catch (NullPointerException success) {}
633 dl 1.1 }
634    
635 dl 1.5 /**
636     * put(x, null) throws NPE
637     */
638     public void testPut2_NullPointerException() {
639     try {
640 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
641     c.put("whatever", null);
642 dl 1.5 shouldThrow();
643 jsr166 1.19 } catch (NullPointerException success) {}
644 dl 1.1 }
645    
646 dl 1.5 /**
647     * putIfAbsent(null, x) throws NPE
648     */
649     public void testPutIfAbsent1_NullPointerException() {
650     try {
651 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
652     c.putIfAbsent(null, "whatever");
653 dl 1.5 shouldThrow();
654 jsr166 1.19 } catch (NullPointerException success) {}
655 dl 1.1 }
656    
657 dl 1.5 /**
658 dl 1.7 * replace(null, x) throws NPE
659     */
660     public void testReplace_NullPointerException() {
661     try {
662     ConcurrentHashMap c = new ConcurrentHashMap(5);
663     c.replace(null, "whatever");
664     shouldThrow();
665 jsr166 1.19 } catch (NullPointerException success) {}
666 dl 1.7 }
667    
668     /**
669     * replace(null, x, y) throws NPE
670     */
671     public void testReplaceValue_NullPointerException() {
672     try {
673     ConcurrentHashMap c = new ConcurrentHashMap(5);
674     c.replace(null, one, "whatever");
675     shouldThrow();
676 jsr166 1.19 } catch (NullPointerException success) {}
677 dl 1.7 }
678    
679     /**
680 dl 1.5 * putIfAbsent(x, null) throws NPE
681     */
682     public void testPutIfAbsent2_NullPointerException() {
683     try {
684 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
685     c.putIfAbsent("whatever", null);
686 dl 1.7 shouldThrow();
687 jsr166 1.19 } catch (NullPointerException success) {}
688 dl 1.7 }
689    
690     /**
691     * replace(x, null) throws NPE
692     */
693     public void testReplace2_NullPointerException() {
694     try {
695     ConcurrentHashMap c = new ConcurrentHashMap(5);
696     c.replace("whatever", null);
697     shouldThrow();
698 jsr166 1.19 } catch (NullPointerException success) {}
699 dl 1.7 }
700    
701     /**
702     * replace(x, null, y) throws NPE
703     */
704     public void testReplaceValue2_NullPointerException() {
705     try {
706     ConcurrentHashMap c = new ConcurrentHashMap(5);
707     c.replace("whatever", null, "A");
708     shouldThrow();
709 jsr166 1.19 } catch (NullPointerException success) {}
710 dl 1.7 }
711    
712     /**
713     * replace(x, y, null) throws NPE
714     */
715     public void testReplaceValue3_NullPointerException() {
716     try {
717     ConcurrentHashMap c = new ConcurrentHashMap(5);
718     c.replace("whatever", one, null);
719 dl 1.5 shouldThrow();
720 jsr166 1.19 } catch (NullPointerException success) {}
721 dl 1.1 }
722    
723 dl 1.5 /**
724     * remove(null) throws NPE
725     */
726     public void testRemove1_NullPointerException() {
727     try {
728 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
729     c.put("sadsdf", "asdads");
730     c.remove(null);
731 dl 1.5 shouldThrow();
732 jsr166 1.19 } catch (NullPointerException success) {}
733 dl 1.1 }
734    
735 dl 1.5 /**
736     * remove(null, x) throws NPE
737     */
738     public void testRemove2_NullPointerException() {
739     try {
740 dl 1.1 ConcurrentHashMap c = new ConcurrentHashMap(5);
741     c.put("sadsdf", "asdads");
742     c.remove(null, "whatever");
743 dl 1.5 shouldThrow();
744 jsr166 1.19 } catch (NullPointerException success) {}
745 dl 1.1 }
746 dl 1.2
747 dl 1.5 /**
748 dl 1.12 * remove(x, null) returns false
749     */
750     public void testRemove3() {
751 jsr166 1.19 ConcurrentHashMap c = new ConcurrentHashMap(5);
752     c.put("sadsdf", "asdads");
753     assertFalse(c.remove("sadsdf", null));
754 dl 1.12 }
755    
756     /**
757 dl 1.5 * A deserialized map equals original
758     */
759 jsr166 1.18 public void testSerialization() throws Exception {
760 jsr166 1.25 Map x = map5();
761     Map y = serialClone(x);
762 dl 1.2
763 jsr166 1.31 assertNotSame(x, y);
764 jsr166 1.25 assertEquals(x.size(), y.size());
765     assertEquals(x, y);
766     assertEquals(y, x);
767 dl 1.2 }
768 dl 1.6
769     /**
770     * SetValue of an EntrySet entry sets value in the map.
771     */
772     public void testSetValueWriteThrough() {
773 jsr166 1.14 // Adapted from a bug report by Eric Zoerner
774 dl 1.6 ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
775     assertTrue(map.isEmpty());
776     for (int i = 0; i < 20; i++)
777     map.put(new Integer(i), new Integer(i));
778     assertFalse(map.isEmpty());
779     Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
780 dl 1.29 // Unless it happens to be first (in which case remainder of
781     // test is skipped), remove a possibly-colliding key from map
782     // which, under some implementations, may cause entry1 to be
783     // cloned in map
784     if (!entry1.getKey().equals(new Integer(16))) {
785     map.remove(new Integer(16));
786     entry1.setValue("XYZ");
787     assertTrue(map.containsValue("XYZ")); // fails if write-through broken
788     }
789 dl 1.6 }
790 jsr166 1.14
791 dl 1.1 }