From 3d80f43033b646291e9fe80380b38adf503ec3a9 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Mon, 11 Jul 2022 17:42:29 -0400 Subject: [PATCH] Add checkArgumentIsNull and checkArgumentIsBlank to KiwiPreconditions (#747) Closes #745 Closes #746 --- .../kiwiproject/base/KiwiPreconditions.java | 77 +++++++++++ .../base/KiwiPreconditionsTest.java | 121 ++++++++++++++++++ 2 files changed, 198 insertions(+) diff --git a/src/main/java/org/kiwiproject/base/KiwiPreconditions.java b/src/main/java/org/kiwiproject/base/KiwiPreconditions.java index 9893a814..2f3a5566 100644 --- a/src/main/java/org/kiwiproject/base/KiwiPreconditions.java +++ b/src/main/java/org/kiwiproject/base/KiwiPreconditions.java @@ -212,6 +212,46 @@ public static 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 the object type + */ + public static 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 the object type + */ + public static 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 the object type + */ + public static 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); @@ -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. diff --git a/src/test/java/org/kiwiproject/base/KiwiPreconditionsTest.java b/src/test/java/org/kiwiproject/base/KiwiPreconditionsTest.java index d6ddd3ec..b1671f75 100644 --- a/src/test/java/org/kiwiproject/base/KiwiPreconditionsTest.java +++ b/src/test/java/org/kiwiproject/base/KiwiPreconditionsTest.java @@ -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; @@ -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") @@ -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 {