From 17bed2b5551c6489264d47e1009f45c27236f71c Mon Sep 17 00:00:00 2001 From: Angus Date: Sat, 24 Sep 2022 21:32:53 +0100 Subject: [PATCH] [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