Search in sources :

Example 31 with PropertyEditorSupport

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

the class DataBinderTests method testCustomEditorForPrimitiveProperty.

@Test
public void testCustomEditorForPrimitiveProperty() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(int.class, "age", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new Integer(99));
        }

        @Override
        public String getAsText() {
            return "argh";
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("age", "");
    binder.bind(pvs);
    assertEquals("argh", binder.getBindingResult().getFieldValue("age"));
    assertEquals(99, tb.getAge());
}
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 32 with PropertyEditorSupport

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

the class DataBinderTests method testDirectBindingToIndexedField.

@Test
public void testDirectBindingToIndexedField() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            DerivedTestBean tb = new DerivedTestBean();
            tb.setName("array" + text);
            setValue(tb);
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0]", "a");
    binder.bind(pvs);
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0]", "NOT_ROD", "are you sure you're not Rod?");
    errors.rejectValue("map[key1]", "NOT_ROD", "are you sure you're not Rod?");
    errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
    assertEquals("arraya", errors.getFieldValue("array[0]"));
    assertEquals(1, errors.getFieldErrorCount("array[0]"));
    assertEquals("NOT_ROD", errors.getFieldError("array[0]").getCode());
    assertEquals("NOT_ROD.tb.array[0]", errors.getFieldError("array[0]").getCodes()[0]);
    assertEquals("NOT_ROD.tb.array", errors.getFieldError("array[0]").getCodes()[1]);
    assertEquals("NOT_ROD.array[0]", errors.getFieldError("array[0]").getCodes()[2]);
    assertEquals("NOT_ROD.array", errors.getFieldError("array[0]").getCodes()[3]);
    assertEquals("NOT_ROD.org.springframework.tests.sample.beans.DerivedTestBean", errors.getFieldError("array[0]").getCodes()[4]);
    assertEquals("NOT_ROD", errors.getFieldError("array[0]").getCodes()[5]);
    assertEquals("arraya", errors.getFieldValue("array[0]"));
    assertEquals(1, errors.getFieldErrorCount("map[key1]"));
    assertEquals("NOT_ROD", errors.getFieldError("map[key1]").getCode());
    assertEquals("NOT_ROD.tb.map[key1]", errors.getFieldError("map[key1]").getCodes()[0]);
    assertEquals("NOT_ROD.tb.map", errors.getFieldError("map[key1]").getCodes()[1]);
    assertEquals("NOT_ROD.map[key1]", errors.getFieldError("map[key1]").getCodes()[2]);
    assertEquals("NOT_ROD.map", errors.getFieldError("map[key1]").getCodes()[3]);
    assertEquals("NOT_ROD.org.springframework.tests.sample.beans.TestBean", errors.getFieldError("map[key1]").getCodes()[4]);
    assertEquals("NOT_ROD", errors.getFieldError("map[key1]").getCodes()[5]);
    assertEquals(1, errors.getFieldErrorCount("map[key0]"));
    assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCode());
    assertEquals("NOT_NULL.tb.map[key0]", errors.getFieldError("map[key0]").getCodes()[0]);
    assertEquals("NOT_NULL.tb.map", errors.getFieldError("map[key0]").getCodes()[1]);
    assertEquals("NOT_NULL.map[key0]", errors.getFieldError("map[key0]").getCodes()[2]);
    assertEquals("NOT_NULL.map", errors.getFieldError("map[key0]").getCodes()[3]);
    assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCodes()[4]);
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 33 with PropertyEditorSupport

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

the class DataBinderTests method testCustomEditorForAllStringProperties.

@Test
public void testCustomEditorForAllStringProperties() {
    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 {
            setValue("prefix" + text);
        }

        @Override
        public String getAsText() {
            return ((String) getValue()).substring(6);
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "value");
    pvs.add("touchy", "value");
    binder.bind(pvs);
    binder.getBindingResult().rejectValue("name", "someCode", "someMessage");
    binder.getBindingResult().rejectValue("touchy", "someCode", "someMessage");
    assertEquals("value", binder.getBindingResult().getFieldValue("name"));
    assertEquals("prefixvalue", binder.getBindingResult().getFieldError("name").getRejectedValue());
    assertEquals("prefixvalue", tb.getName());
    assertEquals("value", binder.getBindingResult().getFieldValue("touchy"));
    assertEquals("prefixvalue", binder.getBindingResult().getFieldError("touchy").getRejectedValue());
    assertEquals("prefixvalue", tb.getTouchy());
}
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 34 with PropertyEditorSupport

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

the class DataBinderTests method testBindingToNestedIndexedField.

@Test
public void testBindingToNestedIndexedField() {
    IndexedTestBean tb = new IndexedTestBean();
    tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
    tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("list" + text);
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0].nestedIndexedBean.list[0].name", "a");
    binder.bind(pvs);
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0].nestedIndexedBean.list[0].name", "NOT_ROD", "are you sure you're not Rod?");
    assertEquals(1, errors.getFieldErrorCount("array[0].nestedIndexedBean.list[0].name"));
    assertEquals("NOT_ROD", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCode());
    assertEquals("NOT_ROD.tb.array[0].nestedIndexedBean.list[0].name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[0]);
    assertEquals("NOT_ROD.tb.array[0].nestedIndexedBean.list.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[1]);
    assertEquals("NOT_ROD.tb.array.nestedIndexedBean.list.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[2]);
    assertEquals("NOT_ROD.array[0].nestedIndexedBean.list[0].name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[3]);
    assertEquals("NOT_ROD.array[0].nestedIndexedBean.list.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[4]);
    assertEquals("NOT_ROD.array.nestedIndexedBean.list.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[5]);
    assertEquals("NOT_ROD.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[6]);
    assertEquals("NOT_ROD.java.lang.String", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[7]);
    assertEquals("NOT_ROD", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[8]);
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 35 with PropertyEditorSupport

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

the class DataBinderTests method testBindToStringArrayWithComponentEditor.

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

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("X" + text);
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("stringArray", new String[] { "a1", "b2" });
    binder.bind(pvs);
    assertTrue(!binder.getBindingResult().hasErrors());
    assertEquals(2, tb.getStringArray().length);
    assertEquals("Xa1", tb.getStringArray()[0]);
    assertEquals("Xb2", tb.getStringArray()[1]);
}
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)

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