1 /* 2 * Copyright (c) 2012, 2014, 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 import java.util.Arrays; 25 import java.util.Collection; 26 import java.util.Collections; 27 import java.util.HashMap; 28 import java.util.HashSet; 29 import java.util.Iterator; 30 import java.util.LinkedHashMap; 31 import java.util.LinkedHashSet; 32 import java.util.LinkedList; 33 import java.util.List; 34 import java.util.Set; 35 36 import java.util.SortedSet; 37 38 import org.testng.annotations.DataProvider; 39 import org.testng.annotations.Test; 40 41 import static org.testng.Assert.assertTrue; 42 import static org.testng.Assert.fail; 43 44 import java.util.TreeMap; 45 import java.util.TreeSet; 46 import java.util.concurrent.ConcurrentHashMap; 47 import java.util.concurrent.ConcurrentSkipListMap; 48 import java.util.function.Function; 49 import java.util.function.Predicate; 50 51 /** 52 * @test 53 * @summary Unit tests for extension methods on Collection 54 * @library testlibrary 55 * @build CollectionAsserts CollectionSupplier ExtendsAbstractSet ExtendsAbstractCollection 56 * @run testng CollectionDefaults 57 */ 58 public class CollectionDefaults { 59 60 public static final Predicate<Integer> pEven = x -> 0 == x % 2; 61 public static final Predicate<Integer> pOdd = x -> 1 == x % 2; 62 63 private static final List<Function<Collection<Integer>, Collection<Integer>>> TEST_SUPPLIERS = Arrays.asList( 64 // Collection 65 ExtendsAbstractCollection<Integer>::new, 66 67 // Lists 68 java.util.ArrayList<Integer>::new, 69 java.util.LinkedList<Integer>::new, 70 java.util.Vector<Integer>::new, 71 java.util.concurrent.CopyOnWriteArrayList<Integer>::new, 72 ExtendsAbstractList<Integer>::new, 73 74 // Sets 75 java.util.HashSet<Integer>::new, 76 java.util.LinkedHashSet<Integer>::new, 77 java.util.TreeSet<Integer>::new, 78 java.util.concurrent.ConcurrentSkipListSet<Integer>::new, 79 java.util.concurrent.CopyOnWriteArraySet<Integer>::new, 80 ExtendsAbstractSet<Integer>::new 81 ); 82 83 private static final int SIZE = 100; 84 85 @DataProvider(name="setProvider", parallel=true) setCases()86 public static Iterator<Object[]> setCases() { 87 final List<Object[]> cases = new LinkedList<>(); 88 cases.add(new Object[] { new HashSet<>() }); 89 cases.add(new Object[] { new LinkedHashSet<>() }); 90 cases.add(new Object[] { new TreeSet<>() }); 91 cases.add(new Object[] { new java.util.concurrent.ConcurrentSkipListSet<>() }); 92 cases.add(new Object[] { new java.util.concurrent.CopyOnWriteArraySet<>() }); 93 94 cases.add(new Object[] { new ExtendsAbstractSet<>() }); 95 96 cases.add(new Object[] { Collections.newSetFromMap(new HashMap<>()) }); 97 cases.add(new Object[] { Collections.newSetFromMap(new LinkedHashMap<>()) }); 98 cases.add(new Object[] { Collections.newSetFromMap(new TreeMap<>()) }); 99 cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentHashMap<>()) }); 100 cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentSkipListMap<>()) }); 101 102 cases.add(new Object[] { new HashSet<Integer>(){{add(42);}} }); 103 cases.add(new Object[] { new ExtendsAbstractSet<Integer>(){{add(42);}} }); 104 cases.add(new Object[] { new LinkedHashSet<Integer>(){{add(42);}} }); 105 cases.add(new Object[] { new TreeSet<Integer>(){{add(42);}} }); 106 return cases.iterator(); 107 } 108 109 @Test(dataProvider = "setProvider") testProvidedWithNull(final Set<Integer> set)110 public void testProvidedWithNull(final Set<Integer> set) { 111 try { 112 set.forEach(null); 113 fail("expected NPE not thrown"); 114 } catch (NullPointerException expected) { // expected 115 } 116 try { 117 set.removeIf(null); 118 fail("expected NPE not thrown"); 119 } catch (NullPointerException expected) { // expected 120 } 121 } 122 123 @Test testForEach()124 public void testForEach() { 125 @SuppressWarnings("unchecked") 126 final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE); 127 128 for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) { 129 final Collection<Integer> original = test.expected; 130 final Collection<Integer> set = test.collection; 131 132 try { 133 set.forEach(null); 134 fail("expected NPE not thrown"); 135 } catch (NullPointerException expected) { // expected 136 } 137 if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) { 138 CollectionAsserts.assertContentsUnordered(set, original, test.toString()); 139 } else { 140 CollectionAsserts.assertContents(set, original, test.toString()); 141 } 142 143 final List<Integer> actual = new LinkedList<>(); 144 set.forEach(actual::add); 145 if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) { 146 CollectionAsserts.assertContentsUnordered(actual, set, test.toString()); 147 CollectionAsserts.assertContentsUnordered(actual, original, test.toString()); 148 } else { 149 CollectionAsserts.assertContents(actual, set, test.toString()); 150 CollectionAsserts.assertContents(actual, original, test.toString()); 151 } 152 } 153 } 154 155 @Test testRemoveIf()156 public void testRemoveIf() { 157 @SuppressWarnings("unchecked") 158 final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE); 159 for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) { 160 final Collection<Integer> original = test.expected; 161 final Collection<Integer> set = test.collection; 162 163 try { 164 set.removeIf(null); 165 fail("expected NPE not thrown"); 166 } catch (NullPointerException expected) { // expected 167 } 168 if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) { 169 CollectionAsserts.assertContentsUnordered(set, original, test.toString()); 170 } else { 171 CollectionAsserts.assertContents(set, original, test.toString()); 172 } 173 174 set.removeIf(pEven); 175 for (int i : set) { 176 assertTrue((i % 2) == 1); 177 } 178 for (int i : original) { 179 if (i % 2 == 1) { 180 assertTrue(set.contains(i)); 181 } 182 } 183 set.removeIf(pOdd); 184 assertTrue(set.isEmpty()); 185 } 186 } 187 } 188