Search in sources :

Example 31 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)

Example 32 with BeanWrapperImpl

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

the class CustomEditorTests method testCustomBooleanEditorWithAllowEmpty.

@Test
public void testCustomBooleanEditorWithAllowEmpty() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
    bw.setPropertyValue("bool2", "true");
    assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "false");
    assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "on");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "off");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "yes");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "no");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "1");
    assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "0");
    assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
    bw.setPropertyValue("bool2", "");
    assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") == null);
    assertTrue("Correct bool2 value", tb.getBool2() == null);
}
Also used : BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.Test)

Example 33 with BeanWrapperImpl

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

the class CustomEditorTests method testCustomNumberEditorWithAllowEmpty.

@Test
public void testCustomNumberEditorWithAllowEmpty() {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, true));
    bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true));
    bw.setPropertyValue("long1", "5");
    bw.setPropertyValue("long2", "6");
    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()));
    bw.setPropertyValue("long2", "");
    assertTrue("Correct long2 value", bw.getPropertyValue("long2") == null);
    assertTrue("Correct long2 value", tb.getLong2() == null);
    try {
        bw.setPropertyValue("long1", "");
        fail("Should have thrown BeansException");
    } catch (BeansException ex) {
        // expected
        assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
        assertTrue("Correct long1 value", tb.getLong1() == 5);
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) NumberFormat(java.text.NumberFormat) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Example 34 with BeanWrapperImpl

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

the class CustomEditorTests method testNestedIndexedPropertiesWithCustomEditorForProperty.

@Test
public void testNestedIndexedPropertiesWithCustomEditorForProperty() {
    IndexedTestBean bean = new IndexedTestBean();
    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"));
    tb0.setNestedIndexedBean(new IndexedTestBean());
    tb1.setNestedIndexedBean(new IndexedTestBean());
    tb2.setNestedIndexedBean(new IndexedTestBean());
    tb3.setNestedIndexedBean(new IndexedTestBean());
    tb4.setNestedIndexedBean(new IndexedTestBean());
    tb5.setNestedIndexedBean(new IndexedTestBean());
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(String.class, "array.nestedIndexedBean.array.name", new PropertyEditorSupport() {

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

        @Override
        public String getAsText() {
            return ((String) getValue()).substring(5);
        }
    });
    bw.registerCustomEditor(String.class, "list.nestedIndexedBean.list.name", new PropertyEditorSupport() {

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

        @Override
        public String getAsText() {
            return ((String) getValue()).substring(4);
        }
    });
    bw.registerCustomEditor(String.class, "map.nestedIndexedBean.map.name", new PropertyEditorSupport() {

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

        @Override
        public String getAsText() {
            return ((String) getValue()).substring(4);
        }
    });
    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].nestedIndexedBean.array[0].name"));
    assertEquals("name1", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
    assertEquals("name2", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
    assertEquals("name3", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
    assertEquals("name4", bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name"));
    assertEquals("name5", bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name"));
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0].nestedIndexedBean.array[0].name", "name5");
    pvs.add("array[1].nestedIndexedBean.array[1].name", "name4");
    pvs.add("list[0].nestedIndexedBean.list[0].name", "name3");
    pvs.add("list[1].nestedIndexedBean.list[1].name", "name2");
    pvs.add("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
    pvs.add("map['key2'].nestedIndexedBean.map[key2].name", "name0");
    bw.setPropertyValues(pvs);
    assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName());
    assertEquals("arrayname4", tb1.getNestedIndexedBean().getArray()[1].getName());
    assertEquals("listname3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName());
    assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName());
    assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName());
    assertEquals("mapname0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
    assertEquals("arrayname5", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name"));
    assertEquals("arrayname4", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
    assertEquals("listname3", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
    assertEquals("listname2", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
    assertEquals("mapname1", bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name"));
    assertEquals("mapname0", bw.getPropertyValue("map[key2].nestedIndexedBean.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 35 with BeanWrapperImpl

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

the class CustomEditorTests method testUninitializedArrayPropertyWithCustomEditor.

@Test
public void testUninitializedArrayPropertyWithCustomEditor() {
    IndexedTestBean bean = new IndexedTestBean(false);
    BeanWrapper bw = new BeanWrapperImpl(bean);
    PropertyEditor pe = new CustomNumberEditor(Integer.class, true);
    bw.registerCustomEditor(null, "list.age", pe);
    TestBean tb = new TestBean();
    bw.setPropertyValue("list", new ArrayList<>());
    bw.setPropertyValue("list[0]", tb);
    assertEquals(tb, bean.getList().get(0));
    assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
    assertEquals(pe, bw.findCustomEditor(null, "list.age"));
    assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age"));
    assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
}
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) PropertyEditor(java.beans.PropertyEditor) 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