-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(deps): support predicates in arguments checks
- Loading branch information
Showing
2 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> T assertArgument(T argument, boolean correct, String message) { | ||
if (!correct) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
return argument; | ||
} | ||
|
||
public static <T> T rejectNull(T argument, String message) { | ||
checkArgument(argument != null, message); | ||
public static <T> T assertArgument(T argument, Predicate<? super T> correct, String message) { | ||
if (!correct.test(argument)) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
return argument; | ||
} | ||
|
||
public static <T> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |