Search in sources :

Example 46 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testBindOverlappingNestedMaps.

@SuppressWarnings("unchecked")
@Test
public void testBindOverlappingNestedMaps() throws Exception {
    Map<String, Object> target = new LinkedHashMap<>();
    BindingResult result = bind(target, "a.b.c.d: abc\na.b.c1.d1: efg");
    assertThat(result.getErrorCount()).isEqualTo(0);
    Map<String, Object> a = (Map<String, Object>) target.get("a");
    Map<String, Object> b = (Map<String, Object>) a.get("b");
    Map<String, Object> c = (Map<String, Object>) b.get("c");
    assertThat(c.get("d")).isEqualTo("abc");
    Map<String, Object> c1 = (Map<String, Object>) b.get("c1");
    assertThat(c1.get("d1")).isEqualTo("efg");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 47 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testSimpleValidation.

@Test
public void testSimpleValidation() throws Exception {
    ValidatedTarget target = new ValidatedTarget();
    BindingResult result = bind(target, "");
    assertThat(result.getErrorCount()).isEqualTo(1);
}
Also used : BindingResult(org.springframework.validation.BindingResult) Test(org.junit.Test)

Example 48 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testRequiredFieldsValidation.

@Test
public void testRequiredFieldsValidation() throws Exception {
    TargetWithValidatedMap target = new TargetWithValidatedMap();
    BindingResult result = bind(target, "info[foo]: bar");
    assertThat(result.getErrorCount()).isEqualTo(2);
    for (FieldError error : result.getFieldErrors()) {
        System.err.println(new StaticMessageSource().getMessage(error, Locale.getDefault()));
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) StaticMessageSource(org.springframework.context.support.StaticMessageSource) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test)

Example 49 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testBindMapWithDifferentDeepClashInProperties.

@Test
public void testBindMapWithDifferentDeepClashInProperties() throws Exception {
    Map<String, Object> target = new LinkedHashMap<>();
    BindingResult result = bind(target, "vanilla.spam.bar: bar\n" + "vanilla.spam.bar.value: 123", "vanilla");
    assertThat(result.getErrorCount()).isEqualTo(0);
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) target.get("spam");
    assertThat(map.get("bar.value")).isEqualTo("123");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 50 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testBindErrorNotWritable.

@Test
public void testBindErrorNotWritable() throws Exception {
    this.expected.expectMessage("property 'spam'");
    this.expected.expectMessage("not writable");
    VanillaTarget target = new VanillaTarget();
    BindingResult result = bind(target, "spam: bar\n" + "value: 123");
    assertThat(result.getErrorCount()).isEqualTo(1);
}
Also used : BindingResult(org.springframework.validation.BindingResult) Test(org.junit.Test)

Aggregations

BindingResult (org.springframework.validation.BindingResult)59 Test (org.junit.Test)29 Model (org.springframework.ui.Model)12 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)11 LinkedHashMap (java.util.LinkedHashMap)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ObjectError (org.springframework.validation.ObjectError)6 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)5 CreateUserFormValidator (org.asqatasun.webapp.validator.CreateUserFormValidator)5 ScriptableObject (org.mozilla.javascript.ScriptableObject)5 MvcResult (org.springframework.test.web.servlet.MvcResult)5 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)5 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)5 RedirectAttributes (org.springframework.web.servlet.mvc.support.RedirectAttributes)5 SimpleDateFormat (java.text.SimpleDateFormat)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 Valid (javax.validation.Valid)4 Controller (org.springframework.stereotype.Controller)4