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.*; |
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() { |
523 |
|
unexpectedException(); |
524 |
|
} |
525 |
|
} |
526 |
+ |
|
527 |
+ |
|
528 |
+ |
/** |
529 |
+ |
* SetValue of an EntrySet entry sets value in the map. |
530 |
+ |
*/ |
531 |
+ |
public void testSetValueWriteThrough() { |
532 |
+ |
// Adapted from a bug report by Eric Zoerner |
533 |
+ |
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); |
534 |
+ |
assertTrue(map.isEmpty()); |
535 |
+ |
for (int i = 0; i < 20; i++) |
536 |
+ |
map.put(new Integer(i), new Integer(i)); |
537 |
+ |
assertFalse(map.isEmpty()); |
538 |
+ |
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
539 |
+ |
|
540 |
+ |
// assert that entry1 is not 16 |
541 |
+ |
assertTrue("entry is 16, test not valid", |
542 |
+ |
!entry1.getKey().equals(new Integer(16))); |
543 |
+ |
|
544 |
+ |
// remove 16 (a different key) from map |
545 |
+ |
// which just happens to cause entry1 to be cloned in map |
546 |
+ |
map.remove(new Integer(16)); |
547 |
+ |
entry1.setValue("XYZ"); |
548 |
+ |
assertTrue(map.containsValue("XYZ")); // fails |
549 |
+ |
} |
550 |
|
|
551 |
|
} |