Search in sources :

Example 41 with BeanPropertyBindingResult

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");
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) FieldError(cn.taketoday.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 42 with BeanPropertyBindingResult

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");
}
Also used : Errors(cn.taketoday.validation.Errors) PropertyValues(cn.taketoday.beans.PropertyValues) BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) FieldError(cn.taketoday.validation.FieldError) DataBinder(cn.taketoday.validation.DataBinder) Test(org.junit.jupiter.api.Test)

Example 43 with BeanPropertyBindingResult

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");
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) StaticMessageSource(cn.taketoday.context.support.StaticMessageSource) Test(org.junit.jupiter.api.Test)

Example 44 with BeanPropertyBindingResult

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);
}
Also used : Errors(cn.taketoday.validation.Errors) BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Validator(cn.taketoday.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 45 with BeanPropertyBindingResult

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);
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DefaultMessageCodesResolver(cn.taketoday.validation.DefaultMessageCodesResolver) Validator(cn.taketoday.validation.Validator) Test(org.junit.jupiter.api.Test)

Aggregations

BeanPropertyBindingResult (cn.taketoday.validation.BeanPropertyBindingResult)45 Test (org.junit.jupiter.api.Test)45 FieldError (cn.taketoday.validation.FieldError)19 Errors (cn.taketoday.validation.Errors)8 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)7 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)7 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)7 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)7 ObjectError (cn.taketoday.validation.ObjectError)4 Validator (cn.taketoday.validation.Validator)4 HashMap (java.util.HashMap)4 PropertyValues (cn.taketoday.beans.PropertyValues)2 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)2 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)2 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)2 DataBinder (cn.taketoday.validation.DataBinder)2 StaticMessageSource (cn.taketoday.context.support.StaticMessageSource)1 BindException (cn.taketoday.validation.BindException)1 BindingResult (cn.taketoday.validation.BindingResult)1 DefaultMessageCodesResolver (cn.taketoday.validation.DefaultMessageCodesResolver)1