1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
81 |
|
*/ |
82 |
|
public void testContainsValue() { |
83 |
|
ConcurrentHashMap map = map5(); |
84 |
< |
assertTrue(map.contains("A")); |
85 |
< |
assertFalse(map.contains("Z")); |
84 |
> |
assertTrue(map.containsValue("A")); |
85 |
> |
assertFalse(map.containsValue("Z")); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
101 |
|
} |
102 |
|
|
103 |
|
/** |
103 |
– |
* Clone creates an equal map |
104 |
– |
*/ |
105 |
– |
public void testClone() { |
106 |
– |
ConcurrentHashMap map = map5(); |
107 |
– |
ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone()); |
108 |
– |
assertEquals(map, m2); |
109 |
– |
} |
110 |
– |
|
111 |
– |
/** |
104 |
|
* get returns the correct element at the given key, |
105 |
|
* or null if not present |
106 |
|
*/ |
202 |
|
*/ |
203 |
|
public void testPutIfAbsent() { |
204 |
|
ConcurrentHashMap map = map5(); |
205 |
< |
map.putIfAbsent(new Integer(6), "Z"); |
206 |
< |
assertTrue(map.containsKey(new Integer(6))); |
205 |
> |
map.putIfAbsent(six, "Z"); |
206 |
> |
assertTrue(map.containsKey(six)); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
215 |
|
} |
216 |
|
|
217 |
|
/** |
218 |
+ |
* replace fails when the given key is not present |
219 |
+ |
*/ |
220 |
+ |
public void testReplace() { |
221 |
+ |
ConcurrentHashMap map = map5(); |
222 |
+ |
assertNull(map.replace(six, "Z")); |
223 |
+ |
assertFalse(map.containsKey(six)); |
224 |
+ |
} |
225 |
+ |
|
226 |
+ |
/** |
227 |
+ |
* replace succeeds if the key is already present |
228 |
+ |
*/ |
229 |
+ |
public void testReplace2() { |
230 |
+ |
ConcurrentHashMap map = map5(); |
231 |
+ |
assertNotNull(map.replace(one, "Z")); |
232 |
+ |
assertEquals("Z", map.get(one)); |
233 |
+ |
} |
234 |
+ |
|
235 |
+ |
|
236 |
+ |
/** |
237 |
+ |
* replace value fails when the given key not mapped to expected value |
238 |
+ |
*/ |
239 |
+ |
public void testReplaceValue() { |
240 |
+ |
ConcurrentHashMap map = map5(); |
241 |
+ |
assertEquals("A", map.get(one)); |
242 |
+ |
assertFalse(map.replace(one, "Z", "Z")); |
243 |
+ |
assertEquals("A", map.get(one)); |
244 |
+ |
} |
245 |
+ |
|
246 |
+ |
/** |
247 |
+ |
* replace value succeeds when the given key mapped to expected value |
248 |
+ |
*/ |
249 |
+ |
public void testReplaceValue2() { |
250 |
+ |
ConcurrentHashMap map = map5(); |
251 |
+ |
assertEquals("A", map.get(one)); |
252 |
+ |
assertTrue(map.replace(one, "A", "Z")); |
253 |
+ |
assertEquals("Z", map.get(one)); |
254 |
+ |
} |
255 |
+ |
|
256 |
+ |
|
257 |
+ |
/** |
258 |
|
* remove removes the correct key-value pair from the map |
259 |
|
*/ |
260 |
|
public void testRemove() { |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
+ |
* replace(null, x) throws NPE |
413 |
+ |
*/ |
414 |
+ |
public void testReplace_NullPointerException() { |
415 |
+ |
try { |
416 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
417 |
+ |
c.replace(null, "whatever"); |
418 |
+ |
shouldThrow(); |
419 |
+ |
} catch(NullPointerException e){} |
420 |
+ |
} |
421 |
+ |
|
422 |
+ |
/** |
423 |
+ |
* replace(null, x, y) throws NPE |
424 |
+ |
*/ |
425 |
+ |
public void testReplaceValue_NullPointerException() { |
426 |
+ |
try { |
427 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
428 |
+ |
c.replace(null, one, "whatever"); |
429 |
+ |
shouldThrow(); |
430 |
+ |
} catch(NullPointerException e){} |
431 |
+ |
} |
432 |
+ |
|
433 |
+ |
/** |
434 |
|
* putIfAbsent(x, null) throws NPE |
435 |
|
*/ |
436 |
|
public void testPutIfAbsent2_NullPointerException() { |
443 |
|
|
444 |
|
|
445 |
|
/** |
446 |
+ |
* replace(x, null) throws NPE |
447 |
+ |
*/ |
448 |
+ |
public void testReplace2_NullPointerException() { |
449 |
+ |
try { |
450 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
451 |
+ |
c.replace("whatever", null); |
452 |
+ |
shouldThrow(); |
453 |
+ |
} catch(NullPointerException e){} |
454 |
+ |
} |
455 |
+ |
|
456 |
+ |
/** |
457 |
+ |
* replace(x, null, y) throws NPE |
458 |
+ |
*/ |
459 |
+ |
public void testReplaceValue2_NullPointerException() { |
460 |
+ |
try { |
461 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
462 |
+ |
c.replace("whatever", null, "A"); |
463 |
+ |
shouldThrow(); |
464 |
+ |
} catch(NullPointerException e){} |
465 |
+ |
} |
466 |
+ |
|
467 |
+ |
/** |
468 |
+ |
* replace(x, y, null) throws NPE |
469 |
+ |
*/ |
470 |
+ |
public void testReplaceValue3_NullPointerException() { |
471 |
+ |
try { |
472 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
473 |
+ |
c.replace("whatever", one, null); |
474 |
+ |
shouldThrow(); |
475 |
+ |
} catch(NullPointerException e){} |
476 |
+ |
} |
477 |
+ |
|
478 |
+ |
|
479 |
+ |
/** |
480 |
|
* remove(null) throws NPE |
481 |
|
*/ |
482 |
|
public void testRemove1_NullPointerException() { |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
+ |
* remove(x, null) returns false |
505 |
+ |
*/ |
506 |
+ |
public void testRemove3() { |
507 |
+ |
try { |
508 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
509 |
+ |
c.put("sadsdf", "asdads"); |
510 |
+ |
assertFalse(c.remove("sadsdf", null)); |
511 |
+ |
} catch(NullPointerException e){ |
512 |
+ |
fail(); |
513 |
+ |
} |
514 |
+ |
} |
515 |
+ |
|
516 |
+ |
/** |
517 |
|
* A deserialized map equals original |
518 |
|
*/ |
519 |
|
public void testSerialization() { |