From 71b9866deb3b8a3fb4026a45ab824a5b666c0eee Mon Sep 17 00:00:00 2001 From: Dmytro Kovalchuk Date: Sat, 11 Nov 2023 11:34:08 +0200 Subject: [PATCH] fix(deps): support predicates in arguments checks --- .../net/anatolich/iris/util/Arguments.java | 23 ++++-- .../anatolich/iris/util/ArgumentsTest.java | 70 +++++++++++++++++++ 2 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 src/test/java/net/anatolich/iris/util/ArgumentsTest.java diff --git a/src/main/java/net/anatolich/iris/util/Arguments.java b/src/main/java/net/anatolich/iris/util/Arguments.java index fabe119..5818d1b 100644 --- a/src/main/java/net/anatolich/iris/util/Arguments.java +++ b/src/main/java/net/anatolich/iris/util/Arguments.java @@ -1,20 +1,33 @@ package net.anatolich.iris.util; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import java.util.Objects; +import java.util.function.Predicate; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class Arguments { - public static void checkArgument(boolean correct, String message) { + public static T assertArgument(T argument, boolean correct, String message) { if (!correct) { throw new IllegalArgumentException(message); } + return argument; } - public static T rejectNull(T argument, String message) { - checkArgument(argument != null, message); + public static T assertArgument(T argument, Predicate correct, String message) { + if (!correct.test(argument)) { + throw new IllegalArgumentException(message); + } return argument; } + public static T rejectNull(T argument, String message) { + return assertArgument(argument, Objects::nonNull, message); + } + public static String rejectEmptyString(String argument, String message) { - checkArgument(argument != null && !argument.isBlank(), message); - return argument; + return assertArgument(argument, argument != null && !argument.isBlank(), message); } } diff --git a/src/test/java/net/anatolich/iris/util/ArgumentsTest.java b/src/test/java/net/anatolich/iris/util/ArgumentsTest.java new file mode 100644 index 0000000..c86c839 --- /dev/null +++ b/src/test/java/net/anatolich/iris/util/ArgumentsTest.java @@ -0,0 +1,70 @@ +package net.anatolich.iris.util; + + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +class ArgumentsTest { + + public static final String ERROR_MESSAGE = "error message"; + + @Test + @DisplayName("check assertion") + void baseAssertionCheck() { + Object argument = "argument"; + assertThatNoException() + .isThrownBy(() -> Arguments.assertArgument(argument, true, ERROR_MESSAGE)); + + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> Arguments.assertArgument(argument, false, ERROR_MESSAGE)); + } + + @Test + @DisplayName("reject null values") + void rejectNullValues() { + Object passing = "passing"; + Object failing = null; + + assertThatNoException().isThrownBy(() -> Arguments.rejectNull(passing, ERROR_MESSAGE)); + assertThat(Arguments.rejectNull(passing, ERROR_MESSAGE)) + .isEqualTo(passing); + + assertThatExceptionOfType(IllegalArgumentException.class) + .as("null values must be rejected") + .isThrownBy(() -> Arguments.rejectNull(failing, ERROR_MESSAGE)); + } + + @Test + @DisplayName("reject null strings") + void rejectNullStrings() { + assertThatExceptionOfType(IllegalArgumentException.class) + .as("null strings must be rejected") + .isThrownBy(() -> Arguments.rejectEmptyString(null, ERROR_MESSAGE)); + } + + @Test + @DisplayName("reject empty strings") + void rejectEmptyStrings() { + assertThatExceptionOfType(IllegalArgumentException.class) + .as("empty strings must be rejected") + .isThrownBy(() -> Arguments.rejectEmptyString("", ERROR_MESSAGE)); + } + + @Test + @DisplayName("reject blank strings") + void rejectBlankStrings() { + assertThatExceptionOfType(IllegalArgumentException.class) + .as("blank strings must be rejected") + .isThrownBy(() -> Arguments.rejectEmptyString(" ", ERROR_MESSAGE)); + } + + @Test + @DisplayName("accept correct strings") + void acceptCorrectStrings() { + assertThatNoException() + .as("correct strings must be accepted") + .isThrownBy(() -> Arguments.rejectEmptyString("name", ERROR_MESSAGE)); + } +} \ No newline at end of file