diff -r 4ebc2e2fb97c -r 71c04702a3d5 test/jdk/java/util/Collection/CollectionDefaults.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/jdk/java/util/Collection/CollectionDefaults.java Tue Sep 12 19:03:39 2017 +0200 @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +import java.util.SortedSet; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * @test + * @summary Unit tests for extension methods on Collection + * @library testlibrary + * @build CollectionAsserts CollectionSupplier ExtendsAbstractSet ExtendsAbstractCollection + * @run testng CollectionDefaults + */ +public class CollectionDefaults { + + public static final Predicate pEven = x -> 0 == x % 2; + public static final Predicate pOdd = x -> 1 == x % 2; + + private static final int SIZE = 100; + + private static final List, Collection>> TEST_SUPPLIERS = Arrays.asList( + // Collection + ExtendsAbstractCollection::new, + java.util.ArrayDeque::new, + java.util.concurrent.ConcurrentLinkedDeque::new, + java.util.concurrent.ConcurrentLinkedQueue::new, + java.util.concurrent.LinkedBlockingDeque::new, + java.util.concurrent.LinkedBlockingQueue::new, + java.util.concurrent.LinkedTransferQueue::new, + (coll) -> new java.util.concurrent.ArrayBlockingQueue( + 3 * SIZE, false, coll), + + // Lists + java.util.ArrayList::new, + java.util.LinkedList::new, + java.util.Vector::new, + java.util.concurrent.CopyOnWriteArrayList::new, + ExtendsAbstractList::new, + + // Sets + java.util.HashSet::new, + java.util.LinkedHashSet::new, + java.util.TreeSet::new, + java.util.concurrent.ConcurrentSkipListSet::new, + java.util.concurrent.CopyOnWriteArraySet::new, + ExtendsAbstractSet::new + ); + + @DataProvider(name="setProvider", parallel=true) + public static Iterator setCases() { + final List cases = new LinkedList<>(); + cases.add(new Object[] { new HashSet<>() }); + cases.add(new Object[] { new LinkedHashSet<>() }); + cases.add(new Object[] { new TreeSet<>() }); + cases.add(new Object[] { new java.util.concurrent.ConcurrentSkipListSet<>() }); + cases.add(new Object[] { new java.util.concurrent.CopyOnWriteArraySet<>() }); + + cases.add(new Object[] { new ExtendsAbstractSet<>() }); + + cases.add(new Object[] { Collections.newSetFromMap(new HashMap<>()) }); + cases.add(new Object[] { Collections.newSetFromMap(new LinkedHashMap<>()) }); + cases.add(new Object[] { Collections.newSetFromMap(new TreeMap<>()) }); + cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentHashMap<>()) }); + cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentSkipListMap<>()) }); + + cases.add(new Object[] { new HashSet(){{add(42);}} }); + cases.add(new Object[] { new ExtendsAbstractSet(){{add(42);}} }); + cases.add(new Object[] { new LinkedHashSet(){{add(42);}} }); + cases.add(new Object[] { new TreeSet(){{add(42);}} }); + return cases.iterator(); + } + + @Test(dataProvider = "setProvider") + public void testProvidedWithNull(final Set set) { + try { + set.forEach(null); + fail("expected NPE not thrown"); + } catch (NullPointerException expected) { // expected + } + try { + set.removeIf(null); + fail("expected NPE not thrown"); + } catch (NullPointerException expected) { // expected + } + } + + @Test + public void testForEach() { + @SuppressWarnings("unchecked") + final CollectionSupplier> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE); + + for (final CollectionSupplier.TestCase> test : supplier.get()) { + final Collection original = test.expected; + final Collection set = test.collection; + + try { + set.forEach(null); + fail("expected NPE not thrown"); + } catch (NullPointerException expected) { // expected + } + if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) { + CollectionAsserts.assertContentsUnordered(set, original, test.toString()); + } else { + CollectionAsserts.assertContents(set, original, test.toString()); + } + + final List actual = new LinkedList<>(); + set.forEach(actual::add); + if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) { + CollectionAsserts.assertContentsUnordered(actual, set, test.toString()); + CollectionAsserts.assertContentsUnordered(actual, original, test.toString()); + } else { + CollectionAsserts.assertContents(actual, set, test.toString()); + CollectionAsserts.assertContents(actual, original, test.toString()); + } + } + } + + @Test + public void testRemoveIf() { + @SuppressWarnings("unchecked") + final CollectionSupplier> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE); + for (final CollectionSupplier.TestCase> test : supplier.get()) { + final Collection original = test.expected; + final Collection set = test.collection; + + try { + set.removeIf(null); + fail("expected NPE not thrown"); + } catch (NullPointerException expected) { // expected + } + if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) { + CollectionAsserts.assertContentsUnordered(set, original, test.toString()); + } else { + CollectionAsserts.assertContents(set, original, test.toString()); + } + + set.removeIf(pEven); + for (int i : set) { + assertTrue((i % 2) == 1); + } + for (int i : original) { + if (i % 2 == 1) { + assertTrue(set.contains(i)); + } + } + set.removeIf(pOdd); + assertTrue(set.isEmpty()); + } + } +}