Compare commits

..

1 Commits

Author SHA1 Message Date
1e1980469e test: 完善单元测试 2025-05-20 00:07:49 +08:00
5 changed files with 69 additions and 1004 deletions

View File

@@ -33,7 +33,7 @@ class BaseComparablePropertyValidator<TObj,
public TPropertyValidator inRange(Range<TProperty> range) {
withRule(value -> value != null && range.contains(value),
convertExceptionCreator("The value is not in the interval " + range.toString()));
convertExceptionCreator("The value is not in " + range.toString()));
return thisObject();
}

View File

@@ -22,6 +22,8 @@ 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;
@@ -59,7 +61,7 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
public <E extends RuntimeException> StringPropertyValidator<DTO> matches(
Pattern regex,
Function<String, E> exceptionCreator) {
withRule(input -> (input == null || RegexTools.matches(input, regex)), exceptionCreator);
withRule(input -> RegexTools.matches(input, regex), exceptionCreator);
return this;
}
@@ -101,7 +103,7 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesOne(
List<Pattern> regexs,
Function<String, E> exceptionCreator) {
withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
withRule(input -> RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
return this;
}
@@ -143,7 +145,7 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesAll(
Collection<Pattern> regexs,
Function<String, E> exceptionCreator) {
withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
withRule(input -> RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
return this;
}
@@ -156,7 +158,7 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// ================================
public StringPropertyValidator<DTO> notBlank() {
return notBlank("The value must have text; it must not be null, empty, or blank.");
return notBlank("This String argument must have text; it must not be null, empty, or blank");
}
public StringPropertyValidator<DTO> notBlank(String errMsg) {
@@ -207,10 +209,6 @@ 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));
}
@@ -229,6 +227,28 @@ 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
// ================================
@@ -246,16 +266,16 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
Function<String, E> exceptionCreator) {
AssertTools.checkArgument(length >= 0,
"The required length must be greater than or equal to 0.");
withRule(s -> s == null || s.length() == length, exceptionCreator);
withRule(s -> s != null && s.length() == length, exceptionCreator);
return this;
}
static boolean length(String str, int min, int max) {
if (str == null) {
return true;
return false;
}
final int len = str.length();
return len >= min && len <= max;
return len >= min && len < max;
}
public StringPropertyValidator<DTO> length(int min, int max, String errMsg) {

View File

@@ -1,249 +0,0 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.example.validator;
import static org.junit.jupiter.api.Assertions.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDateTime;
import org.junit.jupiter.api.Test;
import com.google.common.collect.Range;
import xyz.zhouxy.plusone.example.ExampleCommand;
import xyz.zhouxy.plusone.validator.BaseValidator;
public class ComparablePropertyValidatorTests {
static final LocalDateTime MIN = LocalDate.of(2025, 5, 1).atStartOfDay();
static final LocalDateTime MAX = LocalDate.of(2025, 5, 31).atStartOfDay();
static final Range<ChronoLocalDateTime<?>> DATE_TIME_RANGE = Range.closedOpen(MIN, MAX);
static final String MESSAGE = String.format("The value should in the interval [%s,%s)", MIN, MAX);
// ================================
// #region - in the interval
// ================================
@Test
void inRange_valueIsInTheInterval() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE);
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, MESSAGE);
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, () -> ExampleException.withMessage(MESSAGE));
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, property -> ExampleException.withMessage(
"The dateTimeProperty should in the interval [%s,%s), but it is %s", MIN, MAX, property));
ruleForInt(ExampleCommand::getIntProperty)
.inRange(Range.closed(18, 60));
ruleForLong(ExampleCommand::getLongProperty)
.inRange(Range.closed(10000000000L, 20000000000L));
}
};
ExampleCommand command = exampleCommandWithComparableProperty(18, 10000000000L, MIN);
assertDoesNotThrow(() -> validator.validate(command));
}
// ================================
// #endregion - in the interval
// ================================
// ================================
// #region - not in the interval
// ================================
@Test
void inRange_default_valueIsNotInTheInterval() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE);
}
};
ExampleCommand command = exampleCommandWithComparableProperty(18, 10000000000L, MAX);
IllegalArgumentException e = assertThrows(
IllegalArgumentException.class,
() -> validator.validate(command));
final String expected = String.format("The value is not in the interval %s", DATE_TIME_RANGE);
assertEquals(expected, e.getMessage());
}
@Test
void inRange_message_valueIsNotInTheInterval() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, MESSAGE);
}
};
ExampleCommand command = exampleCommandWithComparableProperty(18, 10000000000L, MAX);
IllegalArgumentException e = assertThrows(
IllegalArgumentException.class,
() -> validator.validate(command));
assertEquals(MESSAGE, e.getMessage());
}
@Test
void inRange_exceptionSupplier_valueIsNotInTheInterval() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, () -> ExampleException.withMessage(MESSAGE));
}
};
ExampleCommand command = exampleCommandWithComparableProperty(18, 10000000000L, MAX);
ExampleException e = assertThrows(
ExampleException.class,
() -> validator.validate(command));
assertEquals(MESSAGE, e.getMessage());
}
@Test
void inRange_exceptionFunction_valueIsNotInTheInterval() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, property -> ExampleException.withMessage(
"The dateTimeProperty should in the interval [%s,%s), but it is %s", MIN, MAX, property));
}
};
ExampleCommand command = exampleCommandWithComparableProperty(18, 10000000000L, MAX);
ExampleException e = assertThrows(
ExampleException.class,
() -> validator.validate(command));
final String expected = String.format("The dateTimeProperty should in the interval [%s,%s), but it is %s", MIN, MAX, MAX);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - not in the interval
// ================================
// ================================
// #region - null
// ================================
@Test
void inRange_default_valueIsNull() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE);
}
};
ExampleCommand command = exampleCommandWithComparableProperty(null, null, null);
IllegalArgumentException e = assertThrows(
IllegalArgumentException.class,
() -> validator.validate(command));
final String expected = String.format("The value is not in the interval %s", DATE_TIME_RANGE);
assertEquals(expected, e.getMessage());
}
@Test
void inRange_message_valueIsNull() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, MESSAGE);
}
};
ExampleCommand command = exampleCommandWithComparableProperty(null, null, null);
IllegalArgumentException e = assertThrows(
IllegalArgumentException.class,
() -> validator.validate(command));
assertEquals(MESSAGE, e.getMessage());
}
@Test
void inRange_exceptionSupplier_valueIsNull() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, () -> ExampleException.withMessage(MESSAGE));
}
};
ExampleCommand command = exampleCommandWithComparableProperty(null, null, null);
ExampleException e = assertThrows(
ExampleException.class,
() -> validator.validate(command));
assertEquals(MESSAGE, e.getMessage());
}
@Test
void inRange_exceptionFunction_valueIsNull() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, property -> ExampleException.withMessage(
"The dateTimeProperty should in the interval [%s,%s), but it is %s", MIN, MAX, property));
}
};
ExampleCommand command = exampleCommandWithComparableProperty(null, null, null);
ExampleException e = assertThrows(
ExampleException.class,
() -> validator.validate(command));
final String expected = String.format("The dateTimeProperty should in the interval [%s,%s), but it is null", MIN, MAX);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - null
// ================================
static ExampleCommand exampleCommandWithComparableProperty(
Integer intProperty,
Long longProperty,
LocalDateTime dateTimeProperty) {
ExampleCommand exampleCommand = new ExampleCommand();
exampleCommand.setIntProperty(intProperty);
exampleCommand.setLongProperty(longProperty);
exampleCommand.setDateTimeProperty(dateTimeProperty);
return exampleCommand;
}
}

