Search in sources :

Example 16 with PropertyEditorSupport

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

the class DataBinderTests method testCustomEditorForSingleProperty.

@Test
public void testCustomEditorForSingleProperty() {
    TestBean tb = new TestBean();
    tb.setSpouse(new TestBean());
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(String.class, "name", 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");
    pvs.add("spouse.name", "sue");
    binder.bind(pvs);
    binder.getBindingResult().rejectValue("name", "someCode", "someMessage");
    binder.getBindingResult().rejectValue("touchy", "someCode", "someMessage");
    binder.getBindingResult().rejectValue("spouse.name", "someCode", "someMessage");
    assertEquals("", binder.getBindingResult().getNestedPath());
    assertEquals("value", binder.getBindingResult().getFieldValue("name"));
    assertEquals("prefixvalue", binder.getBindingResult().getFieldError("name").getRejectedValue());
    assertEquals("prefixvalue", tb.getName());
    assertEquals("value", binder.getBindingResult().getFieldValue("touchy"));
    assertEquals("value", binder.getBindingResult().getFieldError("touchy").getRejectedValue());
    assertEquals("value", tb.getTouchy());
    assertTrue(binder.getBindingResult().hasFieldErrors("spouse.*"));
    assertEquals(1, binder.getBindingResult().getFieldErrorCount("spouse.*"));
    assertEquals("spouse.name", binder.getBindingResult().getFieldError("spouse.*").getField());
}
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 17 with PropertyEditorSupport

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

the class DataBinderTests method testDirectBindingToEmptyIndexedFieldWithRegisteredSpecificEditor.

@Test
public void testDirectBindingToEmptyIndexedFieldWithRegisteredSpecificEditor() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, "map[key0]", 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();
        }
    });
    Errors errors = binder.getBindingResult();
    errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
    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]);
    // This next code is only generated because of the registered editor, using the
    // registered type of the editor as guess for the content type of the collection.
    assertEquals("NOT_NULL.org.springframework.tests.sample.beans.TestBean", errors.getFieldError("map[key0]").getCodes()[4]);
    assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCodes()[5]);
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 18 with PropertyEditorSupport

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

the class DataBinderTests method testBindingToIndexedField.

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

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

Example 19 with PropertyEditorSupport

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

the class DataBinderTests method testInnerSpecificEditorForNestedIndexedField.

@Test
public void testInnerSpecificEditorForNestedIndexedField() {
    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[0].name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("list" + text);
        }

        @Override
        public String getAsText() {
            return ((String) getValue()).substring(4);
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
    pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
    binder.bind(pvs);
    assertEquals("listtest1", ((TestBean) tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
    assertEquals("test2", ((TestBean) tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
    assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
    assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 20 with PropertyEditorSupport

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

the class DataBinderTests method testBindingWithErrorsAndCustomEditors.

@Test
public void testBindingWithErrorsAndCustomEditors() throws Exception {
    TestBean rod = new TestBean();
    DataBinder binder = new DataBinder(rod, "person");
    binder.registerCustomEditor(String.class, "touchy", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("prefix_" + text);
        }

        @Override
        public String getAsText() {
            return getValue().toString().substring(7);
        }
    });
    binder.registerCustomEditor(TestBean.class, "spouse", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean(text, 0));
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "Rod");
    pvs.add("age", "32x");
    pvs.add("touchy", "m.y");
    pvs.add("spouse", "Kerry");
    binder.bind(pvs);
    try {
        binder.close();
        fail("Should have thrown BindException");
    } catch (BindException ex) {
        assertTrue("changed name correctly", rod.getName().equals("Rod"));
        //assertTrue("changed age correctly", rod.getAge() == 32);
        Map<?, ?> model = binder.getBindingResult().getModel();
        //assertTrue("There are 3 element in map", m.size() == 1);
        TestBean tb = (TestBean) model.get("person");
        assertTrue("Same object", tb.equals(rod));
        BindingResult br = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "person");
        assertTrue("Added itself to map", br == binder.getBindingResult());
        assertTrue(br.hasErrors());
        assertTrue("Correct number of errors", br.getErrorCount() == 2);
        assertTrue("Has age errors", br.hasFieldErrors("age"));
        assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
        assertEquals("typeMismatch", binder.getBindingResult().getFieldError("age").getCode());
        assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
        assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
        assertEquals(0, tb.getAge());
        assertTrue("Has touchy errors", br.hasFieldErrors("touchy"));
        assertTrue("Correct number of touchy errors", br.getFieldErrorCount("touchy") == 1);
        assertEquals("methodInvocation", binder.getBindingResult().getFieldError("touchy").getCode());
        assertEquals("m.y", binder.getBindingResult().getFieldValue("touchy"));
        assertEquals("m.y", binder.getBindingResult().getFieldError("touchy").getRejectedValue());
        assertNull(tb.getTouchy());
        assertTrue("Does not have spouse errors", !br.hasFieldErrors("spouse"));
        assertEquals("Kerry", binder.getBindingResult().getFieldValue("spouse"));
        assertNotNull(tb.getSpouse());
    }
}
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) HashMap(java.util.HashMap) Map(java.util.Map) 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