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