Search in sources :

Example 96 with BeanWrapperImpl

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

the class CustomEditorTests method testIndexedPropertiesWithIndividualCustomEditorForProperty.

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

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

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

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

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

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

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("mapkey2" + 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("array0name5");
    assertThat(tb1.getName()).isEqualTo("array1name4");
    assertThat(tb2.getName()).isEqualTo("list0name3");
    assertThat(tb3.getName()).isEqualTo("list1name2");
    assertThat(tb4.getName()).isEqualTo("mapkey1name1");
    assertThat(tb5.getName()).isEqualTo("mapkey2name0");
    assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("array0name5");
    assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("array1name4");
    assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("list0name3");
    assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("list1name2");
    assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("mapkey1name1");
    assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("mapkey2name0");
}
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 97 with BeanWrapperImpl

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

the class CustomEditorTests method testComplexObject.

@Test
void testComplexObject() {
    TestBean tb = new TestBean();
    String newName = "Rod";
    String tbString = "Kerry_34";
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("age", 55));
    pvs.addPropertyValue(new PropertyValue("name", newName));
    pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
    pvs.addPropertyValue(new PropertyValue("spouse", tbString));
    bw.setPropertyValues(pvs);
    assertThat(tb.getSpouse()).as("spouse is non-null").isNotNull();
    assertThat(tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34).as("spouse name is Kerry and age is 34").isTrue();
}
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) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValue(org.springframework.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Example 98 with BeanWrapperImpl

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

the class CustomEditorTests method testCustomEditorForSingleProperty.

@Test
void testCustomEditorForSingleProperty() {
    TestBean tb = new TestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("prefix" + text);
        }
    });
    bw.setPropertyValue("name", "value");
    bw.setPropertyValue("touchy", "value");
    assertThat(bw.getPropertyValue("name")).isEqualTo("prefixvalue");
    assertThat(tb.getName()).isEqualTo("prefixvalue");
    assertThat(bw.getPropertyValue("touchy")).isEqualTo("value");
    assertThat(tb.getTouchy()).isEqualTo("value");
}
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 99 with BeanWrapperImpl

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

the class CustomEditorTests method testCustomNumberEditorWithFrenchBigDecimal.

@Test
void testCustomNumberEditorWithFrenchBigDecimal() throws Exception {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, true));
    bw.setPropertyValue("bigDecimal", "1000");
    assertThat(tb.getBigDecimal().floatValue()).isCloseTo(1000.0f, within(0f));
    bw.setPropertyValue("bigDecimal", "1000,5");
    assertThat(tb.getBigDecimal().floatValue()).isCloseTo(1000.5f, within(0f));
    bw.setPropertyValue("bigDecimal", "1 000,5");
    assertThat(tb.getBigDecimal().floatValue()).isCloseTo(1000.5f, within(0f));
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BigDecimal(java.math.BigDecimal) NumberFormat(java.text.NumberFormat) Test(org.junit.jupiter.api.Test)

Example 100 with BeanWrapperImpl

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

the class CustomEditorTests method testCustomNumberEditorWithoutAllowEmpty.

@Test
void testCustomNumberEditorWithoutAllowEmpty() {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(short.class, new CustomNumberEditor(Short.class, nf, false));
    bw.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, nf, false));
    bw.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, nf, false));
    bw.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, nf, false));
    bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, false));
    bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, false));
    bw.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, nf, false));
    bw.registerCustomEditor(float.class, new CustomNumberEditor(Float.class, nf, false));
    bw.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, false));
    bw.registerCustomEditor(double.class, new CustomNumberEditor(Double.class, nf, false));
    bw.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, nf, false));
    bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, false));
    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");
    assertThat(bw.getPropertyValue("short1")).as("Correct short1 value").isEqualTo(Short.valueOf("1"));
    assertThat(tb.getShort1() == 1).as("Correct short1 value").isTrue();
    assertThat(bw.getPropertyValue("short2")).as("Correct short2 value").isEqualTo(Short.valueOf("2"));
    assertThat(tb.getShort2()).as("Correct short2 value").isEqualTo(Short.valueOf("2"));
    assertThat(bw.getPropertyValue("int1")).as("Correct int1 value").isEqualTo(Integer.valueOf("7"));
    assertThat(tb.getInt1() == 7).as("Correct int1 value").isTrue();
    assertThat(bw.getPropertyValue("int2")).as("Correct int2 value").isEqualTo(Integer.valueOf("8"));
    assertThat(tb.getInt2()).as("Correct int2 value").isEqualTo(Integer.valueOf("8"));
    assertThat(bw.getPropertyValue("long1")).as("Correct long1 value").isEqualTo(Long.valueOf("5"));
    assertThat(tb.getLong1() == 5).as("Correct long1 value").isTrue();
    assertThat(bw.getPropertyValue("long2")).as("Correct long2 value").isEqualTo(Long.valueOf("6"));
    assertThat(tb.getLong2()).as("Correct long2 value").isEqualTo(Long.valueOf("6"));
    assertThat(new BigInteger("3").equals(bw.getPropertyValue("bigInteger"))).as("Correct bigInteger value").isTrue();
    assertThat(new BigInteger("3").equals(tb.getBigInteger())).as("Correct bigInteger value").isTrue();
    assertThat(bw.getPropertyValue("float1")).as("Correct float1 value").isEqualTo(Float.valueOf("7.1"));
    assertThat(Float.valueOf(tb.getFloat1())).as("Correct float1 value").isEqualTo(Float.valueOf("7.1"));
    assertThat(bw.getPropertyValue("float2")).as("Correct float2 value").isEqualTo(Float.valueOf("8.1"));
    assertThat(tb.getFloat2()).as("Correct float2 value").isEqualTo(Float.valueOf("8.1"));
    assertThat(bw.getPropertyValue("double1")).as("Correct double1 value").isEqualTo(Double.valueOf("5.1"));
    assertThat(tb.getDouble1() == 5.1).as("Correct double1 value").isTrue();
    assertThat(bw.getPropertyValue("double2")).as("Correct double2 value").isEqualTo(Double.valueOf("6.1"));
    assertThat(tb.getDouble2()).as("Correct double2 value").isEqualTo(Double.valueOf("6.1"));
    assertThat(new BigDecimal("4.5").equals(bw.getPropertyValue("bigDecimal"))).as("Correct bigDecimal value").isTrue();
    assertThat(new BigDecimal("4.5").equals(tb.getBigDecimal())).as("Correct bigDecimal value").isTrue();
}
Also used : BigInteger(java.math.BigInteger) BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) NumberFormat(java.text.NumberFormat) Test(org.junit.jupiter.api.Test)

Aggregations

BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)113 BeanWrapper (org.springframework.beans.BeanWrapper)75 Test (org.junit.jupiter.api.Test)32 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 Test (org.junit.Test)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)17 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)17 TestBean (org.springframework.beans.testfixture.beans.TestBean)17 PropertyDescriptor (java.beans.PropertyDescriptor)14 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)12 HashSet (java.util.HashSet)11 BeansException (org.springframework.beans.BeansException)9 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 BeanCreationException (org.springframework.beans.factory.BeanCreationException)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 IOException (java.io.IOException)4