forked from plusone/plusone-validator
test: 完善单元测试
This commit is contained in:
parent
ad74a0bcd0
commit
bce648dc51
@ -22,8 +22,6 @@ import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||
@ -158,7 +156,7 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
|
||||
// ================================
|
||||
|
||||
public StringPropertyValidator<DTO> notBlank() {
|
||||
return notBlank("This String argument must have text; it must not be null, empty, or blank");
|
||||
return notBlank("The value must have text; it must not be null, empty, or blank.");
|
||||
}
|
||||
|
||||
public StringPropertyValidator<DTO> notBlank(String errMsg) {
|
||||
@ -209,6 +207,10 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
|
||||
// #region - notEmpty
|
||||
// ================================
|
||||
|
||||
public StringPropertyValidator<DTO> notEmpty() {
|
||||
return notEmpty("The value must not be empty.");
|
||||
}
|
||||
|
||||
public StringPropertyValidator<DTO> notEmpty(String errMsg) {
|
||||
return notEmpty(convertExceptionCreator(errMsg));
|
||||
}
|
||||
@ -227,28 +229,6 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
|
||||
// #endregion - notEmpty
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - isNullOrEmpty
|
||||
// ================================
|
||||
|
||||
public StringPropertyValidator<DTO> isNullOrEmpty(String errMsg) {
|
||||
return isNullOrEmpty(convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringPropertyValidator<DTO> isNullOrEmpty(Supplier<E> exceptionCreator) {
|
||||
return isNullOrEmpty(convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringPropertyValidator<DTO> isNullOrEmpty(
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(Strings::isNullOrEmpty, exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - isNullOrEmpty
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - length
|
||||
// ================================
|
||||
|
@ -13,32 +13,326 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.example.validator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.time.DateTimeException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
import xyz.zhouxy.plusone.commons.util.DateTimeTools;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
import xyz.zhouxy.plusone.example.ExampleCommand;
|
||||
import xyz.zhouxy.plusone.example.Foo;
|
||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||
|
||||
public class ObjectPropertyValidatorTests {
|
||||
|
||||
// TODO
|
||||
// ================================
|
||||
// #region - withRule
|
||||
// ================================
|
||||
|
||||
static ExampleCommand exampleCommand() {
|
||||
return new ExampleCommand(
|
||||
@Test
|
||||
void withRule() {
|
||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getBoolProperty)
|
||||
.notNull("The boolProperty cannot be null.")
|
||||
.withRule(Boolean.TRUE::equals, "The boolProperty should be true.");
|
||||
|
||||
ruleFor(ExampleCommand::getIntProperty)
|
||||
.withRule(intProperty -> intProperty > 0, "The intProperty should be greater than 0.");
|
||||
|
||||
ruleFor(ExampleCommand::getLongProperty)
|
||||
.withRule(longProperty -> longProperty > 0L,
|
||||
() -> ExampleException.withMessage("The longProperty should be greater than 0."));
|
||||
|
||||
ruleFor(ExampleCommand::getDoubleProperty)
|
||||
.withRule(doubleProperty -> doubleProperty > 0.00,
|
||||
doubleProperty -> ExampleException.withMessage("The doubleProperty should be greater than 0, but it was: %s", doubleProperty));
|
||||
|
||||
ruleFor(ExampleCommand::getStringProperty)
|
||||
.notNull()
|
||||
.withRule(stringProperty -> stringProperty.length() > 2,
|
||||
() -> ExampleException.withMessage("The length of stringProperty should be greater than 2."));
|
||||
|
||||
ruleFor(ExampleCommand::getDateTimeProperty)
|
||||
.withRule(DateTimeTools::isFuture,
|
||||
() -> new DateTimeException("The dateTimeProperty should be a future time."));
|
||||
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.notNull("The objectProperty cannot be null.");
|
||||
|
||||
ruleFor(ExampleCommand::getStringListProperty)
|
||||
.withRule(CollectionTools::isNotEmpty, "The stringListProperty cannot be empty.");
|
||||
|
||||
withRule(command -> {
|
||||
Foo objectProperty = command.getObjectProperty();
|
||||
if (!Objects.equals(command.getIntProperty(), objectProperty.getIntProperty())) {
|
||||
throw ExampleException.withMessage("intProperty invalid.");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
ExampleCommand command = new ExampleCommand(
|
||||
true,
|
||||
Integer.MAX_VALUE,
|
||||
Long.MAX_VALUE,
|
||||
3.14,
|
||||
"Foo",
|
||||
LocalDateTime.of(2008, 8, 8, 20, 8),
|
||||
new Foo(Integer.MIN_VALUE, "Bar"),
|
||||
Lists.newArrayList("A", "B", "C", "D"));
|
||||
Double.MAX_VALUE,
|
||||
"StringValue",
|
||||
LocalDateTime.now().plusDays(1),
|
||||
new Foo(Integer.MAX_VALUE, "StringValue"),
|
||||
Lists.newArrayList("ABC", "DEF"));
|
||||
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - withRule
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - notNull
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void notNull_validInput() {
|
||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForBool(ExampleCommand::getBoolProperty)
|
||||
.notNull();
|
||||
ruleForInt(ExampleCommand::getIntProperty)
|
||||
.notNull("The intProperty cannot be null");
|
||||
ruleForLong(ExampleCommand::getLongProperty)
|
||||
.notNull(() -> ExampleException.withMessage("The longProperty cannot be null"));
|
||||
ruleForDouble(ExampleCommand::getDoubleProperty)
|
||||
.notNull(d -> ExampleException.withMessage("The doubleProperty cannot be null, but it was %s", d));
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notNull();
|
||||
ruleForComparable(ExampleCommand::getDateTimeProperty)
|
||||
.notNull("The dateTimeProperty cannot be null");
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.notNull(() -> ExampleException.withMessage("The objectProperty cannot be null"));
|
||||
ruleForCollection(ExampleCommand::getStringListProperty)
|
||||
.notNull(d -> ExampleException.withMessage("The stringListProperty cannot be null, but it was %s", d));
|
||||
}
|
||||
};
|
||||
ExampleCommand command = new ExampleCommand(
|
||||
true,
|
||||
Integer.MAX_VALUE,
|
||||
Long.MAX_VALUE,
|
||||
Double.MAX_VALUE,
|
||||
"StringValue",
|
||||
LocalDateTime.now().plusDays(1),
|
||||
new Foo(Integer.MAX_VALUE, "StringValue"),
|
||||
Lists.newArrayList("ABC", "DEF"));
|
||||
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notNull_invalidInput() {
|
||||
ExampleCommand command = new ExampleCommand();
|
||||
|
||||
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.notNull();
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||
IllegalArgumentException.class, () -> defaultRule.validate(command));
|
||||
assertEquals("Value could not be null.", eWithDefaultMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.notNull("The objectProperty could not be null.");
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithSpecifiedMessage = assertThrows(
|
||||
IllegalArgumentException.class, () -> ruleWithMessage.validate(command));
|
||||
assertEquals("The objectProperty could not be null.", eWithSpecifiedMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.notNull(() -> ExampleException.withMessage("The objectProperty could not be null."));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException = assertThrows(
|
||||
ExampleException.class, () -> ruleWithExceptionSupplier.validate(command));
|
||||
assertEquals("The objectProperty could not be null.", specifiedException.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.notNull(str -> ExampleException.withMessage("The objectProperty could not be null, but is was " + str));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException2 = assertThrows(
|
||||
ExampleException.class, () -> ruleWithExceptionFunction.validate(command));
|
||||
assertEquals("The objectProperty could not be null, but is was null", specifiedException2.getMessage());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - notNull
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - isNull
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void isNull_validInput() {
|
||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForBool(ExampleCommand::getBoolProperty)
|
||||
.isNull("The boolProperty should be null");
|
||||
ruleForInt(ExampleCommand::getIntProperty)
|
||||
.isNull("The intProperty should be null");
|
||||
ruleForLong(ExampleCommand::getLongProperty)
|
||||
.isNull(() -> ExampleException.withMessage("The longProperty should be null"));
|
||||
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");
|
||||
ruleForComparable(ExampleCommand::getDateTimeProperty)
|
||||
.isNull("The dateTimeProperty should be null");
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.isNull(() -> ExampleException.withMessage("The objectProperty should be null"));
|
||||
ruleForCollection(ExampleCommand::getStringListProperty)
|
||||
.isNull(d -> ExampleException.withMessage("The stringListProperty should be null, but it was %s", d));
|
||||
}
|
||||
};
|
||||
ExampleCommand command = new ExampleCommand();
|
||||
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNull_invalidInput() {
|
||||
ExampleCommand command = new ExampleCommand();
|
||||
command.setObjectProperty(new Foo(Integer.MAX_VALUE, "StringValue"));
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.isNull("The objectProperty should be null.");
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithSpecifiedMessage = assertThrows(
|
||||
IllegalArgumentException.class, () -> ruleWithMessage.validate(command));
|
||||
assertEquals("The objectProperty should be null.", eWithSpecifiedMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.isNull(() -> ExampleException.withMessage("The objectProperty should be null."));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException = assertThrows(
|
||||
ExampleException.class, () -> ruleWithExceptionSupplier.validate(command));
|
||||
assertEquals("The objectProperty should be null.", specifiedException.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getObjectProperty)
|
||||
.isNull(str -> ExampleException.withMessage("The objectProperty should be null, but is was " + str));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException2 = assertThrows(
|
||||
ExampleException.class, () -> ruleWithExceptionFunction.validate(command));
|
||||
assertEquals("The objectProperty should be null, but is was " + command.getObjectProperty(), specifiedException2.getMessage());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - isNull
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - equalsThat
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void equalsThat() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsThat2() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsThat3() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsThat4() {
|
||||
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - equalsThat
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - must
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void must() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void must2() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void must3() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void must4() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void must5() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void must6() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void must7() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void must8() {
|
||||
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - must
|
||||
// ================================
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@ -32,6 +34,12 @@ import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||
public class StringPropertyValidatorTests {
|
||||
|
||||
private static final String MESSAGE_SHOULD_MATCH = "Input should match pattern";
|
||||
private static final String MESSAGE_NOT_BLANK = "Input cannot be blank";
|
||||
private static final String MESSAGE_NOT_EMPTY = "Input cannot be empty";
|
||||
private static final String MESSAGE_NOT_EMAIL = "Input should be an email address";
|
||||
|
||||
private static final int MIN_LENGTH = 6;
|
||||
private static final int MAX_LENGTH = 8;
|
||||
|
||||
// ================================
|
||||
// #region - matches
|
||||
@ -112,10 +120,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
|
||||
@ -128,10 +133,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
|
||||
@ -145,10 +147,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));
|
||||
}
|
||||
|
||||
// ================================
|
||||
@ -729,47 +728,460 @@ public class StringPropertyValidatorTests {
|
||||
// #region - notBlank
|
||||
// ================================
|
||||
|
||||
// TODO
|
||||
@Test
|
||||
void notBlank_all_validInput() {
|
||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notBlank()
|
||||
.notBlank(MESSAGE_NOT_BLANK)
|
||||
.notBlank(() -> ExampleException.withMessage(MESSAGE_NOT_BLANK))
|
||||
.notBlank(str -> ExampleException.withMessage("The stringProperty cannot be blank, but is was %s", StringTools.toQuotedString(str)));
|
||||
}
|
||||
};
|
||||
|
||||
ExampleCommand command = exampleCommandWithStringProperty("abcd");
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "", " ", " ", "\t", "\n" })
|
||||
void notBlank_invalidInput(String value) {
|
||||
ExampleCommand command = exampleCommandWithStringProperty(value);
|
||||
|
||||
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notBlank();
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> defaultRule.validate(command));
|
||||
assertEquals("The value must have text; it must not be null, empty, or blank.", eWithDefaultMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notBlank(MESSAGE_NOT_BLANK);
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithSpecifiedMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ruleWithMessage.validate(command));
|
||||
assertEquals(MESSAGE_NOT_BLANK, eWithSpecifiedMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notBlank(() -> ExampleException.withMessage(MESSAGE_NOT_BLANK));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionSupplier.validate(command));
|
||||
assertEquals(MESSAGE_NOT_BLANK, specifiedException.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notBlank(str -> ExampleException.withMessage("The stringProperty cannot be blank, but is was %s", StringTools.toQuotedString(str)));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException2 = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionFunction.validate(command));
|
||||
assertEquals("The stringProperty cannot be blank, but is was " + StringTools.toQuotedString(value),
|
||||
specifiedException2.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void notBlank_nullInput() {
|
||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||
|
||||
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notBlank();
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> defaultRule.validate(command));
|
||||
assertEquals("The value must have text; it must not be null, empty, or blank.", eWithDefaultMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notBlank(MESSAGE_NOT_BLANK);
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithSpecifiedMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ruleWithMessage.validate(command));
|
||||
assertEquals(MESSAGE_NOT_BLANK, eWithSpecifiedMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notBlank(() -> ExampleException.withMessage(MESSAGE_NOT_BLANK));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionSupplier.validate(command));
|
||||
assertEquals(MESSAGE_NOT_BLANK, specifiedException.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notBlank(str -> ExampleException.withMessage("The stringProperty cannot be blank, but is was %s", StringTools.toQuotedString(str)));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException2 = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionFunction.validate(command));
|
||||
assertEquals("The stringProperty cannot be blank, but is was null",
|
||||
specifiedException2.getMessage());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - notBlank
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - email
|
||||
// #region - emailAddress
|
||||
// ================================
|
||||
|
||||
// TODO
|
||||
@Test
|
||||
void emailAddress_validInput() {
|
||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.emailAddress()
|
||||
.emailAddress(MESSAGE_NOT_EMAIL)
|
||||
.emailAddress(() -> ExampleException.withMessage(MESSAGE_NOT_EMAIL))
|
||||
.emailAddress(str -> ExampleException.withMessage("Input should be an email address, but it was \"%s\"", str));
|
||||
}
|
||||
};
|
||||
ExampleCommand validCommand = exampleCommandWithStringProperty("abc@example.com");
|
||||
assertDoesNotThrow(() -> validator.validate(validCommand));
|
||||
|
||||
ExampleCommand commandWithNullStringProperty = exampleCommandWithStringProperty(null);
|
||||
assertDoesNotThrow(() -> validator.validate(commandWithNullStringProperty));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "", "abc", "abc@def@example.com" })
|
||||
void emailAddress_invalidInput(String value) {
|
||||
ExampleCommand command = exampleCommandWithStringProperty(value);
|
||||
|
||||
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.emailAddress();
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> defaultRule.validate(command));
|
||||
assertEquals("The value is not an email address.", eWithDefaultMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.emailAddress(MESSAGE_NOT_EMAIL);
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithSpecifiedMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ruleWithMessage.validate(command));
|
||||
assertEquals(MESSAGE_NOT_EMAIL, eWithSpecifiedMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.emailAddress(() -> ExampleException.withMessage(MESSAGE_NOT_EMAIL));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionSupplier.validate(command));
|
||||
assertEquals(MESSAGE_NOT_EMAIL, specifiedException.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.emailAddress(str -> ExampleException.withMessage("Input should be an email address, but it was \"%s\"", str));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException2 = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionFunction.validate(command));
|
||||
assertEquals(String.format("Input should be an email address, but it was \"%s\"", value), specifiedException2.getMessage());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - email
|
||||
// #endregion - emailAddress
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - notEmpty
|
||||
// ================================
|
||||
|
||||
// TODO
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "abcd", " ", " ", "\t", "\n" })
|
||||
void notEmpty_all_validInput() {
|
||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notEmpty()
|
||||
.notEmpty(MESSAGE_NOT_EMPTY)
|
||||
.notEmpty(() -> ExampleException.withMessage(MESSAGE_NOT_EMPTY))
|
||||
.notEmpty(str -> ExampleException.withMessage("The stringProperty cannot be empty, but is was %s", StringTools.toQuotedString(str)));
|
||||
}
|
||||
};
|
||||
|
||||
ExampleCommand command = exampleCommandWithStringProperty("abcd");
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notEmpty_invalidInput() {
|
||||
final String value = "";
|
||||
ExampleCommand command = exampleCommandWithStringProperty(value);
|
||||
|
||||
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notEmpty();
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> defaultRule.validate(command));
|
||||
assertEquals("The value must not be empty.", eWithDefaultMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notEmpty(MESSAGE_NOT_EMPTY);
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithSpecifiedMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ruleWithMessage.validate(command));
|
||||
assertEquals(MESSAGE_NOT_EMPTY, eWithSpecifiedMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notEmpty(() -> ExampleException.withMessage(MESSAGE_NOT_EMPTY));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionSupplier.validate(command));
|
||||
assertEquals(MESSAGE_NOT_EMPTY, specifiedException.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notEmpty(str -> ExampleException.withMessage("The stringProperty cannot be empty, but is was %s", StringTools.toQuotedString(str)));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException2 = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionFunction.validate(command));
|
||||
assertEquals("The stringProperty cannot be empty, but is was " + StringTools.toQuotedString(value),
|
||||
specifiedException2.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void notEmpty_nullInput() {
|
||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||
|
||||
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notEmpty();
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithDefaultMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> defaultRule.validate(command));
|
||||
assertEquals("The value must not be empty.", eWithDefaultMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notEmpty(MESSAGE_NOT_EMPTY);
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithSpecifiedMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ruleWithMessage.validate(command));
|
||||
assertEquals(MESSAGE_NOT_EMPTY, eWithSpecifiedMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notEmpty(() -> ExampleException.withMessage(MESSAGE_NOT_EMPTY));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionSupplier.validate(command));
|
||||
assertEquals(MESSAGE_NOT_EMPTY, specifiedException.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.notEmpty(str -> ExampleException.withMessage("The stringProperty cannot be empty, but is was %s", StringTools.toQuotedString(str)));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException2 = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionFunction.validate(command));
|
||||
assertEquals("The stringProperty cannot be empty, but is was null",
|
||||
specifiedException2.getMessage());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - notEmpty
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - isNullOrEmpty
|
||||
// ================================
|
||||
|
||||
// TODO
|
||||
|
||||
// ================================
|
||||
// #endregion - isNullOrEmpty
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - length
|
||||
// ================================
|
||||
|
||||
// TODO
|
||||
@Test
|
||||
void length_specifiedLength_validLength() {
|
||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.length(MIN_LENGTH, "The length of the string must be 6")
|
||||
.length(MIN_LENGTH, () -> ExampleException.withMessage("The length of the string must be 6"))
|
||||
.length(MIN_LENGTH, str -> ExampleException.withMessage("The length of the string must be 6, but it was %d", str.length()));
|
||||
}
|
||||
};
|
||||
ExampleCommand validCommand = exampleCommandWithStringProperty("123456");
|
||||
assertDoesNotThrow(() -> validator.validate(validCommand));
|
||||
|
||||
ExampleCommand commandWithNullStringProperty = exampleCommandWithStringProperty(null);
|
||||
assertDoesNotThrow(() -> validator.validate(commandWithNullStringProperty));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "", "12345", "1234567" })
|
||||
void length_specifiedLength_invalidLength(String value) {
|
||||
ExampleCommand command = exampleCommandWithStringProperty(value);
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.length(MIN_LENGTH, "The length of the string must be 6");
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithSpecifiedMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ruleWithMessage.validate(command));
|
||||
assertEquals("The length of the string must be 6", eWithSpecifiedMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.length(MIN_LENGTH, () -> ExampleException.withMessage("The length of the string must be 6"));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionSupplier.validate(command));
|
||||
assertEquals("The length of the string must be 6", specifiedException.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.length(MIN_LENGTH, str -> ExampleException.withMessage("The length of the string must be 6, but it was %d", str.length()));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException2 = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionFunction.validate(command));
|
||||
assertEquals(
|
||||
String.format("The length of the string must be 6, but it was %d", value.length()),
|
||||
specifiedException2.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "123456", "1234567", "12345678" })
|
||||
void length_specifiedMinLengthAndMaxLength_validLength(String value) {
|
||||
ExampleCommand command = exampleCommandWithStringProperty(value);
|
||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.length(MIN_LENGTH, MAX_LENGTH, String.format("Min length is %d, max length is %d", MIN_LENGTH, MAX_LENGTH))
|
||||
.length(MIN_LENGTH, MAX_LENGTH, () -> ExampleException.withMessage("Min length is %d, max length is %d", MIN_LENGTH, MAX_LENGTH))
|
||||
.length(MIN_LENGTH, MAX_LENGTH, str -> ExampleException.withMessage("Length of StringProperty is %d, min length is %d, max length is %d", str.length(), MIN_LENGTH, MAX_LENGTH));
|
||||
}
|
||||
};
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
|
||||
@Test
|
||||
void length_specifiedMinLengthAndMaxLength_null() {
|
||||
ExampleCommand command = exampleCommandWithStringProperty(null);
|
||||
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.length(MIN_LENGTH, MAX_LENGTH, String.format("Min length is %d, max length is %d", MIN_LENGTH, MAX_LENGTH))
|
||||
.length(MIN_LENGTH, MAX_LENGTH, () -> ExampleException.withMessage("Min length is %d, max length is %d", MIN_LENGTH, MAX_LENGTH))
|
||||
.length(MIN_LENGTH, MAX_LENGTH, str -> ExampleException.withMessage("Length of StringProperty is %d, min length is %d, max length is %d", str.length(), MIN_LENGTH, MAX_LENGTH));
|
||||
}
|
||||
};
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "", "12345", "123456789" })
|
||||
void length_specifiedMinLengthAndMaxLength_invalidLength(String value) {
|
||||
ExampleCommand command = exampleCommandWithStringProperty(value);
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.length(MIN_LENGTH, MAX_LENGTH, String.format("Min length is %d, max length is %d", MIN_LENGTH, MAX_LENGTH));
|
||||
}
|
||||
};
|
||||
IllegalArgumentException eWithSpecifiedMessage = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ruleWithMessage.validate(command));
|
||||
assertEquals("Min length is 6, max length is 8", eWithSpecifiedMessage.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.length(MIN_LENGTH, MAX_LENGTH, () -> ExampleException.withMessage("Min length is %d, max length is %d", MIN_LENGTH, MAX_LENGTH));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionSupplier.validate(command));
|
||||
assertEquals("Min length is 6, max length is 8", specifiedException.getMessage());
|
||||
|
||||
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForString(ExampleCommand::getStringProperty)
|
||||
.length(MIN_LENGTH, MAX_LENGTH, str -> ExampleException.withMessage("Length of StringProperty is %d, min length is %d, max length is %d", str.length(), MIN_LENGTH, MAX_LENGTH));
|
||||
}
|
||||
};
|
||||
ExampleException specifiedException2 = assertThrows(
|
||||
ExampleException.class,
|
||||
() -> ruleWithExceptionFunction.validate(command));
|
||||
assertEquals(
|
||||
String.format("Length of StringProperty is %d, min length is %d, max length is %d", value.length(), MIN_LENGTH, MAX_LENGTH),
|
||||
specifiedException2.getMessage());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - length
|
||||
|
Loading…
x
Reference in New Issue
Block a user