Search in sources :

Example 36 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.

the class DataBinderTests method testBindingWithNestedObjectCreation.

@Test
public void testBindingWithNestedObjectCreation() throws Exception {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb, "person");
    binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean());
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("spouse", "someValue");
    pvs.add("spouse.name", "test");
    binder.bind(pvs);
    assertNotNull(tb.getSpouse());
    assertEquals("test", tb.getSpouse().getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 37 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.

the class DataBinderTests method testCustomEditorWithOldValueAccess.

@Test
public void testCustomEditorWithOldValueAccess() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(String.class, null, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (getValue() == null || !text.equalsIgnoreCase(getValue().toString())) {
                setValue(text);
            }
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "value");
    binder.bind(pvs);
    assertEquals("value", tb.getName());
    pvs = new MutablePropertyValues();
    pvs.add("name", "vaLue");
    binder.bind(pvs);
    assertEquals("value", tb.getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 38 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method setPrimitiveArrayPropertyLargeMatchingWithIndexSpecificEditor.

@Test
public void setPrimitiveArrayPropertyLargeMatchingWithIndexSpecificEditor() {
    PrimitiveArrayBean target = new PrimitiveArrayBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(int.class, "array[1]", new PropertyEditorSupport() {

        @Override
        public void setValue(Object value) {
            if (value instanceof Integer) {
                super.setValue(new Integer((Integer) value + 1));
            }
        }
    });
    int[] input = new int[1024];
    accessor.setPropertyValue("array", input);
    assertEquals(1024, target.getArray().length);
    assertEquals(0, target.getArray()[0]);
    assertEquals(1, target.getArray()[1]);
}
Also used : BigInteger(java.math.BigInteger) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 39 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method setStringPropertyWithCustomEditor.

@Test
public void setStringPropertyWithCustomEditor() throws Exception {
    TestBean target = new TestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {

        @Override
        public void setValue(Object value) {
            if (value instanceof String[]) {
                setValue(StringUtils.arrayToDelimitedString(((String[]) value), "-"));
            } else {
                super.setValue(value != null ? value : "");
            }
        }
    });
    accessor.setPropertyValue("name", new String[] {});
    assertEquals("", target.getName());
    accessor.setPropertyValue("name", new String[] { "a1", "b2" });
    assertEquals("a1-b2", target.getName());
    accessor.setPropertyValue("name", null);
    assertEquals("", target.getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 40 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method setPrimitiveArrayPropertyLargeMatchingWithSpecificEditor.

@Test
public void setPrimitiveArrayPropertyLargeMatchingWithSpecificEditor() {
    PrimitiveArrayBean target = new PrimitiveArrayBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(int.class, "array", new PropertyEditorSupport() {

        @Override
        public void setValue(Object value) {
            if (value instanceof Integer) {
                super.setValue(new Integer((Integer) value + 1));
            }
        }
    });
    int[] input = new int[1024];
    accessor.setPropertyValue("array", input);
    assertEquals(1024, target.getArray().length);
    assertEquals(1, target.getArray()[0]);
    assertEquals(1, target.getArray()[1]);
}
Also used : BigInteger(java.math.BigInteger) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Aggregations

PropertyEditorSupport (java.beans.PropertyEditorSupport)56 Test (org.junit.Test)55 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)37 TestBean (org.springframework.tests.sample.beans.TestBean)31 ITestBean (org.springframework.tests.sample.beans.ITestBean)30 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)23 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)19 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)19 BeanWrapper (org.springframework.beans.BeanWrapper)14 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)14 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)12 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)10 StringReader (java.io.StringReader)5 BigInteger (java.math.BigInteger)5 ArrayList (java.util.ArrayList)5 Document (org.dom4j.Document)5 Element (org.dom4j.Element)5 SAXReader (org.dom4j.io.SAXReader)5 HashMap (java.util.HashMap)4 List (java.util.List)4