1 |
/* |
2 |
* 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 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import junit.framework.*; |
10 |
import java.util.*; |
11 |
import java.util.concurrent.ConcurrentHashMap; |
12 |
|
13 |
public class ConcurrentHashMapTest extends JSR166TestCase { |
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run(suite()); |
16 |
} |
17 |
public static Test suite() { |
18 |
return new TestSuite(ConcurrentHashMapTest.class); |
19 |
} |
20 |
|
21 |
/** |
22 |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
23 |
*/ |
24 |
private static ConcurrentHashMap map5() { |
25 |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
26 |
assertTrue(map.isEmpty()); |
27 |
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 |
assertFalse(map.isEmpty()); |
33 |
assertEquals(5, map.size()); |
34 |
return map; |
35 |
} |
36 |
|
37 |
/** 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 |
// 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 |
return compare(value, other.value); |
46 |
} |
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 |
r = compare(size(), other.size()); |
80 |
return r; |
81 |
} |
82 |
private static final long serialVersionUID = 0; |
83 |
} |
84 |
|
85 |
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 |
/** |
102 |
* Inserted elements that are subclasses of the same Comparable |
103 |
* class are found. |
104 |
*/ |
105 |
public void testComparableFamily() { |
106 |
ConcurrentHashMap<BI, Boolean> m = |
107 |
new ConcurrentHashMap<BI, Boolean>(); |
108 |
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 |
ConcurrentHashMap<Object, Boolean> m = |
123 |
new ConcurrentHashMap<Object, Boolean>(); |
124 |
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 |
ConcurrentHashMap<Object, Boolean> m = |
147 |
new ConcurrentHashMap<Object, Boolean>(); |
148 |
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 |
/** |
159 |
* Mixtures of instances of comparable and non-comparable classes |
160 |
* can be inserted and found. |
161 |
*/ |
162 |
public void testMixedComparable() { |
163 |
int size = 10000; |
164 |
ConcurrentHashMap<Object, Object> map = |
165 |
new ConcurrentHashMap<Object, Object>(); |
166 |
Random rng = new Random(1370014958369218000L); |
167 |
for(int i = 0; i < size; i++) { |
168 |
Object x; |
169 |
switch(rng.nextInt(4)) { |
170 |
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 |
* clear removes all pairs |
195 |
*/ |
196 |
public void testClear() { |
197 |
ConcurrentHashMap map = map5(); |
198 |
map.clear(); |
199 |
assertEquals(0, map.size()); |
200 |
} |
201 |
|
202 |
/** |
203 |
* Maps with same contents are equal |
204 |
*/ |
205 |
public void testEquals() { |
206 |
ConcurrentHashMap map1 = map5(); |
207 |
ConcurrentHashMap map2 = map5(); |
208 |
assertEquals(map1, map2); |
209 |
assertEquals(map2, map1); |
210 |
map1.clear(); |
211 |
assertFalse(map1.equals(map2)); |
212 |
assertFalse(map2.equals(map1)); |
213 |
} |
214 |
|
215 |
/** |
216 |
* contains returns true for contained value |
217 |
*/ |
218 |
public void testContains() { |
219 |
ConcurrentHashMap map = map5(); |
220 |
assertTrue(map.contains("A")); |
221 |
assertFalse(map.contains("Z")); |
222 |
} |
223 |
|
224 |
/** |
225 |
* containsKey returns true for contained key |
226 |
*/ |
227 |
public void testContainsKey() { |
228 |
ConcurrentHashMap map = map5(); |
229 |
assertTrue(map.containsKey(one)); |
230 |
assertFalse(map.containsKey(zero)); |
231 |
} |
232 |
|
233 |
/** |
234 |
* containsValue returns true for held values |
235 |
*/ |
236 |
public void testContainsValue() { |
237 |
ConcurrentHashMap map = map5(); |
238 |
assertTrue(map.containsValue("A")); |
239 |
assertFalse(map.containsValue("Z")); |
240 |
} |
241 |
|
242 |
/** |
243 |
* enumeration returns an enumeration containing the correct |
244 |
* elements |
245 |
*/ |
246 |
public void testEnumeration() { |
247 |
ConcurrentHashMap map = map5(); |
248 |
Enumeration e = map.elements(); |
249 |
int count = 0; |
250 |
while (e.hasMoreElements()) { |
251 |
count++; |
252 |
e.nextElement(); |
253 |
} |
254 |
assertEquals(5, count); |
255 |
} |
256 |
|
257 |
/** |
258 |
* get returns the correct element at the given key, |
259 |
* or null if not present |
260 |
*/ |
261 |
public void testGet() { |
262 |
ConcurrentHashMap map = map5(); |
263 |
assertEquals("A", (String)map.get(one)); |
264 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
265 |
assertNull(map.get("anything")); |
266 |
} |
267 |
|
268 |
/** |
269 |
* isEmpty is true of empty map and false for non-empty |
270 |
*/ |
271 |
public void testIsEmpty() { |
272 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
273 |
ConcurrentHashMap map = map5(); |
274 |
assertTrue(empty.isEmpty()); |
275 |
assertFalse(map.isEmpty()); |
276 |
} |
277 |
|
278 |
/** |
279 |
* keys returns an enumeration containing all the keys from the map |
280 |
*/ |
281 |
public void testKeys() { |
282 |
ConcurrentHashMap map = map5(); |
283 |
Enumeration e = map.keys(); |
284 |
int count = 0; |
285 |
while (e.hasMoreElements()) { |
286 |
count++; |
287 |
e.nextElement(); |
288 |
} |
289 |
assertEquals(5, count); |
290 |
} |
291 |
|
292 |
/** |
293 |
* keySet returns a Set containing all the keys |
294 |
*/ |
295 |
public void testKeySet() { |
296 |
ConcurrentHashMap map = map5(); |
297 |
Set s = map.keySet(); |
298 |
assertEquals(5, s.size()); |
299 |
assertTrue(s.contains(one)); |
300 |
assertTrue(s.contains(two)); |
301 |
assertTrue(s.contains(three)); |
302 |
assertTrue(s.contains(four)); |
303 |
assertTrue(s.contains(five)); |
304 |
} |
305 |
|
306 |
/** |
307 |
* keySet.toArray returns contains all keys |
308 |
*/ |
309 |
public void testKeySetToArray() { |
310 |
ConcurrentHashMap map = map5(); |
311 |
Set s = map.keySet(); |
312 |
Object[] ar = s.toArray(); |
313 |
assertTrue(s.containsAll(Arrays.asList(ar))); |
314 |
assertEquals(5, ar.length); |
315 |
ar[0] = m10; |
316 |
assertFalse(s.containsAll(Arrays.asList(ar))); |
317 |
} |
318 |
|
319 |
/** |
320 |
* Values.toArray contains all values |
321 |
*/ |
322 |
public void testValuesToArray() { |
323 |
ConcurrentHashMap map = map5(); |
324 |
Collection v = map.values(); |
325 |
Object[] ar = v.toArray(); |
326 |
ArrayList s = new ArrayList(Arrays.asList(ar)); |
327 |
assertEquals(5, ar.length); |
328 |
assertTrue(s.contains("A")); |
329 |
assertTrue(s.contains("B")); |
330 |
assertTrue(s.contains("C")); |
331 |
assertTrue(s.contains("D")); |
332 |
assertTrue(s.contains("E")); |
333 |
} |
334 |
|
335 |
/** |
336 |
* entrySet.toArray contains all entries |
337 |
*/ |
338 |
public void testEntrySetToArray() { |
339 |
ConcurrentHashMap map = map5(); |
340 |
Set s = map.entrySet(); |
341 |
Object[] ar = s.toArray(); |
342 |
assertEquals(5, ar.length); |
343 |
for (int i = 0; i < 5; ++i) { |
344 |
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
345 |
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
346 |
} |
347 |
} |
348 |
|
349 |
/** |
350 |
* values collection contains all values |
351 |
*/ |
352 |
public void testValues() { |
353 |
ConcurrentHashMap map = map5(); |
354 |
Collection s = map.values(); |
355 |
assertEquals(5, s.size()); |
356 |
assertTrue(s.contains("A")); |
357 |
assertTrue(s.contains("B")); |
358 |
assertTrue(s.contains("C")); |
359 |
assertTrue(s.contains("D")); |
360 |
assertTrue(s.contains("E")); |
361 |
} |
362 |
|
363 |
/** |
364 |
* entrySet contains all pairs |
365 |
*/ |
366 |
public void testEntrySet() { |
367 |
ConcurrentHashMap map = map5(); |
368 |
Set s = map.entrySet(); |
369 |
assertEquals(5, s.size()); |
370 |
Iterator it = s.iterator(); |
371 |
while (it.hasNext()) { |
372 |
Map.Entry e = (Map.Entry) it.next(); |
373 |
assertTrue( |
374 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
375 |
(e.getKey().equals(two) && e.getValue().equals("B")) || |
376 |
(e.getKey().equals(three) && e.getValue().equals("C")) || |
377 |
(e.getKey().equals(four) && e.getValue().equals("D")) || |
378 |
(e.getKey().equals(five) && e.getValue().equals("E"))); |
379 |
} |
380 |
} |
381 |
|
382 |
/** |
383 |
* putAll adds all key-value pairs from the given map |
384 |
*/ |
385 |
public void testPutAll() { |
386 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
387 |
ConcurrentHashMap map = map5(); |
388 |
empty.putAll(map); |
389 |
assertEquals(5, empty.size()); |
390 |
assertTrue(empty.containsKey(one)); |
391 |
assertTrue(empty.containsKey(two)); |
392 |
assertTrue(empty.containsKey(three)); |
393 |
assertTrue(empty.containsKey(four)); |
394 |
assertTrue(empty.containsKey(five)); |
395 |
} |
396 |
|
397 |
/** |
398 |
* putIfAbsent works when the given key is not present |
399 |
*/ |
400 |
public void testPutIfAbsent() { |
401 |
ConcurrentHashMap map = map5(); |
402 |
map.putIfAbsent(six, "Z"); |
403 |
assertTrue(map.containsKey(six)); |
404 |
} |
405 |
|
406 |
/** |
407 |
* putIfAbsent does not add the pair if the key is already present |
408 |
*/ |
409 |
public void testPutIfAbsent2() { |
410 |
ConcurrentHashMap map = map5(); |
411 |
assertEquals("A", map.putIfAbsent(one, "Z")); |
412 |
} |
413 |
|
414 |
/** |
415 |
* replace fails when the given key is not present |
416 |
*/ |
417 |
public void testReplace() { |
418 |
ConcurrentHashMap map = map5(); |
419 |
assertNull(map.replace(six, "Z")); |
420 |
assertFalse(map.containsKey(six)); |
421 |
} |
422 |
|
423 |
/** |
424 |
* replace succeeds if the key is already present |
425 |
*/ |
426 |
public void testReplace2() { |
427 |
ConcurrentHashMap map = map5(); |
428 |
assertNotNull(map.replace(one, "Z")); |
429 |
assertEquals("Z", map.get(one)); |
430 |
} |
431 |
|
432 |
/** |
433 |
* replace value fails when the given key not mapped to expected value |
434 |
*/ |
435 |
public void testReplaceValue() { |
436 |
ConcurrentHashMap map = map5(); |
437 |
assertEquals("A", map.get(one)); |
438 |
assertFalse(map.replace(one, "Z", "Z")); |
439 |
assertEquals("A", map.get(one)); |
440 |
} |
441 |
|
442 |
/** |
443 |
* replace value succeeds when the given key mapped to expected value |
444 |
*/ |
445 |
public void testReplaceValue2() { |
446 |
ConcurrentHashMap map = map5(); |
447 |
assertEquals("A", map.get(one)); |
448 |
assertTrue(map.replace(one, "A", "Z")); |
449 |
assertEquals("Z", map.get(one)); |
450 |
} |
451 |
|
452 |
/** |
453 |
* remove removes the correct key-value pair from the map |
454 |
*/ |
455 |
public void testRemove() { |
456 |
ConcurrentHashMap map = map5(); |
457 |
map.remove(five); |
458 |
assertEquals(4, map.size()); |
459 |
assertFalse(map.containsKey(five)); |
460 |
} |
461 |
|
462 |
/** |
463 |
* remove(key,value) removes only if pair present |
464 |
*/ |
465 |
public void testRemove2() { |
466 |
ConcurrentHashMap map = map5(); |
467 |
map.remove(five, "E"); |
468 |
assertEquals(4, map.size()); |
469 |
assertFalse(map.containsKey(five)); |
470 |
map.remove(four, "A"); |
471 |
assertEquals(4, map.size()); |
472 |
assertTrue(map.containsKey(four)); |
473 |
} |
474 |
|
475 |
/** |
476 |
* size returns the correct values |
477 |
*/ |
478 |
public void testSize() { |
479 |
ConcurrentHashMap map = map5(); |
480 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
481 |
assertEquals(0, empty.size()); |
482 |
assertEquals(5, map.size()); |
483 |
} |
484 |
|
485 |
/** |
486 |
* toString contains toString of elements |
487 |
*/ |
488 |
public void testToString() { |
489 |
ConcurrentHashMap map = map5(); |
490 |
String s = map.toString(); |
491 |
for (int i = 1; i <= 5; ++i) { |
492 |
assertTrue(s.contains(String.valueOf(i))); |
493 |
} |
494 |
} |
495 |
|
496 |
// Exception tests |
497 |
|
498 |
/** |
499 |
* Cannot create with negative capacity |
500 |
*/ |
501 |
public void testConstructor1() { |
502 |
try { |
503 |
new ConcurrentHashMap(-1,0,1); |
504 |
shouldThrow(); |
505 |
} catch (IllegalArgumentException success) {} |
506 |
} |
507 |
|
508 |
/** |
509 |
* Cannot create with negative concurrency level |
510 |
*/ |
511 |
public void testConstructor2() { |
512 |
try { |
513 |
new ConcurrentHashMap(1,0,-1); |
514 |
shouldThrow(); |
515 |
} catch (IllegalArgumentException success) {} |
516 |
} |
517 |
|
518 |
/** |
519 |
* Cannot create with only negative capacity |
520 |
*/ |
521 |
public void testConstructor3() { |
522 |
try { |
523 |
new ConcurrentHashMap(-1); |
524 |
shouldThrow(); |
525 |
} catch (IllegalArgumentException success) {} |
526 |
} |
527 |
|
528 |
/** |
529 |
* get(null) throws NPE |
530 |
*/ |
531 |
public void testGet_NullPointerException() { |
532 |
try { |
533 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
534 |
c.get(null); |
535 |
shouldThrow(); |
536 |
} catch (NullPointerException success) {} |
537 |
} |
538 |
|
539 |
/** |
540 |
* containsKey(null) throws NPE |
541 |
*/ |
542 |
public void testContainsKey_NullPointerException() { |
543 |
try { |
544 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
545 |
c.containsKey(null); |
546 |
shouldThrow(); |
547 |
} catch (NullPointerException success) {} |
548 |
} |
549 |
|
550 |
/** |
551 |
* containsValue(null) throws NPE |
552 |
*/ |
553 |
public void testContainsValue_NullPointerException() { |
554 |
try { |
555 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
556 |
c.containsValue(null); |
557 |
shouldThrow(); |
558 |
} catch (NullPointerException success) {} |
559 |
} |
560 |
|
561 |
/** |
562 |
* contains(null) throws NPE |
563 |
*/ |
564 |
public void testContains_NullPointerException() { |
565 |
try { |
566 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
567 |
c.contains(null); |
568 |
shouldThrow(); |
569 |
} catch (NullPointerException success) {} |
570 |
} |
571 |
|
572 |
/** |
573 |
* put(null,x) throws NPE |
574 |
*/ |
575 |
public void testPut1_NullPointerException() { |
576 |
try { |
577 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
578 |
c.put(null, "whatever"); |
579 |
shouldThrow(); |
580 |
} catch (NullPointerException success) {} |
581 |
} |
582 |
|
583 |
/** |
584 |
* put(x, null) throws NPE |
585 |
*/ |
586 |
public void testPut2_NullPointerException() { |
587 |
try { |
588 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
589 |
c.put("whatever", null); |
590 |
shouldThrow(); |
591 |
} catch (NullPointerException success) {} |
592 |
} |
593 |
|
594 |
/** |
595 |
* putIfAbsent(null, x) throws NPE |
596 |
*/ |
597 |
public void testPutIfAbsent1_NullPointerException() { |
598 |
try { |
599 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
600 |
c.putIfAbsent(null, "whatever"); |
601 |
shouldThrow(); |
602 |
} catch (NullPointerException success) {} |
603 |
} |
604 |
|
605 |
/** |
606 |
* replace(null, x) throws NPE |
607 |
*/ |
608 |
public void testReplace_NullPointerException() { |
609 |
try { |
610 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
611 |
c.replace(null, "whatever"); |
612 |
shouldThrow(); |
613 |
} catch (NullPointerException success) {} |
614 |
} |
615 |
|
616 |
/** |
617 |
* replace(null, x, y) throws NPE |
618 |
*/ |
619 |
public void testReplaceValue_NullPointerException() { |
620 |
try { |
621 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
622 |
c.replace(null, one, "whatever"); |
623 |
shouldThrow(); |
624 |
} catch (NullPointerException success) {} |
625 |
} |
626 |
|
627 |
/** |
628 |
* putIfAbsent(x, null) throws NPE |
629 |
*/ |
630 |
public void testPutIfAbsent2_NullPointerException() { |
631 |
try { |
632 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
633 |
c.putIfAbsent("whatever", null); |
634 |
shouldThrow(); |
635 |
} catch (NullPointerException success) {} |
636 |
} |
637 |
|
638 |
/** |
639 |
* replace(x, null) throws NPE |
640 |
*/ |
641 |
public void testReplace2_NullPointerException() { |
642 |
try { |
643 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
644 |
c.replace("whatever", null); |
645 |
shouldThrow(); |
646 |
} catch (NullPointerException success) {} |
647 |
} |
648 |
|
649 |
/** |
650 |
* replace(x, null, y) throws NPE |
651 |
*/ |
652 |
public void testReplaceValue2_NullPointerException() { |
653 |
try { |
654 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
655 |
c.replace("whatever", null, "A"); |
656 |
shouldThrow(); |
657 |
} catch (NullPointerException success) {} |
658 |
} |
659 |
|
660 |
/** |
661 |
* replace(x, y, null) throws NPE |
662 |
*/ |
663 |
public void testReplaceValue3_NullPointerException() { |
664 |
try { |
665 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
666 |
c.replace("whatever", one, null); |
667 |
shouldThrow(); |
668 |
} catch (NullPointerException success) {} |
669 |
} |
670 |
|
671 |
/** |
672 |
* remove(null) throws NPE |
673 |
*/ |
674 |
public void testRemove1_NullPointerException() { |
675 |
try { |
676 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
677 |
c.put("sadsdf", "asdads"); |
678 |
c.remove(null); |
679 |
shouldThrow(); |
680 |
} catch (NullPointerException success) {} |
681 |
} |
682 |
|
683 |
/** |
684 |
* remove(null, x) throws NPE |
685 |
*/ |
686 |
public void testRemove2_NullPointerException() { |
687 |
try { |
688 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
689 |
c.put("sadsdf", "asdads"); |
690 |
c.remove(null, "whatever"); |
691 |
shouldThrow(); |
692 |
} catch (NullPointerException success) {} |
693 |
} |
694 |
|
695 |
/** |
696 |
* remove(x, null) returns false |
697 |
*/ |
698 |
public void testRemove3() { |
699 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
700 |
c.put("sadsdf", "asdads"); |
701 |
assertFalse(c.remove("sadsdf", null)); |
702 |
} |
703 |
|
704 |
/** |
705 |
* A deserialized map equals original |
706 |
*/ |
707 |
public void testSerialization() throws Exception { |
708 |
Map x = map5(); |
709 |
Map y = serialClone(x); |
710 |
|
711 |
assertNotSame(x, y); |
712 |
assertEquals(x.size(), y.size()); |
713 |
assertEquals(x, y); |
714 |
assertEquals(y, x); |
715 |
} |
716 |
|
717 |
/** |
718 |
* SetValue of an EntrySet entry sets value in the map. |
719 |
*/ |
720 |
public void testSetValueWriteThrough() { |
721 |
// Adapted from a bug report by Eric Zoerner |
722 |
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); |
723 |
assertTrue(map.isEmpty()); |
724 |
for (int i = 0; i < 20; i++) |
725 |
map.put(new Integer(i), new Integer(i)); |
726 |
assertFalse(map.isEmpty()); |
727 |
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
728 |
// Unless it happens to be first (in which case remainder of |
729 |
// test is skipped), remove a possibly-colliding key from map |
730 |
// which, under some implementations, may cause entry1 to be |
731 |
// cloned in map |
732 |
if (!entry1.getKey().equals(new Integer(16))) { |
733 |
map.remove(new Integer(16)); |
734 |
entry1.setValue("XYZ"); |
735 |
assertTrue(map.containsValue("XYZ")); // fails if write-through broken |
736 |
} |
737 |
} |
738 |
|
739 |
} |