[COLLECTIONS-814] CollectionUtils.removeAll() not throwing proper NullPointerException(NPE) if the first parameter is empty

Add null check
This commit is contained in:
Angus 2022-09-24 21:32:53 +01:00 committed by Bruno P. Kinoshita
parent 06cc4554b5
commit 17bed2b555
2 changed files with 8 additions and 0 deletions

View File

@ -533,6 +533,8 @@ public class ListUtils {
* @since 3.2 * @since 3.2
*/ */
public static <E> List<E> removeAll(final Collection<E> collection, final Collection<?> remove) { public static <E> List<E> removeAll(final Collection<E> collection, final Collection<?> remove) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(remove, "remove");
final List<E> list = new ArrayList<>(); final List<E> list = new ArrayList<>();
for (final E obj : collection) { for (final E obj : collection) {
if (!remove.contains(obj)) { if (!remove.contains(obj)) {

View File

@ -369,6 +369,12 @@ public class ListUtilsTest {
assertThrows(NullPointerException.class, () -> ListUtils.removeAll(null, null), assertThrows(NullPointerException.class, () -> ListUtils.removeAll(null, null),
"expecting NullPointerException"); "expecting NullPointerException");
assertThrows(NullPointerException.class, () -> ListUtils.removeAll(null, new ArrayList<Object>()),
"expecting NullPointerException");
assertThrows(NullPointerException.class, () -> ListUtils.removeAll(new ArrayList<Object>(), null),
"expecting NullPointerException");
} }
@Test @Test