ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.42
Committed: Sun Feb 22 04:34:44 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.41: +1 -0 lines
Log Message:
unused variable cleanup

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