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.Collection;
  25 import java.util.Collections;
  26 import java.util.HashMap;
  27 import java.util.HashSet;
  28 import java.util.Iterator;
  29 import java.util.LinkedHashMap;
  30 import java.util.LinkedHashSet;
  31 import java.util.LinkedList;
  32 import java.util.List;
  33 import java.util.Set;
  34 
  35 import java.util.SortedSet;
  36 
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;
  39 
  40 import static org.testng.Assert.assertTrue;
  41 import static org.testng.Assert.fail;
  42 
  43 import java.util.TreeMap;
  44 import java.util.TreeSet;
  45 import java.util.concurrent.ConcurrentHashMap;
  46 import java.util.concurrent.ConcurrentSkipListMap;
  47 import java.util.function.Predicate;
  48 import java.util.function.Supplier;
  49 
  50 /**
  51  * @test
  52  * @summary Unit tests for extension methods on Collection
  53  * @library testlibrary
  54  * @build CollectionAsserts CollectionSupplier ExtendsAbstractSet ExtendsAbstractCollection
  55  * @run testng CollectionDefaults
  56  */
  57 public class CollectionDefaults {
  58 
  59     public static final Predicate<Integer> pEven = x -> 0 == x % 2;
  60     public static final Predicate<Integer> pOdd = x -> 1 == x % 2;
  61 
  62     @SuppressWarnings("unchecked")
  63     private static final Supplier<?>[] TEST_CLASSES = {
  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)
  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")
 110     public void testProvidedWithNull(final Set<Integer> set) throws Exception {
 111         try {
 112             set.forEach(null);
 113             fail("expected NPE not thrown");
 114         } catch (NullPointerException expected) {
 115                 ; // expected
 116             }
 117         try {
 118             set.removeIf(null);
 119             fail("expected NPE not thrown");
 120         } catch (NullPointerException expected) {
 121                ; // expected
 122         }
 123     }
 124 
 125     @Test
 126     public void testForEach() throws Exception {
 127         final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier((Supplier<Collection<Integer>>[]) TEST_CLASSES, SIZE);
 128 
 129         for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) {
 130             final Collection<Integer> original = test.expected;
 131             final Collection<Integer> set = test.collection;
 132 
 133             try {
 134                 set.forEach(null);
 135                 fail("expected NPE not thrown");
 136             } catch (NullPointerException expected) {
 137                 ; // expected
 138             }
 139             if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {
 140                 CollectionAsserts.assertContentsUnordered(set, original, test.toString());
 141             } else {
 142                 CollectionAsserts.assertContents(set, original, test.toString());
 143             }
 144 
 145             final List<Integer> actual = new LinkedList<>();
 146             set.forEach(actual::add);
 147             if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {
 148                 CollectionAsserts.assertContentsUnordered(actual, set, test.toString());
 149                 CollectionAsserts.assertContentsUnordered(actual, original, test.toString());
 150             } else {
 151                 CollectionAsserts.assertContents(actual, set, test.toString());
 152                 CollectionAsserts.assertContents(actual, original, test.toString());
 153             }
 154         }
 155     }
 156 
 157     @Test
 158     public void testRemoveIf() throws Exception {
 159         final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier((Supplier<Collection<Integer>>[]) TEST_CLASSES, SIZE);
 160         for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) {
 161             final Collection<Integer> original = test.expected;
 162             final Collection<Integer> set = test.collection;
 163 
 164             try {
 165                 set.removeIf(null);
 166                 fail("expected NPE not thrown");
 167             } catch (NullPointerException expected) {
 168                 ; // expected
 169             }
 170             if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {
 171                 CollectionAsserts.assertContentsUnordered(set, original, test.toString());
 172             } else {
 173                 CollectionAsserts.assertContents(set, original, test.toString());
 174             }
 175 
 176             set.removeIf(pEven);
 177             for (int i : set) {
 178                 assertTrue((i % 2) == 1);
 179             }
 180             for (int i : original) {
 181                 if (i % 2 == 1) {
 182                     assertTrue(set.contains(i));
 183                 }
 184             }
 185             set.removeIf(pOdd);
 186             assertTrue(set.isEmpty());
 187         }
 188     }
 189 }