1 /* 2 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6323374 27 * @run testng WrappedUnmodifiableCollections 28 */ 29 30 package test.java.util.Collections; 31 32 import java.util.*; 33 import java.util.function.Function; 34 import org.testng.annotations.Test; 35 import static org.testng.Assert.*; 36 37 38 @Test 39 public class WrappedUnmodifiableCollections { 40 testWrapping(T collection, Function<T,E> wrapper)41 private static <T,E extends T> void testWrapping(T collection, Function<T,E> wrapper) { 42 var collection1 = wrapper.apply(collection); 43 var collection2 = wrapper.apply(collection1); 44 assertNotSame(collection, collection2); 45 assertSame(collection1, collection2); 46 } 47 testUnmodifiableListsDontWrap()48 public void testUnmodifiableListsDontWrap() { 49 List<List<?>> lists = List.of(List.of(), List.of(1,2,3), List.of(1), 50 List.of(1,2,3,4,5,6), 51 List.of(1,2,3).subList(0,1), 52 new LinkedList<>(List.of(1,2,3)), 53 new ArrayList<>(List.of(1,2,3))); 54 55 for(List<?> list : lists) { 56 testWrapping(list, Collections::unmodifiableList); 57 } 58 } 59 testUnmodifiableCollectionsDontWrap()60 public void testUnmodifiableCollectionsDontWrap() { 61 Collection<?> list = List.of(); 62 testWrapping(list, Collections::unmodifiableCollection); 63 } 64 testUnmodifiableSetsDontWrap()65 public void testUnmodifiableSetsDontWrap() { 66 67 List<Set<?>> sets = List.of(new TreeSet<>(), 68 Set.of(1, 2), 69 Set.of(1,2,3,4,5,6)); 70 71 for (Set<?> set : sets) { 72 testWrapping(set, Collections::unmodifiableSet); 73 } 74 75 TreeSet<?> treeSet = new TreeSet<>(); 76 77 //Collections.UnmodifiableSortedSet 78 testWrapping((SortedSet<?>) treeSet, Collections::unmodifiableSortedSet); 79 80 //Collections.UnmodifiableNavigableSet 81 testWrapping((NavigableSet<?>) treeSet, Collections::unmodifiableNavigableSet); 82 83 } 84 testUnmodifiableMapsDontWrap()85 public void testUnmodifiableMapsDontWrap() { 86 TreeMap<?,?> treeMap = new TreeMap<>(); 87 88 List<Map<?,?>> maps = List.of(treeMap, 89 Map.of(1,1), 90 Map.of(1, 1, 2, 2, 3, 3, 4, 4)); 91 92 for (Map<?,?> map : maps) { 93 testWrapping(map, Collections::unmodifiableMap); 94 } 95 96 //Collections.UnModifiableSortedMap 97 testWrapping((SortedMap<?,?>) treeMap, Collections::unmodifiableSortedMap); 98 99 //Collections.UnModifiableNavigableMap 100 testWrapping((NavigableMap<?,?>) treeMap, Collections::unmodifiableNavigableMap); 101 102 } 103 104 } 105