Search in sources :

Example 16 with BeanPropertyBindingResult

use of cn.taketoday.validation.BeanPropertyBindingResult in project today-framework by TAKETODAY.

the class ValidatorFactoryTests method testSpringValidationWithErrorInSetElement.

@Test
public void testSpringValidationWithErrorInSetElement() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    person.getAddressSet().add(new ValidAddress());
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, result);
    assertThat(result.getErrorCount()).isEqualTo(3);
    FieldError fieldError = result.getFieldError("name");
    assertThat(fieldError.getField()).isEqualTo("name");
    fieldError = result.getFieldError("address.street");
    assertThat(fieldError.getField()).isEqualTo("address.street");
    fieldError = result.getFieldError("addressSet[].street");
    assertThat(fieldError.getField()).isEqualTo("addressSet[].street");
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) FieldError(cn.taketoday.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 17 with BeanPropertyBindingResult

use of cn.taketoday.validation.BeanPropertyBindingResult in project today-framework by TAKETODAY.

the class ValidatorFactoryTests method testInnerBeanValidation.

@Test
public void testInnerBeanValidation() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    MainBean mainBean = new MainBean();
    Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean");
    validator.validate(mainBean, errors);
    Object rejected = errors.getFieldValue("inner.value");
    assertThat(rejected).isNull();
}
Also used : Errors(cn.taketoday.validation.Errors) BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) Test(org.junit.jupiter.api.Test)

Example 18 with BeanPropertyBindingResult

use of cn.taketoday.validation.BeanPropertyBindingResult in project today-framework by TAKETODAY.

the class ValidatorFactoryTests method testSpringValidationFieldType.

@Test
public void testSpringValidationFieldType() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    person.setName("Phil");
    person.getAddress().setStreet("Phil's Street");
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, errors);
    assertThat(errors.getErrorCount()).isEqualTo(1);
    assertThat(errors.getFieldError("address").getRejectedValue()).as("Field/Value type mismatch").isInstanceOf(ValidAddress.class);
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) Test(org.junit.jupiter.api.Test)

Example 19 with BeanPropertyBindingResult

use of cn.taketoday.validation.BeanPropertyBindingResult in project today-framework by TAKETODAY.

the class ValidatorFactoryTests method testSpringValidationWithErrorInListElement.

@Test
public void testSpringValidationWithErrorInListElement() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    person.getAddressList().add(new ValidAddress());
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, result);
    assertThat(result.getErrorCount()).isEqualTo(3);
    FieldError fieldError = result.getFieldError("name");
    assertThat(fieldError.getField()).isEqualTo("name");
    fieldError = result.getFieldError("address.street");
    assertThat(fieldError.getField()).isEqualTo("address.street");
    fieldError = result.getFieldError("addressList[0].street");
    assertThat(fieldError.getField()).isEqualTo("addressList[0].street");
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) FieldError(cn.taketoday.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 20 with BeanPropertyBindingResult

use of cn.taketoday.validation.BeanPropertyBindingResult in project today-framework by TAKETODAY.

the class ValidatorFactoryTests method testSpringValidation.

@Test
public void testSpringValidation() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, result);
    assertThat(result.getErrorCount()).isEqualTo(2);
    FieldError fieldError = result.getFieldError("name");
    assertThat(fieldError.getField()).isEqualTo("name");
    List<String> errorCodes = Arrays.asList(fieldError.getCodes());
    assertThat(errorCodes.size()).isEqualTo(4);
    assertThat(errorCodes.contains("NotNull.person.name")).isTrue();
    assertThat(errorCodes.contains("NotNull.name")).isTrue();
    assertThat(errorCodes.contains("NotNull.java.lang.String")).isTrue();
    assertThat(errorCodes.contains("NotNull")).isTrue();
    fieldError = result.getFieldError("address.street");
    assertThat(fieldError.getField()).isEqualTo("address.street");
    errorCodes = Arrays.asList(fieldError.getCodes());
    assertThat(errorCodes.size()).isEqualTo(5);
    assertThat(errorCodes.contains("NotNull.person.address.street")).isTrue();
    assertThat(errorCodes.contains("NotNull.address.street")).isTrue();
    assertThat(errorCodes.contains("NotNull.street")).isTrue();
    assertThat(errorCodes.contains("NotNull.java.lang.String")).isTrue();
    assertThat(errorCodes.contains("NotNull")).isTrue();
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) FieldError(cn.taketoday.validation.FieldError) 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