/** * Create a new BindException instance for a target bean. * @param target target bean to bind onto * @param objectName the name of the target object * @see BeanPropertyBindingResult */ public BindException(Object target, String objectName) { Assert.notNull(target, "Target object must not be null"); this.bindingResult = new BeanPropertyBindingResult(target, objectName); }
@Test(expected = IllegalArgumentException.class) public void testInvokeValidatorWithNullValidator() throws Exception { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.invokeValidator(null, tb, errors); }
@Test // SPR-15839 public void testMapValueConstraint() { Map<String, String> property = new HashMap<>(); property.put("no value can be", null); BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint(); bean.setProperty(property); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean"); validatorAdapter.validate(bean, errors); assertThat(errors.getFieldErrorCount("property[no value can be]"), is(1)); assertNull(errors.getFieldValue("property[no value can be]")); }
@Test // SPR-15839 public void testListElementConstraint() { BeanWithListElementConstraint bean = new BeanWithListElementConstraint(); bean.setProperty(Arrays.asList("no", "element", "can", "be", null)); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean"); validatorAdapter.validate(bean, errors); assertThat(errors.getFieldErrorCount("property[4]"), is(1)); assertNull(errors.getFieldValue("property[4]")); }
@Test // SPR-13406 public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLogicalFieldName() { TestBean testBean = new TestBean(); testBean.setPassword("password"); testBean.setConfirmPassword("PASSWORD"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); assertThat(errors.getFieldErrorCount("password"), is(1)); assertThat(errors.getFieldValue("password"), is("password")); assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), is("Password must be same value with Password(Confirm)")); }
@Test // SPR-13406 public void testNoStringArgumentValue() { TestBean testBean = new TestBean(); testBean.setPassword("pass"); testBean.setConfirmPassword("pass"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean"); validatorAdapter.validate(testBean, errors); assertThat(errors.getFieldErrorCount("password"), is(1)); assertThat(errors.getFieldValue("password"), is("pass")); assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), is("Size of Password is must be between 8 and 128")); }
@Test // SPR-16177 public void testWithList() { Parent parent = new Parent(); parent.setName("Parent whit list"); parent.getChildList().addAll(createChildren(parent)); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent"); validatorAdapter.validate(parent, errors); assertTrue(errors.getErrorCount() > 0); }
@Test // SPR-16177 public void testWithSet() { Parent parent = new Parent(); parent.setName("Parent with set"); parent.getChildSet().addAll(createChildren(parent)); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent"); validatorAdapter.validate(parent, errors); assertTrue(errors.getErrorCount() > 0); }
@Test // SPR-16177 public void testWithSet() { Parent parent = new Parent(); parent.setName("Parent with set"); parent.getChildSet().addAll(createChildren(parent)); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent"); validatorAdapter.validate(parent, errors); assertTrue(errors.getErrorCount() > 0); }
@Test // SPR-16177 public void testWithList() { Parent parent = new Parent(); parent.setName("Parent whit list"); parent.getChildList().addAll(createChildren(parent)); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent"); validatorAdapter.validate(parent, errors); assertTrue(errors.getErrorCount() > 0); }
@Test public void handleMethodArgumentNotValid() throws Exception { BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean"); errors.rejectValue("name", "invalid"); MethodParameter parameter = new MethodParameter(this.getClass().getMethod("handle", String.class), 0); MethodArgumentNotValidException ex = new MethodArgumentNotValidException(parameter, errors); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); }
@Test public void testValidationWithOptionalField() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); MainBeanWithOptional mainBean = new MainBeanWithOptional(); Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean"); validator.validate(mainBean, errors); Object rejected = errors.getFieldValue("inner.value"); assertNull(rejected); }
@Test public void withoutErrors() throws Exception { Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertEquals(0, output.length()); }
@Test public void webBindingInitializer() throws Exception { RequestMappingHandlerAdapter adapter = this.config.requestMappingHandlerAdapter(); ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer(); assertNotNull(initializer); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(null, ""); initializer.getValidator().validate(null, bindingResult); assertEquals("invalid", bindingResult.getAllErrors().get(0).getCode()); String[] codes = initializer.getMessageCodesResolver().resolveMessageCodes("invalid", null); assertEquals("custom.invalid", codes[0]); }
@Test public void testValidationUtilsNull() throws Exception { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); Validator testValidator = new EmptyValidator(); testValidator.validate(tb, errors); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY", errors.getFieldError("name").getCode()); }
@Test public void testValidationUtilsEmpty() throws Exception { TestBean tb = new TestBean(""); Errors errors = new BeanPropertyBindingResult(tb, "tb"); Validator testValidator = new EmptyValidator(); testValidator.validate(tb, errors); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY", errors.getFieldError("name").getCode()); }
@Test public void testInvokeValidatorSunnyDay() throws Exception { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.invokeValidator(new EmptyValidator(), tb, errors); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY", errors.getFieldError("name").getCode()); }
@Test public void testNestedValidatorWithoutNestedPath() { TestBean tb = new TestBean(); tb.setName("XXX"); Errors errors = new BeanPropertyBindingResult(tb, "tb"); Validator spouseValidator = new SpouseValidator(); spouseValidator.validate(tb, errors); assertTrue(errors.hasGlobalErrors()); assertEquals(1, errors.getGlobalErrorCount()); assertEquals("SPOUSE_NOT_AVAILABLE", errors.getGlobalError().getCode()); assertEquals("tb", (errors.getGlobalErrors().get(0)).getObjectName()); }
@Test public void testListValidation() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ListContainer listContainer = new ListContainer(); listContainer.addString("A"); listContainer.addString("X"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(listContainer, "listContainer"); errors.initConversion(new DefaultConversionService()); validator.validate(listContainer, errors); assertEquals("X", errors.getFieldValue("list[1]")); }
@Test public void customBind() throws Exception { BeanPropertyBindingResult result = new BeanPropertyBindingResult(createTestBean(), "testBean"); result.getPropertyAccessor().registerCustomEditor(Float.class, new SimpleFloatEditor()); exposeBindingResult(result); this.tag.setPath("myFloat"); assertEquals(Tag.SKIP_BODY, this.tag.doStartTag()); String output = getOutput(); assertContainsAttribute(output, "name", "myFloat"); assertBlockTagContains(output, "12.34f"); }