Path: blob/master/test/jdk/java/util/Collections/WrappedUnmodifiableCollections.java
41149 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 632337426* @run testng WrappedUnmodifiableCollections27*/2829import java.util.*;30import java.util.function.Function;31import org.testng.annotations.Test;32import static org.testng.Assert.*;333435@Test36public class WrappedUnmodifiableCollections {3738private static <T,E extends T> void testWrapping(T collection, Function<T,E> wrapper) {39var collection1 = wrapper.apply(collection);40var collection2 = wrapper.apply(collection1);41assertNotSame(collection, collection2);42assertSame(collection1, collection2);43}4445public void testUnmodifiableListsDontWrap() {46List<List<?>> lists = List.of(List.of(), List.of(1,2,3), List.of(1),47List.of(1,2,3,4,5,6),48List.of(1,2,3).subList(0,1),49new LinkedList<>(List.of(1,2,3)),50new ArrayList<>(List.of(1,2,3)));5152for(List<?> list : lists) {53testWrapping(list, Collections::unmodifiableList);54}55}5657public void testUnmodifiableCollectionsDontWrap() {58Collection<?> list = List.of();59testWrapping(list, Collections::unmodifiableCollection);60}6162public void testUnmodifiableSetsDontWrap() {6364List<Set<?>> sets = List.of(new TreeSet<>(),65Set.of(1, 2),66Set.of(1,2,3,4,5,6));6768for (Set<?> set : sets) {69testWrapping(set, Collections::unmodifiableSet);70}7172TreeSet<?> treeSet = new TreeSet<>();7374//Collections.UnmodifiableSortedSet75testWrapping((SortedSet<?>) treeSet, Collections::unmodifiableSortedSet);7677//Collections.UnmodifiableNavigableSet78testWrapping((NavigableSet<?>) treeSet, Collections::unmodifiableNavigableSet);7980}8182public void testUnmodifiableMapsDontWrap() {83TreeMap<?,?> treeMap = new TreeMap<>();8485List<Map<?,?>> maps = List.of(treeMap,86Map.of(1,1),87Map.of(1, 1, 2, 2, 3, 3, 4, 4));8889for (Map<?,?> map : maps) {90testWrapping(map, Collections::unmodifiableMap);91}9293//Collections.UnModifiableSortedMap94testWrapping((SortedMap<?,?>) treeMap, Collections::unmodifiableSortedMap);9596//Collections.UnModifiableNavigableMap97testWrapping((NavigableMap<?,?>) treeMap, Collections::unmodifiableNavigableMap);9899}100101}102103104