--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2003/09/07 20:39:11 1.2 +++ 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.*; @@ -11,7 +12,7 @@ import java.util.concurrent.*; import java.util.Enumeration; import java.io.*; -public class ConcurrentHashMapTest extends TestCase{ +public class ConcurrentHashMapTest extends JSR166TestCase{ public static void main(String[] args) { junit.textui.TestRunner.run (suite()); } @@ -19,12 +20,9 @@ public class ConcurrentHashMapTest exten return new TestSuite(ConcurrentHashMapTest.class); } - static final Integer one = new Integer(1); - static final Integer two = new Integer(2); - static final Integer three = new Integer(3); - static final Integer four = new Integer(4); - static final Integer five = new Integer(5); - + /** + * Create a map from Integers 1-5 to Strings "A"-"E". + */ private static ConcurrentHashMap map5() { ConcurrentHashMap map = new ConcurrentHashMap(5); assertTrue(map.isEmpty()); @@ -39,45 +37,59 @@ public class ConcurrentHashMapTest exten } /** - * Test to verify clear correctly removes all key-element pairs from the map + * clear removes all pairs */ - public void testClear(){ + public void testClear() { ConcurrentHashMap map = map5(); map.clear(); assertEquals(map.size(), 0); } /** - * Test to verify contains gives the appropriate value + * Maps with same contents are equal + */ + public void testEquals() { + ConcurrentHashMap map1 = map5(); + ConcurrentHashMap map2 = map5(); + assertEquals(map1, map2); + assertEquals(map2, map1); + map1.clear(); + assertFalse(map1.equals(map2)); + assertFalse(map2.equals(map1)); + } + + /** + * contains returns true for contained value */ - public void testContains(){ + public void testContains() { ConcurrentHashMap map = map5(); assertTrue(map.contains("A")); assertFalse(map.contains("Z")); } /** - * Test to verify containsKey gives the appropriate value + * containsKey returns true for contained key */ - public void testContainsKey(){ + public void testContainsKey() { ConcurrentHashMap map = map5(); assertTrue(map.containsKey(one)); - assertFalse(map.containsKey(new Integer(100))); + assertFalse(map.containsKey(zero)); } /** - * Identical to normal contains + * containsValue returns true for held values */ - public void testContainsValue(){ + public void testContainsValue() { ConcurrentHashMap map = map5(); assertTrue(map.contains("A")); assertFalse(map.contains("Z")); } /** - * tes to verify enumeration returns an enumeration containing the correct elements + * enumeration returns an enumeration containing the correct + * elements */ - public void testEnumeration(){ + public void testEnumeration() { ConcurrentHashMap map = map5(); Enumeration e = map.elements(); int count = 0; @@ -89,25 +101,29 @@ public class ConcurrentHashMapTest exten } /** - * Test to verify get returns the correct element at the given index + * Clone creates an equal map */ - public void testGet(){ + public void testClone() { ConcurrentHashMap map = map5(); - assertEquals("A", (String)map.get(one)); + ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone()); + assertEquals(map, m2); } /** - * Test to verify get on a nonexistant key returns null + * get returns the correct element at the given key, + * or null if not present */ - public void testGet2(){ + public void testGet() { + ConcurrentHashMap map = map5(); + assertEquals("A", (String)map.get(one)); ConcurrentHashMap empty = new ConcurrentHashMap(); - assertNull(empty.get("anything")); + assertNull(map.get("anything")); } /** - * Simple test to verify isEmpty returns the correct value + * isEmpty is true of empty map and false for non-empty */ - public void testIsEmpty(){ + public void testIsEmpty() { ConcurrentHashMap empty = new ConcurrentHashMap(); ConcurrentHashMap map = map5(); assertTrue(empty.isEmpty()); @@ -115,9 +131,9 @@ public class ConcurrentHashMapTest exten } /** - * Test to verify keys returns an enumeration containing all the keys from the map + * keys returns an enumeration containing all the keys from the map */ - public void testKeys(){ + public void testKeys() { ConcurrentHashMap map = map5(); Enumeration e = map.keys(); int count = 0; @@ -129,9 +145,9 @@ public class ConcurrentHashMapTest exten } /** - * Test to verify keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ - public void testKeySet(){ + public void testKeySet() { ConcurrentHashMap map = map5(); Set s = map.keySet(); assertEquals(5, s.size()); @@ -142,7 +158,10 @@ public class ConcurrentHashMapTest exten assertTrue(s.contains(five)); } - public void testValues(){ + /** + * values collection contains all values + */ + public void testValues() { ConcurrentHashMap map = map5(); Collection s = map.values(); assertEquals(5, s.size()); @@ -153,7 +172,10 @@ public class ConcurrentHashMapTest exten assertTrue(s.contains("E")); } - public void testEntrySet(){ + /** + * entrySet contains all pairs + */ + public void testEntrySet() { ConcurrentHashMap map = map5(); Set s = map.entrySet(); assertEquals(5, s.size()); @@ -170,9 +192,9 @@ public class ConcurrentHashMapTest exten } /** - * Test to verify putAll correctly adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ - public void testPutAll(){ + public void testPutAll() { ConcurrentHashMap empty = new ConcurrentHashMap(); ConcurrentHashMap map = map5(); empty.putAll(map); @@ -185,33 +207,76 @@ public class ConcurrentHashMapTest exten } /** - * Test to verify putIfAbsent works when the given key is not present + * putIfAbsent works when the given key is not present */ - public void testPutIfAbsent(){ + 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)); } /** - * Test to verify putIfAbsent does not add the pair if the key is already present + * putIfAbsent does not add the pair if the key is already present */ - public void testPutIfAbsent2(){ + public void testPutIfAbsent2() { ConcurrentHashMap map = map5(); assertEquals("A", map.putIfAbsent(one, "Z")); } /** - * Test to verify remove removes the correct key-value pair from the map + * 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 testRemove(){ + 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() { ConcurrentHashMap map = map5(); map.remove(five); assertEquals(4, map.size()); assertFalse(map.containsKey(five)); } - public void testRemove2(){ + /** + * remove(key,value) removes only if pair present + */ + public void testRemove2() { ConcurrentHashMap map = map5(); map.remove(five, "E"); assertEquals(4, map.size()); @@ -223,16 +288,19 @@ public class ConcurrentHashMapTest exten } /** - * Simple test to verify size returns the correct values + * size returns the correct values */ - public void testSize(){ + public void testSize() { ConcurrentHashMap map = map5(); ConcurrentHashMap empty = new ConcurrentHashMap(); assertEquals(0, empty.size()); assertEquals(5, map.size()); } - public void testToString(){ + /** + * toString contains toString of elements + */ + public void testToString() { ConcurrentHashMap map = map5(); String s = map.toString(); for (int i = 1; i <= 5; ++i) { @@ -242,110 +310,208 @@ public class ConcurrentHashMapTest exten // Exception tests - public void testConstructor1(){ - try{ + /** + * Cannot create with negative capacity + */ + public void testConstructor1() { + try { new ConcurrentHashMap(-1,0,1); - fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); - }catch(IllegalArgumentException e){} + shouldThrow(); + } catch(IllegalArgumentException e){} } - public void testConstructor2(){ - try{ + /** + * Cannot create with negative concurrency level + */ + public void testConstructor2() { + try { new ConcurrentHashMap(1,0,-1); - fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); - }catch(IllegalArgumentException e){} + shouldThrow(); + } catch(IllegalArgumentException e){} } - public void testConstructor3(){ - try{ + /** + * Cannot create with only negative capacity + */ + public void testConstructor3() { + try { new ConcurrentHashMap(-1); - fail("ConcurrentHashMap(int) should throw Illegal Argument Exception"); - }catch(IllegalArgumentException e){} + shouldThrow(); + } catch(IllegalArgumentException e){} } - public void testGet_NullPointerException(){ - try{ + /** + * get(null) throws NPE + */ + public void testGet_NullPointerException() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.get(null); - fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} } - public void testContainsKey_NullPointerException(){ - try{ + /** + * containsKey(null) throws NPE + */ + public void testContainsKey_NullPointerException() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsKey(null); - fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} } - public void testContainsValue_NullPointerException(){ - try{ + /** + * containsValue(null) throws NPE + */ + public void testContainsValue_NullPointerException() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsValue(null); - fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} } - public void testContains_NullPointerException(){ - try{ + /** + * contains(null) throws NPE + */ + public void testContains_NullPointerException() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.contains(null); - fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} } - public void testPut1_NullPointerException(){ - try{ + /** + * put(null,x) throws NPE + */ + public void testPut1_NullPointerException() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.put(null, "whatever"); - fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} } - public void testPut2_NullPointerException(){ - try{ + /** + * put(x, null) throws NPE + */ + public void testPut2_NullPointerException() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.put("whatever", null); - fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} } - public void testPutIfAbsent1_NullPointerException(){ - try{ + /** + * putIfAbsent(null, x) throws NPE + */ + public void testPutIfAbsent1_NullPointerException() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent(null, "whatever"); - fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} + } + + /** + * replace(null, x) throws NPE + */ + public void testReplace_NullPointerException() { + try { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.replace(null, "whatever"); + shouldThrow(); + } catch(NullPointerException e){} } - public void testPutIfAbsent2_NullPointerException(){ - try{ + /** + * 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() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent("whatever", null); - fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} } - public void testRemove1_NullPointerException(){ - try{ + /** + * 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() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.put("sadsdf", "asdads"); c.remove(null); - fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} } - public void testRemove2_NullPointerException(){ - try{ + /** + * remove(null, x) throws NPE + */ + public void testRemove2_NullPointerException() { + try { ConcurrentHashMap c = new ConcurrentHashMap(5); c.put("sadsdf", "asdads"); c.remove(null, "whatever"); - fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione"); - }catch(NullPointerException e){} + shouldThrow(); + } catch(NullPointerException e){} } + /** + * A deserialized map equals original + */ public void testSerialization() { ConcurrentHashMap q = map5(); @@ -363,9 +529,32 @@ public class ConcurrentHashMapTest exten assertTrue(r.equals(q)); } catch(Exception e){ e.printStackTrace(); - fail("unexpected exception"); + 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 + } }