use of cn.taketoday.validation.BindException in project today-framework by TAKETODAY.
the class DataBinderTests method testBindExceptionSerializable.
@Test
void testBindExceptionSerializable() throws Exception {
SerializablePerson tb = new SerializablePerson();
tb.setName("myName");
tb.setAge(99);
BindException ex = new BindException(tb, "tb");
ex.reject("invalid", "someMessage");
ex.rejectValue("age", "invalidField", "someMessage");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(ex);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
BindException ex2 = (BindException) ois.readObject();
assertThat(ex2.hasGlobalErrors()).isTrue();
assertThat(ex2.getGlobalError().getCode()).isEqualTo("invalid");
assertThat(ex2.hasFieldErrors("age")).isTrue();
assertThat(ex2.getFieldError("age").getCode()).isEqualTo("invalidField");
assertThat(ex2.getFieldValue("age")).isEqualTo(99);
ex2.rejectValue("name", "invalidField", "someMessage");
assertThat(ex2.hasFieldErrors("name")).isTrue();
assertThat(ex2.getFieldError("name").getCode()).isEqualTo("invalidField");
assertThat(ex2.getFieldValue("name")).isEqualTo("myName");
}
use of cn.taketoday.validation.BindException in project today-framework by TAKETODAY.
the class BindValidationFailureAnalyzerTests method otherBindExceptionShouldReturnAnalysis.
@Test
void otherBindExceptionShouldReturnAnalysis() {
BindException cause = new BindException(new FieldValidationFailureProperties(), "fieldValidationFailureProperties");
cause.addError(new FieldError("test", "value", "must not be null"));
BeanCreationException rootFailure = new BeanCreationException("bean creation failure", cause);
FailureAnalysis analysis = new BindValidationFailureAnalyzer().analyze(rootFailure, rootFailure);
assertThat(analysis.getDescription()).contains(failure("test.value", "null", "must not be null"));
}
use of cn.taketoday.validation.BindException in project today-framework 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.BindException in project today-framework by TAKETODAY.
the class ResponseEntityExceptionHandlerTests method bindException.
@Test
public void bindException() {
Exception ex = new BindException(new Object(), "name");
testException(ex);
}
use of cn.taketoday.validation.BindException in project today-framework 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