refactor!: 校验不通过时默认抛出 ValidationException 而不是 IllegalArgumentException

- 新增 `ValidationException`
- 校验不通过时默认抛出 `ValidationException` 而不是 `IllegalArgumentException`
- 更新了相关的测试用例
This commit is contained in:
zhouxy108 2025-06-01 02:58:12 +08:00
parent a4d91dde35
commit 2d769fde26
18 changed files with 373 additions and 225 deletions

View File

@ -47,9 +47,8 @@ public abstract class BaseComparablePropertyValidator<T, TProperty extends Compa
* @return 属性校验器
*/
public TPropertyValidator inRange(Range<TProperty> 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();
}

View File

@ -51,7 +51,7 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
* @return 属性校验器
*/
public final TPropertyValidator withRule(Predicate<? super TProperty> rule) {
return withRule(rule, v -> new IllegalArgumentException());
return withRule(rule, v -> ValidationException.withDefaultMessage());
}
/**
@ -222,8 +222,8 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
* @return 属性校验器
*/
public TPropertyValidator equalTo(Object that) {
return equalTo(that,
value -> 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<T, TProperty, TPropertyValidator ext
// #endregion - must
// ================================
static <V> Function<V, IllegalArgumentException> convertToExceptionFunction(String errMsg) {
return value -> new IllegalArgumentException(errMsg);
static <V> Function<V, ValidationException> convertToExceptionFunction(String errMsg) {
return value -> ValidationException.withMessage(errMsg);
}
static <V> Function<V, ValidationException> convertToExceptionFunction(
String errorMessageTemplate, Object... errorMessageArgs) {
return value -> ValidationException.withMessage(errorMessageTemplate, errorMessageArgs);
}
static <V, E extends RuntimeException> Function<V, E> convertToExceptionFunction(

View File

@ -48,7 +48,7 @@ public abstract class BaseValidator<T> implements IValidator<T> {
* @param errorMessage 错误信息
*/
protected final void withRule(final Predicate<? super T> rule, final String errorMessage) {
withRule(rule, () -> new IllegalArgumentException(errorMessage));
withRule(rule, () -> ValidationException.withMessage(errorMessage));
}
/**

View File

@ -45,8 +45,7 @@ public class DoublePropertyValidator<T>
* @return 属性校验器
*/
public DoublePropertyValidator<T> 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<T>
* @return 属性校验器
*/
public DoublePropertyValidator<T> 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<T>
* @return 属性校验器
*/
public DoublePropertyValidator<T> 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<T>
* @return 属性校验器
*/
public DoublePropertyValidator<T> 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));
}
/**

View File

@ -45,8 +45,7 @@ public class IntPropertyValidator<T>
* @return 属性校验器
*/
public IntPropertyValidator<T> 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<T>
* @return 属性校验器
*/
public IntPropertyValidator<T> 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<T>
* @return 属性校验器
*/
public IntPropertyValidator<T> 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<T>
* @return 属性校验器
*/
public IntPropertyValidator<T> 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));
}
/**

View File

@ -45,8 +45,7 @@ public class LongPropertyValidator<T>
* @return 属性校验器
*/
public LongPropertyValidator<T> 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<T>
* @return 属性校验器
*/
public LongPropertyValidator<T> 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<T>
* @return 属性校验器
*/
public LongPropertyValidator<T> 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<T>
* @return 属性校验器
*/
public LongPropertyValidator<T> 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));
}
/**

View File

@ -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);
}
}

View File

@ -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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {

View File

@ -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());

View File

@ -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());
}

View File

@ -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());

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
{
@ -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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
@ -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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {

View File

@ -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());

View File

@ -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());

View File

@ -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());
}
}