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