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