Skip to content

Commit

Permalink
Add checkArgumentIsNull and checkArgumentIsBlank to KiwiPreconditions
Browse files Browse the repository at this point in the history
Closes #745
Closes #746
  • Loading branch information
sleberknight committed Jul 10, 2022
1 parent 0c630d8 commit f62309a
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/main/java/org/kiwiproject/base/KiwiPreconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,46 @@ public static <T> void checkArgumentNotNull(T reference, String errorMessageTemp
}
}

/**
* Ensures that an object reference passed as a parameter to the calling method is null, throwing
* an {@link IllegalArgumentException} if not null.
*
* @param reference an object reference
* @param <T> the object type
*/
public static <T> void checkArgumentIsNull(T reference) {
Preconditions.checkArgument(isNull(reference));
}

/**
* Ensures that an object reference passed as a parameter to the calling method is null, throwing
* an {@link IllegalArgumentException} if not null.
*
* @param reference an object reference
* @param errorMessage the error message for the exception
* @param <T> the object type
*/
public static <T> void checkArgumentIsNull(T reference, String errorMessage) {
Preconditions.checkArgument(isNull(reference), errorMessage);
}

/**
* Ensures that an object reference passed as a parameter to the calling method is null, throwing
* an {@link IllegalArgumentException} if not null.
*
* @param reference an object reference
* @param errorMessageTemplate a template for the exception message should the check fail, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to be substituted into the message template. Arguments
* are converted to Strings using {@link String#valueOf(Object)}.
* @param <T> the object type
*/
public static <T> void checkArgumentIsNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) {
if (nonNull(reference)) {
throw newIllegalArgumentException(errorMessageTemplate, errorMessageArgs);
}
}

private static IllegalArgumentException newIllegalArgumentException(String errorMessageTemplate,
Object... errorMessageArgs) {
var errorMessage = format(errorMessageTemplate, errorMessageArgs);
Expand Down Expand Up @@ -255,6 +295,43 @@ public static void checkArgumentNotBlank(String string, String errorMessageTempl
}
}

/**
* Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing
* an {@link IllegalArgumentException} if it is not null, empty, or blank.
*
* @param string a string
*/
public static void checkArgumentIsBlank(String string) {
Preconditions.checkArgument(isBlank(string));
}

/**
* Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing
* an {@link IllegalArgumentException} if it is not null, empty, or blank.
*
* @param string a string
* @param errorMessage the error message for the exception
*/
public static void checkArgumentIsBlank(String string, String errorMessage) {
Preconditions.checkArgument(isBlank(string), errorMessage);
}

/**
* Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing
* an {@link IllegalArgumentException} if it is not null, empty, or blank.
*
* @param string a string
* @param errorMessageTemplate a template for the exception message should the check fail, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to be substituted into the message template. Arguments
* are converted to Strings using {@link String#valueOf(Object)}.
*/
public static void checkArgumentIsBlank(String string, String errorMessageTemplate, Object... errorMessageArgs) {
if (isNotBlank(string)) {
throw newIllegalArgumentException(errorMessageTemplate, errorMessageArgs);
}
}

/**
* Ensures that a collection of items has an even count, throwing an {@link IllegalArgumentException} if
* items is null or there is an odd number of items.
Expand Down
121 changes: 121 additions & 0 deletions src/test/java/org/kiwiproject/base/KiwiPreconditionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.util.Sets.newLinkedHashSet;
Expand All @@ -24,6 +25,7 @@
import org.junit.jupiter.params.provider.ValueSource;
import org.kiwiproject.util.BlankStringArgumentsProvider;

import java.math.BigInteger;
import java.util.ArrayList;

@DisplayName("KiwiPreconditions")
Expand Down Expand Up @@ -191,6 +193,125 @@ void testCheckArgumentNotNull_MessageWithTemplate(SoftAssertions softly) {
softly.assertThat(catchThrowable(() -> checkArgumentNotNull(new Object(), errorMessageTemplate, errorMessageArgs))).isNull();
}

@Nested
class CheckArgumentIsNull {

@Nested
class WithNoMessage {

@Test
void shouldNotThrow_WhenArgumentIsNull() {
assertThatCode(() -> KiwiPreconditions.checkArgumentIsNull(null))
.doesNotThrowAnyException();
}

@Test
void shouldThrowIllegalArgument_WhenArgumentIsNotNull() {
assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiPreconditions.checkArgumentIsNull(new Object()));
}
}

@Nested
class WithMessage {

@Test
void shouldNotThrow_WhenArgumentIsNull() {
assertThatCode(() -> KiwiPreconditions.checkArgumentIsNull(null, "the argument cannot be null"))
.doesNotThrowAnyException();
}

@Test
void shouldThrowIllegalArgument_WhenArgumentIsNotNull() {
var errorMessage = "the argument cannot be null";

assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiPreconditions.checkArgumentIsNull("foo", errorMessage))
.withMessage(errorMessage);
}
}

@Nested
class WithTemplateMessage {

@Test
void shouldNotThrow_WhenArgumentIsNull() {
assertThatCode(() ->
KiwiPreconditions.checkArgumentIsNull(null, "{} cannot be null", "foo"))
.doesNotThrowAnyException();
}

@Test
void shouldThrowIllegalArgument_WhenArgumentIsNotNull() {
assertThatIllegalArgumentException()
.isThrownBy(() ->
KiwiPreconditions.checkArgumentIsNull(BigInteger.ONE, "{} cannot be null (code: {})", "bar", 84))
.withMessage("bar cannot be null (code: 84)");
}
}
}

@Nested
class CheckArgumentIsBlank {

@Nested
class WithNoMessage {

@ParameterizedTest
@ArgumentsSource(BlankStringArgumentsProvider.class)
void shouldNotThrow_WhenArgumentIsBlank(String string) {
assertThatCode(() -> KiwiPreconditions.checkArgumentIsBlank(string))
.doesNotThrowAnyException();
}

@Test
void shouldThrowIllegalArgument_WhenArgumentIsNotBlank() {
assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiPreconditions.checkArgumentIsBlank("hello, world"));
}
}

@Nested
class WithMessage {

@ParameterizedTest
@ArgumentsSource(BlankStringArgumentsProvider.class)
void shouldNotThrow_WhenArgumentIsBlank(String string) {
assertThatCode(() -> KiwiPreconditions.checkArgumentIsBlank(string, "the argument cannot be blank"))
.doesNotThrowAnyException();
}

@Test
void shouldThrowIllegalArgument_WhenArgumentIsNotBlank() {
var errorMessage = "the argument cannot be blank";

assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiPreconditions.checkArgumentIsBlank("bar", errorMessage))
.withMessage(errorMessage);
}
}

@Nested
class WithTemplateMessage {

@ParameterizedTest
@ArgumentsSource(BlankStringArgumentsProvider.class)
void shouldNotThrow_WhenArgumentIsBlank(String string) {
assertThatCode(() ->
KiwiPreconditions.checkArgumentIsBlank(string, "{} cannot be blank", "foo"))
.doesNotThrowAnyException();
}

@Test
void shouldThrowIllegalArgument_WhenArgumentIsNotBlank() {
assertThatIllegalArgumentException()
.isThrownBy(() ->
KiwiPreconditions.checkArgumentIsBlank("a non-blank value", "{} cannot be blank (code: {})", "bar", 84))
.withMessage("bar cannot be blank (code: 84)");
}
}
}

@SuppressWarnings("unused")
static class SomeCheckedException extends Exception {

Expand Down

0 comments on commit f62309a

Please sign in to comment.