From 17bed2b5551c6489264d47e1009f45c27236f71c Mon Sep 17 00:00:00 2001 From: Angus Date: Sat, 24 Sep 2022 21:32:53 +0100 Subject: [PATCH 1/2] [COLLECTIONS-814] CollectionUtils.removeAll() not throwing proper NullPointerException(NPE) if the first parameter is empty Add null check --- .../java/org/apache/commons/collections4/ListUtils.java | 2 ++ .../java/org/apache/commons/collections4/ListUtilsTest.java | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/org/apache/commons/collections4/ListUtils.java b/src/main/java/org/apache/commons/collections4/ListUtils.java index e62a72f08..274e2b50a 100644 --- a/src/main/java/org/apache/commons/collections4/ListUtils.java +++ b/src/main/java/org/apache/commons/collections4/ListUtils.java @@ -533,6 +533,8 @@ public class ListUtils { * @since 3.2 */ public static List removeAll(final Collection collection, final Collection remove) { + Objects.requireNonNull(collection, "collection"); + Objects.requireNonNull(remove, "remove"); final List list = new ArrayList<>(); for (final E obj : collection) { if (!remove.contains(obj)) { diff --git a/src/test/java/org/apache/commons/collections4/ListUtilsTest.java b/src/test/java/org/apache/commons/collections4/ListUtilsTest.java index b22f177ef..12d552584 100644 --- a/src/test/java/org/apache/commons/collections4/ListUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/ListUtilsTest.java @@ -369,6 +369,12 @@ public class ListUtilsTest { assertThrows(NullPointerException.class, () -> ListUtils.removeAll(null, null), "expecting NullPointerException"); + + assertThrows(NullPointerException.class, () -> ListUtils.removeAll(null, new ArrayList()), + "expecting NullPointerException"); + + assertThrows(NullPointerException.class, () -> ListUtils.removeAll(new ArrayList(), null), + "expecting NullPointerException"); } @Test From 8c91a515467826ab1b75b516cea7c6a3303e590d Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Wed, 19 Oct 2022 15:09:21 +1300 Subject: [PATCH 2/2] [COLLECTIONS-814] Add change log --- src/changes/changes.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 096e732c8..f30cdb9e6 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -98,7 +98,10 @@ Fix flaky EmptyPropertiesTest#testSave. - Use java.lang.Objects#equals; eliminate a couple of nulls #307 + Use java.lang.Objects#equals; eliminate a couple of nulls #307. + + + CollectionUtils.removeAll() not throwing NPE #340.