From 2d769fde2654068ceb226c61419aba04b2feefa7 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 1 Jun 2025 02:58:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor!:=20=E6=A0=A1=E9=AA=8C=E4=B8=8D?= =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=97=B6=E9=BB=98=E8=AE=A4=E6=8A=9B=E5=87=BA?= =?UTF-8?q?=20`ValidationException`=20=E8=80=8C=E4=B8=8D=E6=98=AF=20`Illeg?= =?UTF-8?q?alArgumentException`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 `ValidationException` - 校验不通过时默认抛出 `ValidationException` 而不是 `IllegalArgumentException` - 更新了相关的测试用例 --- .../BaseComparablePropertyValidator.java | 5 +- .../validator/BasePropertyValidator.java | 15 ++- .../plusone/validator/BaseValidator.java | 2 +- .../validator/DoublePropertyValidator.java | 12 +-- .../validator/IntPropertyValidator.java | 12 +-- .../validator/LongPropertyValidator.java | 12 +-- .../validator/ValidationException.java | 91 +++++++++++++++++++ .../example/validator/BaseValidatorTest.java | 5 +- .../validator/BoolPropertyValidatorTests.java | 33 +++---- .../CollectionPropertyValidatorTests.java | 11 ++- .../ComparablePropertyValidatorTests.java | 17 ++-- .../DoublePropertyValidatorTests.java | 65 ++++++------- .../validator/IntPropertyValidatorTests.java | 65 ++++++------- .../validator/LongPropertyValidatorTests.java | 65 ++++++------- .../ObjectPropertyValidatorTests.java | 60 ++++++------ .../StringPropertyValidatorTests.java | 69 +++++++------- .../map/validator/MapValidatorTests.java | 3 +- .../validator/ValidationExceptionTests.java | 56 ++++++++++++ 18 files changed, 373 insertions(+), 225 deletions(-) create mode 100644 plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ValidationException.java create mode 100644 plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/ValidationExceptionTests.java diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java index a88cf26..452f5c5 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java @@ -47,9 +47,8 @@ public abstract class BaseComparablePropertyValidator range) { - withRule(value -> value != null && range.contains(value), - value -> new IllegalArgumentException( - String.format("The input must in the interval %s. You entered %s.", range, value))); + withRule(value -> value != null && range.contains(value), value -> ValidationException.withMessage( + "The input must in the interval %s. You entered %s.", range, value)); return thisObject(); } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java index 34dd961..b870edf 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java @@ -51,7 +51,7 @@ public abstract class BasePropertyValidator rule) { - return withRule(rule, v -> new IllegalArgumentException()); + return withRule(rule, v -> ValidationException.withDefaultMessage()); } /** @@ -222,8 +222,8 @@ public abstract class BasePropertyValidator new IllegalArgumentException(String.format("The input must be equal to '%s'.", that))); + return equalTo(that, value -> ValidationException + .withMessage("The input must be equal to '%s'.", that)); } /** @@ -377,8 +377,13 @@ public abstract class BasePropertyValidator Function convertToExceptionFunction(String errMsg) { - return value -> new IllegalArgumentException(errMsg); + static Function convertToExceptionFunction(String errMsg) { + return value -> ValidationException.withMessage(errMsg); + } + + static Function convertToExceptionFunction( + String errorMessageTemplate, Object... errorMessageArgs) { + return value -> ValidationException.withMessage(errorMessageTemplate, errorMessageArgs); } static Function convertToExceptionFunction( diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index bcb95ea..b42a33f 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -48,7 +48,7 @@ public abstract class BaseValidator implements IValidator { * @param errorMessage 错误信息 */ protected final void withRule(final Predicate rule, final String errorMessage) { - withRule(rule, () -> new IllegalArgumentException(errorMessage)); + withRule(rule, () -> ValidationException.withMessage(errorMessage)); } /** diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java index 8c37068..72c28a9 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java @@ -45,8 +45,7 @@ public class DoublePropertyValidator * @return 属性校验器 */ public DoublePropertyValidator gt(double min) { - return gt(min, () -> new IllegalArgumentException( - String.format("The input must be greater than '%s'.", min))); + return gt(min, convertToExceptionFunction("The input must be greater than '%s'.", min)); } /** @@ -100,8 +99,7 @@ public class DoublePropertyValidator * @return 属性校验器 */ public DoublePropertyValidator ge(double min) { - return ge(min, () -> new IllegalArgumentException( - String.format("The input must be greater than or equal to '%s'.", min))); + return ge(min, convertToExceptionFunction("The input must be greater than or equal to '%s'.", min)); } /** @@ -155,8 +153,7 @@ public class DoublePropertyValidator * @return 属性校验器 */ public DoublePropertyValidator lt(double max) { - return lt(max, () -> new IllegalArgumentException( - String.format("The input must be less than '%s'.", max))); + return lt(max, convertToExceptionFunction("The input must be less than '%s'.", max)); } /** @@ -210,8 +207,7 @@ public class DoublePropertyValidator * @return 属性校验器 */ public DoublePropertyValidator le(double max) { - return le(max, () -> new IllegalArgumentException( - String.format("The input must be less than or equal to '%s'.", max))); + return le(max, convertToExceptionFunction("The input must be less than or equal to '%s'.", max)); } /** diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java index 6459f07..51c44e5 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java @@ -45,8 +45,7 @@ public class IntPropertyValidator * @return 属性校验器 */ public IntPropertyValidator gt(int min) { - return gt(min, () -> new IllegalArgumentException( - String.format("The input must be greater than '%d'.", min))); + return gt(min, convertToExceptionFunction("The input must be greater than '%d'.", min)); } /** @@ -100,8 +99,7 @@ public class IntPropertyValidator * @return 属性校验器 */ public IntPropertyValidator ge(int min) { - return ge(min, () -> new IllegalArgumentException( - String.format("The input must be greater than or equal to '%d'.", min))); + return ge(min, convertToExceptionFunction("The input must be greater than or equal to '%d'.", min)); } /** @@ -155,8 +153,7 @@ public class IntPropertyValidator * @return 属性校验器 */ public IntPropertyValidator lt(int max) { - return lt(max, () -> new IllegalArgumentException( - String.format("The input must be less than '%d'.", max))); + return lt(max, convertToExceptionFunction("The input must be less than '%d'.", max)); } /** @@ -210,8 +207,7 @@ public class IntPropertyValidator * @return 属性校验器 */ public IntPropertyValidator le(int max) { - return le(max, () -> new IllegalArgumentException( - String.format("The input must be less than or equal to '%d'.", max))); + return le(max, convertToExceptionFunction("The input must be less than or equal to '%d'.", max)); } /** diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java index 5aef774..f6eeb7c 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java @@ -45,8 +45,7 @@ public class LongPropertyValidator * @return 属性校验器 */ public LongPropertyValidator gt(long min) { - return gt(min, () -> new IllegalArgumentException( - String.format("The input must be greater than '%d'.", min))); + return gt(min, convertToExceptionFunction("The input must be greater than '%d'.", min)); } /** @@ -100,8 +99,7 @@ public class LongPropertyValidator * @return 属性校验器 */ public LongPropertyValidator ge(long min) { - return ge(min, () -> new IllegalArgumentException( - String.format("The input must be greater than or equal to '%d'.", min))); + return ge(min, convertToExceptionFunction("The input must be greater than or equal to '%d'.", min)); } /** @@ -155,8 +153,7 @@ public class LongPropertyValidator * @return 属性校验器 */ public LongPropertyValidator lt(long max) { - return lt(max, () -> new IllegalArgumentException( - String.format("The input must be less than '%d'.", max))); + return lt(max, convertToExceptionFunction("The input must be less than '%d'.", max)); } /** @@ -210,8 +207,7 @@ public class LongPropertyValidator * @return 属性校验器 */ public LongPropertyValidator le(long max) { - return le(max, () -> new IllegalArgumentException( - String.format("The input must be less than or equal to '%d'.", max))); + return le(max, convertToExceptionFunction("The input must be less than or equal to '%d'.", max)); } /** diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ValidationException.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ValidationException.java new file mode 100644 index 0000000..11133ba --- /dev/null +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ValidationException.java @@ -0,0 +1,91 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package xyz.zhouxy.plusone.validator; + +/** + * 验证失败的异常 + * + * @author ZhouXY + */ +public class ValidationException extends RuntimeException { + + private static final long serialVersionUID = -49385010625414401L; + + public static final String DEFAULT_MESSAGE = "Validation failed."; + + private ValidationException(String message) { + super(message); + } + + private ValidationException(Throwable cause) { + super(cause); + } + + private ValidationException(String message, Throwable cause) { + super(message, cause); + } + + /** + * 创建一个验证失败异常 + * + * @return 异常 + */ + public static ValidationException withDefaultMessage() { + return new ValidationException(DEFAULT_MESSAGE); + } + + /** + * 创建一个验证失败异常 + * + * @param message 错误信息 + * @return 异常 + */ + public static ValidationException withMessage(String message) { + return new ValidationException(message); + } + + /** + * 创建一个验证失败异常 + * + * @param errorMessageTemplate 错误信息模版 + * @param errorMessageArgs 错误信息参数 + * @return 异常 + */ + public static ValidationException withMessage(String errorMessageTemplate, Object... errorMessageArgs) { + return new ValidationException(String.format(errorMessageTemplate, errorMessageArgs)); + } + + /** + * 创建一个验证失败异常 + * + * @param cause 错误 cause + * @return 异常 + */ + public static ValidationException withCause(Throwable cause) { + return new ValidationException(cause); + } + + /** + * 创建一个验证失败异常 + * + * @param message 错误信息 + * @param cause 错误 cause + * @return 异常 + */ + public static ValidationException withMessageAndCause(String message, Throwable cause) { + return new ValidationException(message, cause); + } +} diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BaseValidatorTest.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BaseValidatorTest.java index 04f5af6..17cef19 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BaseValidatorTest.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BaseValidatorTest.java @@ -26,6 +26,7 @@ import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.IValidator; +import xyz.zhouxy.plusone.validator.ValidationException; class BaseValidatorTest { @@ -63,8 +64,8 @@ class BaseValidatorTest { "The stringProperty must be equal to 'Foo'"); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("The stringProperty must be equal to 'Foo'", eWithSpecifiedMessage.getMessage()); IValidator ruleWithExceptionSupplier = new BaseValidator() { diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BoolPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BoolPropertyValidatorTests.java index 3fac0ec..40ad45b 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BoolPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BoolPropertyValidatorTests.java @@ -23,6 +23,7 @@ import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.IValidator; +import xyz.zhouxy.plusone.validator.ValidationException; public class BoolPropertyValidatorTests { @@ -60,8 +61,8 @@ public class BoolPropertyValidatorTests { ExampleCommand command = exampleCommandWithBoolProperty(false); - IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, + ValidationException exception = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals("The input must be true.", exception.getMessage()); @@ -79,8 +80,8 @@ public class BoolPropertyValidatorTests { ExampleCommand command = exampleCommandWithBoolProperty(false); - IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, + ValidationException exception = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(message, exception.getMessage()); @@ -134,8 +135,8 @@ public class BoolPropertyValidatorTests { ExampleCommand command = exampleCommandWithBoolProperty(null); - IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, + ValidationException exception = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals("The input must be true.", exception.getMessage()); @@ -153,8 +154,8 @@ public class BoolPropertyValidatorTests { ExampleCommand command = exampleCommandWithBoolProperty(null); - IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, + ValidationException exception = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(message, exception.getMessage()); @@ -236,8 +237,8 @@ public class BoolPropertyValidatorTests { ExampleCommand command = exampleCommandWithBoolProperty(true); - IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, + ValidationException exception = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals("The input must be false.", exception.getMessage()); @@ -255,8 +256,8 @@ public class BoolPropertyValidatorTests { ExampleCommand command = exampleCommandWithBoolProperty(true); - IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, + ValidationException exception = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(message, exception.getMessage()); @@ -310,8 +311,8 @@ public class BoolPropertyValidatorTests { ExampleCommand command = exampleCommandWithBoolProperty(null); - IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, + ValidationException exception = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals("The input must be false.", exception.getMessage()); @@ -329,8 +330,8 @@ public class BoolPropertyValidatorTests { ExampleCommand command = exampleCommandWithBoolProperty(null); - IllegalArgumentException exception = assertThrows( - IllegalArgumentException.class, + ValidationException exception = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(message, exception.getMessage()); diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/CollectionPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/CollectionPropertyValidatorTests.java index 2bd7830..09f130b 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/CollectionPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/CollectionPropertyValidatorTests.java @@ -31,6 +31,7 @@ import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.IValidator; +import xyz.zhouxy.plusone.validator.ValidationException; public class CollectionPropertyValidatorTests { @@ -69,7 +70,7 @@ public class CollectionPropertyValidatorTests { ExampleCommand command = exampleCommandWithStringListProperty(Collections.emptyList()); - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command)); assertEquals("The input must not be empty.", e.getMessage()); } @@ -83,7 +84,7 @@ public class CollectionPropertyValidatorTests { ExampleCommand command = exampleCommandWithStringListProperty(Collections.emptyList()); - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_NOT_EMPTY, e.getMessage()); } @@ -128,7 +129,7 @@ public class CollectionPropertyValidatorTests { ExampleCommand command = exampleCommandWithStringListProperty(null); - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_NOT_EMPTY, e.getMessage()); } @@ -217,7 +218,7 @@ public class CollectionPropertyValidatorTests { ExampleCommand command = exampleCommandWithStringListProperty(Lists.newArrayList("A", "B", "C")); - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command)); assertEquals("The input must be empty.", e.getMessage()); } @@ -231,7 +232,7 @@ public class CollectionPropertyValidatorTests { ExampleCommand command = exampleCommandWithStringListProperty(Lists.newArrayList("A", "B", "C")); - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_EMPTY, e.getMessage()); } diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ComparablePropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ComparablePropertyValidatorTests.java index 1dc0270..258cd70 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ComparablePropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ComparablePropertyValidatorTests.java @@ -29,6 +29,7 @@ import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.IValidator; +import xyz.zhouxy.plusone.validator.ValidationException; public class ComparablePropertyValidatorTests { @@ -87,8 +88,8 @@ public class ComparablePropertyValidatorTests { ExampleCommand command = exampleCommandWithComparableProperty(18, 10000000000L, MAX); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); final String expected = String.format("The input must in the interval %s. You entered %s.", DATE_TIME_RANGE, MAX); @@ -106,8 +107,8 @@ public class ComparablePropertyValidatorTests { ExampleCommand command = exampleCommandWithComparableProperty(18, 10000000000L, MAX); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE, e.getMessage()); @@ -170,8 +171,8 @@ public class ComparablePropertyValidatorTests { ExampleCommand command = exampleCommandWithComparableProperty(null, null, null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); final String expected = String.format("The input must in the interval %s. You entered null.", DATE_TIME_RANGE); @@ -189,8 +190,8 @@ public class ComparablePropertyValidatorTests { ExampleCommand command = exampleCommandWithComparableProperty(null, null, null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE, e.getMessage()); diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/DoublePropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/DoublePropertyValidatorTests.java index dfa00ed..42c8c3a 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/DoublePropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/DoublePropertyValidatorTests.java @@ -26,6 +26,7 @@ import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.IValidator; +import xyz.zhouxy.plusone.validator.ValidationException; public class DoublePropertyValidatorTests { @@ -84,8 +85,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than '%s'.", MIN), e.getMessage()); } @@ -101,8 +102,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GT, e.getMessage()); } @@ -161,8 +162,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than '%s'.", MIN), e.getMessage()); } @@ -177,8 +178,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GT, e.getMessage()); } @@ -266,8 +267,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than or equal to '%s'.", MIN), e.getMessage()); } @@ -283,8 +284,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GE, e.getMessage()); } @@ -343,8 +344,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than or equal to '%s'.", MIN), e.getMessage()); } @@ -359,8 +360,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GE, e.getMessage()); } @@ -448,8 +449,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than '%s'.", MAX), e.getMessage()); } @@ -465,8 +466,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LT, e.getMessage()); } @@ -525,8 +526,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than '%s'.", MAX), e.getMessage()); } @@ -541,8 +542,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LT, e.getMessage()); } @@ -630,8 +631,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than or equal to '%s'.", MAX), e.getMessage()); } @@ -647,8 +648,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LE, e.getMessage()); } @@ -707,8 +708,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than or equal to '%s'.", MAX), e.getMessage()); } @@ -723,8 +724,8 @@ public class DoublePropertyValidatorTests { ExampleCommand command = exampleCommandWithDoubleProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LE, e.getMessage()); } diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/IntPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/IntPropertyValidatorTests.java index 8087594..7a5a489 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/IntPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/IntPropertyValidatorTests.java @@ -25,6 +25,7 @@ import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.IValidator; +import xyz.zhouxy.plusone.validator.ValidationException; public class IntPropertyValidatorTests { @@ -83,8 +84,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); } @@ -100,8 +101,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GT, e.getMessage()); } @@ -160,8 +161,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); } @@ -176,8 +177,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GT, e.getMessage()); } @@ -265,8 +266,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); } @@ -282,8 +283,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GE, e.getMessage()); } @@ -342,8 +343,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); } @@ -358,8 +359,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GE, e.getMessage()); } @@ -447,8 +448,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); } @@ -464,8 +465,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LT, e.getMessage()); } @@ -524,8 +525,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); } @@ -540,8 +541,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LT, e.getMessage()); } @@ -629,8 +630,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); } @@ -646,8 +647,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LE, e.getMessage()); } @@ -706,8 +707,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); } @@ -722,8 +723,8 @@ public class IntPropertyValidatorTests { ExampleCommand command = exampleCommandWithIntProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LE, e.getMessage()); } diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/LongPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/LongPropertyValidatorTests.java index 3051cab..0a3a3cd 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/LongPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/LongPropertyValidatorTests.java @@ -25,6 +25,7 @@ import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.IValidator; +import xyz.zhouxy.plusone.validator.ValidationException; public class LongPropertyValidatorTests { @@ -84,8 +85,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); } @@ -101,8 +102,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GT, e.getMessage()); } @@ -161,8 +162,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); } @@ -177,8 +178,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GT, e.getMessage()); } @@ -266,8 +267,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); } @@ -283,8 +284,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GE, e.getMessage()); } @@ -343,8 +344,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); } @@ -359,8 +360,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_GE, e.getMessage()); } @@ -448,8 +449,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); } @@ -465,8 +466,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LT, e.getMessage()); } @@ -525,8 +526,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); } @@ -541,8 +542,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LT, e.getMessage()); } @@ -630,8 +631,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); } @@ -647,8 +648,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(value); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LE, e.getMessage()); } @@ -707,8 +708,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); } @@ -723,8 +724,8 @@ public class LongPropertyValidatorTests { ExampleCommand command = exampleCommandWithLongProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, () -> validator.validate(command)); + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_LE, e.getMessage()); } diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java index cef261e..16ad0f8 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java @@ -18,7 +18,6 @@ package xyz.zhouxy.plusone.example.validator; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import java.time.DateTimeException; @@ -38,6 +37,7 @@ import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.example.Foo; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.IValidator; +import xyz.zhouxy.plusone.validator.ValidationException; public class ObjectPropertyValidatorTests { @@ -110,9 +110,9 @@ public class ObjectPropertyValidatorTests { .withRule(x -> false); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command)); - assertNull(eWithDefaultMessage.getMessage()); + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> ruleWithDefaultMessage.validate(command)); + assertEquals(ValidationException.DEFAULT_MESSAGE, eWithDefaultMessage.getMessage()); IValidator ruleWithMessage = new BaseValidator() { { @@ -120,8 +120,8 @@ public class ObjectPropertyValidatorTests { .withRule(x -> false, "invalid input."); } }; - IllegalArgumentException eWithMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + ValidationException eWithMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("invalid input.", eWithMessage.getMessage()); IValidator ruleWithExceptionSupplier = new BaseValidator() { @@ -198,8 +198,8 @@ public class ObjectPropertyValidatorTests { .notNull(); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, () -> defaultRule.validate(command)); + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> defaultRule.validate(command)); assertEquals("The input must not be null.", eWithDefaultMessage.getMessage()); IValidator ruleWithMessage = new BaseValidator() { @@ -208,8 +208,8 @@ public class ObjectPropertyValidatorTests { .notNull("The objectProperty could not be null."); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("The objectProperty could not be null.", eWithSpecifiedMessage.getMessage()); IValidator ruleWithExceptionSupplier = new BaseValidator() { @@ -279,8 +279,8 @@ public class ObjectPropertyValidatorTests { .isNull(); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command)); + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> ruleWithDefaultMessage.validate(command)); assertEquals("The input must be null.", eWithDefaultMessage.getMessage()); IValidator ruleWithMessage = new BaseValidator() { @@ -289,8 +289,8 @@ public class ObjectPropertyValidatorTests { .isNull("The objectProperty should be null."); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("The objectProperty should be null.", eWithSpecifiedMessage.getMessage()); IValidator ruleWithExceptionSupplier = new BaseValidator() { @@ -351,8 +351,8 @@ public class ObjectPropertyValidatorTests { ruleForString(ExampleCommand::getStringProperty).equalTo("Foo"); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, () -> defaultRule.validate(command)); + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> defaultRule.validate(command)); assertEquals("The input must be equal to 'Foo'.", eWithDefaultMessage.getMessage()); IValidator ruleWithMessage = new BaseValidator() { @@ -361,8 +361,8 @@ public class ObjectPropertyValidatorTests { "The stringProperty should be equal to 'Foo'."); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("The stringProperty should be equal to 'Foo'.", eWithSpecifiedMessage.getMessage()); IValidator ruleWithExceptionSupplier = new BaseValidator() { @@ -395,8 +395,8 @@ public class ObjectPropertyValidatorTests { ruleForString(ExampleCommand::getStringProperty).equalTo("Foo"); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, () -> defaultRule.validate(command)); + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> defaultRule.validate(command)); assertEquals("The input must be equal to 'Foo'.", eWithDefaultMessage.getMessage()); IValidator ruleWithMessage = new BaseValidator() { @@ -405,8 +405,8 @@ public class ObjectPropertyValidatorTests { "The stringProperty should be equal to 'Foo'."); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("The stringProperty should be equal to 'Foo'.", eWithSpecifiedMessage.getMessage()); IValidator ruleWithExceptionSupplier = new BaseValidator() { @@ -465,8 +465,8 @@ public class ObjectPropertyValidatorTests { .must(str -> Objects.equals(str, "Foo")); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command)); + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> ruleWithDefaultMessage.validate(command)); assertEquals("The specified condition was not met for the input.", eWithDefaultMessage.getMessage()); IValidator ruleWithMessage = new BaseValidator() { @@ -476,8 +476,8 @@ public class ObjectPropertyValidatorTests { "The stringProperty must be equal to 'Foo'."); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("The stringProperty must be equal to 'Foo'.", eWithSpecifiedMessage.getMessage()); IValidator ruleWithExceptionSupplier = new BaseValidator() { @@ -531,8 +531,8 @@ public class ObjectPropertyValidatorTests { .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo"))); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command)); + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> ruleWithDefaultMessage.validate(command)); assertEquals("The specified conditions were not met for the input.", eWithDefaultMessage.getMessage()); IValidator ruleWithMessage = new BaseValidator() { @@ -542,8 +542,8 @@ public class ObjectPropertyValidatorTests { "The stringProperty must be equal to 'Foo'."); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("The stringProperty must be equal to 'Foo'.", eWithSpecifiedMessage.getMessage()); IValidator ruleWithExceptionSupplier = new BaseValidator() { diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/StringPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/StringPropertyValidatorTests.java index 3564baf..9b349c5 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/StringPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/StringPropertyValidatorTests.java @@ -32,6 +32,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.IValidator; +import xyz.zhouxy.plusone.validator.ValidationException; public class StringPropertyValidatorTests { @@ -73,8 +74,8 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty("a"); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); } @@ -194,8 +195,8 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty("1234567"); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); } @@ -325,8 +326,8 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty("1234567"); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); } @@ -461,8 +462,8 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty("1234567"); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); } @@ -592,8 +593,8 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty("1234567"); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, + ValidationException e = assertThrows( + ValidationException.class, () -> validator.validate(command)); assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); } @@ -721,8 +722,8 @@ public class StringPropertyValidatorTests { .notBlank(); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> defaultRule.validate(command)); assertEquals("The input must not be blank.", eWithDefaultMessage.getMessage()); @@ -732,8 +733,8 @@ public class StringPropertyValidatorTests { .notBlank(MESSAGE_NOT_BLANK); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals(MESSAGE_NOT_BLANK, eWithSpecifiedMessage.getMessage()); @@ -771,8 +772,8 @@ public class StringPropertyValidatorTests { .notBlank(); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> defaultRule.validate(command)); assertEquals("The input must not be blank.", eWithDefaultMessage.getMessage()); @@ -782,8 +783,8 @@ public class StringPropertyValidatorTests { .notBlank(MESSAGE_NOT_BLANK); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals(MESSAGE_NOT_BLANK, eWithSpecifiedMessage.getMessage()); @@ -848,8 +849,8 @@ public class StringPropertyValidatorTests { .emailAddress(); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> defaultRule.validate(command)); assertEquals("The input is not a valid email address.", eWithDefaultMessage.getMessage()); @@ -859,8 +860,8 @@ public class StringPropertyValidatorTests { .emailAddress(MESSAGE_NOT_EMAIL); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals(MESSAGE_NOT_EMAIL, eWithSpecifiedMessage.getMessage()); @@ -923,8 +924,8 @@ public class StringPropertyValidatorTests { .notEmpty(); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> defaultRule.validate(command)); assertEquals("The input must not be empty.", eWithDefaultMessage.getMessage()); @@ -934,8 +935,8 @@ public class StringPropertyValidatorTests { .notEmpty(MESSAGE_NOT_EMPTY); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals(MESSAGE_NOT_EMPTY, eWithSpecifiedMessage.getMessage()); @@ -973,8 +974,8 @@ public class StringPropertyValidatorTests { .notEmpty(); } }; - IllegalArgumentException eWithDefaultMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithDefaultMessage = assertThrows( + ValidationException.class, () -> defaultRule.validate(command)); assertEquals("The input must not be empty.", eWithDefaultMessage.getMessage()); @@ -984,8 +985,8 @@ public class StringPropertyValidatorTests { .notEmpty(MESSAGE_NOT_EMPTY); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals(MESSAGE_NOT_EMPTY, eWithSpecifiedMessage.getMessage()); @@ -1049,8 +1050,8 @@ public class StringPropertyValidatorTests { .length(MIN_LENGTH, "The length of the string must be 6"); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("The length of the string must be 6", eWithSpecifiedMessage.getMessage()); @@ -1119,8 +1120,8 @@ public class StringPropertyValidatorTests { .length(MIN_LENGTH, MAX_LENGTH, String.format("Min length is %d, max length is %d", MIN_LENGTH, MAX_LENGTH)); } }; - IllegalArgumentException eWithSpecifiedMessage = assertThrows( - IllegalArgumentException.class, + ValidationException eWithSpecifiedMessage = assertThrows( + ValidationException.class, () -> ruleWithMessage.validate(command)); assertEquals("Min length is 6, max length is 8", eWithSpecifiedMessage.getMessage()); diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/map/validator/MapValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/map/validator/MapValidatorTests.java index b2a2afe..b3ebae8 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/map/validator/MapValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/map/validator/MapValidatorTests.java @@ -32,6 +32,7 @@ import com.google.common.collect.ImmutableSet; import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.Foo; import xyz.zhouxy.plusone.validator.MapValidator; +import xyz.zhouxy.plusone.validator.ValidationException; class MapValidatorTests { @@ -50,7 +51,7 @@ class MapValidatorTests { params.put(ParamsValidator.OBJECT_PROPERTY, new Foo(1, "Foo")); params.put(ParamsValidator.STRING_LIST_PROPERTY, Collections.emptyList()); - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { + ValidationException e = assertThrows(ValidationException.class, () -> { validator.validateAndCopy(params); }); assertEquals("'stringProperty' must be equal to 'stringProperty2'.", e.getMessage()); diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/ValidationExceptionTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/ValidationExceptionTests.java new file mode 100644 index 0000000..af84be2 --- /dev/null +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/ValidationExceptionTests.java @@ -0,0 +1,56 @@ +package xyz.zhouxy.plusone.validator; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +class ValidationExceptionTests { + + @Test + void withoutMessage() { + ValidationException ex = ValidationException.withDefaultMessage(); + assertNotNull(ex); + assertEquals(ValidationException.DEFAULT_MESSAGE, ex.getMessage()); + assertNull(ex.getCause()); + } + + @Test + void withMessage_String() { + String message = "Validation failed"; + ValidationException ex = ValidationException.withMessage(message); + assertNotNull(ex); + assertEquals(message, ex.getMessage()); + assertNull(ex.getCause()); + } + + @Test + void withMessage_TemplateAndArgs() { + String template = "Field %s is invalid: %s"; + Object[] args = {"username", "too short"}; + ValidationException ex = ValidationException.withMessage(template, args); + assertNotNull(ex); + assertEquals("Field username is invalid: too short", ex.getMessage()); + assertNull(ex.getCause()); + } + + @Test + void withCause() { + Throwable cause = new IOException("IO error"); + ValidationException ex = ValidationException.withCause(cause); + assertNotNull(ex); + assertEquals(cause, ex.getCause()); + assertEquals(cause.getClass().getName() + ": " + cause.getMessage(), ex.getMessage()); + } + + @Test + void withMessageAndCause() { + String message = "Validation failed"; + Throwable cause = new IllegalArgumentException("Invalid argument"); + ValidationException ex = ValidationException.withMessageAndCause(message, cause); + assertNotNull(ex); + assertEquals(message, ex.getMessage()); + assertEquals(cause, ex.getCause()); + } +}