Search in sources :

Example 1 with BeanPropertyBindingResult

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

the class ValidatorAdapterTests method testPatternMessage.

@Test
public void testPatternMessage() {
    TestBean testBean = new TestBean();
    testBean.setEmail("X");
    testBean.setConfirmEmail("X");
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
    validatorAdapter.validate(testBean, errors);
    assertThat(errors.getFieldErrorCount("email")).isEqualTo(1);
    assertThat(errors.getFieldValue("email")).isEqualTo("X");
    FieldError error = errors.getFieldError("email");
    assertThat(error).isNotNull();
    assertThat(messageSource.getMessage(error, Locale.ENGLISH)).contains("[\\w.'-]{1,}@[\\w.'-]{1,}");
    assertThat(error.contains(ConstraintViolation.class)).isTrue();
    assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("email");
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) FieldError(cn.taketoday.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 2 with BeanPropertyBindingResult

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

the class ValidatorAdapterTests method testMapEntryConstraint.

// SPR-15839
@Test
public void testMapEntryConstraint() {
    Map<String, String> property = new HashMap<>();
    property.put(null, null);
    BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint();
    bean.setProperty(property);
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean");
    validatorAdapter.validate(bean, errors);
    assertThat(errors.hasFieldErrors("property[]")).isTrue();
    assertThat(errors.getFieldValue("property[]")).isNull();
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 3 with BeanPropertyBindingResult

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

the class ValidatorAdapterTests method testNoStringArgumentValue.

// SPR-13406
@Test
public void testNoStringArgumentValue() throws Exception {
    TestBean testBean = new TestBean();
    testBean.setPassword("pass");
    testBean.setConfirmPassword("pass");
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
    validatorAdapter.validate(testBean, errors);
    assertThat(errors.getFieldErrorCount("password")).isEqualTo(1);
    assertThat(errors.getFieldValue("password")).isEqualTo("pass");
    FieldError error = errors.getFieldError("password");
    assertThat(error).isNotNull();
    assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password must be between 8 and 128");
    assertThat(error.contains(ConstraintViolation.class)).isTrue();
    assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password");
    assertThat(SerializationTestUtils.serializeAndDeserialize(error.toString())).isEqualTo(error.toString());
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) FieldError(cn.taketoday.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 4 with BeanPropertyBindingResult

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

the class ValidatorAdapterTests method testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLogicalFieldName.

// SPR-13406
@Test
public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLogicalFieldName() throws Exception {
    TestBean testBean = new TestBean();
    testBean.setPassword("password");
    testBean.setConfirmPassword("PASSWORD");
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
    validatorAdapter.validate(testBean, errors);
    assertThat(errors.getFieldErrorCount("password")).isEqualTo(1);
    assertThat(errors.getFieldValue("password")).isEqualTo("password");
    FieldError error = errors.getFieldError("password");
    assertThat(error).isNotNull();
    assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Password must be same value as Password(Confirm)");
    assertThat(error.contains(ConstraintViolation.class)).isTrue();
    assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password");
    assertThat(SerializationTestUtils.serializeAndDeserialize(error.toString())).isEqualTo(error.toString());
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) FieldError(cn.taketoday.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 5 with BeanPropertyBindingResult

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

the class ValidatorAdapterTests method testWithList.

// SPR-16177
@Test
public void testWithList() {
    Parent parent = new Parent();
    parent.setName("Parent whit list");
    parent.getChildList().addAll(createChildren(parent));
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent");
    validatorAdapter.validate(parent, errors);
    assertThat(errors.getErrorCount() > 0).isTrue();
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) 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