test/java/util/Map/Defaults.java

Print this page
rev 8056 : 8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
Summary: Explicitly check for null remappingFunction parameter.
Reviewed-by: TBD
Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>

@@ -21,11 +21,11 @@
  * questions.
  */
 
 /*
  * @test
- * @bug 8010122 8004518
+ * @bug 8010122 8004518 8024331
  * @summary Test Map default methods
  * @author Mike Duigou
  * @run testng Defaults
  */
 import java.util.AbstractMap;

@@ -286,10 +286,25 @@
         assertFalse(map.containsKey(EXTRA_KEY));
         assertSame(map.computeIfAbsent(EXTRA_KEY, (k) -> EXTRA_VALUE), EXTRA_VALUE);
         assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
     }
 
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testComputeIfAbsentNPEHashMap() {
+        Object value = new HashMap().computeIfAbsent(KEYS[1], null);
+    }
+
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testComputeIfAbsentNPEHashtable() {
+        Object value = new Hashtable().computeIfAbsent(KEYS[1], null);
+    }
+
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testComputeIfAbsentNPETreeMap() {
+        Object value = new TreeMap().computeIfAbsent(KEYS[1], null);
+    }
+
     @Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
     public void testComputeIfPresentNulls(String description, Map<IntegerEnum, String> map) {
         assertTrue(map.containsKey(null), description + ": null key absent");
         assertNull(map.get(null), description + ": value not null");
         assertSame(map.computeIfPresent(null, (k, v) -> {

@@ -326,10 +341,25 @@
         }), null);
         assertFalse(map.containsKey(EXTRA_KEY));
         assertSame(map.get(EXTRA_KEY), null);
     }
 
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testComputeIfPresentNPEHashMap() {
+        Object value = new HashMap().computeIfPresent(KEYS[1], null);
+    }
+
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testComputeIfPresentNPEHashtable() {
+        Object value = new Hashtable().computeIfPresent(KEYS[1], null);
+    }
+
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testComputeIfPresentNPETreeMap() {
+        Object value = new TreeMap().computeIfPresent(KEYS[1], null);
+    }
+
     @Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
     public void testComputeNulls(String description, Map<IntegerEnum, String> map) {
         assertTrue(map.containsKey(null), "null key absent");
         assertNull(map.get(null), "value not null");
         assertSame(map.compute(null, (k, v) -> {

@@ -412,10 +442,24 @@
         }), EXTRA_VALUE);
         assertTrue(map.containsKey(EXTRA_KEY));
         assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
     }
 
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testComputeNPEHashMap() {
+        Object value = new HashMap().compute(KEYS[1], null);
+    }
+
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testComputeNPEHashtable() {
+        Object value = new Hashtable().compute(KEYS[1], null);
+    }
+
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testComputeNPETreeMap() {
+        Object value = new TreeMap().compute(KEYS[1], null);
+    }
 
     @Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
     public void testMergeNulls(String description, Map<IntegerEnum, String> map) {
         assertTrue(map.containsKey(null), "null key absent");
         assertNull(map.get(null), "value not null");

@@ -454,10 +498,25 @@
         }), EXTRA_VALUE);
         assertTrue(map.containsKey(EXTRA_KEY));
         assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
     }
 
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testMergeNPEHashMap() {
+        Object value = new HashMap().merge(KEYS[1], VALUES[1], null);
+    }
+
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testMergeNPEHashtable() {
+        Object value = new Hashtable().merge(KEYS[1], VALUES[1], null);
+    }
+
+    @Test(expectedExceptions = {NullPointerException.class})
+    public void testMergeNPETreeMap() {
+        Object value = new TreeMap().merge(KEYS[1], VALUES[1], null);
+    }
+
     enum IntegerEnum {
 
         e0, e1, e2, e3, e4, e5, e6, e7, e8, e9,
         e10, e11, e12, e13, e14, e15, e16, e17, e18, e19,
         e20, e21, e22, e23, e24, e25, e26, e27, e28, e29,