ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.41
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.40: +0 -1 lines
Log Message:
delete extraneous blank lines

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