refactor!: BasePropertyValidatorequalsThat 重命名为 equalTo

This commit is contained in:
zhouxy108 2025-05-28 11:51:14 +08:00
parent f21e9727fa
commit 0e9dbf9bd9
2 changed files with 26 additions and 26 deletions

View File

@ -122,31 +122,31 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
// ================================ // ================================
// ================================ // ================================
// #region - equals // #region - equalTo
// ================================ // ================================
public TPropertyValidator equalsThat(Object that) { public TPropertyValidator equalTo(Object that) {
return equalsThat(that, return equalTo(that,
value -> new IllegalArgumentException(String.format("The input must be equal to '%s'.", that))); value -> new IllegalArgumentException(String.format("The input must be equal to '%s'.", that)));
} }
public TPropertyValidator equalsThat(Object that, String errMsg) { public TPropertyValidator equalTo(Object that, String errMsg) {
return equalsThat(that, convertExceptionCreator(errMsg)); return equalTo(that, convertExceptionCreator(errMsg));
} }
public <E extends RuntimeException> TPropertyValidator equalsThat( public <E extends RuntimeException> TPropertyValidator equalTo(
Object that, Supplier<E> exceptionCreator) { Object that, Supplier<E> exceptionCreator) {
return equalsThat(that, convertExceptionCreator(exceptionCreator)); return equalTo(that, convertExceptionCreator(exceptionCreator));
} }
public <E extends RuntimeException> TPropertyValidator equalsThat( public <E extends RuntimeException> TPropertyValidator equalTo(
Object that, Function<TProperty, E> exceptionCreator) { Object that, Function<TProperty, E> exceptionCreator) {
withRule(value -> Objects.equals(value, that), exceptionCreator); withRule(value -> Objects.equals(value, that), exceptionCreator);
return thisObject(); return thisObject();
} }
// ================================ // ================================
// #endregion - equals // #endregion - equalTo
// ================================ // ================================
// ================================ // ================================

View File

@ -318,19 +318,19 @@ public class ObjectPropertyValidatorTests {
// ================================ // ================================
// ================================ // ================================
// #region - equalsThat // #region - equalTo
// ================================ // ================================
@Test @Test
void equalsThat_validInput() { void equalTo_validInput() {
BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() { BaseValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{ {
ruleForString(ExampleCommand::getStringProperty) ruleForString(ExampleCommand::getStringProperty)
.equalsThat("Foo") .equalTo("Foo")
.equalsThat("Foo", "The stringProperty should be equal to 'Foo'.") .equalTo("Foo", "The stringProperty should be equal to 'Foo'.")
.equalsThat("Foo", () -> .equalTo("Foo", () ->
ExampleException.withMessage("The stringProperty should be equal to 'Foo'.")) ExampleException.withMessage("The stringProperty should be equal to 'Foo'."))
.equalsThat("Foo", str -> .equalTo("Foo", str ->
ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str)); ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str));
} }
}; };
@ -341,13 +341,13 @@ public class ObjectPropertyValidatorTests {
} }
@Test @Test
void equalsThat_invalidInput() { void equalTo_invalidInput() {
ExampleCommand command = new ExampleCommand(); ExampleCommand command = new ExampleCommand();
command.setStringProperty("Bar"); command.setStringProperty("Bar");
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() { BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
{ {
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo"); ruleForString(ExampleCommand::getStringProperty).equalTo("Foo");
} }
}; };
IllegalArgumentException eWithDefaultMessage = assertThrows( IllegalArgumentException eWithDefaultMessage = assertThrows(
@ -356,7 +356,7 @@ public class ObjectPropertyValidatorTests {
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() { BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
{ {
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
"The stringProperty should be equal to 'Foo'."); "The stringProperty should be equal to 'Foo'.");
} }
}; };
@ -366,7 +366,7 @@ public class ObjectPropertyValidatorTests {
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() { BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
{ {
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
() -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'.")); () -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'."));
} }
}; };
@ -376,7 +376,7 @@ public class ObjectPropertyValidatorTests {
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() { BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
{ {
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str)); str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str));
} }
}; };
@ -386,12 +386,12 @@ public class ObjectPropertyValidatorTests {
} }
@Test @Test
void equalsThat_nullInput() { void equalTo_nullInput() {
ExampleCommand command = new ExampleCommand(); ExampleCommand command = new ExampleCommand();
BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() { BaseValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
{ {
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo"); ruleForString(ExampleCommand::getStringProperty).equalTo("Foo");
} }
}; };
IllegalArgumentException eWithDefaultMessage = assertThrows( IllegalArgumentException eWithDefaultMessage = assertThrows(
@ -400,7 +400,7 @@ public class ObjectPropertyValidatorTests {
BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() { BaseValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
{ {
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
"The stringProperty should be equal to 'Foo'."); "The stringProperty should be equal to 'Foo'.");
} }
}; };
@ -410,7 +410,7 @@ public class ObjectPropertyValidatorTests {
BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() { BaseValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
{ {
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
() -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'.")); () -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'."));
} }
}; };
@ -420,7 +420,7 @@ public class ObjectPropertyValidatorTests {
BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() { BaseValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
{ {
ruleForString(ExampleCommand::getStringProperty).equalsThat("Foo", ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str)); str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str));
} }
}; };
@ -430,7 +430,7 @@ public class ObjectPropertyValidatorTests {
} }
// ================================ // ================================
// #endregion - equalsThat // #endregion - equalTo
// ================================ // ================================
// ================================ // ================================