Path: blob/master/test/jdk/java/util/Collection/CollectionDefaults.java
41149 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 int SIZE = 100;6364private static final List<Function<Collection<Integer>, Collection<Integer>>> TEST_SUPPLIERS = Arrays.asList(65// Collection66ExtendsAbstractCollection<Integer>::new,67java.util.ArrayDeque<Integer>::new,68java.util.concurrent.ConcurrentLinkedDeque<Integer>::new,69java.util.concurrent.ConcurrentLinkedQueue<Integer>::new,70java.util.concurrent.LinkedBlockingDeque<Integer>::new,71java.util.concurrent.LinkedBlockingQueue<Integer>::new,72java.util.concurrent.LinkedTransferQueue<Integer>::new,73(coll) -> new java.util.concurrent.ArrayBlockingQueue<Integer>(743 * SIZE, false, coll),7576// Lists77java.util.ArrayList<Integer>::new,78java.util.LinkedList<Integer>::new,79java.util.Vector<Integer>::new,80java.util.concurrent.CopyOnWriteArrayList<Integer>::new,81ExtendsAbstractList<Integer>::new,8283// Sets84java.util.HashSet<Integer>::new,85java.util.LinkedHashSet<Integer>::new,86java.util.TreeSet<Integer>::new,87java.util.concurrent.ConcurrentSkipListSet<Integer>::new,88java.util.concurrent.CopyOnWriteArraySet<Integer>::new,89ExtendsAbstractSet<Integer>::new90);9192@DataProvider(name="setProvider", parallel=true)93public static Iterator<Object[]> setCases() {94final List<Object[]> cases = new LinkedList<>();95cases.add(new Object[] { new HashSet<>() });96cases.add(new Object[] { new LinkedHashSet<>() });97cases.add(new Object[] { new TreeSet<>() });98cases.add(new Object[] { new java.util.concurrent.ConcurrentSkipListSet<>() });99cases.add(new Object[] { new java.util.concurrent.CopyOnWriteArraySet<>() });100101cases.add(new Object[] { new ExtendsAbstractSet<>() });102103cases.add(new Object[] { Collections.newSetFromMap(new HashMap<>()) });104cases.add(new Object[] { Collections.newSetFromMap(new LinkedHashMap<>()) });105cases.add(new Object[] { Collections.newSetFromMap(new TreeMap<>()) });106cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentHashMap<>()) });107cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentSkipListMap<>()) });108109cases.add(new Object[] { new HashSet<Integer>(){{add(42);}} });110cases.add(new Object[] { new ExtendsAbstractSet<Integer>(){{add(42);}} });111cases.add(new Object[] { new LinkedHashSet<Integer>(){{add(42);}} });112cases.add(new Object[] { new TreeSet<Integer>(){{add(42);}} });113return cases.iterator();114}115116@Test(dataProvider = "setProvider")117public void testProvidedWithNull(final Set<Integer> set) {118try {119set.forEach(null);120fail("expected NPE not thrown");121} catch (NullPointerException expected) { // expected122}123try {124set.removeIf(null);125fail("expected NPE not thrown");126} catch (NullPointerException expected) { // expected127}128}129130@Test131public void testForEach() {132@SuppressWarnings("unchecked")133final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE);134135for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) {136final Collection<Integer> original = test.expected;137final Collection<Integer> set = test.collection;138139try {140set.forEach(null);141fail("expected NPE not thrown");142} catch (NullPointerException expected) { // expected143}144if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {145CollectionAsserts.assertContentsUnordered(set, original, test.toString());146} else {147CollectionAsserts.assertContents(set, original, test.toString());148}149150final List<Integer> actual = new LinkedList<>();151set.forEach(actual::add);152if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {153CollectionAsserts.assertContentsUnordered(actual, set, test.toString());154CollectionAsserts.assertContentsUnordered(actual, original, test.toString());155} else {156CollectionAsserts.assertContents(actual, set, test.toString());157CollectionAsserts.assertContents(actual, original, test.toString());158}159}160}161162@Test163public void testRemoveIf() {164@SuppressWarnings("unchecked")165final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE);166for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) {167final Collection<Integer> original = test.expected;168final Collection<Integer> set = test.collection;169170try {171set.removeIf(null);172fail("expected NPE not thrown");173} catch (NullPointerException expected) { // expected174}175if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {176CollectionAsserts.assertContentsUnordered(set, original, test.toString());177} else {178CollectionAsserts.assertContents(set, original, test.toString());179}180181set.removeIf(pEven);182for (int i : set) {183assertTrue((i % 2) == 1);184}185for (int i : original) {186if (i % 2 == 1) {187assertTrue(set.contains(i));188}189}190set.removeIf(pOdd);191assertTrue(set.isEmpty());192}193}194}195196197