Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collection/CollectionDefaults.java
38811 views
/*1* Copyright (c) 2012, 2014, 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*/2223import java.util.Arrays;24import java.util.Collection;25import java.util.Collections;26import java.util.HashMap;27import java.util.HashSet;28import java.util.Iterator;29import java.util.LinkedHashMap;30import java.util.LinkedHashSet;31import java.util.LinkedList;32import java.util.List;33import java.util.Set;3435import java.util.SortedSet;3637import org.testng.annotations.DataProvider;38import org.testng.annotations.Test;3940import static org.testng.Assert.assertTrue;41import static org.testng.Assert.fail;4243import java.util.TreeMap;44import java.util.TreeSet;45import java.util.concurrent.ConcurrentHashMap;46import java.util.concurrent.ConcurrentSkipListMap;47import java.util.function.Function;48import java.util.function.Predicate;4950/**51* @test52* @summary Unit tests for extension methods on Collection53* @library testlibrary54* @build CollectionAsserts CollectionSupplier ExtendsAbstractSet ExtendsAbstractCollection55* @run testng CollectionDefaults56*/57public class CollectionDefaults {5859public static final Predicate<Integer> pEven = x -> 0 == x % 2;60public static final Predicate<Integer> pOdd = x -> 1 == x % 2;6162private static final List<Function<Collection<Integer>, Collection<Integer>>> TEST_SUPPLIERS = Arrays.asList(63// Collection64ExtendsAbstractCollection<Integer>::new,6566// Lists67java.util.ArrayList<Integer>::new,68java.util.LinkedList<Integer>::new,69java.util.Vector<Integer>::new,70java.util.concurrent.CopyOnWriteArrayList<Integer>::new,71ExtendsAbstractList<Integer>::new,7273// Sets74java.util.HashSet<Integer>::new,75java.util.LinkedHashSet<Integer>::new,76java.util.TreeSet<Integer>::new,77java.util.concurrent.ConcurrentSkipListSet<Integer>::new,78java.util.concurrent.CopyOnWriteArraySet<Integer>::new,79ExtendsAbstractSet<Integer>::new80);8182private static final int SIZE = 100;8384@DataProvider(name="setProvider", parallel=true)85public static Iterator<Object[]> setCases() {86final List<Object[]> cases = new LinkedList<>();87cases.add(new Object[] { new HashSet<>() });88cases.add(new Object[] { new LinkedHashSet<>() });89cases.add(new Object[] { new TreeSet<>() });90cases.add(new Object[] { new java.util.concurrent.ConcurrentSkipListSet<>() });91cases.add(new Object[] { new java.util.concurrent.CopyOnWriteArraySet<>() });9293cases.add(new Object[] { new ExtendsAbstractSet<>() });9495cases.add(new Object[] { Collections.newSetFromMap(new HashMap<>()) });96cases.add(new Object[] { Collections.newSetFromMap(new LinkedHashMap<>()) });97cases.add(new Object[] { Collections.newSetFromMap(new TreeMap<>()) });98cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentHashMap<>()) });99cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentSkipListMap<>()) });100101cases.add(new Object[] { new HashSet<Integer>(){{add(42);}} });102cases.add(new Object[] { new ExtendsAbstractSet<Integer>(){{add(42);}} });103cases.add(new Object[] { new LinkedHashSet<Integer>(){{add(42);}} });104cases.add(new Object[] { new TreeSet<Integer>(){{add(42);}} });105return cases.iterator();106}107108@Test(dataProvider = "setProvider")109public void testProvidedWithNull(final Set<Integer> set) {110try {111set.forEach(null);112fail("expected NPE not thrown");113} catch (NullPointerException expected) { // expected114}115try {116set.removeIf(null);117fail("expected NPE not thrown");118} catch (NullPointerException expected) { // expected119}120}121122@Test123public void testForEach() {124@SuppressWarnings("unchecked")125final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE);126127for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) {128final Collection<Integer> original = test.expected;129final Collection<Integer> set = test.collection;130131try {132set.forEach(null);133fail("expected NPE not thrown");134} catch (NullPointerException expected) { // expected135}136if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {137CollectionAsserts.assertContentsUnordered(set, original, test.toString());138} else {139CollectionAsserts.assertContents(set, original, test.toString());140}141142final List<Integer> actual = new LinkedList<>();143set.forEach(actual::add);144if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {145CollectionAsserts.assertContentsUnordered(actual, set, test.toString());146CollectionAsserts.assertContentsUnordered(actual, original, test.toString());147} else {148CollectionAsserts.assertContents(actual, set, test.toString());149CollectionAsserts.assertContents(actual, original, test.toString());150}151}152}153154@Test155public void testRemoveIf() {156@SuppressWarnings("unchecked")157final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE);158for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) {159final Collection<Integer> original = test.expected;160final Collection<Integer> set = test.collection;161162try {163set.removeIf(null);164fail("expected NPE not thrown");165} catch (NullPointerException expected) { // expected166}167if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {168CollectionAsserts.assertContentsUnordered(set, original, test.toString());169} else {170CollectionAsserts.assertContents(set, original, test.toString());171}172173set.removeIf(pEven);174for (int i : set) {175assertTrue((i % 2) == 1);176}177for (int i : original) {178if (i % 2 == 1) {179assertTrue(set.contains(i));180}181}182set.removeIf(pOdd);183assertTrue(set.isEmpty());184}185}186}187188189