--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2003/09/20 18:20:07 1.5 +++ 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() { @@ -389,6 +452,40 @@ public class ConcurrentHashMapTest exten /** + * 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){} + } + + + /** * remove(null) throws NPE */ public void testRemove1_NullPointerException() { @@ -435,5 +532,29 @@ public class ConcurrentHashMapTest exten unexpectedException(); } } + + + /** + * SetValue of an EntrySet entry sets value in the map. + */ + public void testSetValueWriteThrough() { + // Adapted from a bug report by Eric Zoerner + ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); + assertTrue(map.isEmpty()); + for (int i = 0; i < 20; i++) + map.put(new Integer(i), new Integer(i)); + assertFalse(map.isEmpty()); + Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); + + // assert that entry1 is not 16 + assertTrue("entry is 16, test not valid", + !entry1.getKey().equals(new Integer(16))); + + // remove 16 (a different key) from map + // which just happens to cause entry1 to be cloned in map + map.remove(new Integer(16)); + entry1.setValue("XYZ"); + assertTrue(map.containsValue("XYZ")); // fails + } }