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");
}
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();
}
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();
}
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());
}
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));
}
Aggregations