test/java/util/Collection/CollectionDefaults.java

Print this page
rev 7682 : 8021591: Additional explicit null checks
Reviewed-by: duke


  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 }


  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 }