test/java/util/List/ListDefaults.java

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

*** 26,37 **** 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; --- 26,35 ----
*** 44,74 **** import static org.testng.Assert.fail; import java.lang.reflect.Constructor; import java.util.ConcurrentModificationException; import java.util.function.Predicate; /** * @test * @bug 8023367 ! * @library testlibrary ! * @build CollectionAsserts CollectionSupplier * @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 String[] LIST_CME_CLASSES = { ! "java.util.ArrayList", ! "java.util.Vector" }; private static final Predicate<Integer> pEven = x -> 0 == x % 2; private static final Predicate<Integer> pOdd = x -> 1 == x % 2; --- 42,74 ---- 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 ../Collection/testlibrary ! * @build CollectionAsserts CollectionSupplier ExtendsAbstractList * @run testng ListDefaults */ public class ListDefaults { ! 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 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,153 **** } } @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 List<Integer> list = ((List<Integer>) test.collection); try { list.forEach(null); fail("expected NPE not thrown"); --- 137,149 ---- } } @Test public void testForEach() throws Exception { ! 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,193 **** } } @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 List<Integer> list = ((List<Integer>) test.collection); try { list.removeIf(null); fail("expected NPE not thrown"); --- 176,188 ---- } } @Test public void testRemoveIf() throws Exception { ! 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,209 **** removeFirst(original, list, offset); } } for (final CollectionSupplier.TestCase test : supplier.get()) { ! final List<Integer> original = ((List<Integer>) test.original); final List<Integer> list = ((List<Integer>) test.collection); list.removeIf(pOdd); for (int i : list) { assertTrue((i % 2) == 0); } --- 194,204 ---- removeFirst(original, list, offset); } } for (final CollectionSupplier.TestCase test : supplier.get()) { ! 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,225 **** list.removeIf(pEven); assertTrue(list.isEmpty()); } for (final CollectionSupplier.TestCase test : supplier.get()) { ! final List<Integer> original = ((List<Integer>) test.original); 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); --- 210,220 ---- list.removeIf(pEven); assertTrue(list.isEmpty()); } for (final CollectionSupplier.TestCase test : supplier.get()) { ! 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,284 **** } @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 List<Integer> list = ((List<Integer>) test.collection); try { list.replaceAll(null); fail("expected NPE not thrown"); --- 267,279 ---- } @Test public void testReplaceAll() throws Exception { final int scale = 3; ! 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,339 **** } } @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 List<Integer> list = ((List<Integer>) test.collection); CollectionSupplier.shuffle(list); list.sort(Integer::compare); CollectionAsserts.assertSorted(list, Integer::compare); if (test.name.startsWith("reverse")) { --- 322,334 ---- } } @Test public void testSort() throws Exception { ! 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,396 **** 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 List<AtomicInteger> incomparables = (List<AtomicInteger>) defaultConstructor.newInstance(); ! for (int i=0; i < test.original.size(); i++) { incomparables.add(new AtomicInteger(i)); } CollectionSupplier.shuffle(incomparables); incomparables.sort(ATOMIC_INTEGER_COMPARATOR); ! for (int i=0; i < test.original.size(); i++) { assertEquals(i, incomparables.get(i).intValue()); } if (original.size() > SUBLIST_SIZE) { final List<Integer> copy = new ArrayList<>(list); --- 371,389 ---- assertTrue(bitCount >= minBitCount); minBitCount = bitCount; } @SuppressWarnings("unchecked") ! 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.expected.size(); i++) { incomparables.add(new AtomicInteger(i)); } CollectionSupplier.shuffle(incomparables); incomparables.sort(ATOMIC_INTEGER_COMPARATOR); ! 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,437 **** } } @Test public void testForEachThrowsCME() throws Exception { ! final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE); ! for (final CollectionSupplier.TestCase test : supplier.get()) { final List<Integer> list = ((List<Integer>) test.collection); if (list.size() <= 1) { continue; } boolean gotException = false; try { --- 418,431 ---- } } @Test public void testForEachThrowsCME() throws Exception { ! 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,458 **** } } @Test public void testRemoveIfThrowsCME() throws Exception { ! final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE); ! for (final CollectionSupplier.TestCase test : supplier.get()) { final List<Integer> list = ((List<Integer>) test.collection); if (list.size() <= 1) { continue; } boolean gotException = false; try { --- 440,454 ---- } } @Test public void testRemoveIfThrowsCME() throws Exception { ! 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,479 **** } } @Test public void testReplaceAllThrowsCME() throws Exception { ! final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE); ! for (final CollectionSupplier.TestCase test : supplier.get()) { final List<Integer> list = ((List<Integer>) test.collection); if (list.size() <= 1) { continue; } boolean gotException = false; try { --- 463,476 ---- } } @Test public void testReplaceAllThrowsCME() throws Exception { ! 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,500 **** } } @Test public void testSortThrowsCME() throws Exception { ! final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE); ! for (final CollectionSupplier.TestCase test : supplier.get()) { final List<Integer> list = ((List<Integer>) test.collection); if (list.size() <= 1) { continue; } boolean gotException = false; try { --- 485,498 ---- } } @Test public void testSortThrowsCME() throws Exception { ! 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,527 **** --- 516,526 ---- 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 {