use of cn.taketoday.validation.BeanPropertyBindingResult in project today-infrastructure by TAKETODAY.
the class ValidatorAdapterTests method testApplyMessageSourceResolvableToStringArgumentValueWithUnresolvedLogicalFieldName.
// SPR-13406
@Test
public void testApplyMessageSourceResolvableToStringArgumentValueWithUnresolvedLogicalFieldName() {
TestBean testBean = new TestBean();
testBean.setEmail("test@example.com");
testBean.setConfirmEmail("TEST@EXAMPLE.IO");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
validatorAdapter.validate(testBean, errors);
assertThat(errors.getFieldErrorCount("email")).isEqualTo(1);
assertThat(errors.getFieldValue("email")).isEqualTo("test@example.com");
assertThat(errors.getFieldErrorCount("confirmEmail")).isEqualTo(1);
FieldError error1 = errors.getFieldError("email");
FieldError error2 = errors.getFieldError("confirmEmail");
assertThat(error1).isNotNull();
assertThat(error2).isNotNull();
assertThat(messageSource.getMessage(error1, Locale.ENGLISH)).isEqualTo("email must be same value as confirmEmail");
assertThat(messageSource.getMessage(error2, Locale.ENGLISH)).isEqualTo("Email required");
assertThat(error1.contains(ConstraintViolation.class)).isTrue();
assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("email");
assertThat(error2.contains(ConstraintViolation.class)).isTrue();
assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("confirmEmail");
}
use of cn.taketoday.validation.BeanPropertyBindingResult in project today-framework by TAKETODAY.
the class DataBinderTests method testAddAllErrors.
@Test
void testAddAllErrors() {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
PropertyValues pvs = new PropertyValues();
pvs.add("age", "32x");
binder.bind(pvs);
Errors errors = binder.getBindingResult();
BeanPropertyBindingResult errors2 = new BeanPropertyBindingResult(rod, "person");
errors.rejectValue("name", "badName");
errors.addAllErrors(errors2);
FieldError ageError = errors.getFieldError("age");
assertThat(ageError.getCode()).isEqualTo("typeMismatch");
FieldError nameError = errors.getFieldError("name");
assertThat(nameError.getCode()).isEqualTo("badName");
}
use of cn.taketoday.validation.BeanPropertyBindingResult in project today-framework by TAKETODAY.
the class DataBinderTests method testRejectWithoutDefaultMessage.
@Test
void testRejectWithoutDefaultMessage() {
TestBean tb = new TestBean();
tb.setName("myName");
tb.setAge(99);
BeanPropertyBindingResult ex = new BeanPropertyBindingResult(tb, "tb");
ex.reject("invalid");
ex.rejectValue("age", "invalidField");
StaticMessageSource ms = new StaticMessageSource();
ms.addMessage("invalid", Locale.US, "general error");
ms.addMessage("invalidField", Locale.US, "invalid field");
assertThat(ms.getMessage(ex.getGlobalError(), Locale.US)).isEqualTo("general error");
assertThat(ms.getMessage(ex.getFieldError("age"), Locale.US)).isEqualTo("invalid field");
}
use of cn.taketoday.validation.BeanPropertyBindingResult in project today-framework by TAKETODAY.
the class DataBinderTests method testValidatorWithErrors.
@Test
void testValidatorWithErrors() {
TestBean tb = new TestBean();
tb.setSpouse(new TestBean());
Errors errors = new BeanPropertyBindingResult(tb, "tb");
Validator testValidator = new TestBeanValidator();
testValidator.validate(tb, errors);
errors.setNestedPath("spouse.");
assertThat(errors.getNestedPath()).isEqualTo("spouse.");
Validator spouseValidator = new SpouseValidator();
spouseValidator.validate(tb.getSpouse(), errors);
errors.setNestedPath("");
assertThat(errors.hasErrors()).isTrue();
assertThat(errors.getErrorCount()).isEqualTo(6);
assertThat(errors.getGlobalErrorCount()).isEqualTo(2);
assertThat(errors.getGlobalError().getCode()).isEqualTo("NAME_TOUCHY_MISMATCH");
assertThat((errors.getGlobalErrors().get(0)).getCode()).isEqualTo("NAME_TOUCHY_MISMATCH");
assertThat((errors.getGlobalErrors().get(0)).getCodes()[0]).isEqualTo("NAME_TOUCHY_MISMATCH.tb");
assertThat((errors.getGlobalErrors().get(0)).getCodes()[1]).isEqualTo("NAME_TOUCHY_MISMATCH");
assertThat((errors.getGlobalErrors().get(0)).getObjectName()).isEqualTo("tb");
assertThat((errors.getGlobalErrors().get(1)).getCode()).isEqualTo("GENERAL_ERROR");
assertThat((errors.getGlobalErrors().get(1)).getCodes()[0]).isEqualTo("GENERAL_ERROR.tb");
assertThat((errors.getGlobalErrors().get(1)).getCodes()[1]).isEqualTo("GENERAL_ERROR");
assertThat((errors.getGlobalErrors().get(1)).getDefaultMessage()).isEqualTo("msg");
assertThat((errors.getGlobalErrors().get(1)).getArguments()[0]).isEqualTo("arg");
assertThat(errors.hasFieldErrors()).isTrue();
assertThat(errors.getFieldErrorCount()).isEqualTo(4);
assertThat(errors.getFieldError().getCode()).isEqualTo("TOO_YOUNG");
assertThat((errors.getFieldErrors().get(0)).getCode()).isEqualTo("TOO_YOUNG");
assertThat((errors.getFieldErrors().get(0)).getField()).isEqualTo("age");
assertThat((errors.getFieldErrors().get(1)).getCode()).isEqualTo("AGE_NOT_ODD");
assertThat((errors.getFieldErrors().get(1)).getField()).isEqualTo("age");
assertThat((errors.getFieldErrors().get(2)).getCode()).isEqualTo("NOT_ROD");
assertThat((errors.getFieldErrors().get(2)).getField()).isEqualTo("name");
assertThat((errors.getFieldErrors().get(3)).getCode()).isEqualTo("TOO_YOUNG");
assertThat((errors.getFieldErrors().get(3)).getField()).isEqualTo("spouse.age");
assertThat(errors.hasFieldErrors("age")).isTrue();
assertThat(errors.getFieldErrorCount("age")).isEqualTo(2);
assertThat(errors.getFieldError("age").getCode()).isEqualTo("TOO_YOUNG");
assertThat((errors.getFieldErrors("age").get(0)).getCode()).isEqualTo("TOO_YOUNG");
assertThat((errors.getFieldErrors("age").get(0)).getObjectName()).isEqualTo("tb");
assertThat((errors.getFieldErrors("age").get(0)).getField()).isEqualTo("age");
assertThat((errors.getFieldErrors("age").get(0)).getRejectedValue()).isEqualTo(0);
assertThat((errors.getFieldErrors("age").get(1)).getCode()).isEqualTo("AGE_NOT_ODD");
assertThat(errors.hasFieldErrors("name")).isTrue();
assertThat(errors.getFieldErrorCount("name")).isEqualTo(1);
assertThat(errors.getFieldError("name").getCode()).isEqualTo("NOT_ROD");
assertThat(errors.getFieldError("name").getCodes()[0]).isEqualTo("NOT_ROD.tb.name");
assertThat(errors.getFieldError("name").getCodes()[1]).isEqualTo("NOT_ROD.name");
assertThat(errors.getFieldError("name").getCodes()[2]).isEqualTo("NOT_ROD.java.lang.String");
assertThat(errors.getFieldError("name").getCodes()[3]).isEqualTo("NOT_ROD");
assertThat((errors.getFieldErrors("name").get(0)).getField()).isEqualTo("name");
assertThat((errors.getFieldErrors("name").get(0)).getRejectedValue()).isNull();
assertThat(errors.hasFieldErrors("spouse.age")).isTrue();
assertThat(errors.getFieldErrorCount("spouse.age")).isEqualTo(1);
assertThat(errors.getFieldError("spouse.age").getCode()).isEqualTo("TOO_YOUNG");
assertThat((errors.getFieldErrors("spouse.age").get(0)).getObjectName()).isEqualTo("tb");
assertThat((errors.getFieldErrors("spouse.age").get(0)).getRejectedValue()).isEqualTo(0);
}
use of cn.taketoday.validation.BeanPropertyBindingResult in project today-framework by TAKETODAY.
the class DataBinderTests method testValidatorWithErrorsAndCodesPrefix.
@Test
void testValidatorWithErrorsAndCodesPrefix() {
TestBean tb = new TestBean();
tb.setSpouse(new TestBean());
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(tb, "tb");
DefaultMessageCodesResolver codesResolver = new DefaultMessageCodesResolver();
codesResolver.setPrefix("validation.");
errors.setMessageCodesResolver(codesResolver);
Validator testValidator = new TestBeanValidator();
testValidator.validate(tb, errors);
errors.setNestedPath("spouse.");
assertThat(errors.getNestedPath()).isEqualTo("spouse.");
Validator spouseValidator = new SpouseValidator();
spouseValidator.validate(tb.getSpouse(), errors);
errors.setNestedPath("");
assertThat(errors.hasErrors()).isTrue();
assertThat(errors.getErrorCount()).isEqualTo(6);
assertThat(errors.getGlobalErrorCount()).isEqualTo(2);
assertThat(errors.getGlobalError().getCode()).isEqualTo("validation.NAME_TOUCHY_MISMATCH");
assertThat((errors.getGlobalErrors().get(0)).getCode()).isEqualTo("validation.NAME_TOUCHY_MISMATCH");
assertThat((errors.getGlobalErrors().get(0)).getCodes()[0]).isEqualTo("validation.NAME_TOUCHY_MISMATCH.tb");
assertThat((errors.getGlobalErrors().get(0)).getCodes()[1]).isEqualTo("validation.NAME_TOUCHY_MISMATCH");
assertThat((errors.getGlobalErrors().get(0)).getObjectName()).isEqualTo("tb");
assertThat((errors.getGlobalErrors().get(1)).getCode()).isEqualTo("validation.GENERAL_ERROR");
assertThat((errors.getGlobalErrors().get(1)).getCodes()[0]).isEqualTo("validation.GENERAL_ERROR.tb");
assertThat((errors.getGlobalErrors().get(1)).getCodes()[1]).isEqualTo("validation.GENERAL_ERROR");
assertThat((errors.getGlobalErrors().get(1)).getDefaultMessage()).isEqualTo("msg");
assertThat((errors.getGlobalErrors().get(1)).getArguments()[0]).isEqualTo("arg");
assertThat(errors.hasFieldErrors()).isTrue();
assertThat(errors.getFieldErrorCount()).isEqualTo(4);
assertThat(errors.getFieldError().getCode()).isEqualTo("validation.TOO_YOUNG");
assertThat((errors.getFieldErrors().get(0)).getCode()).isEqualTo("validation.TOO_YOUNG");
assertThat((errors.getFieldErrors().get(0)).getField()).isEqualTo("age");
assertThat((errors.getFieldErrors().get(1)).getCode()).isEqualTo("validation.AGE_NOT_ODD");
assertThat((errors.getFieldErrors().get(1)).getField()).isEqualTo("age");
assertThat((errors.getFieldErrors().get(2)).getCode()).isEqualTo("validation.NOT_ROD");
assertThat((errors.getFieldErrors().get(2)).getField()).isEqualTo("name");
assertThat((errors.getFieldErrors().get(3)).getCode()).isEqualTo("validation.TOO_YOUNG");
assertThat((errors.getFieldErrors().get(3)).getField()).isEqualTo("spouse.age");
assertThat(errors.hasFieldErrors("age")).isTrue();
assertThat(errors.getFieldErrorCount("age")).isEqualTo(2);
assertThat(errors.getFieldError("age").getCode()).isEqualTo("validation.TOO_YOUNG");
assertThat((errors.getFieldErrors("age").get(0)).getCode()).isEqualTo("validation.TOO_YOUNG");
assertThat((errors.getFieldErrors("age").get(0)).getObjectName()).isEqualTo("tb");
assertThat((errors.getFieldErrors("age").get(0)).getField()).isEqualTo("age");
assertThat((errors.getFieldErrors("age").get(0)).getRejectedValue()).isEqualTo(0);
assertThat((errors.getFieldErrors("age").get(1)).getCode()).isEqualTo("validation.AGE_NOT_ODD");
assertThat(errors.hasFieldErrors("name")).isTrue();
assertThat(errors.getFieldErrorCount("name")).isEqualTo(1);
assertThat(errors.getFieldError("name").getCode()).isEqualTo("validation.NOT_ROD");
assertThat(errors.getFieldError("name").getCodes()[0]).isEqualTo("validation.NOT_ROD.tb.name");
assertThat(errors.getFieldError("name").getCodes()[1]).isEqualTo("validation.NOT_ROD.name");
assertThat(errors.getFieldError("name").getCodes()[2]).isEqualTo("validation.NOT_ROD.java.lang.String");
assertThat(errors.getFieldError("name").getCodes()[3]).isEqualTo("validation.NOT_ROD");
assertThat((errors.getFieldErrors("name").get(0)).getField()).isEqualTo("name");
assertThat((errors.getFieldErrors("name").get(0)).getRejectedValue()).isNull();
assertThat(errors.hasFieldErrors("spouse.age")).isTrue();
assertThat(errors.getFieldErrorCount("spouse.age")).isEqualTo(1);
assertThat(errors.getFieldError("spouse.age").getCode()).isEqualTo("validation.TOO_YOUNG");
assertThat((errors.getFieldErrors("spouse.age").get(0)).getObjectName()).isEqualTo("tb");
assertThat((errors.getFieldErrors("spouse.age").get(0)).getRejectedValue()).isEqualTo(0);
}
Aggregations