Search in sources :

Example 21 with BindingResult

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

the class DefaultErrorAttributesTests method withMethodArgumentNotValidExceptionBindingErrors.

@Test
void withMethodArgumentNotValidExceptionBindingErrors() {
    Method method = ReflectionUtils.findMethod(String.class, "substring", int.class);
    MethodParameter parameter = new MethodParameter(method, 0);
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new MethodArgumentNotValidException(parameter, 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) MapBindingResult(cn.taketoday.validation.MapBindingResult) Method(java.lang.reflect.Method) MethodParameter(cn.taketoday.core.MethodParameter) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) ServletException(jakarta.servlet.ServletException) BindException(cn.taketoday.validation.BindException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 22 with BindingResult

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

the class DataBinderTests method testBindingWithErrors.

@Test
void testBindingWithErrors() {
    TestBean rod = new TestBean();
    DataBinder binder = new DataBinder(rod, "person");
    PropertyValues pvs = new PropertyValues();
    pvs.add("name", "Rod");
    pvs.add("age", "32x");
    pvs.add("touchy", "m.y");
    binder.bind(pvs);
    assertThatExceptionOfType(BindException.class).isThrownBy(binder::close).satisfies(ex -> {
        assertThat(rod.getName()).isEqualTo("Rod");
        Map<?, ?> map = binder.getBindingResult().getModel();
        TestBean tb = (TestBean) map.get("person");
        assertThat(tb).isSameAs(rod);
        BindingResult br = (BindingResult) map.get(BindingResult.MODEL_KEY_PREFIX + "person");
        assertThat(BindingResultUtils.getBindingResult(map, "person")).isEqualTo(br);
        assertThat(BindingResultUtils.getRequiredBindingResult(map, "person")).isEqualTo(br);
        assertThat(BindingResultUtils.getBindingResult(map, "someOtherName")).isNull();
        assertThatIllegalStateException().isThrownBy(() -> BindingResultUtils.getRequiredBindingResult(map, "someOtherName"));
        assertThat(binder.getBindingResult()).as("Added itself to map").isSameAs(br);
        assertThat(br.hasErrors()).isTrue();
        assertThat(br.getErrorCount()).isEqualTo(2);
        assertThat(br.hasFieldErrors("age")).isTrue();
        assertThat(br.getFieldErrorCount("age")).isEqualTo(1);
        assertThat(binder.getBindingResult().getFieldValue("age")).isEqualTo("32x");
        FieldError ageError = binder.getBindingResult().getFieldError("age");
        assertThat(ageError).isNotNull();
        assertThat(ageError.getCode()).isEqualTo("typeMismatch");
        assertThat(ageError.getRejectedValue()).isEqualTo("32x");
        assertThat(ageError.contains(TypeMismatchException.class)).isTrue();
        assertThat(ageError.contains(NumberFormatException.class)).isTrue();
        assertThat(ageError.unwrap(NumberFormatException.class).getMessage()).contains("32x");
        assertThat(tb.getAge()).isEqualTo(0);
        assertThat(br.hasFieldErrors("touchy")).isTrue();
        assertThat(br.getFieldErrorCount("touchy")).isEqualTo(1);
        assertThat(binder.getBindingResult().getFieldValue("touchy")).isEqualTo("m.y");
        FieldError touchyError = binder.getBindingResult().getFieldError("touchy");
        assertThat(touchyError).isNotNull();
        assertThat(touchyError.getCode()).isEqualTo("methodInvocation");
        assertThat(touchyError.getRejectedValue()).isEqualTo("m.y");
        assertThat(touchyError.contains(MethodInvocationException.class)).isTrue();
        assertThat(touchyError.unwrap(MethodInvocationException.class).getCause().getMessage()).contains("a .");
        assertThat(tb.getTouchy()).isNull();
        DataBinder binder2 = new DataBinder(new TestBean(), "person");
        PropertyValues pvs2 = new PropertyValues();
        pvs2.add("name", "Rod");
        pvs2.add("age", "32x");
        pvs2.add("touchy", "m.y");
        binder2.bind(pvs2);
        assertThat(ex.getBindingResult()).isEqualTo(binder2.getBindingResult());
    });
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) BindingResult(cn.taketoday.validation.BindingResult) PropertyValues(cn.taketoday.beans.PropertyValues) 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) FieldError(cn.taketoday.validation.FieldError) DataBinder(cn.taketoday.validation.DataBinder) 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