1 /* 2 * Copyright (c) 2013, 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 * @run testng Wrappers 27 * @summary Ensure Collections wrapping classes provide non-default implementations 28 */ 29 30 package test.java.util.Collections; 31 32 import java.lang.reflect.Method; 33 import java.util.ArrayList; 34 import java.util.Collection; 35 import java.util.Collections; 36 import java.util.LinkedList; 37 import java.util.List; 38 import java.util.Objects; 39 import java.util.TreeMap; 40 import java.util.TreeSet; 41 42 import org.testng.annotations.Test; 43 import org.testng.annotations.DataProvider; 44 45 import static org.testng.Assert.assertFalse; 46 47 @Test(groups = "unit") 48 public class Wrappers { 49 static Object[][] collections; 50 51 @DataProvider(name="collections") collectionCases()52 public static Object[][] collectionCases() { 53 if (collections != null) { 54 return collections; 55 } 56 57 List<Object[]> cases = new ArrayList<>(); 58 LinkedList<Integer> seedList = new LinkedList<>(); 59 ArrayList<Integer> seedRandomAccess = new ArrayList<>(); 60 TreeSet<Integer> seedSet = new TreeSet<>(); 61 TreeMap<Integer, Integer> seedMap = new TreeMap<>(); 62 63 for (int i = 1; i <= 10; i++) { 64 seedList.add(i); 65 seedRandomAccess.add(i); 66 seedSet.add(i); 67 seedMap.put(i, i); 68 } 69 70 cases.add(new Object[] { Collections.unmodifiableCollection(seedList) }); 71 cases.add(new Object[] { Collections.unmodifiableList(seedList) }); 72 cases.add(new Object[] { Collections.unmodifiableList(seedRandomAccess) }); 73 cases.add(new Object[] { Collections.unmodifiableSet(seedSet) }); 74 cases.add(new Object[] { Collections.unmodifiableSortedSet(seedSet) }); 75 cases.add(new Object[] { Collections.unmodifiableNavigableSet(seedSet) }); 76 77 // As sets from map also need to be unmodifiable, thus a wrapping 78 // layer exist and should not have default methods 79 cases.add(new Object[] { Collections.unmodifiableMap(seedMap).entrySet() }); 80 cases.add(new Object[] { Collections.unmodifiableMap(seedMap).keySet() }); 81 cases.add(new Object[] { Collections.unmodifiableMap(seedMap).values() }); 82 cases.add(new Object[] { Collections.unmodifiableSortedMap(seedMap).entrySet() }); 83 cases.add(new Object[] { Collections.unmodifiableSortedMap(seedMap).keySet() }); 84 cases.add(new Object[] { Collections.unmodifiableSortedMap(seedMap).values() }); 85 cases.add(new Object[] { Collections.unmodifiableNavigableMap(seedMap).entrySet() }); 86 cases.add(new Object[] { Collections.unmodifiableNavigableMap(seedMap).keySet() }); 87 cases.add(new Object[] { Collections.unmodifiableNavigableMap(seedMap).values() }); 88 89 // Synchronized 90 cases.add(new Object[] { Collections.synchronizedCollection(seedList) }); 91 cases.add(new Object[] { Collections.synchronizedList(seedList) }); 92 cases.add(new Object[] { Collections.synchronizedList(seedRandomAccess) }); 93 cases.add(new Object[] { Collections.synchronizedSet(seedSet) }); 94 cases.add(new Object[] { Collections.synchronizedSortedSet(seedSet) }); 95 cases.add(new Object[] { Collections.synchronizedNavigableSet(seedSet) }); 96 97 // As sets from map also need to be synchronized on the map, thus a 98 // wrapping layer exist and should not have default methods 99 cases.add(new Object[] { Collections.synchronizedMap(seedMap).entrySet() }); 100 cases.add(new Object[] { Collections.synchronizedMap(seedMap).keySet() }); 101 cases.add(new Object[] { Collections.synchronizedMap(seedMap).values() }); 102 cases.add(new Object[] { Collections.synchronizedSortedMap(seedMap).entrySet() }); 103 cases.add(new Object[] { Collections.synchronizedSortedMap(seedMap).keySet() }); 104 cases.add(new Object[] { Collections.synchronizedSortedMap(seedMap).values() }); 105 cases.add(new Object[] { Collections.synchronizedNavigableMap(seedMap).entrySet() }); 106 cases.add(new Object[] { Collections.synchronizedNavigableMap(seedMap).keySet() }); 107 cases.add(new Object[] { Collections.synchronizedNavigableMap(seedMap).values() }); 108 109 // Checked 110 cases.add(new Object[] { Collections.checkedCollection(seedList, Integer.class) }); 111 cases.add(new Object[] { Collections.checkedList(seedList, Integer.class) }); 112 cases.add(new Object[] { Collections.checkedList(seedRandomAccess, Integer.class) }); 113 cases.add(new Object[] { Collections.checkedSet(seedSet, Integer.class) }); 114 cases.add(new Object[] { Collections.checkedSortedSet(seedSet, Integer.class) }); 115 cases.add(new Object[] { Collections.checkedNavigableSet(seedSet, Integer.class) }); 116 cases.add(new Object[] { Collections.checkedQueue(seedList, Integer.class) }); 117 118 // asLifoQueue is another wrapper 119 cases.add(new Object[] { Collections.asLifoQueue(seedList) }); 120 121 collections = cases.toArray(new Object[0][]); 122 return collections; 123 } 124 125 static Method[] defaultMethods; 126 127 static { 128 List<Method> list = new ArrayList<>(); 129 Method[] methods = Collection.class.getMethods(); 130 for (Method m: methods) { 131 if (m.isDefault()) { 132 list.add(m); 133 } 134 } 135 defaultMethods = list.toArray(new Method[0]); 136 } 137 138 @Test(dataProvider = "collections") testAllDefaultMethodsOverridden(Collection c)139 public static void testAllDefaultMethodsOverridden(Collection c) throws NoSuchMethodException { 140 Class cls = c.getClass(); 141 for (Method m: defaultMethods) { 142 Method m2 = cls.getMethod(m.getName(), m.getParameterTypes()); 143 // default had been override 144 assertFalse(m2.isDefault(), cls.getCanonicalName()); 145 } 146 } 147 } 148 149