--- old/test/jdk/java/util/Collection/MOAT.java 2017-10-31 16:48:06.000000000 -0700 +++ new/test/jdk/java/util/Collection/MOAT.java 2017-10-31 16:48:06.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2017, 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 @@ -57,6 +57,8 @@ import java.util.concurrent.*; import static java.util.Collections.*; import java.lang.reflect.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class MOAT { // Collections under test must not be initialized to contain this value, @@ -230,6 +232,17 @@ testListMutatorsAlwaysThrow(list); } + List listCopy = List.copyOf(Arrays.asList(1, 2, 3)); + testCollection(listCopy); + testImmutableList(listCopy); + testListMutatorsAlwaysThrow(listCopy); + + List listCollected = Stream.of(1, 2, 3).collect(Collectors.toUnmodifiableList()); + equal(listCollected, List.of(1, 2, 3)); + testCollection(listCollected); + testImmutableList(listCollected); + testListMutatorsAlwaysThrow(listCollected); + // Immutable Set testEmptySet(Set.of()); testCollMutatorsAlwaysThrow(Set.of()); @@ -252,6 +265,18 @@ testCollMutatorsAlwaysThrow(set); } + Set setCopy = Set.copyOf(Arrays.asList(1, 2, 3)); + testCollection(setCopy); + testImmutableSet(setCopy); + testCollMutatorsAlwaysThrow(setCopy); + + Set setCollected = Stream.of(1, 1, 2, 3, 2, 3) + .collect(Collectors.toUnmodifiableSet()); + equal(setCollected, Set.of(1, 2, 3)); + testCollection(setCollected); + testImmutableSet(setCollected); + testCollMutatorsAlwaysThrow(setCollected); + // Immutable Map @SuppressWarnings("unchecked") @@ -280,6 +305,35 @@ testImmutableMap(map); testMapMutatorsAlwaysThrow(map); } + + Map mapCopy = Map.copyOf(new HashMap<>(Map.of(1, 101, 2, 202, 3, 303))); + testMap(mapCopy); + testImmutableMap(mapCopy); + testMapMutatorsAlwaysThrow(mapCopy); + + Map mapCollected1 = + Stream.of(1, 2, 3) + .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i)); + equal(mapCollected1, Map.of(1, 101, 2, 202, 3, 303)); + testMap(mapCollected1); + testImmutableMap(mapCollected1); + testMapMutatorsAlwaysThrow(mapCollected1); + + try { + Stream.of(1, 1, 2, 3, 2, 3) + .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i)); + fail("duplicates should have thrown an exception"); + } catch (IllegalStateException ise) { + pass(); + } + + Map mapCollected2 = + Stream.of(1, 1, 2, 3, 2, 3) + .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i, Integer::sum)); + equal(mapCollected2, Map.of(1, 202, 2, 404, 3, 606)); + testMap(mapCollected2); + testImmutableMap(mapCollected2); + testMapMutatorsAlwaysThrow(mapCollected2); } private static void checkContainsSelf(Collection c) {