Search in sources :

Example 1 with BindingResult

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

the class DateFormattingTests method testBindDateAnnotatedPatternWithGlobalFormat.

@Test
void testBindDateAnnotatedPatternWithGlobalFormat() {
    DateFormatterRegistrar registrar = new DateFormatterRegistrar();
    DateFormatter dateFormatter = new DateFormatter();
    dateFormatter.setIso(ISO.DATE_TIME);
    registrar.setFormatter(dateFormatter);
    setup(registrar);
    PropertyValues propertyValues = new PropertyValues();
    propertyValues.add("patternDate", "10/31/09 1:05");
    binder.bind(propertyValues);
    BindingResult bindingResult = binder.getBindingResult();
    assertThat(bindingResult.getErrorCount()).isEqualTo(0);
    assertThat(bindingResult.getFieldValue("patternDate")).isEqualTo("10/31/09 1:05");
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) PropertyValues(cn.taketoday.beans.PropertyValues) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with BindingResult

use of cn.taketoday.validation.BindingResult 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 3 with BindingResult

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

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

the class DefaultErrorAttributesTests method withoutBindingErrors.

@Test
void withoutBindingErrors() {
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new BindException(bindingResult);
    testBindingResult(bindingResult, ex, ErrorAttributeOptions.defaults());
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) MapBindingResult(cn.taketoday.validation.MapBindingResult) ObjectError(cn.taketoday.validation.ObjectError) BindException(cn.taketoday.validation.BindException) MapBindingResult(cn.taketoday.validation.MapBindingResult) ServletException(jakarta.servlet.ServletException) BindException(cn.taketoday.validation.BindException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 5 with BindingResult

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

the class DefaultErrorAttributesTests method withBindingErrors.

@Test
void withBindingErrors() {
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new BindException(bindingResult);
    testBindingResult(bindingResult, ex, ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) MapBindingResult(cn.taketoday.validation.MapBindingResult) ObjectError(cn.taketoday.validation.ObjectError) BindException(cn.taketoday.validation.BindException) MapBindingResult(cn.taketoday.validation.MapBindingResult) ServletException(jakarta.servlet.ServletException) BindException(cn.taketoday.validation.BindException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Aggregations

BindingResult (cn.taketoday.validation.BindingResult)22 Test (org.junit.jupiter.api.Test)20 PropertyValues (cn.taketoday.beans.PropertyValues)11 BindException (cn.taketoday.validation.BindException)9 FieldError (cn.taketoday.validation.FieldError)8 ObjectError (cn.taketoday.validation.ObjectError)8 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)8 MapBindingResult (cn.taketoday.validation.MapBindingResult)6 ServletException (jakarta.servlet.ServletException)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)5 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)5 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)5 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)5 BeanPropertyBindingResult (cn.taketoday.validation.BeanPropertyBindingResult)5 DataBinder (cn.taketoday.validation.DataBinder)5 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)2 MethodParameter (cn.taketoday.core.MethodParameter)2 ConversionFailedException (cn.taketoday.core.conversion.ConversionFailedException)2 Method (java.lang.reflect.Method)2