From 83d9b05d63ab5a1a9f16ec4236618e28be33b08a Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 28 May 2025 02:36:34 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=AE=8C=E6=88=90=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validator => }/ExampleException.java | 2 +- .../java/xyz/zhouxy/plusone/example/Foo.java | 17 ++ .../example/validator/BaseValidatorTest.java | 88 ++++++ .../validator/BoolPropertyValidatorTests.java | 9 +- .../CollectionPropertyValidatorTests.java | 32 +++ .../ComparablePropertyValidatorTests.java | 5 +- .../DoublePropertyValidatorTests.java | 25 +- .../validator/IntPropertyValidatorTests.java | 17 +- .../validator/LongPropertyValidatorTests.java | 17 +- .../ObjectPropertyValidatorTests.java | 251 +++++++++++++++--- .../StringPropertyValidatorTests.java | 71 ++--- .../example/validator/ValidatorTests.java | 4 +- .../map/validator/MapValidatorTests.java | 135 ++++++---- .../validator/test/BaseValidatorTest.java | 169 ------------ 14 files changed, 503 insertions(+), 339 deletions(-) rename plusone-validator/src/test/java/xyz/zhouxy/plusone/{example/validator => }/ExampleException.java (96%) create mode 100644 plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BaseValidatorTest.java delete mode 100644 plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ExampleException.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/ExampleException.java similarity index 96% rename from plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ExampleException.java rename to plusone-validator/src/test/java/xyz/zhouxy/plusone/ExampleException.java index b19047b..8c2666a 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ExampleException.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/ExampleException.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package xyz.zhouxy.plusone.example.validator; +package xyz.zhouxy.plusone; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/Foo.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/Foo.java index d97cd37..dd7f38e 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/Foo.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/Foo.java @@ -15,6 +15,8 @@ */ package xyz.zhouxy.plusone.example; +import java.util.Objects; + public class Foo { private Integer intProperty; private String stringProperty; @@ -47,4 +49,19 @@ public class Foo { public String toString() { return "Foo [intProperty=" + intProperty + ", stringProperty=" + stringProperty + "]"; } + + @Override + public int hashCode() { + return Objects.hash(intProperty, stringProperty); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!(obj instanceof Foo)) + return false; + Foo other = (Foo) obj; + return Objects.equals(intProperty, other.intProperty) && Objects.equals(stringProperty, other.stringProperty); + } } 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 new file mode 100644 index 0000000..945bed3 --- /dev/null +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/BaseValidatorTest.java @@ -0,0 +1,88 @@ +package xyz.zhouxy.plusone.example.validator; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Objects; + +import org.junit.jupiter.api.Test; + +import xyz.zhouxy.plusone.ExampleException; +import xyz.zhouxy.plusone.example.ExampleCommand; +import xyz.zhouxy.plusone.validator.BaseValidator; + +class BaseValidatorTest { + + @Test + void withRule_validInput() { + ExampleCommand exampleCommand = new ExampleCommand(); + exampleCommand.setStringProperty("Foo"); + + BaseValidator validator = new BaseValidator() { + { + withRule(command -> Objects.equals(command.getStringProperty(), "Foo"), + "The stringProperty must be equal to 'Foo'"); + withRule(command -> Objects.equals(command.getStringProperty(), "Foo"), + () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'")); + withRule(command -> Objects.equals(command.getStringProperty(), "Foo"), + str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str)); + withRule(command -> { + final String stringProperty = command.getStringProperty(); + if (!Objects.equals(stringProperty, "Foo")) { + throw ExampleException.withMessage(""); + } + }); + } + }; + assertDoesNotThrow(() -> validator.validate(exampleCommand)); + } + + @Test + void withRule_invalidInput() { + ExampleCommand command = new ExampleCommand(); + + BaseValidator ruleWithMessage = new BaseValidator() { + { + withRule(command -> Objects.equals(command.getStringProperty(), "Foo"), + "The stringProperty must be equal to 'Foo'"); + } + }; + IllegalArgumentException eWithSpecifiedMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + assertEquals("The stringProperty must be equal to 'Foo'", eWithSpecifiedMessage.getMessage()); + + BaseValidator ruleWithExceptionSupplier = new BaseValidator() { + { + withRule(command -> Objects.equals(command.getStringProperty(), "Foo"), + () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'")); + } + }; + ExampleException specifiedException = assertThrows( + ExampleException.class, + () -> ruleWithExceptionSupplier.validate(command)); + assertEquals("The stringProperty must be equal to 'Foo'", specifiedException.getMessage()); + + BaseValidator ruleWithExceptionFunction = new BaseValidator() { + { + withRule(command -> Objects.equals(command.getStringProperty(), "Foo"), command -> ExampleException + .withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", command.getStringProperty())); + } + }; + ExampleException specifiedException2 = assertThrows( + ExampleException.class, + () -> ruleWithExceptionFunction.validate(command)); + assertEquals("The stringProperty must be equal to 'Foo', but is was 'null'.", specifiedException2.getMessage()); + + BaseValidator rule = new BaseValidator() { + { + withRule(command -> { + final String stringProperty = command.getStringProperty(); + if (!Objects.equals(stringProperty, "Foo")) { + throw ExampleException.withMessage(""); + } + }); + } + }; + ExampleException e = assertThrows(ExampleException.class, () -> rule.validate(command)); + assertEquals("", e.getMessage()); + } +} 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 7018f64..6a2b4e4 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 @@ -19,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; +import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; @@ -62,7 +63,7 @@ public class BoolPropertyValidatorTests { IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value must be true.", exception.getMessage()); + assertEquals("The input must be true.", exception.getMessage()); } @Test @@ -136,7 +137,7 @@ public class BoolPropertyValidatorTests { IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value must be true.", exception.getMessage()); + assertEquals("The input must be true.", exception.getMessage()); } @Test @@ -238,7 +239,7 @@ public class BoolPropertyValidatorTests { IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value must be false.", exception.getMessage()); + assertEquals("The input must be false.", exception.getMessage()); } @Test @@ -312,7 +313,7 @@ public class BoolPropertyValidatorTests { IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value must be false.", exception.getMessage()); + assertEquals("The input must be false.", exception.getMessage()); } @Test 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 6e535a8..7b65ad3 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 @@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test; import com.google.common.collect.Lists; +import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; @@ -43,6 +44,7 @@ public class CollectionPropertyValidatorTests { void notEmpty_stringListIsNotEmpty() { BaseValidator validator = new BaseValidator() { { + ruleForCollection(ExampleCommand::getStringListProperty).notEmpty(); ruleForCollection(ExampleCommand::getStringListProperty).notEmpty(MESSAGE_NOT_EMPTY); ruleForCollection(ExampleCommand::getStringListProperty) .notEmpty(() -> ExampleException.withMessage(MESSAGE_NOT_EMPTY)); @@ -56,6 +58,20 @@ public class CollectionPropertyValidatorTests { assertDoesNotThrow(() -> validator.validate(command)); } + @Test + void notEmpty_default_stringListIsEmpty() { + BaseValidator validator = new BaseValidator() { + { + ruleForCollection(ExampleCommand::getStringListProperty).notEmpty(); + } + }; + + ExampleCommand command = exampleCommandWithStringListProperty(Collections.emptyList()); + + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> validator.validate(command)); + assertEquals("The input must not be empty.", e.getMessage()); + } + @Test void notEmpty_message_stringListIsEmpty() { BaseValidator validator = new BaseValidator() { @@ -158,6 +174,7 @@ public class CollectionPropertyValidatorTests { void isEmpty_stringListIsEmpty() { BaseValidator validator = new BaseValidator() { { + ruleForCollection(ExampleCommand::getStringListProperty).isEmpty(); ruleForCollection(ExampleCommand::getStringListProperty).isEmpty(MESSAGE_EMPTY); ruleForCollection(ExampleCommand::getStringListProperty) .isEmpty(() -> ExampleException.withMessage(MESSAGE_EMPTY)); @@ -175,6 +192,7 @@ public class CollectionPropertyValidatorTests { void isEmpty_stringListIsNull() { BaseValidator validator = new BaseValidator() { { + ruleForCollection(ExampleCommand::getStringListProperty).isEmpty(); ruleForCollection(ExampleCommand::getStringListProperty).isEmpty(MESSAGE_EMPTY); ruleForCollection(ExampleCommand::getStringListProperty) .isEmpty(() -> ExampleException.withMessage(MESSAGE_EMPTY)); @@ -188,6 +206,20 @@ public class CollectionPropertyValidatorTests { assertDoesNotThrow(() -> validator.validate(command)); } + @Test + void isEmpty_default_stringListIsNotEmpty() { + BaseValidator validator = new BaseValidator() { + { + ruleForCollection(ExampleCommand::getStringListProperty).isEmpty(); + } + }; + + ExampleCommand command = exampleCommandWithStringListProperty(Lists.newArrayList("A", "B", "C")); + + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> validator.validate(command)); + assertEquals("The input must be empty.", e.getMessage()); + } + @Test void isEmpty_message_stringListIsNotEmpty() { BaseValidator validator = new BaseValidator() { 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 f8fb0bd..7612fb1 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 @@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test; import com.google.common.collect.Range; +import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; @@ -89,7 +90,7 @@ public class ComparablePropertyValidatorTests { IllegalArgumentException.class, () -> validator.validate(command)); - final String expected = String.format("The value is not in the interval %s", DATE_TIME_RANGE); + final String expected = String.format("The input must in the interval %s. You entered %s.", DATE_TIME_RANGE, MAX); assertEquals(expected, e.getMessage()); } @@ -172,7 +173,7 @@ public class ComparablePropertyValidatorTests { IllegalArgumentException.class, () -> validator.validate(command)); - final String expected = String.format("The value is not in the interval %s", DATE_TIME_RANGE); + final String expected = String.format("The input must in the interval %s. You entered null.", DATE_TIME_RANGE); assertEquals(expected, 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 685e3de..0749655 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 @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; @@ -30,11 +31,11 @@ public class DoublePropertyValidatorTests { static final double MIN = 1.0; static final double MAX = 5.0; - static final String MESSAGE_GT = "The value should be greater than " + MIN; - static final String MESSAGE_GE = "The value should be greater than or equal to " + MIN; + static final String MESSAGE_GT = "The input must be greater than " + MIN; + static final String MESSAGE_GE = "The input must be greater than or equal to " + MIN; - static final String MESSAGE_LT = "The value should be less than " + MAX; - static final String MESSAGE_LE = "The value should be less than or equal to " + MAX; + static final String MESSAGE_LT = "The input must be less than " + MAX; + static final String MESSAGE_LE = "The input must be less than or equal to " + MAX; // ================================ // #region - gt_validValue @@ -84,7 +85,7 @@ public class DoublePropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than '%s'.", MIN), e.getMessage()); } @ParameterizedTest @@ -161,7 +162,7 @@ public class DoublePropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than '%s'.", MIN), e.getMessage()); } @Test @@ -266,7 +267,7 @@ public class DoublePropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than or equal to " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than or equal to '%s'.", MIN), e.getMessage()); } @ParameterizedTest @@ -343,7 +344,7 @@ public class DoublePropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than or equal to " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than or equal to '%s'.", MIN), e.getMessage()); } @Test @@ -448,7 +449,7 @@ public class DoublePropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than '%s'.", MAX), e.getMessage()); } @ParameterizedTest @@ -525,7 +526,7 @@ public class DoublePropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than '%s'.", MAX), e.getMessage()); } @Test @@ -630,7 +631,7 @@ public class DoublePropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than or equal to " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than or equal to '%s'.", MAX), e.getMessage()); } @ParameterizedTest @@ -707,7 +708,7 @@ public class DoublePropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than or equal to " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than or equal to '%s'.", MAX), e.getMessage()); } @Test 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 69be9c1..a04572d 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 @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; @@ -83,7 +84,7 @@ public class IntPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); } @ParameterizedTest @@ -160,7 +161,7 @@ public class IntPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); } @Test @@ -265,7 +266,7 @@ public class IntPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than or equal to " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); } @ParameterizedTest @@ -342,7 +343,7 @@ public class IntPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than or equal to " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); } @Test @@ -447,7 +448,7 @@ public class IntPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); } @ParameterizedTest @@ -524,7 +525,7 @@ public class IntPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); } @Test @@ -629,7 +630,7 @@ public class IntPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than or equal to " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); } @ParameterizedTest @@ -706,7 +707,7 @@ public class IntPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than or equal to " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); } @Test 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 86a0ea7..72a2e07 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 @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; @@ -84,7 +85,7 @@ public class LongPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); } @ParameterizedTest @@ -161,7 +162,7 @@ public class LongPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); } @Test @@ -266,7 +267,7 @@ public class LongPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than or equal to " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); } @ParameterizedTest @@ -343,7 +344,7 @@ public class LongPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be greater than or equal to " + MIN, e.getMessage()); + assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); } @Test @@ -448,7 +449,7 @@ public class LongPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); } @ParameterizedTest @@ -525,7 +526,7 @@ public class LongPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); } @Test @@ -630,7 +631,7 @@ public class LongPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than or equal to " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); } @ParameterizedTest @@ -707,7 +708,7 @@ public class LongPropertyValidatorTests { IllegalArgumentException e = assertThrows( IllegalArgumentException.class, () -> validator.validate(command)); - assertEquals("The value should be less than or equal to " + MAX, e.getMessage()); + assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); } @Test 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 5405bb9..403be15 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 @@ -26,8 +26,10 @@ import java.util.Objects; import org.junit.jupiter.api.Test; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.commons.collection.CollectionTools; import xyz.zhouxy.plusone.commons.util.DateTimeTools; import xyz.zhouxy.plusone.commons.util.StringTools; @@ -152,7 +154,7 @@ public class ObjectPropertyValidatorTests { }; IllegalArgumentException eWithDefaultMessage = assertThrows( IllegalArgumentException.class, () -> defaultRule.validate(command)); - assertEquals("Value could not be null.", eWithDefaultMessage.getMessage()); + assertEquals("The input must not be null.", eWithDefaultMessage.getMessage()); BaseValidator ruleWithMessage = new BaseValidator() { { @@ -198,7 +200,7 @@ public class ObjectPropertyValidatorTests { BaseValidator validator = new BaseValidator() { { ruleForBool(ExampleCommand::getBoolProperty) - .isNull("The boolProperty should be null"); + .isNull(); ruleForInt(ExampleCommand::getIntProperty) .isNull("The intProperty should be null"); ruleForLong(ExampleCommand::getLongProperty) @@ -206,7 +208,7 @@ public class ObjectPropertyValidatorTests { ruleForDouble(ExampleCommand::getDoubleProperty) .isNull(d -> ExampleException.withMessage("The doubleProperty should be null, but it was %s", d)); ruleForString(ExampleCommand::getStringProperty) - .isNull("The stringProperty should be null"); + .isNull(); ruleForComparable(ExampleCommand::getDateTimeProperty) .isNull("The dateTimeProperty should be null"); ruleFor(ExampleCommand::getObjectProperty) @@ -225,6 +227,16 @@ public class ObjectPropertyValidatorTests { ExampleCommand command = new ExampleCommand(); command.setObjectProperty(new Foo(Integer.MAX_VALUE, "StringValue")); + BaseValidator ruleWithDefaultMessage = new BaseValidator() { + { + ruleFor(ExampleCommand::getObjectProperty) + .isNull(); + } + }; + IllegalArgumentException eWithDefaultMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command)); + assertEquals("The input must be null.", eWithDefaultMessage.getMessage()); + BaseValidator ruleWithMessage = new BaseValidator() { { ruleFor(ExampleCommand::getObjectProperty) @@ -265,23 +277,111 @@ public class ObjectPropertyValidatorTests { // ================================ @Test - void equalsThat() { + void equalsThat_validInput() { + BaseValidator validator = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .equalsThat("Foo") + .equalsThat("Foo", "The stringProperty should be equal to 'Foo'.") + .equalsThat("Foo", () -> + ExampleException.withMessage("The stringProperty should be equal to 'Foo'.")) + .equalsThat("Foo", str -> + ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str)); + } + }; + ExampleCommand command = new ExampleCommand(); + command.setStringProperty("Foo"); + assertDoesNotThrow(() -> validator.validate(command)); } @Test - void equalsThat2() { + void equalsThat_invalidInput() { + ExampleCommand command = new ExampleCommand(); + command.setStringProperty("Bar"); + BaseValidator defaultRule = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo"); + } + }; + IllegalArgumentException eWithDefaultMessage = assertThrows( + IllegalArgumentException.class, () -> defaultRule.validate(command)); + assertEquals("The input must be equal to 'Foo'.", eWithDefaultMessage.getMessage()); + + BaseValidator ruleWithMessage = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", + "The stringProperty should be equal to 'Foo'."); + } + }; + IllegalArgumentException eWithSpecifiedMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + assertEquals("The stringProperty should be equal to 'Foo'.", eWithSpecifiedMessage.getMessage()); + + BaseValidator ruleWithExceptionSupplier = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", + () -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'.")); + } + }; + ExampleException specifiedException = assertThrows( + ExampleException.class, () -> ruleWithExceptionSupplier.validate(command)); + assertEquals("The stringProperty should be equal to 'Foo'.", specifiedException.getMessage()); + + BaseValidator ruleWithExceptionFunction = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", + str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str)); + } + }; + ExampleException specifiedException2 = assertThrows( + ExampleException.class, () -> ruleWithExceptionFunction.validate(command)); + assertEquals("The stringProperty should be equal to 'Foo', but is was 'Bar'.", specifiedException2.getMessage()); } @Test - void equalsThat3() { + void equalsThat_nullInput() { + ExampleCommand command = new ExampleCommand(); - } + BaseValidator defaultRule = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo"); + } + }; + IllegalArgumentException eWithDefaultMessage = assertThrows( + IllegalArgumentException.class, () -> defaultRule.validate(command)); + assertEquals("The input must be equal to 'Foo'.", eWithDefaultMessage.getMessage()); - @Test - void equalsThat4() { + BaseValidator ruleWithMessage = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", + "The stringProperty should be equal to 'Foo'."); + } + }; + IllegalArgumentException eWithSpecifiedMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + assertEquals("The stringProperty should be equal to 'Foo'.", eWithSpecifiedMessage.getMessage()); + BaseValidator ruleWithExceptionSupplier = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", + () -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'.")); + } + }; + ExampleException specifiedException = assertThrows( + ExampleException.class, () -> ruleWithExceptionSupplier.validate(command)); + assertEquals("The stringProperty should be equal to 'Foo'.", specifiedException.getMessage()); + + BaseValidator ruleWithExceptionFunction = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", + str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str)); + } + }; + ExampleException specifiedException2 = assertThrows( + ExampleException.class, () -> ruleWithExceptionFunction.validate(command)); + assertEquals("The stringProperty should be equal to 'Foo', but is was 'null'.", specifiedException2.getMessage()); } // ================================ @@ -293,43 +393,134 @@ public class ObjectPropertyValidatorTests { // ================================ @Test - void must() { + void must_oneCondition_valid() { + ExampleCommand command = new ExampleCommand(); + command.setStringProperty("Foo"); + BaseValidator ruleWithDefaultMessage = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(str -> Objects.equals(str, "Foo")) + .must(str -> Objects.equals(str, "Foo"), "The stringProperty must be equal to 'Foo'.") + .must(str -> Objects.equals(str, "Foo"), () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'.")) + .must(str -> Objects.equals(str, "Foo"), str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str)); + } + }; + assertDoesNotThrow(() -> ruleWithDefaultMessage.validate(command)); } @Test - void must2() { + void must_oneCondition_invalid() { + ExampleCommand command = new ExampleCommand(); + BaseValidator ruleWithDefaultMessage = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(str -> Objects.equals(str, "Foo")); + } + }; + IllegalArgumentException eWithDefaultMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command)); + assertEquals("The specified condition was not met for the input.", eWithDefaultMessage.getMessage()); + + BaseValidator ruleWithMessage = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(str -> Objects.equals(str, "Foo"), + "The stringProperty must be equal to 'Foo'."); + } + }; + IllegalArgumentException eWithSpecifiedMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + assertEquals("The stringProperty must be equal to 'Foo'.", eWithSpecifiedMessage.getMessage()); + + BaseValidator ruleWithExceptionSupplier = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(str -> Objects.equals(str, "Foo"), + () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'.")); + } + }; + ExampleException specifiedException = assertThrows( + ExampleException.class, () -> ruleWithExceptionSupplier.validate(command)); + assertEquals("The stringProperty must be equal to 'Foo'.", specifiedException.getMessage()); + + BaseValidator ruleWithExceptionFunction = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(str -> Objects.equals(str, "Foo"), + str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str)); + } + }; + ExampleException specifiedException2 = assertThrows( + ExampleException.class, () -> ruleWithExceptionFunction.validate(command)); + assertEquals("The stringProperty must be equal to 'Foo', but is was 'null'.", specifiedException2.getMessage()); } @Test - void must3() { + void must_multipleConditions_valid() { + ExampleCommand command = new ExampleCommand(); + command.setStringProperty("Foo"); + BaseValidator ruleWithDefaultMessage = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo"))) + .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), "The stringProperty must be equal to 'Foo'.") + .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'.")) + .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str)); + } + }; + assertDoesNotThrow(() -> ruleWithDefaultMessage.validate(command)); } @Test - void must4() { + void must_multipleConditions_invalid() { + ExampleCommand command = new ExampleCommand(); + command.setStringProperty("Bar"); - } + BaseValidator ruleWithDefaultMessage = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo"))); + } + }; + IllegalArgumentException eWithDefaultMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command)); + assertEquals("The specified conditions were not met for the input.", eWithDefaultMessage.getMessage()); - @Test - void must5() { + BaseValidator ruleWithMessage = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), + "The stringProperty must be equal to 'Foo'."); + } + }; + IllegalArgumentException eWithSpecifiedMessage = assertThrows( + IllegalArgumentException.class, () -> ruleWithMessage.validate(command)); + assertEquals("The stringProperty must be equal to 'Foo'.", eWithSpecifiedMessage.getMessage()); - } - - @Test - void must6() { - - } - - @Test - void must7() { - - } - - @Test - void must8() { + BaseValidator ruleWithExceptionSupplier = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), + () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'.")); + } + }; + ExampleException specifiedException = assertThrows( + ExampleException.class, () -> ruleWithExceptionSupplier.validate(command)); + assertEquals("The stringProperty must be equal to 'Foo'.", specifiedException.getMessage()); + BaseValidator ruleWithExceptionFunction = new BaseValidator() { + { + ruleForString(ExampleCommand::getStringProperty) + .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), + str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str)); + } + }; + ExampleException specifiedException2 = assertThrows( + ExampleException.class, () -> ruleWithExceptionFunction.validate(command)); + assertEquals("The stringProperty must be equal to 'Foo', but is was 'Bar'.", specifiedException2.getMessage()); } // ================================ 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 2eb0aae..dec3851 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 @@ -27,6 +27,7 @@ import org.junit.jupiter.params.provider.ValueSource; import com.google.common.collect.Lists; +import xyz.zhouxy.plusone.ExampleException; import xyz.zhouxy.plusone.commons.util.StringTools; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.validator.BaseValidator; @@ -253,10 +254,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, - () -> validator.validate(command)); - assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -273,10 +271,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -294,10 +289,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - assertEquals("Input should match pattern, but it is null", e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -390,10 +382,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, - () -> validator.validate(command)); - assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -409,10 +398,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -429,10 +415,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - assertEquals("Input should match pattern, but it is null", e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } // ================================ @@ -538,10 +521,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, - () -> validator.validate(command)); - assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -558,10 +538,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -579,10 +556,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - assertEquals("Input should match pattern, but it is null", e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -675,10 +649,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - IllegalArgumentException e = assertThrows( - IllegalArgumentException.class, - () -> validator.validate(command)); - assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -694,10 +665,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } @Test @@ -714,10 +682,7 @@ public class StringPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithStringProperty(null); - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - assertEquals("Input should match pattern, but it is null", e.getMessage()); + assertDoesNotThrow(() -> validator.validate(command)); } // ================================ @@ -758,7 +723,7 @@ public class StringPropertyValidatorTests { IllegalArgumentException eWithDefaultMessage = assertThrows( IllegalArgumentException.class, () -> defaultRule.validate(command)); - assertEquals("The value must have text; it must not be null, empty, or blank.", eWithDefaultMessage.getMessage()); + assertEquals("The input must not be blank.", eWithDefaultMessage.getMessage()); BaseValidator ruleWithMessage = new BaseValidator() { { @@ -808,7 +773,7 @@ public class StringPropertyValidatorTests { IllegalArgumentException eWithDefaultMessage = assertThrows( IllegalArgumentException.class, () -> defaultRule.validate(command)); - assertEquals("The value must have text; it must not be null, empty, or blank.", eWithDefaultMessage.getMessage()); + assertEquals("The input must not be blank.", eWithDefaultMessage.getMessage()); BaseValidator ruleWithMessage = new BaseValidator() { { @@ -885,7 +850,7 @@ public class StringPropertyValidatorTests { IllegalArgumentException eWithDefaultMessage = assertThrows( IllegalArgumentException.class, () -> defaultRule.validate(command)); - assertEquals("The value is not an email address.", eWithDefaultMessage.getMessage()); + assertEquals("The input is not a valid email address.", eWithDefaultMessage.getMessage()); BaseValidator ruleWithMessage = new BaseValidator() { { @@ -960,7 +925,7 @@ public class StringPropertyValidatorTests { IllegalArgumentException eWithDefaultMessage = assertThrows( IllegalArgumentException.class, () -> defaultRule.validate(command)); - assertEquals("The value must not be empty.", eWithDefaultMessage.getMessage()); + assertEquals("The input must not be empty.", eWithDefaultMessage.getMessage()); BaseValidator ruleWithMessage = new BaseValidator() { { @@ -1010,7 +975,7 @@ public class StringPropertyValidatorTests { IllegalArgumentException eWithDefaultMessage = assertThrows( IllegalArgumentException.class, () -> defaultRule.validate(command)); - assertEquals("The value must not be empty.", eWithDefaultMessage.getMessage()); + assertEquals("The input must not be empty.", eWithDefaultMessage.getMessage()); BaseValidator ruleWithMessage = new BaseValidator() { { diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ValidatorTests.java index 1f412ea..57d3fb5 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ValidatorTests.java @@ -1,5 +1,6 @@ package xyz.zhouxy.plusone.example.validator; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static xyz.zhouxy.plusone.commons.constant.PatternConsts.*; import java.util.Arrays; @@ -57,8 +58,7 @@ class ValidatorTests { // 传入 predicate 和 error message .addRule(command -> Objects.equals(command.getPassword(), command.getPassword2()), "两次输入的密码不一致"); - registerCommandValidator.validate(registerCommand); - System.out.println(registerCommand); + assertDoesNotThrow(() -> registerCommandValidator.validate(registerCommand)); } /** 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 986589f..3cc7327 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 @@ -2,16 +2,19 @@ package xyz.zhouxy.plusone.map.validator; import static org.junit.jupiter.api.Assertions.*; -import java.util.Arrays; +import java.time.LocalDateTime; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; -import java.util.regex.Pattern; +import java.util.Set; import org.junit.jupiter.api.Test; -import xyz.zhouxy.plusone.commons.constant.PatternConsts; -import xyz.zhouxy.plusone.commons.util.StringTools; +import com.google.common.collect.ImmutableSet; + +import xyz.zhouxy.plusone.ExampleException; +import xyz.zhouxy.plusone.example.Foo; import xyz.zhouxy.plusone.validator.MapValidator; class MapValidatorTests { @@ -21,66 +24,98 @@ class MapValidatorTests { @Test void testValidateAndCopy() { Map params = new HashMap<>(); - params.put(ParamsValidator.USERNAME, "ZhouXY"); - params.put(ParamsValidator.ACCOUNT, "zhouxy@code108.cn"); - params.put(ParamsValidator.PASSWORD, "99Code108"); - params.put(ParamsValidator.PASSWORD2, "99Code108"); - params.put(ParamsValidator.AGE, 18); - params.put(ParamsValidator.BOOLEAN, true); + params.put(ParamsValidator.BOOL_PROPERTY, true); + params.put(ParamsValidator.INT_PROPERTY, Integer.MAX_VALUE); + params.put(ParamsValidator.LONG_PROPERTY, Long.MAX_VALUE); + params.put(ParamsValidator.DOUBLE_PROPERTY, Double.MAX_VALUE); + params.put(ParamsValidator.STRING_PROPERTY, "Foo"); + params.put(ParamsValidator.STRING_PROPERTY2, "Bar"); + params.put(ParamsValidator.DATE_TIME_PROPERTY, LocalDateTime.of(2008, 8, 8, 20, 8)); + params.put(ParamsValidator.OBJECT_PROPERTY, new Foo(1, "Foo")); + params.put(ParamsValidator.STRING_LIST_PROPERTY, Collections.emptyList()); - params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", "")); - assertThrows(IllegalArgumentException.class, () -> { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { validator.validateAndCopy(params); }); + assertEquals("'stringProperty' must be equal to 'stringProperty2'.", e.getMessage()); - params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", "developer")); - Map validatedParams = validator.validateAndCopy(params); - System.out.println(validatedParams); + params.put(ParamsValidator.STRING_PROPERTY2, "Foo"); + assertAll(() -> { + Map validatedParams = validator.validateAndCopy(params); + assertEquals(ParamsValidator.keySet(), validatedParams.keySet()); + + assertEquals(true, validatedParams.get(ParamsValidator.BOOL_PROPERTY)); + assertEquals(Integer.MAX_VALUE, validatedParams.get(ParamsValidator.INT_PROPERTY)); + assertEquals(Long.MAX_VALUE, validatedParams.get(ParamsValidator.LONG_PROPERTY)); + assertEquals(Double.MAX_VALUE, validatedParams.get(ParamsValidator.DOUBLE_PROPERTY)); + assertEquals("Foo", validatedParams.get(ParamsValidator.STRING_PROPERTY)); + assertEquals(LocalDateTime.of(2008, 8, 8, 20, 8), validatedParams.get(ParamsValidator.DATE_TIME_PROPERTY)); + assertEquals(new Foo(1, "Foo"), validatedParams.get(ParamsValidator.OBJECT_PROPERTY)); + assertEquals(Collections.emptyList(), validatedParams.get(ParamsValidator.STRING_LIST_PROPERTY)); + }); + + assertAll(() -> { + Map validatedParams = validator.validateAndCopy(params, + ParamsValidator.LONG_PROPERTY, ParamsValidator.STRING_PROPERTY2); + assertEquals(ImmutableSet.of(ParamsValidator.LONG_PROPERTY, ParamsValidator.STRING_PROPERTY2), + validatedParams.keySet()); + + assertEquals(Long.MAX_VALUE, validatedParams.get(ParamsValidator.LONG_PROPERTY)); + assertEquals("Foo", validatedParams.get(ParamsValidator.STRING_PROPERTY2)); + + }); + + assertAll(() -> { + Set keySet = ImmutableSet.of(ParamsValidator.LONG_PROPERTY, ParamsValidator.STRING_PROPERTY2); + Map validatedParams = validator.validateAndCopy(params, keySet); + assertEquals(keySet, validatedParams.keySet()); + + assertEquals(Long.MAX_VALUE, validatedParams.get(ParamsValidator.LONG_PROPERTY)); + assertEquals("Foo", validatedParams.get(ParamsValidator.STRING_PROPERTY2)); + }); } } class ParamsValidator extends MapValidator { - public static final String USERNAME = "username"; - public static final String ACCOUNT = "account"; - public static final String PASSWORD = "password"; - public static final String PASSWORD2 = "password2"; - public static final String AGE = "age"; - public static final String BOOLEAN = "boolean"; - public static final String ROLE_LIST = "roleList"; + public static final String BOOL_PROPERTY = "boolProperty"; + public static final String INT_PROPERTY = "intProperty"; + public static final String LONG_PROPERTY = "longProperty"; + public static final String DOUBLE_PROPERTY = "doubleProperty"; + public static final String STRING_PROPERTY = "stringProperty"; + public static final String STRING_PROPERTY2 = "stringProperty2"; + public static final String DATE_TIME_PROPERTY = "dateTimeProperty"; + public static final String OBJECT_PROPERTY = "objectProperty"; + public static final String STRING_LIST_PROPERTY = "stringListProperty"; public static final ParamsValidator INSTANCE = new ParamsValidator(); private ParamsValidator() { - super(new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST }); - ruleForString(USERNAME) - .notBlank("用户名不能为空") - .matches(PatternConsts.USERNAME, - username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); - - ruleForString(ACCOUNT) - .notBlank("账号不能为空") - .matchesOne(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE }, "请输入正确的邮箱地址或手机号"); - - ruleForString(PASSWORD) - .notEmpty("密码不能为空") - .matches(PatternConsts.PASSWORD, "密码不符合规范"); + super(new String[] { BOOL_PROPERTY, INT_PROPERTY, LONG_PROPERTY, DOUBLE_PROPERTY, STRING_PROPERTY, + DATE_TIME_PROPERTY, OBJECT_PROPERTY, STRING_LIST_PROPERTY }); + ruleForBool(BOOL_PROPERTY) + .notNull(); + ruleForInt(INT_PROPERTY) + .notNull("The intProperty cannot be null"); + ruleForLong(LONG_PROPERTY) + .notNull(() -> ExampleException.withMessage("The longProperty cannot be null")); + ruleForDouble(DOUBLE_PROPERTY) + .notNull(d -> ExampleException.withMessage("The doubleProperty cannot be null, but it was %s", d)); + ruleForString(STRING_PROPERTY) + .notNull(); + ruleFor(DATE_TIME_PROPERTY) + .notNull("The dateTimeProperty cannot be null"); + ruleFor(OBJECT_PROPERTY) + .notNull(() -> ExampleException.withMessage("The objectProperty cannot be null")); + ruleForCollection(STRING_LIST_PROPERTY) + .notNull(d -> ExampleException.withMessage("The stringListProperty cannot be null, but it was %s", d)); // 校验到多个属性,只能针对 map 本身进行校验 - withRule(m -> Objects.equals(m.get(PASSWORD), m.get(PASSWORD2)), - "两次输入的密码不一样!"); + withRule(m -> Objects.equals(m.get(STRING_PROPERTY), m.get(STRING_PROPERTY2)), + "'stringProperty' must be equal to 'stringProperty2'."); + } - ruleForInt(AGE) - .withRule(Objects::nonNull) - .ge(18) - .le(60); - - ruleForBool(BOOLEAN) - .notNull("Boolean property could not be null.") - .isTrueValue("Boolean property must be true."); - - this.ruleForCollection(ROLE_LIST) - .notEmpty("角色列表不能为空!") - .withRule(l -> l.stream().allMatch(StringTools::isNotBlank), - () -> new IllegalArgumentException("角色标识不能为空!")); + public static Set keySet() { + return ImmutableSet.of(BOOL_PROPERTY, INT_PROPERTY, LONG_PROPERTY, DOUBLE_PROPERTY, STRING_PROPERTY, + DATE_TIME_PROPERTY, OBJECT_PROPERTY, STRING_LIST_PROPERTY); } } diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java deleted file mode 100644 index 859ad62..0000000 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java +++ /dev/null @@ -1,169 +0,0 @@ -package xyz.zhouxy.plusone.validator.test; - -import java.time.Year; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; - -import org.junit.jupiter.api.Test; - -import com.google.common.collect.Range; - -import xyz.zhouxy.plusone.commons.constant.PatternConsts; -import xyz.zhouxy.plusone.commons.function.PredicateTools; -import xyz.zhouxy.plusone.commons.util.RegexTools; -import xyz.zhouxy.plusone.commons.util.StringTools; -import xyz.zhouxy.plusone.validator.BaseValidator; -import xyz.zhouxy.plusone.validator.ValidTools; - -class BaseValidatorTest { - @Test - void testValidate() { - RegisterCommand registerCommand = new RegisterCommand("zhouxy108", "luquanlion@outlook.com", "22336", - "A1b2C3d4", - "A1b2C3d4", - Arrays.asList(new String[] { "admin", "editor" }), - 2000); - RegisterCommandValidator.INSTANCE.validate(registerCommand); - ValidTools.validate(registerCommand, RegisterCommandValidator.INSTANCE); - System.out.println(registerCommand); - } - - static class RegisterCommandValidator extends BaseValidator { - - static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator(); - - private RegisterCommandValidator() { - int thisYear = Year.now().getValue(); - - ruleForString(RegisterCommand::getUsername) - .must(PredicateTools.from(Objects::nonNull) - .and(StringTools::isNotEmpty) - .and(StringTools::isNotBlank) - .and(username -> RegexTools.matches(username, PatternConsts.USERNAME)), - username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); - ruleForString(RegisterCommand::getAccount) - .notNull("请输入邮箱地址或手机号") - .matchesOne(Arrays.asList(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "请输入邮箱地址或手机号"); - ruleForString(RegisterCommand::getCode) - .notNull("验证码不能为空") - .matches(PatternConsts.CAPTCHA, "验证码不符合规范"); - ruleForString(RegisterCommand::getPassword) - .notEmpty("密码不能为空") - .matches(PatternConsts.PASSWORD, "密码不符合规范"); - ruleForCollection(RegisterCommand::getRoles) - .notEmpty(() -> new RuntimeException("角色列表不能为空")); - - ruleForComparable(RegisterCommand::getYearOfBirth) - .notNull() - .inRange(Range.closed(thisYear - 60, thisYear - 18)); - - ruleForInt(RegisterCommand::getYearOfBirth) - .notNull() - .inRange(Range.closed(thisYear - 60, thisYear - 18)); - - withRule(registerCommand -> Objects.equals(registerCommand.getPassword(), registerCommand.getPassword2()), - "两次输入的密码不一致"); - } - } - - /** - * RegisterCommand - */ - static class RegisterCommand { - - private String username; - private String account; - private String code; - private String password; - private String password2; - private List roles; - - private Integer yearOfBirth; - - public RegisterCommand() { - } - - public RegisterCommand(String username, String account, String code, String password, String password2, - List roles, Integer yearOfBirth) { - this.username = username; - this.account = account; - this.code = code; - this.password = password; - this.password2 = password2; - this.roles = roles; - this.yearOfBirth = yearOfBirth; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getAccount() { - return account; - } - - public void setAccount(String account) { - this.account = account; - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getPassword2() { - return password2; - } - - public void setPassword2(String password2) { - this.password2 = password2; - } - - public List getRoles() { - return roles; - } - - public void setRoles(List roles) { - this.roles = roles; - } - - public Integer getYearOfBirth() { - return yearOfBirth; - } - - public void setYearOfBirth(Integer yearOfBirth) { - this.yearOfBirth = yearOfBirth; - } - - @Override - public String toString() { - return new StringBuilder() - .append("RegisterCommand [") - .append("username=").append(username) - .append(", account=").append(account) - .append(", code=").append(code) - .append(", password=").append(password) - .append(", password2=").append(password2) - .append(", roles=").append(roles) - .append(", yearOfBirth=").append(yearOfBirth) - .append("]") - .toString(); - } - } -}