Search in sources :

Example 6 with PropertyEditorSupport

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

the class CustomEditorTests method testArrayToStringConversion.

@Test
public void testArrayToStringConversion() throws PropertyVetoException {
    TestBean tb = new TestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("-" + text + "-");
        }
    });
    bw.setPropertyValue("name", new String[] { "a", "b" });
    assertEquals("-a,b-", tb.getName());
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) 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 7 with PropertyEditorSupport

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

the class AbstractPropertyAccessorTests method setMapPropertyWithUnmodifiableMap.

@Test
public void setMapPropertyWithUnmodifiableMap() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });
    Map<Integer, String> inputMap = new HashMap<>();
    inputMap.put(1, "rod");
    inputMap.put(2, "rob");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", Collections.unmodifiableMap(inputMap));
    accessor.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) target.getMap().get(1)).getName());
    assertEquals("rob", ((TestBean) target.getMap().get(2)).getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BigInteger(java.math.BigInteger) 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) HashMap(java.util.HashMap) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 8 with PropertyEditorSupport

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

the class AbstractPropertyAccessorTests method setPropertyWithCustomEditor.

@Test
public void setPropertyWithCustomEditor() {
    MutablePropertyValues values = new MutablePropertyValues();
    values.add("name", Integer.class);
    TestBean target = new TestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(String.class, new PropertyEditorSupport() {

        @Override
        public void setValue(Object value) {
            super.setValue(value.toString());
        }
    });
    accessor.setPropertyValues(values);
    assertEquals(Integer.class.toString(), target.getName());
}
Also used : BigInteger(java.math.BigInteger) 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 9 with PropertyEditorSupport

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

the class AbstractPropertyAccessorTests method setStringArrayPropertyWithCustomStringEditor.

@Test
public void setStringArrayPropertyWithCustomStringEditor() throws Exception {
    PropsTester target = new PropsTester();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) {
            setValue(text.substring(1));
        }
    });
    accessor.setPropertyValue("stringArray", new String[] { "4foo", "7fi", "6fi", "5fum" });
    assertTrue("stringArray length = 4", target.stringArray.length == 4);
    assertTrue("correct values", target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum"));
    List<String> list = new ArrayList<>();
    list.add("4foo");
    list.add("7fi");
    list.add("6fi");
    list.add("5fum");
    accessor.setPropertyValue("stringArray", list);
    assertTrue("stringArray length = 4", target.stringArray.length == 4);
    assertTrue("correct values", target.stringArray[0].equals("foo") && target.stringArray[1].equals("fi") && target.stringArray[2].equals("fi") && target.stringArray[3].equals("fum"));
    Set<String> set = new HashSet<>();
    set.add("4foo");
    set.add("7fi");
    set.add("6fum");
    accessor.setPropertyValue("stringArray", set);
    assertTrue("stringArray length = 3", target.stringArray.length == 3);
    List<String> result = Arrays.asList(target.stringArray);
    assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum"));
    accessor.setPropertyValue("stringArray", "8one");
    assertTrue("stringArray length = 1", target.stringArray.length == 1);
    assertTrue("correct values", target.stringArray[0].equals("one"));
}
Also used : ArrayList(java.util.ArrayList) PropertyEditorSupport(java.beans.PropertyEditorSupport) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with PropertyEditorSupport

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

the class AbstractPropertyAccessorTests method setIntArrayPropertyWithCustomEditor.

@Test
public void setIntArrayPropertyWithCustomEditor() {
    PropsTester target = new PropsTester();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(int.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) {
            setValue(new Integer(Integer.parseInt(text) + 1));
        }
    });
    accessor.setPropertyValue("intArray", new int[] { 4, 5, 2, 3 });
    assertTrue("intArray length = 4", target.intArray.length == 4);
    assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && target.intArray[2] == 2 && target.intArray[3] == 3);
    accessor.setPropertyValue("intArray", new String[] { "3", "4", "1", "2" });
    assertTrue("intArray length = 4", target.intArray.length == 4);
    assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && target.intArray[2] == 2 && target.intArray[3] == 3);
    accessor.setPropertyValue("intArray", new Integer(1));
    assertTrue("intArray length = 4", target.intArray.length == 1);
    assertTrue("correct values", target.intArray[0] == 1);
    accessor.setPropertyValue("intArray", new String[] { "0" });
    assertTrue("intArray length = 4", target.intArray.length == 1);
    assertTrue("correct values", target.intArray[0] == 1);
    accessor.setPropertyValue("intArray", "0");
    assertTrue("intArray length = 4", target.intArray.length == 1);
    assertTrue("correct values", target.intArray[0] == 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