Search in sources :

Example 1 with FieldError

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

the class DateFormattingTests method styleDateWithInvalidFormat.

@Test
void styleDateWithInvalidFormat() {
    String propertyName = "styleDate";
    String propertyValue = "99/01/01";
    PropertyValues propertyValues = new PropertyValues();
    propertyValues.add(propertyName, propertyValue);
    binder.bind(propertyValues);
    BindingResult bindingResult = binder.getBindingResult();
    assertThat(bindingResult.getErrorCount()).isEqualTo(1);
    FieldError fieldError = bindingResult.getFieldError(propertyName);
    TypeMismatchException exception = fieldError.unwrap(TypeMismatchException.class);
    assertThat(exception).hasMessageContaining("for property 'styleDate'").hasCauseInstanceOf(ConversionFailedException.class).getCause().hasMessageContaining("for value '99/01/01'").hasCauseInstanceOf(IllegalArgumentException.class).getCause().hasMessageContaining("Parse attempt failed for value [99/01/01]").hasCauseInstanceOf(ParseException.class).getCause().hasMessageContainingAll("Unable to parse date time value \"99/01/01\" using configuration from", "@cn.taketoday.format.annotation.DateTimeFormat", "style=", "S-", "iso=NONE").hasCauseInstanceOf(ParseException.class).getCause().hasMessageStartingWith("Unparseable date: \"99/01/01\"").hasNoCause();
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) PropertyValues(cn.taketoday.beans.PropertyValues) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) FieldError(cn.taketoday.validation.FieldError) ParseException(java.text.ParseException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with FieldError

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

the class DateTimeFormattingTests method isoLocalDateWithInvalidFormat.

@Test
void isoLocalDateWithInvalidFormat() {
    PropertyValues propertyValues = new PropertyValues();
    String propertyName = "isoLocalDate";
    propertyValues.add(propertyName, "2009-31-10");
    binder.bind(propertyValues);
    BindingResult bindingResult = binder.getBindingResult();
    assertThat(bindingResult.getErrorCount()).isEqualTo(1);
    FieldError fieldError = bindingResult.getFieldError(propertyName);
    assertThat(fieldError.unwrap(TypeMismatchException.class)).hasMessageContaining("for property 'isoLocalDate'").hasCauseInstanceOf(ConversionFailedException.class).getCause().hasMessageContaining("for value '2009-31-10'").hasCauseInstanceOf(IllegalArgumentException.class).getCause().hasMessageContaining("Parse attempt failed for value [2009-31-10]").hasCauseInstanceOf(DateTimeParseException.class).getCause().hasMessageContainingAll("Unable to parse date time value \"2009-31-10\" using configuration from", "@cn.taketoday.format.annotation.DateTimeFormat", "iso=DATE").hasCauseInstanceOf(DateTimeParseException.class).getCause().hasMessageStartingWith("Text '2009-31-10'").hasCauseInstanceOf(DateTimeException.class).getCause().hasMessageContaining("Invalid value for MonthOfYear (valid values 1 - 12): 31").hasNoCause();
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) BindingResult(cn.taketoday.validation.BindingResult) PropertyValues(cn.taketoday.beans.PropertyValues) DateTimeException(java.time.DateTimeException) ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException) FieldError(cn.taketoday.validation.FieldError) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with FieldError

use of cn.taketoday.validation.FieldError 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 4 with FieldError

use of cn.taketoday.validation.FieldError 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 5 with FieldError

use of cn.taketoday.validation.FieldError 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)

Aggregations

FieldError (cn.taketoday.validation.FieldError)38 Test (org.junit.jupiter.api.Test)38 BeanPropertyBindingResult (cn.taketoday.validation.BeanPropertyBindingResult)21 PropertyValues (cn.taketoday.beans.PropertyValues)8 BindingResult (cn.taketoday.validation.BindingResult)8 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)4 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)4 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)4 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)4 BindException (cn.taketoday.validation.BindException)4 DataBinder (cn.taketoday.validation.DataBinder)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)2 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)2 ConfigurationProperty (cn.taketoday.context.properties.source.ConfigurationProperty)2 ConfigurationPropertyName (cn.taketoday.context.properties.source.ConfigurationPropertyName)2 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)2 ConversionFailedException (cn.taketoday.core.conversion.ConversionFailedException)2 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)2 FailureAnalysis (cn.taketoday.framework.diagnostics.FailureAnalysis)2