From d6e3e38f4ff3cf1c2f71656a2c493b61b46a0e22 Mon Sep 17 00:00:00 2001 From: Atanas Atanasov Date: Tue, 17 Dec 2024 11:17:19 +0200 Subject: [PATCH 1/6] partial migration Signed-off-by: Atanas Atanasov --- .../block/common/utils/Preconditions.java | 83 +++++++++---------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/common/src/main/java/com/hedera/block/common/utils/Preconditions.java b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java index 64dda396..6fc510c9 100644 --- a/common/src/main/java/com/hedera/block/common/utils/Preconditions.java +++ b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java @@ -16,10 +16,14 @@ package com.hedera.block.common.utils; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.Objects; /** A utility class used to assert various preconditions. */ -public final class Preconditions { // @todo(381) change the APIs to accept non-null error messages +public final class Preconditions { + private static final String DEFAULT_NOT_BLANK_MESSAGE = "The input String is required to be non-blank."; + private static final String DEFAULT_REQUIRED_POSITIVE_MESSAGE = "The input number [%d] is required to be positive."; + /** * This method asserts a given {@link String} is not blank, meaning it is * not {@code null} or does not contain only whitespaces as defined by @@ -27,14 +31,14 @@ public final class Preconditions { // @todo(381) change the APIs to accept non-n * we return it, else we throw {@link IllegalArgumentException}. * * @param toCheck a {@link String} to be checked if is blank as defined - * above + * above * @return the {@link String} to be checked if it is not blank as defined - * above + * above * @throws IllegalArgumentException if the input {@link String} to be - * checked is blank + * checked is blank */ public static String requireNotBlank(final String toCheck) { - return requireNotBlank(toCheck, null); + return requireNotBlank(toCheck, DEFAULT_NOT_BLANK_MESSAGE); } /** @@ -43,21 +47,18 @@ public static String requireNotBlank(final String toCheck) { * {@link String#isBlank()}. If the given {@link String} is not blank, then * we return it, else we throw {@link IllegalArgumentException}. * - * @param toCheck a {@link String} to be checked if is blank as defined - * above - * @param errorMessage the error message to be used in the exception if the - * input {@link String} to be checked is blank, if null, a - * default message + * @param toCheck a {@link String} to be checked if is blank as defined + * above + * @param errorMessage the error message to be used if the precondition + * check fails. Must not be {@code null}! * @return the {@link String} to be checked if it is not blank as defined - * above + * above * @throws IllegalArgumentException if the input {@link String} to be - * checked is blank + * checked is blank */ - public static String requireNotBlank(final String toCheck, final String errorMessage) { + public static String requireNotBlank(final String toCheck, @NonNull final String errorMessage) { if (StringUtilities.isBlank(toCheck)) { - final String message = - Objects.isNull(errorMessage) ? "The input String is required to be non-blank." : errorMessage; - throw new IllegalArgumentException(message); + throw new IllegalArgumentException(errorMessage); } else { return toCheck; } @@ -70,31 +71,28 @@ public static String requireNotBlank(final String toCheck, final String errorMes * @param toCheck the number to check if it is a positive power of two * @return the number to check if it is positive * @throws IllegalArgumentException if the input number to check is not - * positive + * positive */ public static int requirePositive(final int toCheck) { - return requirePositive(toCheck, null); + return requirePositive(toCheck, DEFAULT_REQUIRED_POSITIVE_MESSAGE); } /** * This method asserts a given integer is a positive. An integer is positive * if it is NOT equal to zero and is greater than zero. * - * @param toCheck the integer to check if it is a positive power of two - * @param errorMessage the error message to be used in the exception if the - * input integer to check is not positive, if null, a - * default message will - * be used + * @param toCheck the integer to check if it is a positive power of two + * @param errorMessage a formatted string with one decimal parameters for + * {@code toCheck}.
+ * Example error message: {@code "The input number (%d) must be positive."} * @return the number to check if it is positive - * @throws IllegalArgumentException if the input number to check is not - * positive + * @throws IllegalArgumentException if the input integer to check is not + * positive + * @see java.util.Formatter for more information on error message formatting */ - public static int requirePositive(final int toCheck, final String errorMessage) { + public static int requirePositive(final int toCheck, @NonNull final String errorMessage) { if (0 >= toCheck) { - final String message = Objects.isNull(errorMessage) - ? "The input integer [%d] is required be positive.".formatted(toCheck) - : errorMessage; - throw new IllegalArgumentException(message); + throw new IllegalArgumentException(errorMessage); } else { return toCheck; } @@ -107,31 +105,28 @@ public static int requirePositive(final int toCheck, final String errorMessage) * @param toCheck the long to check if it is a positive power of two * @return the long to check if it is positive * @throws IllegalArgumentException if the input long to check is not - * positive + * positive */ public static long requirePositive(final long toCheck) { - return requirePositive(toCheck, null); + return requirePositive(toCheck, DEFAULT_REQUIRED_POSITIVE_MESSAGE); } /** * This method asserts a given long is a positive. A long is positive * if it is NOT equal to zero and is greater than zero. * - * @param toCheck the long to check if it is a positive power of two - * @param errorMessage the error message to be used in the exception if the - * input long to check is not positive, if null, a default - * message will - * be used - * @return the long to check if it is positive + * @param toCheck the long to check if it is a positive power of two + * @param errorMessage a formatted string with one decimal parameters for + * {@code toCheck}.
+ * Example error message: {@code "The input number (%d) must be positive."} + * @return the number to check if it is positive * @throws IllegalArgumentException if the input long to check is not - * positive + * positive + * @see java.util.Formatter for more information on error message formatting */ - public static long requirePositive(final long toCheck, final String errorMessage) { + public static long requirePositive(final long toCheck, @NonNull final String errorMessage) { if (0L >= toCheck) { - final String message = Objects.isNull(errorMessage) - ? "The input long [%d] is required be positive.".formatted(toCheck) - : errorMessage; - throw new IllegalArgumentException(message); + throw new IllegalArgumentException(errorMessage); } else { return toCheck; } From c36216f81cf318d2e4a59d9758656b388c68fcee Mon Sep 17 00:00:00 2001 From: Atanas Atanasov Date: Tue, 17 Dec 2024 13:10:55 +0200 Subject: [PATCH 2/6] partial migration Signed-off-by: Atanas Atanasov --- .../block/common/utils/Preconditions.java | 75 ++++++++++--------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/common/src/main/java/com/hedera/block/common/utils/Preconditions.java b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java index 6fc510c9..b702b29f 100644 --- a/common/src/main/java/com/hedera/block/common/utils/Preconditions.java +++ b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java @@ -22,7 +22,11 @@ /** A utility class used to assert various preconditions. */ public final class Preconditions { private static final String DEFAULT_NOT_BLANK_MESSAGE = "The input String is required to be non-blank."; - private static final String DEFAULT_REQUIRED_POSITIVE_MESSAGE = "The input number [%d] is required to be positive."; + private static final String DEFAULT_REQUIRE_POSITIVE_MESSAGE = "The input number [%d] is required to be positive."; + private static final String DEFAULT_GT_OR_EQ_MESSAGE = + "The input number [%d] is required to be greater or equal than [%d]."; + private static final String DEFAULT_REQUIRE_WHOLE_MESSAGE = + "The input number [%d] is required to be a whole number."; /** * This method asserts a given {@link String} is not blank, meaning it is @@ -50,7 +54,7 @@ public static String requireNotBlank(final String toCheck) { * @param toCheck a {@link String} to be checked if is blank as defined * above * @param errorMessage the error message to be used if the precondition - * check fails. Must not be {@code null}! + * check fails, must not be {@code null} * @return the {@link String} to be checked if it is not blank as defined * above * @throws IllegalArgumentException if the input {@link String} to be @@ -74,7 +78,7 @@ public static String requireNotBlank(final String toCheck, @NonNull final String * positive */ public static int requirePositive(final int toCheck) { - return requirePositive(toCheck, DEFAULT_REQUIRED_POSITIVE_MESSAGE); + return requirePositive(toCheck, DEFAULT_REQUIRE_POSITIVE_MESSAGE); } /** @@ -83,7 +87,7 @@ public static int requirePositive(final int toCheck) { * * @param toCheck the integer to check if it is a positive power of two * @param errorMessage a formatted string with one decimal parameters for - * {@code toCheck}.
+ * {@code toCheck}, must not be {@code null}.
* Example error message: {@code "The input number (%d) must be positive."} * @return the number to check if it is positive * @throws IllegalArgumentException if the input integer to check is not @@ -92,7 +96,7 @@ public static int requirePositive(final int toCheck) { */ public static int requirePositive(final int toCheck, @NonNull final String errorMessage) { if (0 >= toCheck) { - throw new IllegalArgumentException(errorMessage); + throw new IllegalArgumentException(errorMessage.formatted(toCheck)); } else { return toCheck; } @@ -108,7 +112,7 @@ public static int requirePositive(final int toCheck, @NonNull final String error * positive */ public static long requirePositive(final long toCheck) { - return requirePositive(toCheck, DEFAULT_REQUIRED_POSITIVE_MESSAGE); + return requirePositive(toCheck, DEFAULT_REQUIRE_POSITIVE_MESSAGE); } /** @@ -117,7 +121,7 @@ public static long requirePositive(final long toCheck) { * * @param toCheck the long to check if it is a positive power of two * @param errorMessage a formatted string with one decimal parameters for - * {@code toCheck}.
+ * {@code toCheck}, must not be {@code null}.
* Example error message: {@code "The input number (%d) must be positive."} * @return the number to check if it is positive * @throws IllegalArgumentException if the input long to check is not @@ -147,11 +151,12 @@ public static long requirePositive(final long toCheck, @NonNull final String err * @param toTest the long value to test * @param base the base value to compare against * @return the input {@code toTest} if it is greater than or equal to - * {@code base} - * @throws IllegalArgumentException if {@code toTest} is less than {@code base} + * {@code base} + * @throws IllegalArgumentException if {@code toTest} is less than + * {@code base} */ public static long requireGreaterOrEqual(final long toTest, final long base) { - return requireGreaterOrEqual(toTest, base, null); + return requireGreaterOrEqual(toTest, base, DEFAULT_GT_OR_EQ_MESSAGE); } /** @@ -160,24 +165,23 @@ public static long requireGreaterOrEqual(final long toTest, final long base) { * If the value does not meet the requirement, an * {@link IllegalArgumentException} is thrown. * - * @param toTest the long value to test - * @param base the base value to compare against - * @param errorMessage the error message to include in the exception if the - * check fails; - * if {@code null}, a default message is used - * @return the input {@code toTest} if it is greater than or equal to - * {@code base} - * @throws IllegalArgumentException if {@code toTest} is less than {@code base} + * @param toTest the long value to test + * @param base the base value to compare against + * @param errorMessage a formatted string with two decimal parameters for + * {@code toTest} and {@code base}, must not be {@code null}.
+ * Example error message: {@code "The input number (%d) must be '>=' than + * (%d)."} + * @return the number to check if it is greater than or equal to the base + * @throws IllegalArgumentException if the input long toTest is not greater + * than or equal to the base + * @see java.util.Formatter for more information on error message formatting */ - public static long requireGreaterOrEqual(final long toTest, final long base, final String errorMessage) { + public static long requireGreaterOrEqual(final long toTest, final long base, @NonNull final String errorMessage) { if (toTest >= base) { return toTest; + } else { + throw new IllegalArgumentException(errorMessage.formatted(toTest, base)); } - - final String message = Objects.isNull(errorMessage) - ? "The input integer [%d] is required be greater or equal than [%d].".formatted(toTest, base) - : errorMessage; - throw new IllegalArgumentException(message); } /** @@ -231,33 +235,30 @@ public static int requireInRange( * @param toCheck the long to check if it is a whole number * @return the number to check if it is whole number * @throws IllegalArgumentException if the input number to check is not - * positive + * positive */ public static long requireWhole(final long toCheck) { - return requireWhole(toCheck, null); + return requireWhole(toCheck, DEFAULT_REQUIRE_WHOLE_MESSAGE); } /** * This method asserts a given long is a whole number. A long is whole * if it is greater or equal to zero. * - * @param toCheck the long to check if it is a whole number - * @param errorMessage the error message to be used in the exception if the - * input long to check is not a whole number, if null, a - * default message will - * be used + * @param toCheck the long to check if it is a whole number + * @param errorMessage a formatted string with one decimal parameters for + * {@code toCheck}, must not be {@code null}.
+ * Example error message: {@code "The input number (%d) must be whole."} * @return the number to check if it is whole number * @throws IllegalArgumentException if the input number to check is not - * positive + * positive + * @see java.util.Formatter for more information on error message formatting */ - public static long requireWhole(final long toCheck, final String errorMessage) { + public static long requireWhole(final long toCheck, @NonNull final String errorMessage) { if (toCheck >= 0) { return toCheck; } else { - final String message = Objects.isNull(errorMessage) - ? "The input integer [%d] is required be whole.".formatted(toCheck) - : errorMessage; - throw new IllegalArgumentException(message); + throw new IllegalArgumentException(errorMessage.formatted(toCheck)); } } From 564fe3bee4b042cf36b1d78469fd63c5130e0624 Mon Sep 17 00:00:00 2001 From: Atanas Atanasov Date: Tue, 17 Dec 2024 13:22:38 +0200 Subject: [PATCH 3/6] cleanup Signed-off-by: Atanas Atanasov --- .../block/common/utils/Preconditions.java | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/common/src/main/java/com/hedera/block/common/utils/Preconditions.java b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java index b702b29f..b42c4915 100644 --- a/common/src/main/java/com/hedera/block/common/utils/Preconditions.java +++ b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java @@ -17,7 +17,6 @@ package com.hedera.block.common.utils; import edu.umd.cs.findbugs.annotations.NonNull; -import java.util.Objects; /** A utility class used to assert various preconditions. */ public final class Preconditions { @@ -25,8 +24,12 @@ public final class Preconditions { private static final String DEFAULT_REQUIRE_POSITIVE_MESSAGE = "The input number [%d] is required to be positive."; private static final String DEFAULT_GT_OR_EQ_MESSAGE = "The input number [%d] is required to be greater or equal than [%d]."; + private static final String DEFAULT_REQUIRE_IN_RANGE_MESSAGE = + "The input number [%d] is required to be in the range [%d, %d] boundaries included."; private static final String DEFAULT_REQUIRE_WHOLE_MESSAGE = "The input number [%d] is required to be a whole number."; + private static final String DEFAULT_REQUIRE_POWER_OF_TWO_MESSAGE = + "The input number [%d] is required to be a power of two."; /** * This method asserts a given {@link String} is not blank, meaning it is @@ -130,15 +133,15 @@ public static long requirePositive(final long toCheck) { */ public static long requirePositive(final long toCheck, @NonNull final String errorMessage) { if (0L >= toCheck) { - throw new IllegalArgumentException(errorMessage); + throw new IllegalArgumentException(errorMessage.formatted(toCheck)); } else { return toCheck; } } /** - * Ensures that a given long value is greater than or equal to a specified base - * value. + * Ensures that a given long value is greater than or equal to a specified + * base value. * If the value does not meet the requirement, an * {@link IllegalArgumentException} is thrown. * @@ -160,8 +163,8 @@ public static long requireGreaterOrEqual(final long toTest, final long base) { } /** - * Ensures that a given long value is greater than or equal to a specified base - * value. + * Ensures that a given long value is greater than or equal to a specified + * base value. * If the value does not meet the requirement, an * {@link IllegalArgumentException} is thrown. * @@ -197,7 +200,7 @@ public static long requireGreaterOrEqual(final long toTest, final long base, @No * @throws IllegalArgumentException if the input int does not pass the test */ public static int requireInRange(final int toCheck, final int lowerBoundary, final int upperBoundary) { - return requireInRange(toCheck, lowerBoundary, upperBoundary, null); + return requireInRange(toCheck, lowerBoundary, upperBoundary, DEFAULT_REQUIRE_IN_RANGE_MESSAGE); } /** @@ -208,23 +211,22 @@ public static int requireInRange(final int toCheck, final int lowerBoundary, fin * @param toCheck the int value to check * @param lowerBoundary the lower boundary * @param upperBoundary the upper boundary - * @param errorMessage the error message to be used in the exception if the - * input int to test is not within the range, if null, a default message - * will be used + * @param errorMessage a formatted string with three decimal parameters for + * {@code toTest}, {@code upperBoundary} and {@code lowerBoundary}, must not + * be {@code null}.
+ * Example error message: {@code "The input number (%d) must be between + * (%d) and (%d)."} * @return the input {@code toCheck} if it is within the range (boundaries * included) * @throws IllegalArgumentException if the input int does not pass the test + * @see java.util.Formatter for more information on error message formatting */ public static int requireInRange( - final int toCheck, final int lowerBoundary, final int upperBoundary, final String errorMessage) { + final int toCheck, final int lowerBoundary, final int upperBoundary, @NonNull final String errorMessage) { if (toCheck >= lowerBoundary && toCheck <= upperBoundary) { return toCheck; } else { - final String message = Objects.isNull(errorMessage) - ? "The input int [%d] is required to be in the range [%d, %d] boundaries included." - .formatted(toCheck, lowerBoundary, upperBoundary) - : errorMessage; - throw new IllegalArgumentException(message); + throw new IllegalArgumentException(errorMessage.formatted(toCheck, lowerBoundary, upperBoundary)); } } @@ -268,30 +270,28 @@ public static long requireWhole(final long toCheck, @NonNull final String errorM * @param toCheck the number to check if it is a power of two * @return the number to check if it is a power of two * @throws IllegalArgumentException if the input number to check is not a - * power of two + * power of two */ public static int requirePowerOfTwo(final int toCheck) { - return requirePowerOfTwo(toCheck, null); + return requirePowerOfTwo(toCheck, DEFAULT_REQUIRE_POWER_OF_TWO_MESSAGE); } /** * This method asserts a given integer is a power of two. * - * @param toCheck the number to check if it is a power of two - * @param errorMessage the error message to be used in the exception if the - * input integer to check is not a power of two, if null, a - * default message - * will be used + * @param toCheck the number to check if it is a power of two + * @param errorMessage a formatted string with one decimal parameter for + * {@code toCheck}, must not be {@code null}.
+ * Example error message: {@code "The input number (%d) must be a power of + * two."} * @return the number to check if it is a power of two * @throws IllegalArgumentException if the input number to check is not a - * power of two + * power of two + * @see java.util.Formatter for more information on error message formatting */ - public static int requirePowerOfTwo(final int toCheck, final String errorMessage) { + public static int requirePowerOfTwo(final int toCheck, @NonNull final String errorMessage) { if (!MathUtilities.isPowerOfTwo(toCheck)) { - final String message = Objects.isNull(errorMessage) - ? "The input integer [%d] is required to be a power of two.".formatted(toCheck) - : errorMessage; - throw new IllegalArgumentException(message); + throw new IllegalArgumentException(errorMessage.formatted(toCheck)); } else { return toCheck; } From 809c5fb00ca4e8dd1b30e0fa2cb32587bc8827de Mon Sep 17 00:00:00 2001 From: Atanas Atanasov Date: Tue, 17 Dec 2024 13:32:53 +0200 Subject: [PATCH 4/6] tests now support verification of default error messages Signed-off-by: Atanas Atanasov --- .../block/common/utils/PreconditionsTest.java | 63 ++++++++++++++----- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java b/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java index 769ed95c..e9f0846d 100644 --- a/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java +++ b/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java @@ -29,6 +29,17 @@ * Tests for {@link Preconditions} functionality. */ class PreconditionsTest { + private static final String DEFAULT_NOT_BLANK_MESSAGE = "The input String is required to be non-blank."; + private static final String DEFAULT_REQUIRE_POSITIVE_MESSAGE = "The input number [%d] is required to be positive."; + private static final String DEFAULT_GT_OR_EQ_MESSAGE = + "The input number [%d] is required to be greater or equal than [%d]."; + private static final String DEFAULT_REQUIRE_IN_RANGE_MESSAGE = + "The input number [%d] is required to be in the range [%d, %d] boundaries included."; + private static final String DEFAULT_REQUIRE_WHOLE_MESSAGE = + "The input number [%d] is required to be a whole number."; + private static final String DEFAULT_REQUIRE_POWER_OF_TWO_MESSAGE = + "The input number [%d] is required to be a power of two."; + /** * This test aims to verify that the * {@link Preconditions#requireNotBlank(String)} will return the input @@ -61,9 +72,11 @@ void testRequireNotBlankPass(final String toTest) { @ParameterizedTest @MethodSource("com.hedera.block.common.CommonsTestUtility#blankStrings") void testRequireNotBlankFail(final String toTest) { - assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requireNotBlank(toTest)); + assertThatIllegalArgumentException() + .isThrownBy(() -> Preconditions.requireNotBlank(toTest)) + .withMessage(DEFAULT_NOT_BLANK_MESSAGE); - final String testErrorMessage = "test error message"; + final String testErrorMessage = DEFAULT_NOT_BLANK_MESSAGE.concat(" custom test error message"); assertThatIllegalArgumentException() .isThrownBy(() -> Preconditions.requireNotBlank(toTest, testErrorMessage)) .withMessage(testErrorMessage); @@ -100,9 +113,13 @@ void testRequireWholePass(final int toTest) { @ParameterizedTest @MethodSource("com.hedera.block.common.CommonsTestUtility#negativeIntegers") void testRequireWholeFail(final int toTest) { - assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requireWhole(toTest)); + assertThatIllegalArgumentException() + .isThrownBy(() -> Preconditions.requireWhole(toTest)) + .withMessage(DEFAULT_REQUIRE_WHOLE_MESSAGE.formatted(toTest)); - final String testErrorMessage = "test error message"; + final String testErrorMessage = DEFAULT_REQUIRE_WHOLE_MESSAGE + .concat(" custom test error message") + .formatted(toTest); assertThatIllegalArgumentException() .isThrownBy(() -> Preconditions.requireWhole(toTest, testErrorMessage)) .withMessage(testErrorMessage); @@ -138,9 +155,12 @@ void testRequireGreaterOrEqualPass(final long toTest, final long base) { @ParameterizedTest @MethodSource("com.hedera.block.common.CommonsTestUtility#invalidGreaterOrEqualValues") void testRequireGreaterOrEqualFail(final long toTest, final long base) { - assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requireGreaterOrEqual(toTest, base)); + assertThatIllegalArgumentException() + .isThrownBy(() -> Preconditions.requireGreaterOrEqual(toTest, base)) + .withMessage(DEFAULT_GT_OR_EQ_MESSAGE.formatted(toTest, base)); - final String testErrorMessage = "test error message"; + final String testErrorMessage = + DEFAULT_GT_OR_EQ_MESSAGE.concat(" custom test error message").formatted(toTest, base); assertThatIllegalArgumentException() .isThrownBy(() -> Preconditions.requireGreaterOrEqual(toTest, base, testErrorMessage)) .withMessage(testErrorMessage); @@ -177,9 +197,13 @@ void testRequirePositivePass(final int toTest) { @ParameterizedTest @MethodSource("com.hedera.block.common.CommonsTestUtility#zeroAndNegativeIntegers") void testRequirePositiveFail(final int toTest) { - assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requirePositive(toTest)); + assertThatIllegalArgumentException() + .isThrownBy(() -> Preconditions.requirePositive(toTest)) + .withMessage(DEFAULT_REQUIRE_POSITIVE_MESSAGE.formatted(toTest)); - final String testErrorMessage = "test error message"; + final String testErrorMessage = DEFAULT_REQUIRE_POSITIVE_MESSAGE + .concat(" custom test error message") + .formatted(toTest); assertThatIllegalArgumentException() .isThrownBy(() -> Preconditions.requirePositive(toTest, testErrorMessage)) .withMessage(testErrorMessage); @@ -215,9 +239,13 @@ void testRequirePositiveLongPass(final long toTest) { @ParameterizedTest @MethodSource("com.hedera.block.common.CommonsTestUtility#zeroAndNegativeIntegers") void testRequirePositiveLongFail(final long toTest) { - assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requirePositive(toTest)); + assertThatIllegalArgumentException() + .isThrownBy(() -> Preconditions.requirePositive(toTest)) + .withMessage(DEFAULT_REQUIRE_POSITIVE_MESSAGE.formatted(toTest)); - final String testErrorMessage = "test error message"; + final String testErrorMessage = DEFAULT_REQUIRE_POSITIVE_MESSAGE + .concat(" custom test error message") + .formatted(toTest); assertThatIllegalArgumentException() .isThrownBy(() -> Preconditions.requirePositive(toTest, testErrorMessage)) .withMessage(testErrorMessage); @@ -258,9 +286,13 @@ void testRequirePowerOfTwoPass(final int toTest) { "com.hedera.block.common.CommonsTestUtility#negativePowerOfTwoIntegers" }) void testRequirePowerOfTwoFail(final int toTest) { - assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest)); + assertThatIllegalArgumentException() + .isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest)) + .withMessage(DEFAULT_REQUIRE_POWER_OF_TWO_MESSAGE.formatted(toTest)); - final String testErrorMessage = "test error message"; + final String testErrorMessage = DEFAULT_REQUIRE_POWER_OF_TWO_MESSAGE + .concat(" custom test error message") + .formatted(toTest); assertThatIllegalArgumentException() .isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest, testErrorMessage)) .withMessage(testErrorMessage); @@ -304,9 +336,12 @@ void testRequireInRangePass(final int toTest, final int lowerBoundary, final int @MethodSource("invalidRequireInRangeValues") void testRequireInRangeFail(final int toTest, final int lowerBoundary, final int upperBoundary) { assertThatIllegalArgumentException() - .isThrownBy(() -> Preconditions.requireInRange(toTest, lowerBoundary, upperBoundary)); + .isThrownBy(() -> Preconditions.requireInRange(toTest, lowerBoundary, upperBoundary)) + .withMessage(DEFAULT_REQUIRE_IN_RANGE_MESSAGE.formatted(toTest, lowerBoundary, upperBoundary)); - final String testErrorMessage = "test error message"; + final String testErrorMessage = DEFAULT_REQUIRE_IN_RANGE_MESSAGE + .concat(" custom test error message") + .formatted(toTest, lowerBoundary, upperBoundary); assertThatIllegalArgumentException() .isThrownBy(() -> Preconditions.requireInRange(toTest, lowerBoundary, upperBoundary, testErrorMessage)) .withMessage(testErrorMessage); From 3612434af6f1fa5bbedd37400861330db46f3e71 Mon Sep 17 00:00:00 2001 From: Atanas Atanasov Date: Tue, 17 Dec 2024 14:43:53 +0200 Subject: [PATCH 5/6] fix test error messages to check for Signed-off-by: Atanas Atanasov --- .../block/common/utils/PreconditionsTest.java | 53 +++++++++---------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java b/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java index e9f0846d..de044a88 100644 --- a/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java +++ b/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java @@ -117,12 +117,11 @@ void testRequireWholeFail(final int toTest) { .isThrownBy(() -> Preconditions.requireWhole(toTest)) .withMessage(DEFAULT_REQUIRE_WHOLE_MESSAGE.formatted(toTest)); - final String testErrorMessage = DEFAULT_REQUIRE_WHOLE_MESSAGE - .concat(" custom test error message") - .formatted(toTest); + final String testMessage = DEFAULT_REQUIRE_WHOLE_MESSAGE.concat(" custom test error message"); + final String expectedTestMessage = testMessage.formatted(toTest); assertThatIllegalArgumentException() - .isThrownBy(() -> Preconditions.requireWhole(toTest, testErrorMessage)) - .withMessage(testErrorMessage); + .isThrownBy(() -> Preconditions.requireWhole(toTest, testMessage)) + .withMessage(expectedTestMessage); } /** @@ -159,11 +158,11 @@ void testRequireGreaterOrEqualFail(final long toTest, final long base) { .isThrownBy(() -> Preconditions.requireGreaterOrEqual(toTest, base)) .withMessage(DEFAULT_GT_OR_EQ_MESSAGE.formatted(toTest, base)); - final String testErrorMessage = - DEFAULT_GT_OR_EQ_MESSAGE.concat(" custom test error message").formatted(toTest, base); + final String testMessage = DEFAULT_GT_OR_EQ_MESSAGE.concat(" custom test error message"); + final String expectedTestMessage = testMessage.formatted(toTest, base); assertThatIllegalArgumentException() - .isThrownBy(() -> Preconditions.requireGreaterOrEqual(toTest, base, testErrorMessage)) - .withMessage(testErrorMessage); + .isThrownBy(() -> Preconditions.requireGreaterOrEqual(toTest, base, testMessage)) + .withMessage(expectedTestMessage); } /** @@ -201,12 +200,11 @@ void testRequirePositiveFail(final int toTest) { .isThrownBy(() -> Preconditions.requirePositive(toTest)) .withMessage(DEFAULT_REQUIRE_POSITIVE_MESSAGE.formatted(toTest)); - final String testErrorMessage = DEFAULT_REQUIRE_POSITIVE_MESSAGE - .concat(" custom test error message") - .formatted(toTest); + final String testMessage = DEFAULT_REQUIRE_POSITIVE_MESSAGE.concat(" custom test error message"); + final String expectedTestMessage = testMessage.formatted(toTest); assertThatIllegalArgumentException() - .isThrownBy(() -> Preconditions.requirePositive(toTest, testErrorMessage)) - .withMessage(testErrorMessage); + .isThrownBy(() -> Preconditions.requirePositive(toTest, testMessage)) + .withMessage(expectedTestMessage); } /** @@ -243,12 +241,11 @@ void testRequirePositiveLongFail(final long toTest) { .isThrownBy(() -> Preconditions.requirePositive(toTest)) .withMessage(DEFAULT_REQUIRE_POSITIVE_MESSAGE.formatted(toTest)); - final String testErrorMessage = DEFAULT_REQUIRE_POSITIVE_MESSAGE - .concat(" custom test error message") - .formatted(toTest); + final String testMessage = DEFAULT_REQUIRE_POSITIVE_MESSAGE.concat(" custom test error message"); + final String expectedTestMessage = testMessage.formatted(toTest); assertThatIllegalArgumentException() - .isThrownBy(() -> Preconditions.requirePositive(toTest, testErrorMessage)) - .withMessage(testErrorMessage); + .isThrownBy(() -> Preconditions.requirePositive(toTest, testMessage)) + .withMessage(expectedTestMessage); } /** @@ -290,12 +287,11 @@ void testRequirePowerOfTwoFail(final int toTest) { .isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest)) .withMessage(DEFAULT_REQUIRE_POWER_OF_TWO_MESSAGE.formatted(toTest)); - final String testErrorMessage = DEFAULT_REQUIRE_POWER_OF_TWO_MESSAGE - .concat(" custom test error message") - .formatted(toTest); + final String testMessage = DEFAULT_REQUIRE_POWER_OF_TWO_MESSAGE.concat(" custom test error message"); + final String expectedTestMessage = testMessage.formatted(toTest); assertThatIllegalArgumentException() - .isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest, testErrorMessage)) - .withMessage(testErrorMessage); + .isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest, testMessage)) + .withMessage(expectedTestMessage); } /** @@ -339,12 +335,11 @@ void testRequireInRangeFail(final int toTest, final int lowerBoundary, final int .isThrownBy(() -> Preconditions.requireInRange(toTest, lowerBoundary, upperBoundary)) .withMessage(DEFAULT_REQUIRE_IN_RANGE_MESSAGE.formatted(toTest, lowerBoundary, upperBoundary)); - final String testErrorMessage = DEFAULT_REQUIRE_IN_RANGE_MESSAGE - .concat(" custom test error message") - .formatted(toTest, lowerBoundary, upperBoundary); + final String testMessage = DEFAULT_REQUIRE_IN_RANGE_MESSAGE.concat(" custom test error message"); + final String expectedTestMessage = testMessage.formatted(toTest, lowerBoundary, upperBoundary); assertThatIllegalArgumentException() - .isThrownBy(() -> Preconditions.requireInRange(toTest, lowerBoundary, upperBoundary, testErrorMessage)) - .withMessage(testErrorMessage); + .isThrownBy(() -> Preconditions.requireInRange(toTest, lowerBoundary, upperBoundary, testMessage)) + .withMessage(expectedTestMessage); } private static Stream validRequireInRangeValues() { From ee0e40f578b5abf25dd5a40685bbc97a2a6153b3 Mon Sep 17 00:00:00 2001 From: Atanas Atanasov Date: Wed, 18 Dec 2024 10:52:28 +0200 Subject: [PATCH 6/6] addressing pr comments for javadocs Signed-off-by: Atanas Atanasov --- .../block/common/utils/Preconditions.java | 111 ++++++++---------- 1 file changed, 48 insertions(+), 63 deletions(-) diff --git a/common/src/main/java/com/hedera/block/common/utils/Preconditions.java b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java index b42c4915..d2909a15 100644 --- a/common/src/main/java/com/hedera/block/common/utils/Preconditions.java +++ b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java @@ -32,15 +32,14 @@ public final class Preconditions { "The input number [%d] is required to be a power of two."; /** - * This method asserts a given {@link String} is not blank, meaning it is - * not {@code null} or does not contain only whitespaces as defined by - * {@link String#isBlank()}. If the given {@link String} is not blank, then - * we return it, else we throw {@link IllegalArgumentException}. + * This method asserts a given {@link String} is not blank. + * A blank {@link String} is one that is either {@code null} or contains + * only whitespaces as defined by {@link String#isBlank()}. If the given + * {@link String} is not blank, then we return it, else we throw an + * {@link IllegalArgumentException}. * - * @param toCheck a {@link String} to be checked if is blank as defined - * above - * @return the {@link String} to be checked if it is not blank as defined - * above + * @param toCheck a {@link String} to be checked if is blank as defined above + * @return the {@link String} to be checked if it is not blank as defined above * @throws IllegalArgumentException if the input {@link String} to be * checked is blank */ @@ -49,17 +48,16 @@ public static String requireNotBlank(final String toCheck) { } /** - * This method asserts a given {@link String} is not blank, meaning it is - * not {@code null} or does not contain only whitespaces as defined by - * {@link String#isBlank()}. If the given {@link String} is not blank, then - * we return it, else we throw {@link IllegalArgumentException}. + * This method asserts a given {@link String} is not blank. + * A blank {@link String} is one that is either {@code null} or contains + * only whitespaces as defined by {@link String#isBlank()}. If the given + * {@link String} is not blank, then we return it, else we throw an + * {@link IllegalArgumentException}. * - * @param toCheck a {@link String} to be checked if is blank as defined - * above + * @param toCheck a {@link String} to be checked if is blank as defined above * @param errorMessage the error message to be used if the precondition * check fails, must not be {@code null} - * @return the {@link String} to be checked if it is not blank as defined - * above + * @return the {@link String} to be checked if it is not blank as defined above * @throws IllegalArgumentException if the input {@link String} to be * checked is blank */ @@ -72,29 +70,27 @@ public static String requireNotBlank(final String toCheck, @NonNull final String } /** - * This method asserts a given integer is a positive. An integer is positive - * if it is NOT equal to zero and is greater than zero. + * This method asserts a given integer is a positive. + * An integer is positive if it is NOT equal to zero and is greater than zero. * * @param toCheck the number to check if it is a positive power of two * @return the number to check if it is positive - * @throws IllegalArgumentException if the input number to check is not - * positive + * @throws IllegalArgumentException if the input number to check is not positive */ public static int requirePositive(final int toCheck) { return requirePositive(toCheck, DEFAULT_REQUIRE_POSITIVE_MESSAGE); } /** - * This method asserts a given integer is a positive. An integer is positive - * if it is NOT equal to zero and is greater than zero. + * This method asserts a given integer is a positive. + * An integer is positive if it is NOT equal to zero and is greater than zero. * * @param toCheck the integer to check if it is a positive power of two * @param errorMessage a formatted string with one decimal parameters for * {@code toCheck}, must not be {@code null}.
- * Example error message: {@code "The input number (%d) must be positive."} + * Example error message: {@value #DEFAULT_REQUIRE_POSITIVE_MESSAGE} * @return the number to check if it is positive - * @throws IllegalArgumentException if the input integer to check is not - * positive + * @throws IllegalArgumentException if the input integer to check is not positive * @see java.util.Formatter for more information on error message formatting */ public static int requirePositive(final int toCheck, @NonNull final String errorMessage) { @@ -106,29 +102,27 @@ public static int requirePositive(final int toCheck, @NonNull final String error } /** - * This method asserts a given long is a positive. A long is positive - * if it is NOT equal to zero and is greater than zero. + * This method asserts a given long is a positive. + * A long is positive if it is NOT equal to zero and is greater than zero. * * @param toCheck the long to check if it is a positive power of two * @return the long to check if it is positive - * @throws IllegalArgumentException if the input long to check is not - * positive + * @throws IllegalArgumentException if the input long to check is not positive */ public static long requirePositive(final long toCheck) { return requirePositive(toCheck, DEFAULT_REQUIRE_POSITIVE_MESSAGE); } /** - * This method asserts a given long is a positive. A long is positive - * if it is NOT equal to zero and is greater than zero. + * This method asserts a given long is a positive. + * A long is positive if it is NOT equal to zero and is greater than zero. * * @param toCheck the long to check if it is a positive power of two * @param errorMessage a formatted string with one decimal parameters for * {@code toCheck}, must not be {@code null}.
- * Example error message: {@code "The input number (%d) must be positive."} + * Example error message: {@value #DEFAULT_REQUIRE_POSITIVE_MESSAGE} * @return the number to check if it is positive - * @throws IllegalArgumentException if the input long to check is not - * positive + * @throws IllegalArgumentException if the input long to check is not positive * @see java.util.Formatter for more information on error message formatting */ public static long requirePositive(final long toCheck, @NonNull final String errorMessage) { @@ -153,10 +147,8 @@ public static long requirePositive(final long toCheck, @NonNull final String err * * @param toTest the long value to test * @param base the base value to compare against - * @return the input {@code toTest} if it is greater than or equal to - * {@code base} - * @throws IllegalArgumentException if {@code toTest} is less than - * {@code base} + * @return the input {@code toTest} if it is greater than or equal to {@code base} + * @throws IllegalArgumentException if {@code toTest} is less than {@code base} */ public static long requireGreaterOrEqual(final long toTest, final long base) { return requireGreaterOrEqual(toTest, base, DEFAULT_GT_OR_EQ_MESSAGE); @@ -172,8 +164,7 @@ public static long requireGreaterOrEqual(final long toTest, final long base) { * @param base the base value to compare against * @param errorMessage a formatted string with two decimal parameters for * {@code toTest} and {@code base}, must not be {@code null}.
- * Example error message: {@code "The input number (%d) must be '>=' than - * (%d)."} + * Example error message: {@value #DEFAULT_GT_OR_EQ_MESSAGE} * @return the number to check if it is greater than or equal to the base * @throws IllegalArgumentException if the input long toTest is not greater * than or equal to the base @@ -188,15 +179,14 @@ public static long requireGreaterOrEqual(final long toTest, final long base, @No } /** - * This method asserts a given int is within a range (boundaries - * included). If the given int is within the range, then we return it, - * else, an {@link IllegalArgumentException} is thrown. + * This method asserts a given int is within a range (boundaries included). + * If the given int is within the range, then we return it, else, an + * {@link IllegalArgumentException} is thrown. * * @param toCheck the int value to test * @param lowerBoundary the lower boundary * @param upperBoundary the upper boundary - * @return the input {@code toCheck} if it is within the range (boundaries - * included) + * @return the input {@code toCheck} if it is within the range (boundaries included) * @throws IllegalArgumentException if the input int does not pass the test */ public static int requireInRange(final int toCheck, final int lowerBoundary, final int upperBoundary) { @@ -204,9 +194,9 @@ public static int requireInRange(final int toCheck, final int lowerBoundary, fin } /** - * This method asserts a given int is within a range (boundaries - * included). If the given int is within the range, then we return it, - * else, an {@link IllegalArgumentException} is thrown. + * This method asserts a given int is within a range (boundaries included). + * If the given int is within the range, then we return it, else, an + * {@link IllegalArgumentException} is thrown. * * @param toCheck the int value to check * @param lowerBoundary the lower boundary @@ -214,10 +204,8 @@ public static int requireInRange(final int toCheck, final int lowerBoundary, fin * @param errorMessage a formatted string with three decimal parameters for * {@code toTest}, {@code upperBoundary} and {@code lowerBoundary}, must not * be {@code null}.
- * Example error message: {@code "The input number (%d) must be between - * (%d) and (%d)."} - * @return the input {@code toCheck} if it is within the range (boundaries - * included) + * Example error message: {@value #DEFAULT_REQUIRE_IN_RANGE_MESSAGE} + * @return the input {@code toCheck} if it is within the range (boundaries included) * @throws IllegalArgumentException if the input int does not pass the test * @see java.util.Formatter for more information on error message formatting */ @@ -231,29 +219,27 @@ public static int requireInRange( } /** - * This method asserts a given long is a whole number. A long is whole - * if it is greater or equal to zero. + * This method asserts a given long is a whole number. + * A long is whole if it is greater or equal to zero. * * @param toCheck the long to check if it is a whole number * @return the number to check if it is whole number - * @throws IllegalArgumentException if the input number to check is not - * positive + * @throws IllegalArgumentException if the input number to check is not positive */ public static long requireWhole(final long toCheck) { return requireWhole(toCheck, DEFAULT_REQUIRE_WHOLE_MESSAGE); } /** - * This method asserts a given long is a whole number. A long is whole - * if it is greater or equal to zero. + * This method asserts a given long is a whole number. + * A long is whole if it is greater or equal to zero. * * @param toCheck the long to check if it is a whole number * @param errorMessage a formatted string with one decimal parameters for * {@code toCheck}, must not be {@code null}.
- * Example error message: {@code "The input number (%d) must be whole."} + * Example error message: {@value #DEFAULT_REQUIRE_WHOLE_MESSAGE} * @return the number to check if it is whole number - * @throws IllegalArgumentException if the input number to check is not - * positive + * @throws IllegalArgumentException if the input number to check is not positive * @see java.util.Formatter for more information on error message formatting */ public static long requireWhole(final long toCheck, @NonNull final String errorMessage) { @@ -282,8 +268,7 @@ public static int requirePowerOfTwo(final int toCheck) { * @param toCheck the number to check if it is a power of two * @param errorMessage a formatted string with one decimal parameter for * {@code toCheck}, must not be {@code null}.
- * Example error message: {@code "The input number (%d) must be a power of - * two."} + * Example error message: {@value #DEFAULT_REQUIRE_POWER_OF_TWO_MESSAGE} * @return the number to check if it is a power of two * @throws IllegalArgumentException if the input number to check is not a * power of two