< prev index next >

test/jdk/java/util/Map/InPlaceOpsCollisions.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -25,15 +25,23 @@
  * @test
  * @bug 8005698
  * @run testng/othervm -Dtest.map.collisions.shortrun=true InPlaceOpsCollisions
  * @summary Ensure overrides of in-place operations in Maps behave well with lots of collisions.
  */
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.TreeMap;
 import java.util.function.BiFunction;
 import java.util.function.Function;
 import java.util.function.Supplier;
 
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;

@@ -69,10 +77,23 @@
         }
         assertEquals(map.size(), keys.length,
                 String.format("map expected size m%d != k%d", map.size(), keys.length));
     }
 
+    @Test(dataProvider = "nullValueFriendlyMaps")
+    void testPutIfAbsentOverwriteNull(String desc, Supplier<Map<Object, Object>> ms) {
+        Map<Object, Object> map = ms.get();
+        map.put("key", null);
+        assertEquals(map.size(), 1, desc + ": size != 1");
+        assertTrue(map.containsKey("key"), desc + ": does not have key");
+        assertNull(map.get("key"), desc + ": value is not null");
+        map.putIfAbsent("key", "value"); // must rewrite
+        assertEquals(map.size(), 1, desc + ": size != 1");
+        assertTrue(map.containsKey("key"), desc + ": does not have key");
+        assertEquals(map.get("key"), "value", desc + ": value is not 'value'");
+    }
+
     @Test(dataProvider = "mapsWithObjectsAndStrings")
     void testRemoveMapping(String desc, Supplier<Map<Object, Object>> ms, Object val) {
         Map<Object, Object> map = ms.get();
         Object[] keys = map.keySet().toArray();
         boolean removed;

@@ -494,6 +515,15 @@
                 map.put(keys[i], val);
             }
         }
     }
 
+    @DataProvider
+    public Iterator<Object[]> nullValueFriendlyMaps() {
+        return Arrays.asList(
+                new Object[]{"HashMap", (Supplier<Map<?, ?>>) HashMap::new},
+                new Object[]{"LinkedHashMap", (Supplier<Map<?, ?>>) LinkedHashMap::new},
+                new Object[]{"TreeMap", (Supplier<Map<?, ?>>) TreeMap::new},
+                new Object[]{"TreeMap(cmp)", (Supplier<Map<?, ?>>) () -> new TreeMap<>(Comparator.reverseOrder())}
+        ).iterator();
+    }
 }
< prev index next >