< prev index next >

test/jdk/java/util/Map/MapFactories.java

Print this page
rev 47476 : 8177290: add copy factory methods for unmodifiable List, Set, Map
8184690: add Collectors for collecting into unmodifiable List, Set, and Map
Reviewed-by: alanb, dholmes, rriggs, scolebourne

@@ -40,10 +40,13 @@
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertNotSame;
+import static org.testng.Assert.assertSame;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
 /*
  * @test

@@ -68,10 +71,16 @@
         }
         return result;
     }
 
     // for varargs Map.Entry methods
+
+    @SuppressWarnings("unchecked")
+    Map.Entry<Integer,String>[] genEmptyEntryArray1() {
+        return (Map.Entry<Integer,String>[])new Map.Entry<?,?>[1];
+    }
+
     @SuppressWarnings("unchecked")
     Map.Entry<Integer,String>[] genEntries(int n) {
         return IntStream.range(0, n)
             .mapToObj(i -> Map.entry(i, valueFor(i)))
             .toArray(Map.Entry[]::new);

@@ -320,33 +329,53 @@
         Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
                                           5, "f", 6, "g", 7, "h", 8, "i", 9, null);
     }
 
     @Test(expectedExceptions=NullPointerException.class)
-    public void nullKeyDisallowedN() {
+    public void nullKeyDisallowedVar1() {
+        Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();
+        entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a");
+        Map<Integer, String> map = Map.ofEntries(entries);
+    }
+
+    @Test(expectedExceptions=NullPointerException.class)
+    public void nullValueDisallowedVar1() {
+        Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();
+        entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null);
+        Map<Integer, String> map = Map.ofEntries(entries);
+    }
+
+    @Test(expectedExceptions=NullPointerException.class)
+    public void nullEntryDisallowedVar1() {
+        Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();
+        Map<Integer, String> map = Map.ofEntries(entries);
+    }
+
+    @Test(expectedExceptions=NullPointerException.class)
+    public void nullKeyDisallowedVarN() {
         Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
-        entries[0] = new AbstractMap.SimpleImmutableEntry(null, "a");
+        entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a");
         Map<Integer, String> map = Map.ofEntries(entries);
     }
 
     @Test(expectedExceptions=NullPointerException.class)
-    public void nullValueDisallowedN() {
+    public void nullValueDisallowedVarN() {
         Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
-        entries[0] = new AbstractMap.SimpleImmutableEntry(0, null);
+        entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null);
         Map<Integer, String> map = Map.ofEntries(entries);
     }
 
     @Test(expectedExceptions=NullPointerException.class)
-    public void nullEntryDisallowedN() {
+    public void nullEntryDisallowedVarN() {
         Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
         entries[5] = null;
         Map<Integer, String> map = Map.ofEntries(entries);
     }
 
     @Test(expectedExceptions=NullPointerException.class)
     public void nullArrayDisallowed() {
-        Map.ofEntries(null);
+        Map.ofEntries((Map.Entry<?,?>[])null);
     }
 
     @Test(dataProvider="all")
     public void serialEquality(Map<Integer, String> act, Map<Integer, String> exp) {
         // assume that act.equals(exp) tested elsewhere

@@ -357,21 +386,77 @@
 
     @SuppressWarnings("unchecked")
     static <T> T serialClone(T obj) {
         try {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            ObjectOutputStream oos = new ObjectOutputStream(baos);
+            try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
             oos.writeObject(obj);
-            oos.close();
+            }
             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
             ObjectInputStream ois = new ObjectInputStream(bais);
             return (T) ois.readObject();
         } catch (IOException | ClassNotFoundException e) {
             throw new AssertionError(e);
         }
     }
 
+    Map<Integer, String> genMap() {
+        Map<Integer, String> map = new HashMap<>();
+        map.put(1, "a");
+        map.put(2, "b");
+        map.put(3, "c");
+        return map;
+    }
+
+    @Test
+    public void copyOfResultsEqual() {
+        Map<Integer, String> orig = genMap();
+        Map<Integer, String> copy = Map.copyOf(orig);
+
+        assertEquals(orig, copy);
+        assertEquals(copy, orig);
+    }
+
+    @Test
+    public void copyOfModifiedUnequal() {
+        Map<Integer, String> orig = genMap();
+        Map<Integer, String> copy = Map.copyOf(orig);
+        orig.put(4, "d");
+
+        assertNotEquals(orig, copy);
+        assertNotEquals(copy, orig);
+    }
+
+    @Test
+    public void copyOfIdentity() {
+        Map<Integer, String> orig = genMap();
+        Map<Integer, String> copy1 = Map.copyOf(orig);
+        Map<Integer, String> copy2 = Map.copyOf(copy1);
+
+        assertNotSame(orig, copy1);
+        assertSame(copy1, copy2);
+    }
+
+    @Test(expectedExceptions=NullPointerException.class)
+    public void copyOfRejectsNullMap() {
+        Map<Integer, String> map = Map.copyOf(null);
+    }
+
+    @Test(expectedExceptions=NullPointerException.class)
+    public void copyOfRejectsNullKey() {
+        Map<Integer, String> map = genMap();
+        map.put(null, "x");
+        Map<Integer, String> copy = Map.copyOf(map);
+    }
+
+    @Test(expectedExceptions=NullPointerException.class)
+    public void copyOfRejectsNullValue() {
+        Map<Integer, String> map = genMap();
+        map.put(-1, null);
+        Map<Integer, String> copy = Map.copyOf(map);
+    }
+
     // Map.entry() tests
 
     @Test(expectedExceptions=NullPointerException.class)
     public void entryWithNullKeyDisallowed() {
         Map.Entry<Integer,String> e = Map.entry(null, "x");

@@ -384,11 +469,11 @@
 
     @Test
     public void entryBasicTests() {
         Map.Entry<String,String> kvh1 = Map.entry("xyzzy", "plugh");
         Map.Entry<String,String> kvh2 = Map.entry("foobar", "blurfl");
-        Map.Entry<String,String> sie = new AbstractMap.SimpleImmutableEntry("xyzzy", "plugh");
+        Map.Entry<String,String> sie = new AbstractMap.SimpleImmutableEntry<>("xyzzy", "plugh");
 
         assertTrue(kvh1.equals(sie));
         assertTrue(sie.equals(kvh1));
         assertFalse(kvh2.equals(sie));
         assertFalse(sie.equals(kvh2));

@@ -402,7 +487,6 @@
         Map.Entry<Integer,Double> e1 = Map.entry(1, 2.0);
         Map.Entry<Float,Long> e2 = Map.entry(3.0f, 4L);
         Map<Number,Number> map = Map.ofEntries(e1, e2);
         assertEquals(map.size(), 2);
     }
-
 }
< prev index next >