From 6cd47e369e560c755a6d8428c7c0a659f3255828 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 28 May 2025 09:38:48 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20`BasePropertyValid?= =?UTF-8?q?ator#withRule`=20=E7=9A=84=E5=8F=8D=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ObjectPropertyValidatorTests.java | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) 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 403be15..43c1684 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,6 +18,7 @@ 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; @@ -44,12 +45,12 @@ public class ObjectPropertyValidatorTests { // ================================ @Test - void withRule() { + void withRule_validInput() { BaseValidator validator = new BaseValidator() { { ruleFor(ExampleCommand::getBoolProperty) .notNull("The boolProperty cannot be null.") - .withRule(Boolean.TRUE::equals, "The boolProperty should be true."); + .withRule(Boolean.TRUE::equals); ruleFor(ExampleCommand::getIntProperty) .withRule(intProperty -> intProperty > 0, "The intProperty should be greater than 0."); @@ -99,6 +100,50 @@ public class ObjectPropertyValidatorTests { assertDoesNotThrow(() -> validator.validate(command)); } + @Test + void withRule_invalidInputs() { + ExampleCommand command = new ExampleCommand(); + BaseValidator ruleWithDefaultMessage = new BaseValidator() { + { + ruleFor(ExampleCommand::getObjectProperty) + .withRule(x -> false); + } + }; + IllegalArgumentException eWithDefaultMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command)); + assertNull(eWithDefaultMessage.getMessage()); + + BaseValidator ruleWithMessage = new BaseValidator() { + { + ruleFor(ExampleCommand::getObjectProperty) + .withRule(x -> false, "invalid input."); + } + }; + IllegalArgumentException eWithMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + assertEquals("invalid input.", eWithMessage.getMessage()); + + BaseValidator ruleWithExceptionSupplier = new BaseValidator() { + { + ruleFor(ExampleCommand::getObjectProperty) + .withRule(x -> false, () -> ExampleException.withMessage("invalid input.")); + } + }; + ExampleException specifiedException = assertThrows( + ExampleException.class, () -> ruleWithExceptionSupplier.validate(command)); + assertEquals("invalid input.", specifiedException.getMessage()); + + BaseValidator ruleWithExceptionFunction = new BaseValidator() { + { + ruleFor(ExampleCommand::getObjectProperty) + .withRule(x -> false, x -> ExampleException.withMessage("invalid input: [%s].", x)); + } + }; + ExampleException specifiedException2 = assertThrows( + ExampleException.class, () -> ruleWithExceptionFunction.validate(command)); + assertEquals("invalid input: [null].", specifiedException2.getMessage()); + } + // ================================ // #endregion - withRule // ================================