Search in sources :

Example 6 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class BeanInfoTests method testComplexObject.

@Test
public void testComplexObject() {
    ValueBean bean = new ValueBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    Integer value = new Integer(1);
    bw.setPropertyValue("value", value);
    assertEquals("value not set correctly", bean.getValue(), value);
    value = new Integer(2);
    bw.setPropertyValue("value", value.toString());
    assertEquals("value not converted", bean.getValue(), value);
    bw.setPropertyValue("value", null);
    assertNull("value not null", bean.getValue());
    bw.setPropertyValue("value", "");
    assertNull("value not converted to null", bean.getValue());
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.Test)

Example 7 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class CustomEditorTests method testIndexedPropertiesWithDirectAccessAndPropertyEditors.

@Test
public void testIndexedPropertiesWithDirectAccessAndPropertyEditors() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean("array" + text, 99));
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    bw.registerCustomEditor(TestBean.class, "list", new PropertyEditorSupport() {

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

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean("map" + text, 99));
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0]", "a");
    pvs.add("array[1]", "b");
    pvs.add("list[0]", "c");
    pvs.add("list[1]", "d");
    pvs.add("map[key1]", "e");
    pvs.add("map['key2']", "f");
    bw.setPropertyValues(pvs);
    assertEquals("arraya", bean.getArray()[0].getName());
    assertEquals("arrayb", bean.getArray()[1].getName());
    assertEquals("listc", ((TestBean) bean.getList().get(0)).getName());
    assertEquals("listd", ((TestBean) bean.getList().get(1)).getName());
    assertEquals("mape", ((TestBean) bean.getMap().get("key1")).getName());
    assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) 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) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 8 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class CustomEditorTests method testDefaultNumberEditor.

@Test
public void testDefaultNumberEditor() {
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("short1", "1");
    bw.setPropertyValue("short2", "2");
    bw.setPropertyValue("int1", "7");
    bw.setPropertyValue("int2", "8");
    bw.setPropertyValue("long1", "5");
    bw.setPropertyValue("long2", "6");
    bw.setPropertyValue("bigInteger", "3");
    bw.setPropertyValue("float1", "7.1");
    bw.setPropertyValue("float2", "8.1");
    bw.setPropertyValue("double1", "5.1");
    bw.setPropertyValue("double2", "6.1");
    bw.setPropertyValue("bigDecimal", "4.5");
    assertTrue("Correct short1 value", new Short("1").equals(bw.getPropertyValue("short1")));
    assertTrue("Correct short1 value", tb.getShort1() == 1);
    assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
    assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
    assertTrue("Correct int1 value", new Integer("7").equals(bw.getPropertyValue("int1")));
    assertTrue("Correct int1 value", tb.getInt1() == 7);
    assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
    assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
    assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
    assertTrue("Correct long1 value", tb.getLong1() == 5);
    assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
    assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
    assertTrue("Correct float1 value", new Float("7.1").equals(bw.getPropertyValue("float1")));
    assertTrue("Correct float1 value", new Float("7.1").equals(new Float(tb.getFloat1())));
    assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
    assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
    assertTrue("Correct double1 value", new Double("5.1").equals(bw.getPropertyValue("double1")));
    assertTrue("Correct double1 value", tb.getDouble1() == 5.1);
    assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
    assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal")));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal()));
}
Also used : BigInteger(java.math.BigInteger) BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 9 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class CustomEditorTests method testIndexedPropertiesWithCustomEditorForProperty.

@Test
public void testIndexedPropertiesWithCustomEditorForProperty() {
    IndexedTestBean bean = new IndexedTestBean(false);
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(String.class, "array.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("array" + text);
        }
    });
    bw.registerCustomEditor(String.class, "list.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("list" + text);
        }
    });
    bw.registerCustomEditor(String.class, "map.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("map" + text);
        }
    });
    bean.populate();
    TestBean tb0 = bean.getArray()[0];
    TestBean tb1 = bean.getArray()[1];
    TestBean tb2 = ((TestBean) bean.getList().get(0));
    TestBean tb3 = ((TestBean) bean.getList().get(1));
    TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
    TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
    assertEquals("name0", tb0.getName());
    assertEquals("name1", tb1.getName());
    assertEquals("name2", tb2.getName());
    assertEquals("name3", tb3.getName());
    assertEquals("name4", tb4.getName());
    assertEquals("name5", tb5.getName());
    assertEquals("name0", bw.getPropertyValue("array[0].name"));
    assertEquals("name1", bw.getPropertyValue("array[1].name"));
    assertEquals("name2", bw.getPropertyValue("list[0].name"));
    assertEquals("name3", bw.getPropertyValue("list[1].name"));
    assertEquals("name4", bw.getPropertyValue("map[key1].name"));
    assertEquals("name5", bw.getPropertyValue("map[key2].name"));
    assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
    assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name"));
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0].name", "name5");
    pvs.add("array[1].name", "name4");
    pvs.add("list[0].name", "name3");
    pvs.add("list[1].name", "name2");
    pvs.add("map[key1].name", "name1");
    pvs.add("map['key2'].name", "name0");
    bw.setPropertyValues(pvs);
    assertEquals("arrayname5", tb0.getName());
    assertEquals("arrayname4", tb1.getName());
    assertEquals("listname3", tb2.getName());
    assertEquals("listname2", tb3.getName());
    assertEquals("mapname1", tb4.getName());
    assertEquals("mapname0", tb5.getName());
    assertEquals("arrayname5", bw.getPropertyValue("array[0].name"));
    assertEquals("arrayname4", bw.getPropertyValue("array[1].name"));
    assertEquals("listname3", bw.getPropertyValue("list[0].name"));
    assertEquals("listname2", bw.getPropertyValue("list[1].name"));
    assertEquals("mapname1", bw.getPropertyValue("map[\"key1\"].name"));
    assertEquals("mapname0", bw.getPropertyValue("map['key2'].name"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) 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) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 10 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class CustomEditorTests method testCharacterEditor.

@Test
public void testCharacterEditor() {
    CharBean cb = new CharBean();
    BeanWrapper bw = new BeanWrapperImpl(cb);
    bw.setPropertyValue("myChar", new Character('c'));
    assertEquals('c', cb.getMyChar());
    bw.setPropertyValue("myChar", "c");
    assertEquals('c', cb.getMyChar());
    bw.setPropertyValue("myChar", "A");
    assertEquals('A', cb.getMyChar());
    bw.setPropertyValue("myChar", "\\u0022");
    assertEquals('"', cb.getMyChar());
    CharacterEditor editor = new CharacterEditor(false);
    editor.setAsText("M");
    assertEquals("M", editor.getAsText());
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.Test)

Aggregations

BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)85 BeanWrapper (org.springframework.beans.BeanWrapper)57 Test (org.junit.Test)47 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)21 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)20 ITestBean (org.springframework.tests.sample.beans.ITestBean)17 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)17 TestBean (org.springframework.tests.sample.beans.TestBean)17 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)11 PropertyDescriptor (java.beans.PropertyDescriptor)7 BeansException (org.springframework.beans.BeansException)7 ArrayList (java.util.ArrayList)5 BeanCreationException (org.springframework.beans.factory.BeanCreationException)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)4 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 PrivilegedAction (java.security.PrivilegedAction)4 Map (java.util.Map)4