diff --git a/src/main/java/org/kiwiproject/base/KiwiPreconditions.java b/src/main/java/org/kiwiproject/base/KiwiPreconditions.java index 5d85098a..9faf57c6 100644 --- a/src/main/java/org/kiwiproject/base/KiwiPreconditions.java +++ b/src/main/java/org/kiwiproject/base/KiwiPreconditions.java @@ -336,7 +336,32 @@ public static T requireNotNullElseGet(T obj, Supplier supplier) * @see Preconditions#checkState(boolean, Object) */ public static void checkPositive(int value) { - checkState(value > 0, "value must be a positive number"); + checkPositive(value, "value must be a positive number"); + } + + /** + * Ensures int {@code value} is a positive number (greater than zero). + * + * @param value the value to check for positivity + * @param errorMessage the error message to put in the exception if not positive + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static void checkPositive(int value, String errorMessage) { + checkState(value > 0, errorMessage); + } + + /** + * Ensures int {@code value} is a positive number (greater than zero). + * + * @param value the value to check for positivity + * @param errorMessageTemplate the error message template to use in the exception if not positive + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static void checkPositive(int value, String errorMessageTemplate, Object... errorMessageArgs) { + checkState(value > 0, errorMessageTemplate, errorMessageArgs); } /** @@ -347,7 +372,32 @@ public static void checkPositive(int value) { * @see Preconditions#checkState(boolean, Object) */ public static void checkPositive(long value) { - checkState(value > 0, "value must be a positive number"); + checkPositive(value, "value must be a positive number"); + } + + /** + * Ensures long {@code value} is a positive number (greater than zero). + * + * @param value the value to check for positivity + * @param errorMessage the error message to put in the exception if not positive + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static void checkPositive(long value, String errorMessage) { + checkState(value > 0, errorMessage); + } + + /** + * Ensures long {@code value} is a positive number (greater than zero). + * + * @param value the value to check for positivity + * @param errorMessageTemplate the error message template to use in the exception if not positive + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static void checkPositive(long value, String errorMessageTemplate, Object... errorMessageArgs) { + checkState(value > 0, errorMessageTemplate, errorMessageArgs); } /** @@ -358,7 +408,32 @@ public static void checkPositive(long value) { * @see Preconditions#checkState(boolean, Object) */ public static void checkPositiveOrZero(int value) { - checkState(value >= 0, "value must be positive or zero"); + checkPositiveOrZero(value, "value must be positive or zero"); + } + + /** + * Ensures int {@code value} is a positive number (greater than zero) or zero. + * + * @param value the value to check for positivity + * @param errorMessage the error message to put in the exception if not positive + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static void checkPositiveOrZero(int value, String errorMessage) { + checkState(value >= 0, errorMessage); + } + + /** + * Ensures int {@code value} is a positive number (greater than zero) or zero. + * + * @param value the value to check for positivity + * @param errorMessageTemplate the error message template to use in the exception if not positive + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static void checkPositiveOrZero(int value, String errorMessageTemplate, Object... errorMessageArgs) { + checkState(value >= 0, errorMessageTemplate, errorMessageArgs); } /** @@ -369,7 +444,32 @@ public static void checkPositiveOrZero(int value) { * @see Preconditions#checkState(boolean, Object) */ public static void checkPositiveOrZero(long value) { - checkState(value >= 0, "value must be positive or zero"); + checkPositiveOrZero(value, "value must be positive or zero"); + } + + /** + * Ensures long {@code value} is a positive number (greater than zero) or zero. + * + * @param value the value to check for positivity + * @param errorMessage the error message to put in the exception if not positive + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static void checkPositiveOrZero(long value, String errorMessage) { + checkState(value >= 0, errorMessage); + } + + /** + * Ensures long {@code value} is a positive number (greater than zero) or zero. + * + * @param value the value to check for positivity + * @param errorMessageTemplate the error message template to use in the exception if not positive + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static void checkPositiveOrZero(long value, String errorMessageTemplate, Object... errorMessageArgs) { + checkState(value >= 0, errorMessageTemplate, errorMessageArgs); } /** @@ -384,6 +484,33 @@ public static int requirePositive(int value) { return value; } + /** + * Returns the int {@code value} if it is a positive number (greater than zero), throwing an {@link IllegalStateException} if not positive. + * + * @param value the value to check for positivity + * @param errorMessage the error message to put in the exception if not positive + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static int requirePositive(int value, String errorMessage) { + checkPositive(value, errorMessage); + return value; + } + + /** + * Returns the int {@code value} if it is a positive number (greater than zero), throwing an {@link IllegalStateException} if not positive. + * + * @param value the value to check for positivity + * @param errorMessageTemplate the error message template to use in the exception if not positive + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static int requirePositive(int value, String errorMessageTemplate, Object... errorMessageArgs) { + checkPositive(value, errorMessageTemplate, errorMessageArgs); + return value; + } + /** * Returns the long value if it is positive, throwing an {@link IllegalStateException} if not positive. * @@ -396,6 +523,33 @@ public static long requirePositive(long value) { return value; } + /** + * Returns the long {@code value} if it is a positive number (greater than zero), throwing an {@link IllegalStateException} if not positive. + * + * @param value the value to check for positivity + * @param errorMessage the error message to put in the exception if not positive + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static long requirePositive(long value, String errorMessage) { + checkPositive(value, errorMessage); + return value; + } + + /** + * Returns the long {@code value} if it is a positive number (greater than zero), throwing an {@link IllegalStateException} if not positive. + * + * @param value the value to check for positivity + * @param errorMessageTemplate the error message template to use in the exception if not positive + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static long requirePositive(long value, String errorMessageTemplate, Object... errorMessageArgs) { + checkPositive(value, errorMessageTemplate, errorMessageArgs); + return value; + } + /** * Returns the int value if it is positive or zero, throwing an {@link IllegalStateException} if not positive or zero. * @@ -408,6 +562,33 @@ public static int requirePositiveOrZero(int value) { return value; } + /** + * Returns the int {@code value} if it is a positive number (greater than zero) or zero, throwing an {@link IllegalStateException} if not positive. + * + * @param value the value to check for positivity + * @param errorMessage the error message to put in the exception if not positive + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static int requirePositiveOrZero(int value, String errorMessage) { + checkPositiveOrZero(value, errorMessage); + return value; + } + + /** + * Returns the int {@code value} if it is a positive number (greater than zero) or zero, throwing an {@link IllegalStateException} if not positive. + * + * @param value the value to check for positivity + * @param errorMessageTemplate the error message template to use in the exception if not positive + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static int requirePositiveOrZero(int value, String errorMessageTemplate, Object... errorMessageArgs) { + checkPositiveOrZero(value, errorMessageTemplate, errorMessageArgs); + return value; + } + /** * Returns the long value if it is positive or zero, throwing an {@link IllegalStateException} if not positive or zero. * @@ -420,6 +601,33 @@ public static long requirePositiveOrZero(long value) { return value; } + /** + * Returns the long {@code value} if it is a positive number (greater than zero) or zero, throwing an {@link IllegalStateException} if not positive. + * + * @param value the value to check for positivity + * @param errorMessage the error message to put in the exception if not positive + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static long requirePositiveOrZero(long value, String errorMessage) { + checkPositiveOrZero(value, errorMessage); + return value; + } + + /** + * Returns the long {@code value} if it is a positive number (greater than zero) or zero, throwing an {@link IllegalStateException} if not positive. + * + * @param value the value to check for positivity + * @param errorMessageTemplate the error message template to use in the exception if not positive + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if the value is not positive (e.g. greater than zero) + * @see Preconditions#checkState(boolean, Object) + */ + public static long requirePositiveOrZero(long value, String errorMessageTemplate, Object... errorMessageArgs) { + checkPositiveOrZero(value, errorMessageTemplate, errorMessageArgs); + return value; + } + /** * Ensures given port is valid, between 0 and {@link #MAX_PORT_NUMBER}. * @@ -427,7 +635,30 @@ public static long requirePositiveOrZero(long value) { * @throws IllegalStateException if port is not valid */ public static void checkValidPort(int port) { - checkState(port >= 0 && port <= MAX_PORT_NUMBER, "port must be between 0 and %s", MAX_PORT_NUMBER); + checkValidPort(port, "port must be between 0 and %s", MAX_PORT_NUMBER); + } + + /** + * Ensures given port is valid, between 0 and {@link #MAX_PORT_NUMBER}. + * + * @param port the port to check for validity + * @param errorMessage the error message to put in the exception if the port is not valid + * @throws IllegalStateException if port is not valid + */ + public static void checkValidPort(int port, String errorMessage) { + checkState(port >= 0 && port <= MAX_PORT_NUMBER, errorMessage); + } + + /** + * Ensures given port is valid, between 0 and {@link #MAX_PORT_NUMBER}. + * + * @param port the port to check for validity + * @param errorMessageTemplate the error message template to use in the exception if port is not valid + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if port is not valid + */ + public static void checkValidPort(int port, String errorMessageTemplate, Object... errorMessageArgs) { + checkState(port >= 0 && port <= MAX_PORT_NUMBER, errorMessageTemplate, errorMessageArgs); } /** @@ -442,6 +673,31 @@ public static int requireValidPort(int port) { return port; } + /** + * Returns the given port if it is valid + * + * @param port the port to check for validity + * @param errorMessage the error message to put in the exception if the port is not valid + * @throws IllegalStateException if port is not valid + */ + public static int requireValidPort(int port, String errorMessage) { + checkValidPort(port, errorMessage); + return port; + } + + /** + * Returns the given port if it is valid + * + * @param port the port to check for validity + * @param errorMessageTemplate the error message template to use in the exception if port is not valid + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if port is not valid + */ + public static int requireValidPort(int port, String errorMessageTemplate, Object... errorMessageArgs) { + checkValidPort(port, errorMessageTemplate, errorMessageArgs); + return port; + } + /** * Ensures given port is valid (excluding zero), between 1 and {@link #MAX_PORT_NUMBER}. * @@ -449,7 +705,30 @@ public static int requireValidPort(int port) { * @throws IllegalStateException if port is not valid */ public static void checkValidNonZeroPort(int port) { - checkState(port > 0 && port <= MAX_PORT_NUMBER, "port must be between 1 and %s", MAX_PORT_NUMBER); + checkValidNonZeroPort(port, "port must be between 1 and %s", MAX_PORT_NUMBER); + } + + /** + * Ensures given port is valid (excluding zero), between 1 and {@link #MAX_PORT_NUMBER}. + * + * @param port the port to check for validity + * @param errorMessage the error message to put in the exception if the port is not valid + * @throws IllegalStateException if port is not valid + */ + public static void checkValidNonZeroPort(int port, String errorMessage) { + checkState(port > 0 && port <= MAX_PORT_NUMBER, errorMessage); + } + + /** + * Ensures given port is valid (excluding zero), between 1 and {@link #MAX_PORT_NUMBER}. + * + * @param port the port to check for validity + * @param errorMessageTemplate the error message template to use in the exception if port is not valid + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if port is not valid + */ + public static void checkValidNonZeroPort(int port, String errorMessageTemplate, Object... errorMessageArgs) { + checkState(port > 0 && port <= MAX_PORT_NUMBER, errorMessageTemplate, errorMessageArgs); } /** @@ -464,4 +743,29 @@ public static int requireValidNonZeroPort(int port) { return port; } + /** + * Returns the given port if it is valid (excluding zero) + * + * @param port the port to check for validity + * @param errorMessage the error message to put in the exception if the port is not valid + * @throws IllegalStateException if port is not valid + */ + public static int requireValidNonZeroPort(int port, String errorMessage) { + checkValidNonZeroPort(port, errorMessage); + return port; + } + + /** + * Returns the given port if it is valid (excluding zero) + * + * @param port the port to check for validity + * @param errorMessageTemplate the error message template to use in the exception if port is not valid + * @param errorMessageArgs the arguments to populate into the error message template + * @throws IllegalStateException if port is not valid + */ + public static int requireValidNonZeroPort(int port, String errorMessageTemplate, Object... errorMessageArgs) { + checkValidNonZeroPort(port, errorMessageTemplate, errorMessageArgs); + return port; + } + } diff --git a/src/test/java/org/kiwiproject/base/KiwiPreconditionsTest.java b/src/test/java/org/kiwiproject/base/KiwiPreconditionsTest.java index cf5977bd..cf81eb08 100644 --- a/src/test/java/org/kiwiproject/base/KiwiPreconditionsTest.java +++ b/src/test/java/org/kiwiproject/base/KiwiPreconditionsTest.java @@ -342,11 +342,35 @@ void shouldThrowException_WhenIntValue_IsZero() { .hasMessage("value must be a positive number"); } + @Test + void shouldThrowException_WithCustomMessage_WhenIntValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkPositive(-1, "custom error message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenIntValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkPositive(-1, "custom error message template %s", "42")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message template 42"); + } + @Test void shouldNotThrowException_WhenIntValue_IsPositive() { assertThatCode(() -> KiwiPreconditions.checkPositive(1)).doesNotThrowAnyException(); } + @Test + void shouldNotThrowException_WithCustomMessage_WhenIntValue_IsPositive() { + assertThatCode(() -> KiwiPreconditions.checkPositive(1, "custom error message")).doesNotThrowAnyException(); + } + + @Test + void shouldNotThrowException_WithCustomMessageTemplate_WhenIntValue_IsPositive() { + assertThatCode(() -> KiwiPreconditions.checkPositive(1, "custom error message template %s", 42)).doesNotThrowAnyException(); + } + @Test void shouldThrowException_WhenLongValue_IsNegative() { assertThatThrownBy(() -> KiwiPreconditions.checkPositive(-1L)) @@ -354,6 +378,20 @@ void shouldThrowException_WhenLongValue_IsNegative() { .hasMessage("value must be a positive number"); } + @Test + void shouldThrowException_WithCustomMessage_WhenLongValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkPositive(-1L, "custom error message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenLongValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkPositive(-1L, "custom error message template %s", "42")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message template 42"); + } + @Test void shouldThrowException_WhenLongValue_IsZero() { assertThatThrownBy(() -> KiwiPreconditions.checkPositive(0L)) @@ -365,6 +403,16 @@ void shouldThrowException_WhenLongValue_IsZero() { void shouldNotThrowException_WhenLongValue_IsPositive() { assertThatCode(() -> KiwiPreconditions.checkPositive(1L)).doesNotThrowAnyException(); } + + @Test + void shouldNotThrowException_WithCustomMessage_WhenLongValue_IsPositive() { + assertThatCode(() -> KiwiPreconditions.checkPositive(1L, "custom error message")).doesNotThrowAnyException(); + } + + @Test + void shouldNotThrowException_WithCustomMessageTemplate_WhenLongValue_IsPositive() { + assertThatCode(() -> KiwiPreconditions.checkPositive(1L, "custom error message template %s", 42)).doesNotThrowAnyException(); + } } @Nested @@ -377,6 +425,20 @@ void shouldThrowException_WhenIntValue_IsNegative() { .hasMessage("value must be positive or zero"); } + @Test + void shouldThrowException_WithCustomMessage_WhenIntValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkPositiveOrZero(-1, "custom error message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenIntValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkPositiveOrZero(-1, "custom error message template %s", "42")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message template 42"); + } + @Test void shouldNotThrowException_WhenIntValue_IsZero() { assertThatCode(() -> KiwiPreconditions.checkPositiveOrZero(0)).doesNotThrowAnyException(); @@ -387,6 +449,16 @@ void shouldNotThrowException_WhenIntValue_IsPositive() { assertThatCode(() -> KiwiPreconditions.checkPositiveOrZero(1)).doesNotThrowAnyException(); } + @Test + void shouldNotThrowException_WithCustomMessage_WhenIntValue_IsPositive() { + assertThatCode(() -> KiwiPreconditions.checkPositiveOrZero(1, "custom error message")).doesNotThrowAnyException(); + } + + @Test + void shouldNotThrowException_WithCustomMessageTemplate_WhenIntValue_IsPositive() { + assertThatCode(() -> KiwiPreconditions.checkPositiveOrZero(1, "custom error message template %s", 42)).doesNotThrowAnyException(); + } + @Test void shouldThrowException_WhenLongValue_IsNegative() { assertThatThrownBy(() -> KiwiPreconditions.checkPositiveOrZero(-1L)) @@ -394,6 +466,20 @@ void shouldThrowException_WhenLongValue_IsNegative() { .hasMessage("value must be positive or zero"); } + @Test + void shouldThrowException_WithCustomMessage_WhenLongValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkPositiveOrZero(-1L, "custom error message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenLongValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkPositiveOrZero(-1L, "custom error message template %s", "42")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message template 42"); + } + @Test void shouldThrowException_WhenLongValue_IsZero() { assertThatCode(() -> KiwiPreconditions.checkPositiveOrZero(0L)).doesNotThrowAnyException(); @@ -403,6 +489,16 @@ void shouldThrowException_WhenLongValue_IsZero() { void shouldNotThrowException_WhenLongValue_IsPositive() { assertThatCode(() -> KiwiPreconditions.checkPositiveOrZero(1L)).doesNotThrowAnyException(); } + + @Test + void shouldNotThrowException_WithCustomMessage_WhenLongValue_IsPositive() { + assertThatCode(() -> KiwiPreconditions.checkPositive(1L, "custom error message")).doesNotThrowAnyException(); + } + + @Test + void shouldNotThrowException_WithCustomMessageTemplate_WhenLongValue_IsPositive() { + assertThatCode(() -> KiwiPreconditions.checkPositive(1L, "custom error message template %s", 42)).doesNotThrowAnyException(); + } } @Nested @@ -415,6 +511,20 @@ void shouldThrowException_WhenIntValue_IsNegative() { .hasMessage("value must be a positive number"); } + @Test + void shouldThrowException_WithCustomMessage_WhenIntValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requirePositive(-1, "custom error message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenIntValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requirePositive(-1, "custom error message template %s", "42")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message template 42"); + } + @Test void shouldThrowException_WhenIntValue_IsZero() { assertThatThrownBy(() -> KiwiPreconditions.requirePositive(0)) @@ -427,6 +537,16 @@ void shouldReturnValue_WhenIntValue_IsPositive() { assertThat(KiwiPreconditions.requirePositive(1)).isEqualTo(1); } + @Test + void shouldReturnValue_WithCustomMessage_WhenIntValue_IsPositive() { + assertThat(KiwiPreconditions.requirePositive(1, "custom error message")).isEqualTo(1); + } + + @Test + void shouldReturnValue_WithCustomMessageTemplate_WhenIntValue_IsPositive() { + assertThat(KiwiPreconditions.requirePositive(1, "custom error message template %s", 42)).isEqualTo(1); + } + @Test void shouldThrowException_WhenLongValue_IsNegative() { assertThatThrownBy(() -> KiwiPreconditions.requirePositive(-1L)) @@ -434,6 +554,20 @@ void shouldThrowException_WhenLongValue_IsNegative() { .hasMessage("value must be a positive number"); } + @Test + void shouldThrowException_WithCustomMessage_WhenLongValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requirePositive(-1L, "custom error message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenLongValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requirePositive(-1L, "custom error message template %s", "42")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message template 42"); + } + @Test void shouldThrowException_WhenLongValue_IsZero() { assertThatThrownBy(() -> KiwiPreconditions.requirePositive(0L)) @@ -445,6 +579,16 @@ void shouldThrowException_WhenLongValue_IsZero() { void shouldReturnValue_WhenLongValue_IsPositive() { assertThat(KiwiPreconditions.requirePositive(1L)).isEqualTo(1L); } + + @Test + void shouldReturnValue_WithCustomMessage_WhenLongValue_IsPositive() { + assertThat(KiwiPreconditions.requirePositive(1L, "custom error message")).isEqualTo(1); + } + + @Test + void shouldReturnValue_WithCustomMessageTemplate_WhenLongValue_IsPositive() { + assertThat(KiwiPreconditions.requirePositive(1L, "custom error message template %s", 42)).isEqualTo(1); + } } @Nested @@ -457,6 +601,20 @@ void shouldThrowException_WhenIntValue_IsNegative() { .hasMessage("value must be positive or zero"); } + @Test + void shouldThrowException_WithCustomMessage_WhenIntValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requirePositiveOrZero(-1, "custom error message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenIntValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requirePositiveOrZero(-1, "custom error message template %s", "42")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message template 42"); + } + @Test void shouldReturnValue_WhenIntValue_IsZero() { assertThat(KiwiPreconditions.requirePositiveOrZero(0)).isZero(); @@ -467,6 +625,16 @@ void shouldReturnValue_WhenIntValue_IsPositive() { assertThat(KiwiPreconditions.requirePositiveOrZero(1)).isEqualTo(1); } + @Test + void shouldReturnValue_WithCustomMessage_WhenIntValue_IsPositive() { + assertThat(KiwiPreconditions.requirePositiveOrZero(1, "custom error message")).isEqualTo(1); + } + + @Test + void shouldReturnValue_WithCustomMessageTemplate_WhenIntValue_IsPositive() { + assertThat(KiwiPreconditions.requirePositiveOrZero(1, "custom error message template %s", 42)).isEqualTo(1); + } + @Test void shouldThrowException_WhenLongValue_IsNegative() { assertThatThrownBy(() -> KiwiPreconditions.requirePositiveOrZero(-1L)) @@ -474,6 +642,20 @@ void shouldThrowException_WhenLongValue_IsNegative() { .hasMessage("value must be positive or zero"); } + @Test + void shouldThrowException_WithCustomMessage_WhenLongValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requirePositiveOrZero(-1L, "custom error message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenLongValue_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requirePositiveOrZero(-1L, "custom error message template %s", "42")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom error message template 42"); + } + @Test void shouldReturnValue_WhenLongValue_IsZero() { assertThat(KiwiPreconditions.requirePositiveOrZero(0L)).isZero(); @@ -483,6 +665,16 @@ void shouldReturnValue_WhenLongValue_IsZero() { void shouldReturnValue_WhenLongValue_IsPositive() { assertThat(KiwiPreconditions.requirePositiveOrZero(1L)).isEqualTo(1L); } + + @Test + void shouldReturnValue_WithCustomMessage_WhenLongValue_IsPositive() { + assertThat(KiwiPreconditions.requirePositiveOrZero(1L, "custom error message")).isEqualTo(1); + } + + @Test + void shouldReturnValue_WithCustomMessageTemplate_WhenLongValue_IsPositive() { + assertThat(KiwiPreconditions.requirePositiveOrZero(1L, "custom error message template %s", 42)).isEqualTo(1); + } } @Nested @@ -495,6 +687,20 @@ void shouldThrowException_WhenPort_IsNegative() { .hasMessage("port must be between 0 and " + KiwiPreconditions.MAX_PORT_NUMBER); } + @Test + void shouldThrowException_WithCustomMessage_WhenPort_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkValidPort(-1, "custom message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenPort_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkValidPort(-1, "custom message %s", 42)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom message 42"); + } + @Test void shouldThrowException_WhenPort_IsTooHigh() { assertThatThrownBy(() -> KiwiPreconditions.checkValidPort(MAX_PORT_NUMBER + 1)) @@ -508,6 +714,16 @@ void shouldNotThrowException_WhenPort_IsValid(int port) { assertThatCode(() -> KiwiPreconditions.checkValidPort(port)).doesNotThrowAnyException(); } + @Test + void shouldNotThrowException_WithCustomMessage_WhenPort_IsValid() { + assertThatCode(() -> KiwiPreconditions.checkValidPort(0, "custom error message")).doesNotThrowAnyException(); + } + + @Test + void shouldNotThrowException_WithCustomMessageTemplate_WhenPort_IsValid() { + assertThatCode(() -> KiwiPreconditions.checkValidPort(1, "custom error message template %s", 42)).doesNotThrowAnyException(); + } + } @Nested @@ -520,6 +736,20 @@ void shouldThrowException_WhenPort_IsNegative() { .hasMessage("port must be between 0 and " + KiwiPreconditions.MAX_PORT_NUMBER); } + @Test + void shouldThrowException_WithCustomMessage_WhenPort_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requireValidPort(-1, "custom message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenPort_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requireValidPort(-1, "custom message %s", 42)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom message 42"); + } + @Test void shouldThrowException_WhenPort_IsTooHigh() { assertThatThrownBy(() -> KiwiPreconditions.requireValidPort(MAX_PORT_NUMBER + 1)) @@ -533,6 +763,16 @@ void shouldReturnPort_WhenPort_IsValid(int port) { assertThat(KiwiPreconditions.requireValidPort(port)).isEqualTo(port); } + @Test + void shouldReturnPort_WithCustomMessage_WhenPort_IsValid() { + assertThat(KiwiPreconditions.requireValidPort(0, "custom error message")).isZero(); + } + + @Test + void shouldNotThrowException_WithCustomMessageTemplate_WhenPort_IsValid() { + assertThat(KiwiPreconditions.requireValidPort(0, "custom error message %s", 42)).isZero(); + } + } @Nested @@ -545,6 +785,20 @@ void shouldThrowException_WhenPort_IsNegative() { .hasMessage("port must be between 1 and " + KiwiPreconditions.MAX_PORT_NUMBER); } + @Test + void shouldThrowException_WithCustomMessage_WhenPort_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkValidNonZeroPort(-1, "custom message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenPort_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.checkValidNonZeroPort(-1, "custom message %s", 42)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom message 42"); + } + @Test void shouldThrowException_WhenPort_IsZero() { assertThatThrownBy(() -> KiwiPreconditions.checkValidNonZeroPort(0)) @@ -565,6 +819,16 @@ void shouldNotThrowException_WhenPort_IsValid(int port) { assertThatCode(() -> KiwiPreconditions.checkValidNonZeroPort(port)).doesNotThrowAnyException(); } + @Test + void shouldNotThrowException_WithCustomMessage_WhenPort_IsValid() { + assertThatCode(() -> KiwiPreconditions.checkValidNonZeroPort(1, "custom error message")).doesNotThrowAnyException(); + } + + @Test + void shouldNotThrowException_WithCustomMessageTemplate_WhenPort_IsValid() { + assertThatCode(() -> KiwiPreconditions.checkValidNonZeroPort(1, "custom error message template %s", 42)).doesNotThrowAnyException(); + } + } @Nested @@ -577,6 +841,20 @@ void shouldThrowException_WhenPort_IsNegative() { .hasMessage("port must be between 1 and " + KiwiPreconditions.MAX_PORT_NUMBER); } + @Test + void shouldThrowException_WithCustomMessage_WhenPort_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requireValidNonZeroPort(-1, "custom message")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom message"); + } + + @Test + void shouldThrowException_WithCustomMessageTemplate_WhenPort_IsNegative() { + assertThatThrownBy(() -> KiwiPreconditions.requireValidNonZeroPort(-1, "custom message %s", 42)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("custom message 42"); + } + @Test void shouldThrowException_WhenPort_IsZero() { assertThatThrownBy(() -> KiwiPreconditions.requireValidNonZeroPort(0)) @@ -597,5 +875,15 @@ void shouldReturnPort_WhenPort_IsValid(int port) { assertThat(KiwiPreconditions.requireValidNonZeroPort(port)).isEqualTo(port); } + @Test + void shouldReturnPort_WithCustomMessage_WhenPort_IsValid() { + assertThat(KiwiPreconditions.requireValidNonZeroPort(1, "custom error message")).isOne(); + } + + @Test + void shouldNotThrowException_WithCustomMessageTemplate_WhenPort_IsValid() { + assertThat(KiwiPreconditions.requireValidNonZeroPort(1, "custom error message %s", 42)).isOne(); + } + } }