Search in sources :

Example 61 with BeanWrapper

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

the class CustomEditorTests method testArrayToStringConversion.

@Test
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" });
    assertThat(tb.getName()).isEqualTo("-a,b-");
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 62 with BeanWrapper

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

the class CustomEditorTests method testArrayToArrayConversion.

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

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean(text, 99));
        }
    });
    bw.setPropertyValue("array", new String[] { "a", "b" });
    assertThat(tb.getArray().length).isEqualTo(2);
    assertThat(tb.getArray()[0].getName()).isEqualTo("a");
    assertThat(tb.getArray()[1].getName()).isEqualTo("b");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 63 with BeanWrapper

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

the class CustomEditorTests method testIndexedPropertiesWithCustomEditorForProperty.

@Test
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"));
    assertThat(tb0.getName()).isEqualTo("name0");
    assertThat(tb1.getName()).isEqualTo("name1");
    assertThat(tb2.getName()).isEqualTo("name2");
    assertThat(tb3.getName()).isEqualTo("name3");
    assertThat(tb4.getName()).isEqualTo("name4");
    assertThat(tb5.getName()).isEqualTo("name5");
    assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("name0");
    assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("name1");
    assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("name2");
    assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("name3");
    assertThat(bw.getPropertyValue("map[key1].name")).isEqualTo("name4");
    assertThat(bw.getPropertyValue("map[key2].name")).isEqualTo("name5");
    assertThat(bw.getPropertyValue("map['key1'].name")).isEqualTo("name4");
    assertThat(bw.getPropertyValue("map[\"key2\"].name")).isEqualTo("name5");
    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);
    assertThat(tb0.getName()).isEqualTo("arrayname5");
    assertThat(tb1.getName()).isEqualTo("arrayname4");
    assertThat(tb2.getName()).isEqualTo("listname3");
    assertThat(tb3.getName()).isEqualTo("listname2");
    assertThat(tb4.getName()).isEqualTo("mapname1");
    assertThat(tb5.getName()).isEqualTo("mapname0");
    assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("arrayname5");
    assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("arrayname4");
    assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("listname3");
    assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("listname2");
    assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("mapname1");
    assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("mapname0");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 64 with BeanWrapper

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

the class CustomEditorTests method testCharacterEditor.

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

Example 65 with BeanWrapper

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

the class CustomEditorTests method testCharacterEditorWithAllowEmpty.

@Test
void testCharacterEditorWithAllowEmpty() {
    CharBean cb = new CharBean();
    BeanWrapper bw = new BeanWrapperImpl(cb);
    bw.registerCustomEditor(Character.class, new CharacterEditor(true));
    bw.setPropertyValue("myCharacter", 'c');
    assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf('c'));
    bw.setPropertyValue("myCharacter", "c");
    assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf('c'));
    bw.setPropertyValue("myCharacter", "\u0041");
    assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf('A'));
    bw.setPropertyValue("myCharacter", " ");
    assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf(' '));
    bw.setPropertyValue("myCharacter", "");
    assertThat(cb.getMyCharacter()).isNull();
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Aggregations

BeanWrapper (org.springframework.beans.BeanWrapper)144 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)79 Test (org.junit.jupiter.api.Test)31 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)18 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Consumes (javax.ws.rs.Consumes)16 PUT (javax.ws.rs.PUT)16 Path (javax.ws.rs.Path)16 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 HashSet (java.util.HashSet)12 BeansException (org.springframework.beans.BeansException)12 PropertyDescriptor (java.beans.PropertyDescriptor)11 OnmsNode (org.opennms.netmgt.model.OnmsNode)9 Test (org.junit.Test)6 BigDecimal (java.math.BigDecimal)5