--- old/test/jdk/java/util/Collection/SetFactories.java 2017-11-28 17:14:48.000000000 -0800 +++ new/test/jdk/java/util/Collection/SetFactories.java 2017-11-28 17:14:48.000000000 -0800 @@ -40,6 +40,9 @@ import org.testng.annotations.Test; 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; @@ -275,9 +278,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(); @@ -285,4 +288,53 @@ throw new AssertionError(e); } } + + Set genSet() { + return new HashSet<>(Arrays.asList(1, 2, 3)); + } + + @Test + public void copyOfResultsEqual() { + Set orig = genSet(); + Set copy = Set.copyOf(orig); + + assertEquals(orig, copy); + assertEquals(copy, orig); + } + + @Test + public void copyOfModifiedUnequal() { + Set orig = genSet(); + Set copy = Set.copyOf(orig); + orig.add(4); + + assertNotEquals(orig, copy); + assertNotEquals(copy, orig); + } + + @Test + public void copyOfIdentity() { + Set orig = genSet(); + Set copy1 = Set.copyOf(orig); + Set copy2 = Set.copyOf(copy1); + + assertNotSame(orig, copy1); + assertSame(copy1, copy2); + } + + @Test(expectedExceptions=NullPointerException.class) + public void copyOfRejectsNullCollection() { + Set set = Set.copyOf(null); + } + + @Test(expectedExceptions=NullPointerException.class) + public void copyOfRejectsNullElements() { + Set set = Set.copyOf(Arrays.asList(1, null, 3)); + } + + @Test + public void copyOfAcceptsDuplicates() { + Set set = Set.copyOf(Arrays.asList(1, 1, 2, 3, 3, 3)); + assertEquals(set, Set.of(1, 2, 3)); + } }