ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.47
Committed: Mon Jul 27 03:12:26 2015 UTC (8 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.46: +1 -1 lines
Log Message:
whitespace

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