Path: blob/master/test/jdk/java/util/Collections/Wrappers.java
41149 views
/*1* Copyright (c) 2013, 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* @run testng Wrappers26* @summary Ensure Collections wrapping classes provide non-default implementations27*/2829import java.lang.reflect.Method;30import java.util.ArrayList;31import java.util.Collection;32import java.util.Collections;33import java.util.LinkedList;34import java.util.List;35import java.util.Objects;36import java.util.TreeMap;37import java.util.TreeSet;3839import org.testng.annotations.Test;40import org.testng.annotations.DataProvider;4142import static org.testng.Assert.assertFalse;4344@Test(groups = "unit")45public class Wrappers {46static Object[][] collections;4748@DataProvider(name="collections")49public static Object[][] collectionCases() {50if (collections != null) {51return collections;52}5354List<Object[]> cases = new ArrayList<>();55LinkedList<Integer> seedList = new LinkedList<>();56ArrayList<Integer> seedRandomAccess = new ArrayList<>();57TreeSet<Integer> seedSet = new TreeSet<>();58TreeMap<Integer, Integer> seedMap = new TreeMap<>();5960for (int i = 1; i <= 10; i++) {61seedList.add(i);62seedRandomAccess.add(i);63seedSet.add(i);64seedMap.put(i, i);65}6667cases.add(new Object[] { Collections.unmodifiableCollection(seedList) });68cases.add(new Object[] { Collections.unmodifiableList(seedList) });69cases.add(new Object[] { Collections.unmodifiableList(seedRandomAccess) });70cases.add(new Object[] { Collections.unmodifiableSet(seedSet) });71cases.add(new Object[] { Collections.unmodifiableSortedSet(seedSet) });72cases.add(new Object[] { Collections.unmodifiableNavigableSet(seedSet) });7374// As sets from map also need to be unmodifiable, thus a wrapping75// layer exist and should not have default methods76cases.add(new Object[] { Collections.unmodifiableMap(seedMap).entrySet() });77cases.add(new Object[] { Collections.unmodifiableMap(seedMap).keySet() });78cases.add(new Object[] { Collections.unmodifiableMap(seedMap).values() });79cases.add(new Object[] { Collections.unmodifiableSortedMap(seedMap).entrySet() });80cases.add(new Object[] { Collections.unmodifiableSortedMap(seedMap).keySet() });81cases.add(new Object[] { Collections.unmodifiableSortedMap(seedMap).values() });82cases.add(new Object[] { Collections.unmodifiableNavigableMap(seedMap).entrySet() });83cases.add(new Object[] { Collections.unmodifiableNavigableMap(seedMap).keySet() });84cases.add(new Object[] { Collections.unmodifiableNavigableMap(seedMap).values() });8586// Synchronized87cases.add(new Object[] { Collections.synchronizedCollection(seedList) });88cases.add(new Object[] { Collections.synchronizedList(seedList) });89cases.add(new Object[] { Collections.synchronizedList(seedRandomAccess) });90cases.add(new Object[] { Collections.synchronizedSet(seedSet) });91cases.add(new Object[] { Collections.synchronizedSortedSet(seedSet) });92cases.add(new Object[] { Collections.synchronizedNavigableSet(seedSet) });9394// As sets from map also need to be synchronized on the map, thus a95// wrapping layer exist and should not have default methods96cases.add(new Object[] { Collections.synchronizedMap(seedMap).entrySet() });97cases.add(new Object[] { Collections.synchronizedMap(seedMap).keySet() });98cases.add(new Object[] { Collections.synchronizedMap(seedMap).values() });99cases.add(new Object[] { Collections.synchronizedSortedMap(seedMap).entrySet() });100cases.add(new Object[] { Collections.synchronizedSortedMap(seedMap).keySet() });101cases.add(new Object[] { Collections.synchronizedSortedMap(seedMap).values() });102cases.add(new Object[] { Collections.synchronizedNavigableMap(seedMap).entrySet() });103cases.add(new Object[] { Collections.synchronizedNavigableMap(seedMap).keySet() });104cases.add(new Object[] { Collections.synchronizedNavigableMap(seedMap).values() });105106// Checked107cases.add(new Object[] { Collections.checkedCollection(seedList, Integer.class) });108cases.add(new Object[] { Collections.checkedList(seedList, Integer.class) });109cases.add(new Object[] { Collections.checkedList(seedRandomAccess, Integer.class) });110cases.add(new Object[] { Collections.checkedSet(seedSet, Integer.class) });111cases.add(new Object[] { Collections.checkedSortedSet(seedSet, Integer.class) });112cases.add(new Object[] { Collections.checkedNavigableSet(seedSet, Integer.class) });113cases.add(new Object[] { Collections.checkedQueue(seedList, Integer.class) });114115// asLifoQueue is another wrapper116cases.add(new Object[] { Collections.asLifoQueue(seedList) });117118collections = cases.toArray(new Object[0][]);119return collections;120}121122static Method[] defaultMethods;123124static {125List<Method> list = new ArrayList<>();126Method[] methods = Collection.class.getMethods();127for (Method m: methods) {128if (m.isDefault()) {129list.add(m);130}131}132defaultMethods = list.toArray(new Method[0]);133}134135@Test(dataProvider = "collections")136public static void testAllDefaultMethodsOverridden(Collection c) throws NoSuchMethodException {137Class cls = c.getClass();138for (Method m: defaultMethods) {139Method m2 = cls.getMethod(m.getName(), m.getParameterTypes());140// default had been override141assertFalse(m2.isDefault(), cls.getCanonicalName());142}143}144}145146147148