Search in sources :

Example 1 with BindException

use of cn.taketoday.validation.BindException 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 2 with BindException

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

Example 3 with BindException

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

the class ErrorResponseExceptionTests method methodArgumentNotValidException.

@Test
void methodArgumentNotValidException() {
    BindingResult bindingResult = new BindException(new Object(), "object");
    bindingResult.addError(new FieldError("object", "field", "message"));
    ErrorResponse ex = new MethodArgumentNotValidException(this.methodParameter, bindingResult);
    assertStatus(ex, HttpStatus.BAD_REQUEST);
    assertDetail(ex, "Invalid request content.");
    assertThat(ex.getHeaders()).isEmpty();
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) BindException(cn.taketoday.validation.BindException) FieldError(cn.taketoday.validation.FieldError) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 4 with BindException

use of cn.taketoday.validation.BindException in project today-infrastructure 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 5 with BindException

use of cn.taketoday.validation.BindException in project today-framework by TAKETODAY.

the class DataBinderTests method testBindingNoErrors.

@Test
void testBindingNoErrors() throws BindException {
    TestBean rod = new TestBean();
    DataBinder binder = new DataBinder(rod, "person");
    assertThat(binder.isIgnoreUnknownFields()).isTrue();
    PropertyValues pvs = new PropertyValues();
    pvs.add("name", "Rod");
    pvs.add("age", "032");
    pvs.add("nonExisting", "someValue");
    binder.bind(pvs);
    binder.close();
    assertThat(rod.getName().equals("Rod")).as("changed name correctly").isTrue();
    assertThat(rod.getAge() == 32).as("changed age correctly").isTrue();
    Map<?, ?> map = binder.getBindingResult().getModel();
    assertThat(map.size() == 2).as("There is one element in map").isTrue();
    TestBean tb = (TestBean) map.get("person");
    assertThat(tb.equals(rod)).as("Same object").isTrue();
    BindingResult other = new BeanPropertyBindingResult(rod, "person");
    assertThat(binder.getBindingResult()).isEqualTo(other);
    assertThat(other).isEqualTo(binder.getBindingResult());
    BindException ex = new BindException(other);
    assertThat(other).isEqualTo(ex);
    assertThat(ex).isEqualTo(other);
    assertThat(binder.getBindingResult()).isEqualTo(ex);
    assertThat(ex).isEqualTo(binder.getBindingResult());
    other.reject("xxx");
    boolean condition = !other.equals(binder.getBindingResult());
    assertThat(condition).isTrue();
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) BindingResult(cn.taketoday.validation.BindingResult) PropertyValues(cn.taketoday.beans.PropertyValues) BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BindException(cn.taketoday.validation.BindException) DataBinder(cn.taketoday.validation.DataBinder) 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