Search in sources :

Example 6 with BindException

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");
}
Also used : SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) ByteArrayInputStream(java.io.ByteArrayInputStream) BindException(cn.taketoday.validation.BindException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Example 7 with BindException

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"));
}
Also used : BeanCreationException(cn.taketoday.beans.factory.BeanCreationException) BindException(cn.taketoday.validation.BindException) FieldError(cn.taketoday.validation.FieldError) FailureAnalysis(cn.taketoday.framework.diagnostics.FailureAnalysis) Test(org.junit.jupiter.api.Test)

Example 8 with BindException

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());
}
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 9 with BindException

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);
}
Also used : BindException(cn.taketoday.validation.BindException) MissingRequestPartException(cn.taketoday.web.bind.resolver.MissingRequestPartException) MissingPathVariableException(cn.taketoday.web.bind.MissingPathVariableException) ServletException(jakarta.servlet.ServletException) HttpRequestMethodNotSupportedException(cn.taketoday.web.HttpRequestMethodNotSupportedException) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) ConversionNotSupportedException(cn.taketoday.beans.ConversionNotSupportedException) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) BindException(cn.taketoday.validation.BindException) AsyncRequestTimeoutException(cn.taketoday.web.context.async.AsyncRequestTimeoutException) HttpMessageNotWritableException(cn.taketoday.http.converter.HttpMessageNotWritableException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 10 with BindException

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));
}
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

BindException (cn.taketoday.validation.BindException)11 Test (org.junit.jupiter.api.Test)11 BindingResult (cn.taketoday.validation.BindingResult)7 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)7 ServletException (jakarta.servlet.ServletException)5 FieldError (cn.taketoday.validation.FieldError)4 MapBindingResult (cn.taketoday.validation.MapBindingResult)4 ObjectError (cn.taketoday.validation.ObjectError)4 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)2 FailureAnalysis (cn.taketoday.framework.diagnostics.FailureAnalysis)2 ConversionNotSupportedException (cn.taketoday.beans.ConversionNotSupportedException)1 PropertyValues (cn.taketoday.beans.PropertyValues)1 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)1 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)1 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)1 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)1 SerializablePerson (cn.taketoday.beans.testfixture.beans.SerializablePerson)1 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)1 HttpMessageNotReadableException (cn.taketoday.http.converter.HttpMessageNotReadableException)1 HttpMessageNotWritableException (cn.taketoday.http.converter.HttpMessageNotWritableException)1