forked from plusone/plusone-validator
test: 完成单元测试
This commit is contained in:
parent
d81e6acc23
commit
83d9b05d63
@ -13,7 +13,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package xyz.zhouxy.plusone.example.validator;
|
package xyz.zhouxy.plusone;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||||
|
|
@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package xyz.zhouxy.plusone.example;
|
package xyz.zhouxy.plusone.example;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Foo {
|
public class Foo {
|
||||||
private Integer intProperty;
|
private Integer intProperty;
|
||||||
private String stringProperty;
|
private String stringProperty;
|
||||||
@ -47,4 +49,19 @@ public class Foo {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "Foo [intProperty=" + intProperty + ", stringProperty=" + stringProperty + "]";
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> rule = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.ExampleException;
|
||||||
import xyz.zhouxy.plusone.example.ExampleCommand;
|
import xyz.zhouxy.plusone.example.ExampleCommand;
|
||||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||||
|
|
||||||
@ -62,7 +63,7 @@ public class BoolPropertyValidatorTests {
|
|||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> validator.validate(command));
|
() -> validator.validate(command));
|
||||||
|
|
||||||
assertEquals("The value must be true.", exception.getMessage());
|
assertEquals("The input must be true.", exception.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -136,7 +137,7 @@ public class BoolPropertyValidatorTests {
|
|||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> validator.validate(command));
|
() -> validator.validate(command));
|
||||||
|
|
||||||
assertEquals("The value must be true.", exception.getMessage());
|
assertEquals("The input must be true.", exception.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -238,7 +239,7 @@ public class BoolPropertyValidatorTests {
|
|||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> validator.validate(command));
|
() -> validator.validate(command));
|
||||||
|
|
||||||
assertEquals("The value must be false.", exception.getMessage());
|
assertEquals("The input must be false.", exception.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -312,7 +313,7 @@ public class BoolPropertyValidatorTests {
|
|||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> validator.validate(command));
|
() -> validator.validate(command));
|
||||||
|
|
||||||
assertEquals("The value must be false.", exception.getMessage());
|
assertEquals("The input must be false.", exception.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.ExampleException;
|
||||||
import xyz.zhouxy.plusone.example.ExampleCommand;
|
import xyz.zhouxy.plusone.example.ExampleCommand;
|
||||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||||
|
|
||||||
@ -43,6 +44,7 @@ public class CollectionPropertyValidatorTests {
|
|||||||
void notEmpty_stringListIsNotEmpty() {
|
void notEmpty_stringListIsNotEmpty() {
|
||||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
|
ruleForCollection(ExampleCommand::getStringListProperty).notEmpty();
|
||||||
ruleForCollection(ExampleCommand::getStringListProperty).notEmpty(MESSAGE_NOT_EMPTY);
|
ruleForCollection(ExampleCommand::getStringListProperty).notEmpty(MESSAGE_NOT_EMPTY);
|
||||||
ruleForCollection(ExampleCommand::getStringListProperty)
|
ruleForCollection(ExampleCommand::getStringListProperty)
|
||||||
.notEmpty(() -> ExampleException.withMessage(MESSAGE_NOT_EMPTY));
|
.notEmpty(() -> ExampleException.withMessage(MESSAGE_NOT_EMPTY));
|
||||||
@ -56,6 +58,20 @@ public class CollectionPropertyValidatorTests {
|
|||||||
assertDoesNotThrow(() -> validator.validate(command));
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void notEmpty_default_stringListIsEmpty() {
|
||||||
|
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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
|
@Test
|
||||||
void notEmpty_message_stringListIsEmpty() {
|
void notEmpty_message_stringListIsEmpty() {
|
||||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
@ -158,6 +174,7 @@ public class CollectionPropertyValidatorTests {
|
|||||||
void isEmpty_stringListIsEmpty() {
|
void isEmpty_stringListIsEmpty() {
|
||||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
|
ruleForCollection(ExampleCommand::getStringListProperty).isEmpty();
|
||||||
ruleForCollection(ExampleCommand::getStringListProperty).isEmpty(MESSAGE_EMPTY);
|
ruleForCollection(ExampleCommand::getStringListProperty).isEmpty(MESSAGE_EMPTY);
|
||||||
ruleForCollection(ExampleCommand::getStringListProperty)
|
ruleForCollection(ExampleCommand::getStringListProperty)
|
||||||
.isEmpty(() -> ExampleException.withMessage(MESSAGE_EMPTY));
|
.isEmpty(() -> ExampleException.withMessage(MESSAGE_EMPTY));
|
||||||
@ -175,6 +192,7 @@ public class CollectionPropertyValidatorTests {
|
|||||||
void isEmpty_stringListIsNull() {
|
void isEmpty_stringListIsNull() {
|
||||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
|
ruleForCollection(ExampleCommand::getStringListProperty).isEmpty();
|
||||||
ruleForCollection(ExampleCommand::getStringListProperty).isEmpty(MESSAGE_EMPTY);
|
ruleForCollection(ExampleCommand::getStringListProperty).isEmpty(MESSAGE_EMPTY);
|
||||||
ruleForCollection(ExampleCommand::getStringListProperty)
|
ruleForCollection(ExampleCommand::getStringListProperty)
|
||||||
.isEmpty(() -> ExampleException.withMessage(MESSAGE_EMPTY));
|
.isEmpty(() -> ExampleException.withMessage(MESSAGE_EMPTY));
|
||||||
@ -188,6 +206,20 @@ public class CollectionPropertyValidatorTests {
|
|||||||
assertDoesNotThrow(() -> validator.validate(command));
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isEmpty_default_stringListIsNotEmpty() {
|
||||||
|
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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
|
@Test
|
||||||
void isEmpty_message_stringListIsNotEmpty() {
|
void isEmpty_message_stringListIsNotEmpty() {
|
||||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
|
@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import com.google.common.collect.Range;
|
import com.google.common.collect.Range;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.ExampleException;
|
||||||
import xyz.zhouxy.plusone.example.ExampleCommand;
|
import xyz.zhouxy.plusone.example.ExampleCommand;
|
||||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||||
|
|
||||||
@ -89,7 +90,7 @@ public class ComparablePropertyValidatorTests {
|
|||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> validator.validate(command));
|
() -> 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());
|
assertEquals(expected, e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,7 +173,7 @@ public class ComparablePropertyValidatorTests {
|
|||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> validator.validate(command));
|
() -> 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());
|
assertEquals(expected, e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.ValueSource;
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.ExampleException;
|
||||||
import xyz.zhouxy.plusone.example.ExampleCommand;
|
import xyz.zhouxy.plusone.example.ExampleCommand;
|
||||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||||
|
|
||||||
@ -30,11 +31,11 @@ public class DoublePropertyValidatorTests {
|
|||||||
static final double MIN = 1.0;
|
static final double MIN = 1.0;
|
||||||
static final double MAX = 5.0;
|
static final double MAX = 5.0;
|
||||||
|
|
||||||
static final String MESSAGE_GT = "The value should be greater than " + MIN;
|
static final String MESSAGE_GT = "The input must be greater than " + MIN;
|
||||||
static final String MESSAGE_GE = "The value should be greater than or equal to " + 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_LT = "The input must be less than " + MAX;
|
||||||
static final String MESSAGE_LE = "The value should be less than or equal to " + MAX;
|
static final String MESSAGE_LE = "The input must be less than or equal to " + MAX;
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
// #region - gt_validValue
|
// #region - gt_validValue
|
||||||
@ -84,7 +85,7 @@ public class DoublePropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -161,7 +162,7 @@ public class DoublePropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
@ -266,7 +267,7 @@ public class DoublePropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -343,7 +344,7 @@ public class DoublePropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
@ -448,7 +449,7 @@ public class DoublePropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -525,7 +526,7 @@ public class DoublePropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
@ -630,7 +631,7 @@ public class DoublePropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -707,7 +708,7 @@ public class DoublePropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
|
@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.ValueSource;
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.ExampleException;
|
||||||
import xyz.zhouxy.plusone.example.ExampleCommand;
|
import xyz.zhouxy.plusone.example.ExampleCommand;
|
||||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||||
|
|
||||||
@ -83,7 +84,7 @@ public class IntPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -160,7 +161,7 @@ public class IntPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
@ -265,7 +266,7 @@ public class IntPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -342,7 +343,7 @@ public class IntPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
@ -447,7 +448,7 @@ public class IntPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -524,7 +525,7 @@ public class IntPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
@ -629,7 +630,7 @@ public class IntPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -706,7 +707,7 @@ public class IntPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
|
@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.ValueSource;
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.ExampleException;
|
||||||
import xyz.zhouxy.plusone.example.ExampleCommand;
|
import xyz.zhouxy.plusone.example.ExampleCommand;
|
||||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||||
|
|
||||||
@ -84,7 +85,7 @@ public class LongPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -161,7 +162,7 @@ public class LongPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
@ -266,7 +267,7 @@ public class LongPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -343,7 +344,7 @@ public class LongPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
@ -448,7 +449,7 @@ public class LongPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -525,7 +526,7 @@ public class LongPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
@ -630,7 +631,7 @@ public class LongPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@ParameterizedTest
|
||||||
@ -707,7 +708,7 @@ public class LongPropertyValidatorTests {
|
|||||||
|
|
||||||
IllegalArgumentException e = assertThrows(
|
IllegalArgumentException e = assertThrows(
|
||||||
IllegalArgumentException.class, () -> validator.validate(command));
|
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
|
@Test
|
||||||
|
@ -26,8 +26,10 @@ import java.util.Objects;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.ExampleException;
|
||||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||||
import xyz.zhouxy.plusone.commons.util.DateTimeTools;
|
import xyz.zhouxy.plusone.commons.util.DateTimeTools;
|
||||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||||
@ -152,7 +154,7 @@ public class ObjectPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||||
IllegalArgumentException.class, () -> defaultRule.validate(command));
|
IllegalArgumentException.class, () -> defaultRule.validate(command));
|
||||||
assertEquals("Value could not be null.", eWithDefaultMessage.getMessage());
|
assertEquals("The input must not be null.", eWithDefaultMessage.getMessage());
|
||||||
|
|
||||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
@ -198,7 +200,7 @@ public class ObjectPropertyValidatorTests {
|
|||||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
ruleForBool(ExampleCommand::getBoolProperty)
|
ruleForBool(ExampleCommand::getBoolProperty)
|
||||||
.isNull("The boolProperty should be null");
|
.isNull();
|
||||||
ruleForInt(ExampleCommand::getIntProperty)
|
ruleForInt(ExampleCommand::getIntProperty)
|
||||||
.isNull("The intProperty should be null");
|
.isNull("The intProperty should be null");
|
||||||
ruleForLong(ExampleCommand::getLongProperty)
|
ruleForLong(ExampleCommand::getLongProperty)
|
||||||
@ -206,7 +208,7 @@ public class ObjectPropertyValidatorTests {
|
|||||||
ruleForDouble(ExampleCommand::getDoubleProperty)
|
ruleForDouble(ExampleCommand::getDoubleProperty)
|
||||||
.isNull(d -> ExampleException.withMessage("The doubleProperty should be null, but it was %s", d));
|
.isNull(d -> ExampleException.withMessage("The doubleProperty should be null, but it was %s", d));
|
||||||
ruleForString(ExampleCommand::getStringProperty)
|
ruleForString(ExampleCommand::getStringProperty)
|
||||||
.isNull("The stringProperty should be null");
|
.isNull();
|
||||||
ruleForComparable(ExampleCommand::getDateTimeProperty)
|
ruleForComparable(ExampleCommand::getDateTimeProperty)
|
||||||
.isNull("The dateTimeProperty should be null");
|
.isNull("The dateTimeProperty should be null");
|
||||||
ruleFor(ExampleCommand::getObjectProperty)
|
ruleFor(ExampleCommand::getObjectProperty)
|
||||||
@ -225,6 +227,16 @@ public class ObjectPropertyValidatorTests {
|
|||||||
ExampleCommand command = new ExampleCommand();
|
ExampleCommand command = new ExampleCommand();
|
||||||
command.setObjectProperty(new Foo(Integer.MAX_VALUE, "StringValue"));
|
command.setObjectProperty(new Foo(Integer.MAX_VALUE, "StringValue"));
|
||||||
|
|
||||||
|
BaseValidator<ExampleCommand> ruleWithDefaultMessage = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
ruleFor(ExampleCommand::getObjectProperty)
|
||||||
|
.isNull();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||||
|
IllegalArgumentException.class, () -> ruleWithDefaultMessage.validate(command));
|
||||||
|
assertEquals("The input must be null.", eWithDefaultMessage.getMessage());
|
||||||
|
|
||||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
ruleFor(ExampleCommand::getObjectProperty)
|
ruleFor(ExampleCommand::getObjectProperty)
|
||||||
@ -265,23 +277,111 @@ public class ObjectPropertyValidatorTests {
|
|||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void equalsThat() {
|
void equalsThat_validInput() {
|
||||||
|
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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
|
@Test
|
||||||
void equalsThat2() {
|
void equalsThat_invalidInput() {
|
||||||
|
ExampleCommand command = new ExampleCommand();
|
||||||
|
command.setStringProperty("Bar");
|
||||||
|
|
||||||
|
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||||
|
IllegalArgumentException.class, () -> defaultRule.validate(command));
|
||||||
|
assertEquals("The input must be equal to 'Foo'.", eWithDefaultMessage.getMessage());
|
||||||
|
|
||||||
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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
|
@Test
|
||||||
void equalsThat3() {
|
void equalsThat_nullInput() {
|
||||||
|
ExampleCommand command = new ExampleCommand();
|
||||||
|
|
||||||
}
|
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||||
|
IllegalArgumentException.class, () -> defaultRule.validate(command));
|
||||||
|
assertEquals("The input must be equal to 'Foo'.", eWithDefaultMessage.getMessage());
|
||||||
|
|
||||||
@Test
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
void equalsThat4() {
|
{
|
||||||
|
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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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
|
@Test
|
||||||
void must() {
|
void must_oneCondition_valid() {
|
||||||
|
ExampleCommand command = new ExampleCommand();
|
||||||
|
command.setStringProperty("Foo");
|
||||||
|
|
||||||
|
BaseValidator<ExampleCommand> ruleWithDefaultMessage = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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
|
@Test
|
||||||
void must2() {
|
void must_oneCondition_invalid() {
|
||||||
|
ExampleCommand command = new ExampleCommand();
|
||||||
|
|
||||||
|
BaseValidator<ExampleCommand> ruleWithDefaultMessage = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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
|
@Test
|
||||||
void must3() {
|
void must_multipleConditions_valid() {
|
||||||
|
ExampleCommand command = new ExampleCommand();
|
||||||
|
command.setStringProperty("Foo");
|
||||||
|
|
||||||
|
BaseValidator<ExampleCommand> ruleWithDefaultMessage = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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
|
@Test
|
||||||
void must4() {
|
void must_multipleConditions_invalid() {
|
||||||
|
ExampleCommand command = new ExampleCommand();
|
||||||
|
command.setStringProperty("Bar");
|
||||||
|
|
||||||
}
|
BaseValidator<ExampleCommand> ruleWithDefaultMessage = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
void must5() {
|
{
|
||||||
|
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());
|
||||||
|
|
||||||
}
|
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
@Test
|
ruleForString(ExampleCommand::getStringProperty)
|
||||||
void must6() {
|
.must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")),
|
||||||
|
() -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'."));
|
||||||
}
|
}
|
||||||
|
};
|
||||||
@Test
|
ExampleException specifiedException = assertThrows(
|
||||||
void must7() {
|
ExampleException.class, () -> ruleWithExceptionSupplier.validate(command));
|
||||||
|
assertEquals("The stringProperty must be equal to 'Foo'.", specifiedException.getMessage());
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void must8() {
|
|
||||||
|
|
||||||
|
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
|
@ -27,6 +27,7 @@ import org.junit.jupiter.params.provider.ValueSource;
|
|||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.ExampleException;
|
||||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||||
import xyz.zhouxy.plusone.example.ExampleCommand;
|
import xyz.zhouxy.plusone.example.ExampleCommand;
|
||||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||||
@ -253,10 +254,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
IllegalArgumentException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
IllegalArgumentException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -273,10 +271,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
ExampleException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
ExampleException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -294,10 +289,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
ExampleException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
ExampleException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals("Input should match pattern, but it is null", e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -390,10 +382,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
IllegalArgumentException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
IllegalArgumentException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -409,10 +398,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
ExampleException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
ExampleException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -429,10 +415,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
ExampleException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
ExampleException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals("Input should match pattern, but it is null", e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
@ -538,10 +521,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
IllegalArgumentException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
IllegalArgumentException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -558,10 +538,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
ExampleException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
ExampleException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -579,10 +556,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
ExampleException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
ExampleException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals("Input should match pattern, but it is null", e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -675,10 +649,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
IllegalArgumentException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
IllegalArgumentException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -694,10 +665,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
ExampleException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
ExampleException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -714,10 +682,7 @@ public class StringPropertyValidatorTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||||
ExampleException e = assertThrows(
|
assertDoesNotThrow(() -> validator.validate(command));
|
||||||
ExampleException.class,
|
|
||||||
() -> validator.validate(command));
|
|
||||||
assertEquals("Input should match pattern, but it is null", e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
@ -758,7 +723,7 @@ public class StringPropertyValidatorTests {
|
|||||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> defaultRule.validate(command));
|
() -> 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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
@ -808,7 +773,7 @@ public class StringPropertyValidatorTests {
|
|||||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> defaultRule.validate(command));
|
() -> 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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
@ -885,7 +850,7 @@ public class StringPropertyValidatorTests {
|
|||||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> defaultRule.validate(command));
|
() -> 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<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
@ -960,7 +925,7 @@ public class StringPropertyValidatorTests {
|
|||||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> defaultRule.validate(command));
|
() -> defaultRule.validate(command));
|
||||||
assertEquals("The value must not be empty.", eWithDefaultMessage.getMessage());
|
assertEquals("The input must not be empty.", eWithDefaultMessage.getMessage());
|
||||||
|
|
||||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
@ -1010,7 +975,7 @@ public class StringPropertyValidatorTests {
|
|||||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> defaultRule.validate(command));
|
() -> defaultRule.validate(command));
|
||||||
assertEquals("The value must not be empty.", eWithDefaultMessage.getMessage());
|
assertEquals("The input must not be empty.", eWithDefaultMessage.getMessage());
|
||||||
|
|
||||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package xyz.zhouxy.plusone.example.validator;
|
package xyz.zhouxy.plusone.example.validator;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
import static xyz.zhouxy.plusone.commons.constant.PatternConsts.*;
|
import static xyz.zhouxy.plusone.commons.constant.PatternConsts.*;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -57,8 +58,7 @@ class ValidatorTests {
|
|||||||
// 传入 predicate 和 error message
|
// 传入 predicate 和 error message
|
||||||
.addRule(command -> Objects.equals(command.getPassword(), command.getPassword2()),
|
.addRule(command -> Objects.equals(command.getPassword(), command.getPassword2()),
|
||||||
"两次输入的密码不一致");
|
"两次输入的密码不一致");
|
||||||
registerCommandValidator.validate(registerCommand);
|
assertDoesNotThrow(() -> registerCommandValidator.validate(registerCommand));
|
||||||
System.out.println(registerCommand);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,16 +2,19 @@ package xyz.zhouxy.plusone.map.validator;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
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.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.regex.Pattern;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
|
||||||
|
import xyz.zhouxy.plusone.ExampleException;
|
||||||
|
import xyz.zhouxy.plusone.example.Foo;
|
||||||
import xyz.zhouxy.plusone.validator.MapValidator;
|
import xyz.zhouxy.plusone.validator.MapValidator;
|
||||||
|
|
||||||
class MapValidatorTests {
|
class MapValidatorTests {
|
||||||
@ -21,66 +24,98 @@ class MapValidatorTests {
|
|||||||
@Test
|
@Test
|
||||||
void testValidateAndCopy() {
|
void testValidateAndCopy() {
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put(ParamsValidator.USERNAME, "ZhouXY");
|
params.put(ParamsValidator.BOOL_PROPERTY, true);
|
||||||
params.put(ParamsValidator.ACCOUNT, "zhouxy@code108.cn");
|
params.put(ParamsValidator.INT_PROPERTY, Integer.MAX_VALUE);
|
||||||
params.put(ParamsValidator.PASSWORD, "99Code108");
|
params.put(ParamsValidator.LONG_PROPERTY, Long.MAX_VALUE);
|
||||||
params.put(ParamsValidator.PASSWORD2, "99Code108");
|
params.put(ParamsValidator.DOUBLE_PROPERTY, Double.MAX_VALUE);
|
||||||
params.put(ParamsValidator.AGE, 18);
|
params.put(ParamsValidator.STRING_PROPERTY, "Foo");
|
||||||
params.put(ParamsValidator.BOOLEAN, true);
|
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", ""));
|
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
|
||||||
validator.validateAndCopy(params);
|
validator.validateAndCopy(params);
|
||||||
});
|
});
|
||||||
|
assertEquals("'stringProperty' must be equal to 'stringProperty2'.", e.getMessage());
|
||||||
|
|
||||||
params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", "developer"));
|
params.put(ParamsValidator.STRING_PROPERTY2, "Foo");
|
||||||
Map<String, Object> validatedParams = validator.validateAndCopy(params);
|
assertAll(() -> {
|
||||||
System.out.println(validatedParams);
|
Map<String, Object> 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<String, Object> 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<String> keySet = ImmutableSet.of(ParamsValidator.LONG_PROPERTY, ParamsValidator.STRING_PROPERTY2);
|
||||||
|
Map<String, Object> 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<String, Object> {
|
class ParamsValidator extends MapValidator<String, Object> {
|
||||||
public static final String USERNAME = "username";
|
public static final String BOOL_PROPERTY = "boolProperty";
|
||||||
public static final String ACCOUNT = "account";
|
public static final String INT_PROPERTY = "intProperty";
|
||||||
public static final String PASSWORD = "password";
|
public static final String LONG_PROPERTY = "longProperty";
|
||||||
public static final String PASSWORD2 = "password2";
|
public static final String DOUBLE_PROPERTY = "doubleProperty";
|
||||||
public static final String AGE = "age";
|
public static final String STRING_PROPERTY = "stringProperty";
|
||||||
public static final String BOOLEAN = "boolean";
|
public static final String STRING_PROPERTY2 = "stringProperty2";
|
||||||
public static final String ROLE_LIST = "roleList";
|
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();
|
public static final ParamsValidator INSTANCE = new ParamsValidator();
|
||||||
|
|
||||||
private ParamsValidator() {
|
private ParamsValidator() {
|
||||||
super(new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST });
|
super(new String[] { BOOL_PROPERTY, INT_PROPERTY, LONG_PROPERTY, DOUBLE_PROPERTY, STRING_PROPERTY,
|
||||||
ruleForString(USERNAME)
|
DATE_TIME_PROPERTY, OBJECT_PROPERTY, STRING_LIST_PROPERTY });
|
||||||
.notBlank("用户名不能为空")
|
ruleForBool(BOOL_PROPERTY)
|
||||||
.matches(PatternConsts.USERNAME,
|
.notNull();
|
||||||
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
|
ruleForInt(INT_PROPERTY)
|
||||||
|
.notNull("The intProperty cannot be null");
|
||||||
ruleForString(ACCOUNT)
|
ruleForLong(LONG_PROPERTY)
|
||||||
.notBlank("账号不能为空")
|
.notNull(() -> ExampleException.withMessage("The longProperty cannot be null"));
|
||||||
.matchesOne(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE }, "请输入正确的邮箱地址或手机号");
|
ruleForDouble(DOUBLE_PROPERTY)
|
||||||
|
.notNull(d -> ExampleException.withMessage("The doubleProperty cannot be null, but it was %s", d));
|
||||||
ruleForString(PASSWORD)
|
ruleForString(STRING_PROPERTY)
|
||||||
.notEmpty("密码不能为空")
|
.notNull();
|
||||||
.matches(PatternConsts.PASSWORD, "密码不符合规范");
|
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 本身进行校验
|
// 校验到多个属性,只能针对 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)
|
public static Set<String> keySet() {
|
||||||
.withRule(Objects::nonNull)
|
return ImmutableSet.of(BOOL_PROPERTY, INT_PROPERTY, LONG_PROPERTY, DOUBLE_PROPERTY, STRING_PROPERTY,
|
||||||
.ge(18)
|
DATE_TIME_PROPERTY, OBJECT_PROPERTY, STRING_LIST_PROPERTY);
|
||||||
.le(60);
|
|
||||||
|
|
||||||
ruleForBool(BOOLEAN)
|
|
||||||
.notNull("Boolean property could not be null.")
|
|
||||||
.isTrueValue("Boolean property must be true.");
|
|
||||||
|
|
||||||
this.<String>ruleForCollection(ROLE_LIST)
|
|
||||||
.notEmpty("角色列表不能为空!")
|
|
||||||
.withRule(l -> l.stream().allMatch(StringTools::isNotBlank),
|
|
||||||
() -> new IllegalArgumentException("角色标识不能为空!"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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<RegisterCommand> {
|
|
||||||
|
|
||||||
static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator();
|
|
||||||
|
|
||||||
private RegisterCommandValidator() {
|
|
||||||
int thisYear = Year.now().getValue();
|
|
||||||
|
|
||||||
ruleForString(RegisterCommand::getUsername)
|
|
||||||
.must(PredicateTools.<String>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<String> roles;
|
|
||||||
|
|
||||||
private Integer yearOfBirth;
|
|
||||||
|
|
||||||
public RegisterCommand() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public RegisterCommand(String username, String account, String code, String password, String password2,
|
|
||||||
List<String> 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<String> getRoles() {
|
|
||||||
return roles;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRoles(List<String> 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user