Package org.springframework.validation

Examples of org.springframework.validation.DataBinder


  public void shouldReverseConvertWithNull() throws Exception {
    doTestReverseConvert(null, null);
  }

  private void doTestReverseConvert(String value, Object expected) throws Exception {
    DataBinder dataBinder = new DataBinder(null);
    initBinder(dataBinder);
    Object converted = (value == null ? null : dataBinder.convertIfNecessary(value, expected.getClass()));
    assertThat(converted, is(equalTo(expected)));
    ReverseDataBinder reverseDataBinder = new ReverseDataBinder(dataBinder);
    String reversed = reverseDataBinder.reverseConvert(converted);
    assertThat(reversed, is(equalTo(value)));
  }
View Full Code Here


  }

  @Test
  public void shouldReverseBind() throws Exception {
    Sample target = new Sample();
    DataBinder dataBinder = new DataBinder(target);
    initBinder(dataBinder);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue("customTypeValue", "custom");
    pvs.addPropertyValue("dateValue", S01_12_2009);
    pvs.addPropertyValue("integerValue", "123");
    pvs.addPropertyValue("stringValue", "string");
    dataBinder.bind(pvs);

    assertThat(target.getIntegerValue(), is(equalTo(new Integer(123))));
    assertThat(target.getStringValue(), is(equalTo("string")));
    assertThat(target.getDateValue(), is(equalTo(D01_12_2009)));
    assertThat(target.getCustomTypeValue(), is(equalTo(new CustomType("custom"))));
View Full Code Here

  }

  @Test
  public void shouldFailReverseBindIfBindWillFail() throws Exception {
    Sample target = new Sample();
    DataBinder dataBinder = new DataBinder(target);
    dataBinder.setRequiredFields(new String[] { "integerValue" });
    ReverseDataBinder reverseDataBinder = new ReverseDataBinder(dataBinder);
    this.thrown.expect(IllegalStateException.class);
    this.thrown
        .expectMessage("Unable to reverse bind from target 'target', the properties 'PropertyValues: length=0' will result in binding errors "
            + "when re-bound [Field error in object 'target' on field 'integerValue': rejected value []; codes "
View Full Code Here

  }

  private void doTestReverseBindWithDefaultValues(boolean dontSkip, boolean noConstructor) throws Exception {
    Sample target = noConstructor ? new SampleWithoutDefaultConstructor("") : new Sample();
    target.setIntegerValue(new Integer(123));
    DataBinder dataBinder = new DataBinder(target);
    ReverseDataBinder reverseDataBinder = new ReverseDataBinder(dataBinder);
    if (dontSkip) {
      // Only set when skipped to test default is true
      reverseDataBinder.setSkipDefaultValues(false);
    }
View Full Code Here

    };
  }

  @SuppressWarnings("unchecked")
  private void bindAndValidate(Map<String, String> raw) throws BindException {
    DataBinder dataBinder = new DataBinder(beanWrapper.getWrappedInstance());
    dataBinder.setIgnoreUnknownFields(false);
    dataBinder.setConversionService(conversionService);
    MutablePropertySources mps = new MutablePropertySources();
    mps.addFirst(new MapPropertySource("options", (Map) raw));
    try {
      dataBinder.bind(new PropertySourcesPropertyValues(mps));
    }
    catch (InvalidPropertyException e) {
      dataBinder.getBindingResult().addError(new FieldError("options", e.getPropertyName(), e.getMessage()));
    }

    CustomValidatorBean validator = new CustomValidatorBean();
    validator.afterPropertiesSet();
    dataBinder.setValidator(validator);

    Class<?>[] groups = determineGroupsToUse(beanWrapper.getWrappedInstance());
    dataBinder.validate((Object[]) groups);

    if (dataBinder.getBindingResult().hasErrors()) {
      throw new BindException(dataBinder.getBindingResult());
    }
  }
View Full Code Here

   * @see org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet(FieldSet)
   */
    @Override
  public T mapFieldSet(FieldSet fs) throws BindException {
    T copy = getBean();
    DataBinder binder = createBinder(copy);
    binder.bind(new MutablePropertyValues(getBeanProperties(copy, fs.getProperties())));
    if (binder.getBindingResult().hasErrors()) {
      throw new BindException(binder.getBindingResult());
    }
    return copy;
  }
View Full Code Here

   * @param target
   * @return a {@link DataBinder} that can be used to bind properties to the
   * target.
   */
  protected DataBinder createBinder(Object target) {
    DataBinder binder = new DataBinder(target);
    binder.setIgnoreUnknownFields(!this.strict);
    initBinder(binder);
    registerCustomEditors(binder);
    return binder;
  }
View Full Code Here

  }

  @Test
  public void testPlaceholdersBinding() {
    TestBean target = new TestBean();
    DataBinder binder = new DataBinder(target);
    binder.bind(new PropertySourcesPropertyValues(this.propertySources));
    assertEquals("bar", target.getName());
  }
View Full Code Here

  }

  @Test
  public void testPlaceholdersBindingNonEnumerable() {
    FooBean target = new FooBean();
    DataBinder binder = new DataBinder(target);
    binder.bind(new PropertySourcesPropertyValues(this.propertySources,
        (Collection<String>) null, Collections.singleton("foo")));
    assertEquals("bar", target.getFoo());
  }
View Full Code Here

  }

  @Test
  public void testPlaceholdersBindingWithError() {
    TestBean target = new TestBean();
    DataBinder binder = new DataBinder(target);
    this.propertySources.addFirst(new MapPropertySource("another", Collections
        .<String, Object> singletonMap("something", "${nonexistent}")));
    binder.bind(new PropertySourcesPropertyValues(this.propertySources));
    assertEquals("bar", target.getName());
  }
View Full Code Here

TOP

Related Classes of org.springframework.validation.DataBinder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.