test/java/util/List/ListDefaults.java

Print this page
rev 7932 : 8021591: Additional explicit null checks
Reviewed-by: psandoz, martin, alanb

@@ -26,12 +26,10 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
 import java.util.LinkedList;
 import java.util.Stack;
-import java.util.TreeMap;
-import java.util.TreeSet;
 import java.util.Vector;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 

@@ -44,31 +42,33 @@
 import static org.testng.Assert.fail;
 
 import java.lang.reflect.Constructor;
 import java.util.ConcurrentModificationException;
 import java.util.function.Predicate;
+import java.util.function.Supplier;
 
 /**
  * @test
+ * @summary Unit tests for extension methods on List
  * @bug 8023367
- * @library testlibrary
- * @build CollectionAsserts CollectionSupplier
+ * @library ../Collection/testlibrary
+ * @build CollectionAsserts CollectionSupplier ExtendsAbstractList
  * @run testng ListDefaults
- * @summary Unit tests for extension methods on List
  */
 public class ListDefaults {
 
-    private static final String[] LIST_CLASSES = {
-        "java.util.ArrayList",
-        "java.util.LinkedList",
-        "java.util.Vector",
-        "java.util.concurrent.CopyOnWriteArrayList"
+    private static final Supplier<?>[] LIST_CLASSES = {
+        java.util.ArrayList::new,
+        java.util.LinkedList::new,
+        java.util.Vector::new,
+        java.util.concurrent.CopyOnWriteArrayList::new,
+        ExtendsAbstractList::new
     };
 
-    private static final String[] LIST_CME_CLASSES = {
-        "java.util.ArrayList",
-        "java.util.Vector"
+    private static final Supplier<?>[] LIST_CME_CLASSES = {
+        java.util.ArrayList::new,
+        java.util.Vector::new
     };
 
     private static final Predicate<Integer> pEven = x -> 0 == x % 2;
     private static final Predicate<Integer> pOdd = x -> 1 == x % 2;
 

@@ -137,17 +137,13 @@
         }
     }
 
     @Test
     public void testForEach() throws Exception {
-        final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
-        for (final CollectionSupplier.TestCase test : supplier.get()) {
-            final List<Integer> original = ((List<Integer>) test.original);
-            final List<Integer> list = ((List<Integer>) test.collection);
-        }
-        for (final CollectionSupplier.TestCase test : supplier.get()) {
-            final List<Integer> original = ((List<Integer>) test.original);
+        final CollectionSupplier<List<Integer>> supplier = new CollectionSupplier((Supplier<List<Integer>>[])LIST_CLASSES, SIZE);
+        for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) {
+            final List<Integer> original = ((List<Integer>) test.expected);
             final List<Integer> list = ((List<Integer>) test.collection);
 
             try {
                 list.forEach(null);
                 fail("expected NPE not thrown");

@@ -180,14 +176,13 @@
         }
     }
 
     @Test
     public void testRemoveIf() throws Exception {
-        final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
-
-        for (final CollectionSupplier.TestCase test : supplier.get()) {
-            final List<Integer> original = ((List<Integer>) test.original);
+        final CollectionSupplier<List<Integer>> supplier = new CollectionSupplier((Supplier<List<Integer>>[])LIST_CLASSES, SIZE);
+        for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) {
+            final List<Integer> original = ((List<Integer>) test.expected);
             final List<Integer> list = ((List<Integer>) test.collection);
 
             try {
                 list.removeIf(null);
                 fail("expected NPE not thrown");

@@ -199,11 +194,11 @@
                 removeFirst(original, list, offset);
             }
         }
 
         for (final CollectionSupplier.TestCase test : supplier.get()) {
-            final List<Integer> original = ((List<Integer>) test.original);
+            final List<Integer> original = ((List<Integer>) test.expected);
             final List<Integer> list = ((List<Integer>) test.collection);
             list.removeIf(pOdd);
             for (int i : list) {
                 assertTrue((i % 2) == 0);
             }

@@ -215,11 +210,11 @@
             list.removeIf(pEven);
             assertTrue(list.isEmpty());
         }
 
         for (final CollectionSupplier.TestCase test : supplier.get()) {
-            final List<Integer> original = ((List<Integer>) test.original);
+            final List<Integer> original = ((List<Integer>) test.expected);
             final List<Integer> list = ((List<Integer>) test.collection);
             final List<Integer> listCopy = new ArrayList<>(list);
             if (original.size() > SUBLIST_SIZE) {
                 final List<Integer> subList = list.subList(SUBLIST_FROM, SUBLIST_TO);
                 final List<Integer> subListCopy = new ArrayList<>(subList);

@@ -272,13 +267,13 @@
     }
 
     @Test
     public void testReplaceAll() throws Exception {
         final int scale = 3;
-        final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
-        for (final CollectionSupplier.TestCase test : supplier.get()) {
-            final List<Integer> original = ((List<Integer>) test.original);
+        final CollectionSupplier<List<Integer>> supplier = new CollectionSupplier((Supplier<List<Integer>>[])LIST_CLASSES, SIZE);
+        for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) {
+            final List<Integer> original = ((List<Integer>) test.expected);
             final List<Integer> list = ((List<Integer>) test.collection);
 
             try {
                 list.replaceAll(null);
                 fail("expected NPE not thrown");

@@ -327,13 +322,13 @@
         }
     }
 
     @Test
     public void testSort() throws Exception {
-        final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
-        for (final CollectionSupplier.TestCase test : supplier.get()) {
-            final List<Integer> original = ((List<Integer>) test.original);
+        final CollectionSupplier<List<Integer>> supplier = new CollectionSupplier((Supplier<List<Integer>>[])LIST_CLASSES, SIZE);
+        for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) {
+            final List<Integer> original = ((List<Integer>) test.expected);
             final List<Integer> list = ((List<Integer>) test.collection);
             CollectionSupplier.shuffle(list);
             list.sort(Integer::compare);
             CollectionAsserts.assertSorted(list, Integer::compare);
             if (test.name.startsWith("reverse")) {

@@ -376,21 +371,19 @@
                 assertTrue(bitCount >= minBitCount);
                 minBitCount = bitCount;
             }
 
             @SuppressWarnings("unchecked")
-            final Class<? extends List<AtomicInteger>> type =
-                    (Class<? extends List<AtomicInteger>>) Class.forName(test.className);
-            final Constructor<? extends List<AtomicInteger>> defaultConstructor = type.getConstructor();
+            final Constructor<? extends List<?>> defaultConstructor = ((Class<? extends List<?>>)test.collection.getClass()).getConstructor();
             final List<AtomicInteger> incomparables = (List<AtomicInteger>) defaultConstructor.newInstance();
 
-            for (int i=0; i < test.original.size(); i++) {
+            for (int i=0; i < test.expected.size(); i++) {
                 incomparables.add(new AtomicInteger(i));
             }
             CollectionSupplier.shuffle(incomparables);
             incomparables.sort(ATOMIC_INTEGER_COMPARATOR);
-            for (int i=0; i < test.original.size(); i++) {
+            for (int i=0; i < test.expected.size(); i++) {
                 assertEquals(i, incomparables.get(i).intValue());
             }
 
             if (original.size() > SUBLIST_SIZE) {
                 final List<Integer> copy = new ArrayList<>(list);

@@ -425,13 +418,14 @@
         }
     }
 
     @Test
     public void testForEachThrowsCME() throws Exception {
-        final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
-        for (final CollectionSupplier.TestCase test : supplier.get()) {
+        final CollectionSupplier<List<Integer>> supplier = new CollectionSupplier((Supplier<List<Integer>>[])LIST_CME_CLASSES, SIZE);
+        for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) {
             final List<Integer> list = ((List<Integer>) test.collection);
+
             if (list.size() <= 1) {
                 continue;
             }
             boolean gotException = false;
             try {

@@ -446,13 +440,15 @@
         }
     }
 
     @Test
     public void testRemoveIfThrowsCME() throws Exception {
-        final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
-        for (final CollectionSupplier.TestCase test : supplier.get()) {
+        final CollectionSupplier<List<Integer>> supplier = new CollectionSupplier((Supplier<List<Integer>>[])LIST_CME_CLASSES, SIZE);
+        for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) {
+            final List<Integer> original = ((List<Integer>) test.expected);
             final List<Integer> list = ((List<Integer>) test.collection);
+
             if (list.size() <= 1) {
                 continue;
             }
             boolean gotException = false;
             try {

@@ -467,13 +463,14 @@
         }
     }
 
     @Test
     public void testReplaceAllThrowsCME() throws Exception {
-        final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
-        for (final CollectionSupplier.TestCase test : supplier.get()) {
+        final CollectionSupplier<List<Integer>> supplier = new CollectionSupplier((Supplier<List<Integer>>[])LIST_CME_CLASSES, SIZE);
+        for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) {
             final List<Integer> list = ((List<Integer>) test.collection);
+
             if (list.size() <= 1) {
                 continue;
             }
             boolean gotException = false;
             try {

@@ -488,13 +485,14 @@
         }
     }
 
     @Test
     public void testSortThrowsCME() throws Exception {
-        final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
-        for (final CollectionSupplier.TestCase test : supplier.get()) {
+        final CollectionSupplier<List<Integer>> supplier = new CollectionSupplier((Supplier<List<Integer>>[])LIST_CME_CLASSES, SIZE);
+        for (final CollectionSupplier.TestCase<List<Integer>> test : supplier.get()) {
             final List<Integer> list = ((List<Integer>) test.collection);
+
             if (list.size() <= 1) {
                 continue;
             }
             boolean gotException = false;
             try {

@@ -518,10 +516,11 @@
         final List<Object[]> cases = new LinkedList<>();
         cases.add(new Object[] { new ArrayList<>(Arrays.asList(DATA)) });
         cases.add(new Object[] { new LinkedList<>(Arrays.asList(DATA)) });
         cases.add(new Object[] { new Vector<>(Arrays.asList(DATA)) });
         cases.add(new Object[] { new CopyOnWriteArrayList<>(Arrays.asList(DATA)) });
+        cases.add(new Object[] { new ExtendsAbstractList<>(Arrays.asList(DATA)) });
         return cases.toArray(new Object[0][cases.size()]);
     }
 
     @Test(dataProvider = "shortIntListProvider")
     public void testRemoveIfFromSlice(final List<Integer> list) throws Exception {