1 /* 2 * Copyright (c) 2012, 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.Collections; 25 import java.util.HashMap; 26 import java.util.HashSet; 27 import java.util.LinkedHashMap; 28 import java.util.LinkedHashSet; 29 import java.util.LinkedList; 30 import java.util.List; 31 import java.util.Set; 32 33 import org.testng.annotations.DataProvider; 34 import org.testng.annotations.Test; 35 36 import static org.testng.Assert.assertTrue; 37 import static org.testng.Assert.fail; 38 39 import java.util.TreeMap; 40 import java.util.TreeSet; 41 import java.util.function.Predicate; 42 43 /** 44 * @test 45 * @library testlibrary 46 * @build CollectionAsserts CollectionSupplier 47 * @run testng CollectionDefaults 48 * @summary Unit tests for extension methods on Collection 49 */ 50 public class CollectionDefaults { 51 52 public static final Predicate<Integer> pEven = x -> 0 == x % 2; 53 public static final Predicate<Integer> pOdd = x -> 1 == x % 2; 54 55 private static final String[] SET_CLASSES = { 56 "java.util.HashSet", 57 "java.util.LinkedHashSet", 58 "java.util.TreeSet" 59 }; 60 61 private static final int SIZE = 100; 62 63 @DataProvider(name="setProvider", parallel=true) 64 public static Object[][] setCases() { 65 final List<Object[]> cases = new LinkedList<>(); 66 cases.add(new Object[] { new HashSet<>() }); 67 cases.add(new Object[] { new LinkedHashSet<>() }); 68 cases.add(new Object[] { new TreeSet<>() }); 69 70 cases.add(new Object[] { Collections.newSetFromMap(new HashMap<>()) }); 71 cases.add(new Object[] { Collections.newSetFromMap(new LinkedHashMap()) }); 72 cases.add(new Object[] { Collections.newSetFromMap(new TreeMap<>()) }); 73 74 cases.add(new Object[] { new HashSet(){{add(42);}} }); 75 cases.add(new Object[] { new LinkedHashSet(){{add(42);}} }); 76 cases.add(new Object[] { new TreeSet(){{add(42);}} }); 77 return cases.toArray(new Object[0][cases.size()]); 78 } 79 80 @Test(dataProvider = "setProvider") 81 public void testProvidedWithNull(final Set<Integer> set) throws Exception { 82 try { 83 set.forEach(null); 84 fail("expected NPE not thrown"); 85 } catch (NullPointerException npe) {} 86 try { 87 set.removeIf(null); 88 fail("expected NPE not thrown"); 89 } catch (NullPointerException npe) {} 90 } 91 92 @Test 93 public void testForEach() throws Exception { 94 final CollectionSupplier supplier = new CollectionSupplier(SET_CLASSES, SIZE); 95 for (final CollectionSupplier.TestCase test : supplier.get()) { 96 final Set<Integer> original = ((Set<Integer>) test.original); 97 final Set<Integer> set = ((Set<Integer>) test.collection); 98 99 try { 100 set.forEach(null); 101 fail("expected NPE not thrown"); 102 } catch (NullPointerException npe) {} 103 if (test.className.equals("java.util.HashSet")) { 104 CollectionAsserts.assertContentsUnordered(set, original); 105 } else { 106 CollectionAsserts.assertContents(set, original); 107 } 108 109 final List<Integer> actual = new LinkedList<>(); 110 set.forEach(actual::add); 111 if (test.className.equals("java.util.HashSet")) { 112 CollectionAsserts.assertContentsUnordered(actual, set); 113 CollectionAsserts.assertContentsUnordered(actual, original); 114 } else { 115 CollectionAsserts.assertContents(actual, set); 116 CollectionAsserts.assertContents(actual, original); 117 } 118 } 119 } 120 121 @Test 122 public void testRemoveIf() throws Exception { 123 final CollectionSupplier supplier = new CollectionSupplier(SET_CLASSES, SIZE); 124 for (final CollectionSupplier.TestCase test : supplier.get()) { 125 final Set<Integer> original = ((Set<Integer>) test.original); 126 final Set<Integer> set = ((Set<Integer>) test.collection); 127 128 try { 129 set.removeIf(null); 130 fail("expected NPE not thrown"); 131 } catch (NullPointerException npe) {} 132 if (test.className.equals("java.util.HashSet")) { 133 CollectionAsserts.assertContentsUnordered(set, original); 134 } else { 135 CollectionAsserts.assertContents(set, original); 136 } 137 138 set.removeIf(pEven); 139 for (int i : set) { 140 assertTrue((i % 2) == 1); 141 } 142 for (int i : original) { 143 if (i % 2 == 1) { 144 assertTrue(set.contains(i)); 145 } 146 } 147 set.removeIf(pOdd); 148 assertTrue(set.isEmpty()); 149 } 150 } 151 }