Skip to content

SPR-7925 Introduce Assert.noNullElements for collections #1565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions spring-core/src/main/java/org/springframework/util/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,49 @@ public static void notEmpty(@Nullable Collection<?> collection) {
"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
}

/**
* Assert that a collection contains no {@code null} elements.
* <p>Note: Does not complain if the collection is empty!
* <pre class="code">
* Assert.noNullElements(collection, "The collection must contain non-null elements");
* </pre>
* @param collection the collection to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the collection contains a {@code null} element
* @since 5.0
*/
public static void noNullElements(@Nullable Collection<?> collection, String message) {
if (collection != null) {
for (Object element : collection) {
if (element == null) {
throw new IllegalArgumentException(message);
}
}
}
}

/**
* Assert that a collection contains no {@code null} elements.
* <p>Note: Does not complain if the collection is empty!
* <pre class="code">
* Assert.noNullElements(collection, () -&gt; "The " + collectionType + " collection must contain non-null elements");
* </pre>
* @param collection the collection to check
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if the collection contains a {@code null} element
* @since 5.0
*/
public static void noNullElements(@Nullable Collection<?> collection, Supplier<String> messageSupplier) {
if (collection != null) {
for (Object element : collection) {
if (element == null) {
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
}
}
}
}

/**
* Assert that a Map contains entries; that is, it must not be {@code null}
* and must contain at least one entry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static java.util.Arrays.*;
import static java.util.Collections.*;
import static org.hamcrest.CoreMatchers.*;

Expand Down Expand Up @@ -450,6 +451,57 @@ public void notEmptyCollectionWithEmptyCollectionAndNullMessageSupplier() {
Assert.notEmpty(emptyList(), (Supplier<String>) null);
}

@Test
public void noNullElementsWithCollection() {
Assert.noNullElements(asList("foo", "bar"), "enigma");
}

@Test
public void noNullElementsWithEmptyCollection() {
Assert.noNullElements(emptyList(), "enigma");
}

@Test
public void noNullElementsWithNullCollection() {
Assert.noNullElements((Collection<Object>) null, "enigma");
}

@Test
public void noNullElementsWithCollectionAndNullElement() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("enigma");
Assert.noNullElements(asList("foo", null, "bar"), "enigma");
}

@Test
public void noNullElementsWithCollectionAndMessageSupplier() {
Assert.noNullElements(asList("foo", "bar"), () -> "enigma");
}

@Test
public void noNullElementsWithEmptyCollectionAndMessageSupplier() {
Assert.noNullElements(emptyList(), "enigma");
}

@Test
public void noNullElementsWithNullCollectionAndMessageSupplier() {
Assert.noNullElements((Collection<Object>) null, () -> "enigma");
}

@Test
public void noNullElementsWithCollectionAndNullElementAndMessageSupplier() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("enigma");
Assert.noNullElements(asList("foo", null, "bar"), () -> "enigma");
}

@Test
public void noNullElementsWithCollectionAndNullElementAndNullMessageSupplier() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(equalTo(null));
Assert.noNullElements(asList("foo", null, "bar"), (Supplier<String>) null);
}

@Test
public void notEmptyMap() {
Assert.notEmpty(singletonMap("foo", "bar"), "enigma");
Expand Down