--- old/test/jdk/java/util/Map/MapFactories.java 2017-10-31 16:48:13.000000000 -0700 +++ new/test/jdk/java/util/Map/MapFactories.java 2017-10-31 16:48:13.000000000 -0700 @@ -42,6 +42,9 @@ 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; @@ -70,6 +73,12 @@ } // for varargs Map.Entry methods + + @SuppressWarnings("unchecked") + Map.Entry[] genEmptyEntryArray1() { + return (Map.Entry[])new Map.Entry[1]; + } + @SuppressWarnings("unchecked") Map.Entry[] genEntries(int n) { return IntStream.range(0, n) @@ -322,21 +331,41 @@ } @Test(expectedExceptions=NullPointerException.class) - public void nullKeyDisallowedN() { + public void nullKeyDisallowedVar1() { + Map.Entry[] entries = genEmptyEntryArray1(); + entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a"); + Map map = Map.ofEntries(entries); + } + + @Test(expectedExceptions=NullPointerException.class) + public void nullValueDisallowedVar1() { + Map.Entry[] entries = genEmptyEntryArray1(); + entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null); + Map map = Map.ofEntries(entries); + } + + @Test(expectedExceptions=NullPointerException.class) + public void nullEntryDisallowedVar1() { + Map.Entry[] entries = genEmptyEntryArray1(); + Map map = Map.ofEntries(entries); + } + + @Test(expectedExceptions=NullPointerException.class) + public void nullKeyDisallowedVarN() { Map.Entry[] entries = genEntries(MAX_ENTRIES); - entries[0] = new AbstractMap.SimpleImmutableEntry(null, "a"); + entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a"); Map map = Map.ofEntries(entries); } @Test(expectedExceptions=NullPointerException.class) - public void nullValueDisallowedN() { + public void nullValueDisallowedVarN() { Map.Entry[] entries = genEntries(MAX_ENTRIES); - entries[0] = new AbstractMap.SimpleImmutableEntry(0, null); + entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null); Map map = Map.ofEntries(entries); } @Test(expectedExceptions=NullPointerException.class) - public void nullEntryDisallowedN() { + public void nullEntryDisallowedVarN() { Map.Entry[] entries = genEntries(MAX_ENTRIES); entries[5] = null; Map map = Map.ofEntries(entries); @@ -344,7 +373,7 @@ @Test(expectedExceptions=NullPointerException.class) public void nullArrayDisallowed() { - Map.ofEntries(null); + Map.ofEntries((Map.Entry[])null); } @Test(dataProvider="all") @@ -359,9 +388,9 @@ static T serialClone(T obj) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(obj); - oos.close(); + try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(obj); + } ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); return (T) ois.readObject(); @@ -370,6 +399,62 @@ } } + Map genMap() { + Map map = new HashMap<>(); + map.put(1, "a"); + map.put(2, "b"); + map.put(3, "c"); + return map; + } + + @Test + public void copyOfResultsEqual() { + Map orig = genMap(); + Map copy = Map.copyOf(orig); + + assertEquals(orig, copy); + assertEquals(copy, orig); + } + + @Test + public void copyOfModifiedUnequal() { + Map orig = genMap(); + Map copy = Map.copyOf(orig); + orig.put(4, "d"); + + assertNotEquals(orig, copy); + assertNotEquals(copy, orig); + } + + @Test + public void copyOfIdentity() { + Map orig = genMap(); + Map copy1 = Map.copyOf(orig); + Map copy2 = Map.copyOf(copy1); + + assertNotSame(orig, copy1); + assertSame(copy1, copy2); + } + + @Test(expectedExceptions=NullPointerException.class) + public void copyOfRejectsNullMap() { + Map map = Map.copyOf(null); + } + + @Test(expectedExceptions=NullPointerException.class) + public void copyOfRejectsNullKey() { + Map map = genMap(); + map.put(null, "x"); + Map copy = Map.copyOf(map); + } + + @Test(expectedExceptions=NullPointerException.class) + public void copyOfRejectsNullValue() { + Map map = genMap(); + map.put(-1, null); + Map copy = Map.copyOf(map); + } + // Map.entry() tests @Test(expectedExceptions=NullPointerException.class) @@ -386,7 +471,7 @@ public void entryBasicTests() { Map.Entry kvh1 = Map.entry("xyzzy", "plugh"); Map.Entry kvh2 = Map.entry("foobar", "blurfl"); - Map.Entry sie = new AbstractMap.SimpleImmutableEntry("xyzzy", "plugh"); + Map.Entry sie = new AbstractMap.SimpleImmutableEntry<>("xyzzy", "plugh"); assertTrue(kvh1.equals(sie)); assertTrue(sie.equals(kvh1)); @@ -404,5 +489,4 @@ Map map = Map.ofEntries(e1, e2); assertEquals(map.size(), 2); } - }