View File

@@ -13,326 +13,32 @@
* 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.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;
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 {
// ================================
// #region - withRule
// ================================
// TODO
@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(
static ExampleCommand exampleCommand() {
return 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));
3.14,
"Foo",
LocalDateTime.of(2008, 8, 8, 20, 8),
new Foo(Integer.MIN_VALUE, "Bar"),
Lists.newArrayList("A", "B", "C", "D"));
}
// ================================
// #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
// ================================
}

View File

@@ -22,8 +22,6 @@ 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;
@@ -34,12 +32,6 @@ 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
@@ -120,7 +112,10 @@ public class StringPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithStringProperty(null);
assertDoesNotThrow(() -> validator.validate(command));
IllegalArgumentException e = assertThrows(
IllegalArgumentException.class,
() -> validator.validate(command));
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
}
@Test
@@ -133,7 +128,10 @@ public class StringPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithStringProperty(null);
assertDoesNotThrow(() -> validator.validate(command));
ExampleException e = assertThrows(
ExampleException.class,
() -> validator.validate(command));
assertEquals(MESSAGE_SHOULD_MATCH, e.getMessage());
}
@Test
@@ -147,7 +145,10 @@ public class StringPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithStringProperty(null);
assertDoesNotThrow(() -> validator.validate(command));
ExampleException e = assertThrows(
ExampleException.class,
() -> validator.validate(command));
assertEquals("Input should match pattern, but it is null", e.getMessage());
}
// ================================
@@ -728,460 +729,47 @@ public class StringPropertyValidatorTests {
// #region - notBlank
// ================================
@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());
}
// TODO
// ================================
// #endregion - notBlank
// ================================
// ================================
// #region - emailAddress
// #region - email
// ================================
@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());
}
// TODO
// ================================
// #endregion - emailAddress
// #endregion - email
// ================================
// ================================
// #region - notEmpty
// ================================
@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());
}
// TODO
// ================================
// #endregion - notEmpty
// ================================
// ================================
// #region - isNullOrEmpty
// ================================
// TODO
// ================================
// #endregion - isNullOrEmpty
// ================================
// ================================
// #region - length
// ================================
@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());
}
// TODO
// ================================
// #endregion - length