--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2003/11/28 12:38:08 1.6 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2003/12/27 19:26:43 1.9 @@ -1,8 +1,9 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * Written by Doug Lea with assistance from members of JCP JSR-166 + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; @@ -210,8 +211,8 @@ public class ConcurrentHashMapTest exten */ public void testPutIfAbsent() { ConcurrentHashMap map = map5(); - map.putIfAbsent(new Integer(6), "Z"); - assertTrue(map.containsKey(new Integer(6))); + map.putIfAbsent(six, "Z"); + assertTrue(map.containsKey(six)); } /** @@ -223,6 +224,46 @@ public class ConcurrentHashMapTest exten } /** + * replace fails when the given key is not present + */ + public void testReplace() { + ConcurrentHashMap map = map5(); + assertNull(map.replace(six, "Z")); + assertFalse(map.containsKey(six)); + } + + /** + * replace succeeds if the key is already present + */ + public void testReplace2() { + ConcurrentHashMap map = map5(); + assertNotNull(map.replace(one, "Z")); + assertEquals("Z", map.get(one)); + } + + + /** + * replace value fails when the given key not mapped to expected value + */ + public void testReplaceValue() { + ConcurrentHashMap map = map5(); + assertEquals("A", map.get(one)); + assertFalse(map.replace(one, "Z", "Z")); + assertEquals("A", map.get(one)); + } + + /** + * replace value succeeds when the given key mapped to expected value + */ + public void testReplaceValue2() { + ConcurrentHashMap map = map5(); + assertEquals("A", map.get(one)); + assertTrue(map.replace(one, "A", "Z")); + assertEquals("Z", map.get(one)); + } + + + /** * remove removes the correct key-value pair from the map */ public void testRemove() { @@ -377,6 +418,28 @@ public class ConcurrentHashMapTest exten } /** + * replace(null, x) throws NPE + */ + public void testReplace_NullPointerException() { + try { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.replace(null, "whatever"); + shouldThrow(); + } catch(NullPointerException e){} + } + + /** + * replace(null, x, y) throws NPE + */ + public void testReplaceValue_NullPointerException() { + try { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.replace(null, one, "whatever"); + shouldThrow(); + } catch(NullPointerException e){} + } + + /** * putIfAbsent(x, null) throws NPE */ public void testPutIfAbsent2_NullPointerException() { @@ -386,6 +449,40 @@ public class ConcurrentHashMapTest exten shouldThrow(); } catch(NullPointerException e){} } + + + /** + * replace(x, null) throws NPE + */ + public void testReplace2_NullPointerException() { + try { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.replace("whatever", null); + shouldThrow(); + } catch(NullPointerException e){} + } + + /** + * replace(x, null, y) throws NPE + */ + public void testReplaceValue2_NullPointerException() { + try { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.replace("whatever", null, "A"); + shouldThrow(); + } catch(NullPointerException e){} + } + + /** + * replace(x, y, null) throws NPE + */ + public void testReplaceValue3_NullPointerException() { + try { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.replace("whatever", one, null); + shouldThrow(); + } catch(NullPointerException e){} + } /**