210 |
|
*/ |
211 |
|
public void testPutIfAbsent() { |
212 |
|
ConcurrentHashMap map = map5(); |
213 |
< |
map.putIfAbsent(new Integer(6), "Z"); |
214 |
< |
assertTrue(map.containsKey(new Integer(6))); |
213 |
> |
map.putIfAbsent(six, "Z"); |
214 |
> |
assertTrue(map.containsKey(six)); |
215 |
|
} |
216 |
|
|
217 |
|
/** |
223 |
|
} |
224 |
|
|
225 |
|
/** |
226 |
+ |
* replace fails when the given key is not present |
227 |
+ |
*/ |
228 |
+ |
public void testReplace() { |
229 |
+ |
ConcurrentHashMap map = map5(); |
230 |
+ |
assertNull(map.replace(six, "Z")); |
231 |
+ |
assertFalse(map.containsKey(six)); |
232 |
+ |
} |
233 |
+ |
|
234 |
+ |
/** |
235 |
+ |
* replace succeeds if the key is already present |
236 |
+ |
*/ |
237 |
+ |
public void testReplace2() { |
238 |
+ |
ConcurrentHashMap map = map5(); |
239 |
+ |
assertNotNull(map.replace(one, "Z")); |
240 |
+ |
assertEquals("Z", map.get(one)); |
241 |
+ |
} |
242 |
+ |
|
243 |
+ |
|
244 |
+ |
/** |
245 |
+ |
* replace value fails when the given key not mapped to expected value |
246 |
+ |
*/ |
247 |
+ |
public void testReplaceValue() { |
248 |
+ |
ConcurrentHashMap map = map5(); |
249 |
+ |
assertEquals("A", map.get(one)); |
250 |
+ |
assertFalse(map.replace(one, "Z", "Z")); |
251 |
+ |
assertEquals("A", map.get(one)); |
252 |
+ |
} |
253 |
+ |
|
254 |
+ |
/** |
255 |
+ |
* replace value succeeds when the given key mapped to expected value |
256 |
+ |
*/ |
257 |
+ |
public void testReplaceValue2() { |
258 |
+ |
ConcurrentHashMap map = map5(); |
259 |
+ |
assertEquals("A", map.get(one)); |
260 |
+ |
assertTrue(map.replace(one, "A", "Z")); |
261 |
+ |
assertEquals("Z", map.get(one)); |
262 |
+ |
} |
263 |
+ |
|
264 |
+ |
|
265 |
+ |
/** |
266 |
|
* remove removes the correct key-value pair from the map |
267 |
|
*/ |
268 |
|
public void testRemove() { |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
+ |
* replace(null, x) throws NPE |
421 |
+ |
*/ |
422 |
+ |
public void testReplace_NullPointerException() { |
423 |
+ |
try { |
424 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
425 |
+ |
c.replace(null, "whatever"); |
426 |
+ |
shouldThrow(); |
427 |
+ |
} catch(NullPointerException e){} |
428 |
+ |
} |
429 |
+ |
|
430 |
+ |
/** |
431 |
+ |
* replace(null, x, y) throws NPE |
432 |
+ |
*/ |
433 |
+ |
public void testReplaceValue_NullPointerException() { |
434 |
+ |
try { |
435 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
436 |
+ |
c.replace(null, one, "whatever"); |
437 |
+ |
shouldThrow(); |
438 |
+ |
} catch(NullPointerException e){} |
439 |
+ |
} |
440 |
+ |
|
441 |
+ |
/** |
442 |
|
* putIfAbsent(x, null) throws NPE |
443 |
|
*/ |
444 |
|
public void testPutIfAbsent2_NullPointerException() { |
451 |
|
|
452 |
|
|
453 |
|
/** |
454 |
+ |
* replace(x, null) throws NPE |
455 |
+ |
*/ |
456 |
+ |
public void testReplace2_NullPointerException() { |
457 |
+ |
try { |
458 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
459 |
+ |
c.replace("whatever", null); |
460 |
+ |
shouldThrow(); |
461 |
+ |
} catch(NullPointerException e){} |
462 |
+ |
} |
463 |
+ |
|
464 |
+ |
/** |
465 |
+ |
* replace(x, null, y) throws NPE |
466 |
+ |
*/ |
467 |
+ |
public void testReplaceValue2_NullPointerException() { |
468 |
+ |
try { |
469 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
470 |
+ |
c.replace("whatever", null, "A"); |
471 |
+ |
shouldThrow(); |
472 |
+ |
} catch(NullPointerException e){} |
473 |
+ |
} |
474 |
+ |
|
475 |
+ |
/** |
476 |
+ |
* replace(x, y, null) throws NPE |
477 |
+ |
*/ |
478 |
+ |
public void testReplaceValue3_NullPointerException() { |
479 |
+ |
try { |
480 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
481 |
+ |
c.replace("whatever", one, null); |
482 |
+ |
shouldThrow(); |
483 |
+ |
} catch(NullPointerException e){} |
484 |
+ |
} |
485 |
+ |
|
486 |
+ |
|
487 |
+ |
/** |
488 |
|
* remove(null) throws NPE |
489 |
|
*/ |
490 |
|
public void testRemove1_NullPointerException() { |
531 |
|
unexpectedException(); |
532 |
|
} |
533 |
|
} |
534 |
+ |
|
535 |
+ |
|
536 |
+ |
/** |
537 |
+ |
* SetValue of an EntrySet entry sets value in the map. |
538 |
+ |
*/ |
539 |
+ |
public void testSetValueWriteThrough() { |
540 |
+ |
// Adapted from a bug report by Eric Zoerner |
541 |
+ |
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); |
542 |
+ |
assertTrue(map.isEmpty()); |
543 |
+ |
for (int i = 0; i < 20; i++) |
544 |
+ |
map.put(new Integer(i), new Integer(i)); |
545 |
+ |
assertFalse(map.isEmpty()); |
546 |
+ |
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
547 |
+ |
|
548 |
+ |
// assert that entry1 is not 16 |
549 |
+ |
assertTrue("entry is 16, test not valid", |
550 |
+ |
!entry1.getKey().equals(new Integer(16))); |
551 |
+ |
|
552 |
+ |
// remove 16 (a different key) from map |
553 |
+ |
// which just happens to cause entry1 to be cloned in map |
554 |
+ |
map.remove(new Integer(16)); |
555 |
+ |
entry1.setValue("XYZ"); |
556 |
+ |
assertTrue(map.containsValue("XYZ")); // fails |
557 |
+ |
} |
558 |
|
|
559 |
|
} |