--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2009/11/16 04:57:10 1.15 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2015/01/15 18:34:19 1.41 @@ -1,189 +1,367 @@ /* * 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 + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.Enumeration; -import java.io.*; +import java.util.Iterator; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; -public class ConcurrentHashMapTest extends JSR166TestCase{ +import junit.framework.Test; +import junit.framework.TestSuite; + +public class ConcurrentHashMapTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { - return new TestSuite(ConcurrentHashMapTest.class); + return new TestSuite(ConcurrentHashMapTest.class); } /** - * Create a map from Integers 1-5 to Strings "A"-"E". + * Returns a new map from Integers 1-5 to Strings "A"-"E". */ - private static ConcurrentHashMap map5() { - ConcurrentHashMap map = new ConcurrentHashMap(5); + private static ConcurrentHashMap map5() { + ConcurrentHashMap map = new ConcurrentHashMap(5); assertTrue(map.isEmpty()); - map.put(one, "A"); - map.put(two, "B"); - map.put(three, "C"); - map.put(four, "D"); - map.put(five, "E"); + map.put(one, "A"); + map.put(two, "B"); + map.put(three, "C"); + map.put(four, "D"); + map.put(five, "E"); assertFalse(map.isEmpty()); assertEquals(5, map.size()); - return map; + return map; + } + + /** Re-implement Integer.compare for old java versions */ + static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } + + // classes for testing Comparable fallbacks + static class BI implements Comparable { + private final int value; + BI(int value) { this.value = value; } + public int compareTo(BI other) { + return compare(value, other.value); + } + public boolean equals(Object x) { + return (x instanceof BI) && ((BI)x).value == value; + } + public int hashCode() { return 42; } + } + static class CI extends BI { CI(int value) { super(value); } } + static class DI extends BI { DI(int value) { super(value); } } + + static class BS implements Comparable { + private final String value; + BS(String value) { this.value = value; } + public int compareTo(BS other) { + return value.compareTo(other.value); + } + public boolean equals(Object x) { + return (x instanceof BS) && value.equals(((BS)x).value); + } + public int hashCode() { return 42; } + } + + static class LexicographicList> extends ArrayList + implements Comparable> { + LexicographicList(Collection c) { super(c); } + LexicographicList(E e) { super(Collections.singleton(e)); } + public int compareTo(LexicographicList other) { + int common = Math.min(size(), other.size()); + int r = 0; + for (int i = 0; i < common; i++) { + if ((r = get(i).compareTo(other.get(i))) != 0) + break; + } + if (r == 0) + r = compare(size(), other.size()); + return r; + } + private static final long serialVersionUID = 0; + } + + static class CollidingObject { + final String value; + CollidingObject(final String value) { this.value = value; } + public int hashCode() { return this.value.hashCode() & 1; } + public boolean equals(final Object obj) { + return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value); + } + } + + static class ComparableCollidingObject extends CollidingObject implements Comparable { + ComparableCollidingObject(final String value) { super(value); } + public int compareTo(final ComparableCollidingObject o) { + return value.compareTo(o.value); + } } /** - * clear removes all pairs + * Inserted elements that are subclasses of the same Comparable + * class are found. + */ + public void testComparableFamily() { + int size = 500; // makes measured test run time -> 60ms + ConcurrentHashMap m = + new ConcurrentHashMap(); + for (int i = 0; i < size; i++) { + assertTrue(m.put(new CI(i), true) == null); + } + for (int i = 0; i < size; i++) { + assertTrue(m.containsKey(new CI(i))); + assertTrue(m.containsKey(new DI(i))); + } + } + + /** + * Elements of classes with erased generic type parameters based + * on Comparable can be inserted and found. + */ + public void testGenericComparable() { + int size = 120; // makes measured test run time -> 60ms + ConcurrentHashMap m = + new ConcurrentHashMap(); + for (int i = 0; i < size; i++) { + BI bi = new BI(i); + BS bs = new BS(String.valueOf(i)); + LexicographicList bis = new LexicographicList(bi); + LexicographicList bss = new LexicographicList(bs); + assertTrue(m.putIfAbsent(bis, true) == null); + assertTrue(m.containsKey(bis)); + if (m.putIfAbsent(bss, true) == null) + assertTrue(m.containsKey(bss)); + assertTrue(m.containsKey(bis)); + } + for (int i = 0; i < size; i++) { + assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); + } + } + + /** + * Elements of non-comparable classes equal to those of classes + * with erased generic type parameters based on Comparable can be + * inserted and found. + */ + public void testGenericComparable2() { + int size = 500; // makes measured test run time -> 60ms + ConcurrentHashMap m = + new ConcurrentHashMap(); + for (int i = 0; i < size; i++) { + m.put(Collections.singletonList(new BI(i)), true); + } + + for (int i = 0; i < size; i++) { + LexicographicList bis = new LexicographicList(new BI(i)); + assertTrue(m.containsKey(bis)); + } + } + + /** + * Mixtures of instances of comparable and non-comparable classes + * can be inserted and found. + */ + public void testMixedComparable() { + int size = 1200; // makes measured test run time -> 35ms + ConcurrentHashMap map = + new ConcurrentHashMap(); + Random rng = new Random(); + for (int i = 0; i < size; i++) { + Object x; + switch (rng.nextInt(4)) { + case 0: + x = new Object(); + break; + case 1: + x = new CollidingObject(Integer.toString(i)); + break; + default: + x = new ComparableCollidingObject(Integer.toString(i)); + } + assertNull(map.put(x, x)); + } + int count = 0; + for (Object k : map.keySet()) { + assertEquals(map.get(k), k); + ++count; + } + assertEquals(count, size); + assertEquals(map.size(), size); + for (Object k : map.keySet()) { + assertEquals(map.put(k, k), k); + } + } + + /** + * clear removes all pairs */ public void testClear() { ConcurrentHashMap map = map5(); - map.clear(); - assertEquals(map.size(), 0); + map.clear(); + assertEquals(0, map.size()); } /** - * Maps with same contents are equal + * Maps with same contents are equal */ public void testEquals() { ConcurrentHashMap map1 = map5(); ConcurrentHashMap map2 = map5(); assertEquals(map1, map2); assertEquals(map2, map1); - map1.clear(); + map1.clear(); assertFalse(map1.equals(map2)); assertFalse(map2.equals(map1)); } /** - * contains returns true for contained value + * hashCode() equals sum of each key.hashCode ^ value.hashCode + */ + public void testHashCode() { + ConcurrentHashMap map = map5(); + int sum = 0; + for (Map.Entry e : map.entrySet()) + sum += e.getKey().hashCode() ^ e.getValue().hashCode(); + assertEquals(sum, map.hashCode()); + } + + /** + * contains returns true for contained value */ public void testContains() { ConcurrentHashMap map = map5(); - assertTrue(map.contains("A")); + assertTrue(map.contains("A")); assertFalse(map.contains("Z")); } /** - * containsKey returns true for contained key + * containsKey returns true for contained key */ public void testContainsKey() { ConcurrentHashMap map = map5(); - assertTrue(map.containsKey(one)); + assertTrue(map.containsKey(one)); assertFalse(map.containsKey(zero)); } /** - * containsValue returns true for held values + * containsValue returns true for held values */ public void testContainsValue() { ConcurrentHashMap map = map5(); - assertTrue(map.containsValue("A")); + assertTrue(map.containsValue("A")); assertFalse(map.containsValue("Z")); } /** - * enumeration returns an enumeration containing the correct - * elements + * enumeration returns an enumeration containing the correct + * elements */ public void testEnumeration() { ConcurrentHashMap map = map5(); - Enumeration e = map.elements(); - int count = 0; - while (e.hasMoreElements()){ - count++; - e.nextElement(); - } - assertEquals(5, count); + Enumeration e = map.elements(); + int count = 0; + while (e.hasMoreElements()) { + count++; + e.nextElement(); + } + assertEquals(5, count); } /** - * get returns the correct element at the given key, - * or null if not present + * get returns the correct element at the given key, + * or null if not present */ public void testGet() { ConcurrentHashMap map = map5(); - assertEquals("A", (String)map.get(one)); + assertEquals("A", (String)map.get(one)); ConcurrentHashMap empty = new ConcurrentHashMap(); assertNull(map.get("anything")); } /** - * isEmpty is true of empty map and false for non-empty + * isEmpty is true of empty map and false for non-empty */ public void testIsEmpty() { ConcurrentHashMap empty = new ConcurrentHashMap(); ConcurrentHashMap map = map5(); - assertTrue(empty.isEmpty()); + assertTrue(empty.isEmpty()); assertFalse(map.isEmpty()); } /** - * 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() { ConcurrentHashMap map = map5(); - Enumeration e = map.keys(); - int count = 0; - while (e.hasMoreElements()){ - count++; - e.nextElement(); - } - assertEquals(5, count); + Enumeration e = map.keys(); + int count = 0; + while (e.hasMoreElements()) { + count++; + e.nextElement(); + } + assertEquals(5, count); } /** - * keySet returns a Set containing all the keys + * keySet returns a Set containing all the keys */ public void testKeySet() { ConcurrentHashMap map = map5(); - Set s = map.keySet(); - assertEquals(5, s.size()); - assertTrue(s.contains(one)); - assertTrue(s.contains(two)); - assertTrue(s.contains(three)); - assertTrue(s.contains(four)); - assertTrue(s.contains(five)); + Set s = map.keySet(); + assertEquals(5, s.size()); + assertTrue(s.contains(one)); + assertTrue(s.contains(two)); + assertTrue(s.contains(three)); + assertTrue(s.contains(four)); + assertTrue(s.contains(five)); } /** - * keySet.toArray returns contains all keys + * keySet.toArray returns contains all keys */ public void testKeySetToArray() { ConcurrentHashMap map = map5(); - Set s = map.keySet(); + Set s = map.keySet(); Object[] ar = s.toArray(); assertTrue(s.containsAll(Arrays.asList(ar))); - assertEquals(5, ar.length); + assertEquals(5, ar.length); ar[0] = m10; assertFalse(s.containsAll(Arrays.asList(ar))); } /** - * Values.toArray contains all values + * Values.toArray contains all values */ public void testValuesToArray() { ConcurrentHashMap map = map5(); - Collection v = map.values(); + Collection v = map.values(); Object[] ar = v.toArray(); ArrayList s = new ArrayList(Arrays.asList(ar)); - assertEquals(5, ar.length); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); + assertEquals(5, ar.length); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); } /** - * entrySet.toArray contains all entries + * entrySet.toArray contains all entries */ public void testEntrySetToArray() { ConcurrentHashMap map = map5(); - Set s = map.entrySet(); + Set s = map.entrySet(); Object[] ar = s.toArray(); assertEquals(5, ar.length); for (int i = 0; i < 5; ++i) { @@ -197,13 +375,13 @@ public class ConcurrentHashMapTest exten */ public void testValues() { ConcurrentHashMap map = map5(); - Collection s = map.values(); - assertEquals(5, s.size()); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); + Collection s = map.values(); + assertEquals(5, s.size()); + assertTrue(s.contains("A")); + assertTrue(s.contains("B")); + assertTrue(s.contains("C")); + assertTrue(s.contains("D")); + assertTrue(s.contains("E")); } /** @@ -211,8 +389,8 @@ public class ConcurrentHashMapTest exten */ public void testEntrySet() { ConcurrentHashMap map = map5(); - Set s = map.entrySet(); - assertEquals(5, s.size()); + Set s = map.entrySet(); + assertEquals(5, s.size()); Iterator it = s.iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); @@ -226,31 +404,31 @@ public class ConcurrentHashMapTest exten } /** - * putAll adds all key-value pairs from the given map + * putAll adds all key-value pairs from the given map */ public void testPutAll() { ConcurrentHashMap empty = new ConcurrentHashMap(); ConcurrentHashMap map = map5(); - empty.putAll(map); - assertEquals(5, empty.size()); - assertTrue(empty.containsKey(one)); - assertTrue(empty.containsKey(two)); - assertTrue(empty.containsKey(three)); - assertTrue(empty.containsKey(four)); - assertTrue(empty.containsKey(five)); + empty.putAll(map); + assertEquals(5, empty.size()); + assertTrue(empty.containsKey(one)); + assertTrue(empty.containsKey(two)); + assertTrue(empty.containsKey(three)); + assertTrue(empty.containsKey(four)); + assertTrue(empty.containsKey(five)); } /** - * putIfAbsent works when the given key is not present + * putIfAbsent works when the given key is not present */ public void testPutIfAbsent() { ConcurrentHashMap map = map5(); - map.putIfAbsent(six, "Z"); + map.putIfAbsent(six, "Z"); assertTrue(map.containsKey(six)); } /** - * 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() { ConcurrentHashMap map = map5(); @@ -258,16 +436,16 @@ public class ConcurrentHashMapTest exten } /** - * replace fails when the given key is not present + * replace fails when the given key is not present */ public void testReplace() { ConcurrentHashMap map = map5(); - assertNull(map.replace(six, "Z")); + assertNull(map.replace(six, "Z")); assertFalse(map.containsKey(six)); } /** - * replace succeeds if the key is already present + * replace succeeds if the key is already present */ public void testReplace2() { ConcurrentHashMap map = map5(); @@ -275,14 +453,13 @@ public class ConcurrentHashMapTest exten 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")); + assertFalse(map.replace(one, "Z", "Z")); assertEquals("A", map.get(one)); } @@ -292,19 +469,18 @@ public class ConcurrentHashMapTest exten public void testReplaceValue2() { ConcurrentHashMap map = map5(); assertEquals("A", map.get(one)); - assertTrue(map.replace(one, "A", "Z")); + assertTrue(map.replace(one, "A", "Z")); assertEquals("Z", map.get(one)); } - /** - * remove removes the correct key-value pair from the map + * 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)); + map.remove(five); + assertEquals(4, map.size()); + assertFalse(map.containsKey(five)); } /** @@ -312,23 +488,22 @@ public class ConcurrentHashMapTest exten */ public void testRemove2() { ConcurrentHashMap map = map5(); - map.remove(five, "E"); - assertEquals(4, map.size()); - assertFalse(map.containsKey(five)); - map.remove(four, "A"); - assertEquals(4, map.size()); - assertTrue(map.containsKey(four)); - + map.remove(five, "E"); + assertEquals(4, map.size()); + assertFalse(map.containsKey(five)); + map.remove(four, "A"); + assertEquals(4, map.size()); + assertTrue(map.containsKey(four)); } /** - * size returns the correct values + * size returns the correct values */ public void testSize() { ConcurrentHashMap map = map5(); ConcurrentHashMap empty = new ConcurrentHashMap(); - assertEquals(0, empty.size()); - assertEquals(5, map.size()); + assertEquals(0, empty.size()); + assertEquals(5, map.size()); } /** @@ -338,40 +513,80 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap map = map5(); String s = map.toString(); for (int i = 1; i <= 5; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } // Exception tests /** - * Cannot create with negative capacity + * Cannot create with only negative capacity */ public void testConstructor1() { try { - new ConcurrentHashMap(-1,0,1); + new ConcurrentHashMap(-1); shouldThrow(); - } catch (IllegalArgumentException e){} + } catch (IllegalArgumentException success) {} } /** - * Cannot create with negative concurrency level - */ + * Constructor (initialCapacity, loadFactor) throws + * IllegalArgumentException if either argument is negative + */ public void testConstructor2() { try { - new ConcurrentHashMap(1,0,-1); + new ConcurrentHashMap(-1, .75f); shouldThrow(); - } catch (IllegalArgumentException e){} + } catch (IllegalArgumentException e) {} + + try { + new ConcurrentHashMap(16, -1); + shouldThrow(); + } catch (IllegalArgumentException e) {} } /** - * Cannot create with only negative capacity + * Constructor (initialCapacity, loadFactor, concurrencyLevel) + * throws IllegalArgumentException if any argument is negative */ public void testConstructor3() { try { - new ConcurrentHashMap(-1); + new ConcurrentHashMap(-1, .75f, 1); + shouldThrow(); + } catch (IllegalArgumentException e) {} + + try { + new ConcurrentHashMap(16, -1, 1); + shouldThrow(); + } catch (IllegalArgumentException e) {} + + try { + new ConcurrentHashMap(16, .75f, -1); shouldThrow(); - } catch (IllegalArgumentException e){} + } catch (IllegalArgumentException e) {} + } + + /** + * ConcurrentHashMap(map) throws NullPointerException if the given + * map is null + */ + public void testConstructor4() { + try { + new ConcurrentHashMap(null); + shouldThrow(); + } catch (NullPointerException e) {} + } + + /** + * ConcurrentHashMap(map) creates a new map with the same mappings + * as the given map + */ + public void testConstructor5() { + ConcurrentHashMap map1 = map5(); + ConcurrentHashMap map2 = new ConcurrentHashMap(map5()); + assertTrue(map2.equals(map1)); + map2.put(one, "F"); + assertFalse(map2.equals(map1)); } /** @@ -382,7 +597,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.get(null); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -393,7 +608,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsKey(null); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -404,7 +619,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsValue(null); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -415,7 +630,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.contains(null); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -426,7 +641,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.put(null, "whatever"); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -437,7 +652,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.put("whatever", null); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -448,7 +663,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent(null, "whatever"); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -459,7 +674,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, "whatever"); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -470,7 +685,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, one, "whatever"); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -481,10 +696,9 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent("whatever", null); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } - /** * replace(x, null) throws NPE */ @@ -493,7 +707,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -504,7 +718,7 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null, "A"); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -515,10 +729,9 @@ public class ConcurrentHashMapTest exten ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", one, null); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } - /** * remove(null) throws NPE */ @@ -528,7 +741,7 @@ public class ConcurrentHashMapTest exten c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** @@ -540,47 +753,31 @@ public class ConcurrentHashMapTest exten c.put("sadsdf", "asdads"); c.remove(null, "whatever"); shouldThrow(); - } catch (NullPointerException e){} + } catch (NullPointerException success) {} } /** * remove(x, null) returns false */ public void testRemove3() { - try { - ConcurrentHashMap c = new ConcurrentHashMap(5); - c.put("sadsdf", "asdads"); - assertFalse(c.remove("sadsdf", null)); - } catch (NullPointerException e){ - fail(); - } + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.put("sadsdf", "asdads"); + assertFalse(c.remove("sadsdf", null)); } /** * A deserialized map equals original */ - public void testSerialization() { - ConcurrentHashMap q = map5(); - - try { - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); - assertEquals(q.size(), r.size()); - assertTrue(q.equals(r)); - assertTrue(r.equals(q)); - } catch (Exception e){ - e.printStackTrace(); - unexpectedException(); - } + public void testSerialization() throws Exception { + Map x = map5(); + Map y = serialClone(x); + + assertNotSame(x, y); + assertEquals(x.size(), y.size()); + assertEquals(x, y); + assertEquals(y, x); } - /** * SetValue of an EntrySet entry sets value in the map. */ @@ -592,16 +789,15 @@ public class ConcurrentHashMapTest exten 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 + // Unless it happens to be first (in which case remainder of + // test is skipped), remove a possibly-colliding key from map + // which, under some implementations, may cause entry1 to be + // cloned in map + if (!entry1.getKey().equals(new Integer(16))) { + map.remove(new Integer(16)); + entry1.setValue("XYZ"); + assertTrue(map.containsValue("XYZ")); // fails if write-through broken + } } }