Search in sources :

Example 56 with BeanWrapper

use of org.springframework.beans.BeanWrapper 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 = 1;
    bw.setPropertyValue("value", value);
    assertThat(value).as("value not set correctly").isEqualTo(bean.getValue());
    value = 2;
    bw.setPropertyValue("value", value.toString());
    assertThat(value).as("value not converted").isEqualTo(bean.getValue());
    bw.setPropertyValue("value", null);
    assertThat(bean.getValue()).as("value not null").isNull();
    bw.setPropertyValue("value", "");
    assertThat(bean.getValue()).as("value not converted to null").isNull();
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Example 57 with BeanWrapper

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

the class QuartzJobBean method execute.

/**
 * This implementation applies the passed-in job data map as bean property
 * values, and delegates to {@code executeInternal} afterwards.
 * @see #executeInternal
 */
@Override
public final void execute(JobExecutionContext context) throws JobExecutionException {
    try {
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValues(context.getScheduler().getContext());
        pvs.addPropertyValues(context.getMergedJobDataMap());
        bw.setPropertyValues(pvs, true);
    } catch (SchedulerException ex) {
        throw new JobExecutionException(ex);
    }
    executeInternal(context);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) SchedulerException(org.quartz.SchedulerException) JobExecutionException(org.quartz.JobExecutionException) MutablePropertyValues(org.springframework.beans.MutablePropertyValues)

Example 58 with BeanWrapper

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

the class CustomEditorTests method testIndexedPropertiesWithDirectAccessAndPropertyEditors.

@Test
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);
    assertThat(bean.getArray()[0].getName()).isEqualTo("arraya");
    assertThat(bean.getArray()[1].getName()).isEqualTo("arrayb");
    assertThat(((TestBean) bean.getList().get(0)).getName()).isEqualTo("listc");
    assertThat(((TestBean) bean.getList().get(1)).getName()).isEqualTo("listd");
    assertThat(((TestBean) bean.getMap().get("key1")).getName()).isEqualTo("mape");
    assertThat(((TestBean) bean.getMap().get("key2")).getName()).isEqualTo("mapf");
}
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 59 with BeanWrapper

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

the class CustomEditorTests method testCustomNumberEditorWithAllowEmpty.

@Test
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");
    assertThat(Long.valueOf("5").equals(bw.getPropertyValue("long1"))).as("Correct long1 value").isTrue();
    assertThat(tb.getLong1() == 5).as("Correct long1 value").isTrue();
    assertThat(Long.valueOf("6").equals(bw.getPropertyValue("long2"))).as("Correct long2 value").isTrue();
    assertThat(Long.valueOf("6").equals(tb.getLong2())).as("Correct long2 value").isTrue();
    bw.setPropertyValue("long2", "");
    assertThat(bw.getPropertyValue("long2") == null).as("Correct long2 value").isTrue();
    assertThat(tb.getLong2() == null).as("Correct long2 value").isTrue();
    assertThatExceptionOfType(BeansException.class).isThrownBy(() -> bw.setPropertyValue("long1", ""));
    assertThat(bw.getPropertyValue("long1")).isEqualTo(5L);
    assertThat(tb.getLong1()).isEqualTo(5);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) NumberFormat(java.text.NumberFormat) BeansException(org.springframework.beans.BeansException) Test(org.junit.jupiter.api.Test)

Example 60 with BeanWrapper

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

the class CustomEditorTests method testNestedIndexedPropertiesWithCustomEditorForProperty.

@Test
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);
        }
    });
    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].nestedIndexedBean.array[0].name")).isEqualTo("name0");
    assertThat(bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")).isEqualTo("name1");
    assertThat(bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")).isEqualTo("name2");
    assertThat(bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")).isEqualTo("name3");
    assertThat(bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name")).isEqualTo("name4");
    assertThat(bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name")).isEqualTo("name5");
    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);
    assertThat(tb0.getNestedIndexedBean().getArray()[0].getName()).isEqualTo("arrayname5");
    assertThat(tb1.getNestedIndexedBean().getArray()[1].getName()).isEqualTo("arrayname4");
    assertThat(((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName()).isEqualTo("listname3");
    assertThat(((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName()).isEqualTo("listname2");
    assertThat(((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName()).isEqualTo("mapname1");
    assertThat(((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName()).isEqualTo("mapname0");
    assertThat(bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name")).isEqualTo("arrayname5");
    assertThat(bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")).isEqualTo("arrayname4");
    assertThat(bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")).isEqualTo("listname3");
    assertThat(bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")).isEqualTo("listname2");
    assertThat(bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name")).isEqualTo("mapname1");
    assertThat(bw.getPropertyValue("map[key2].nestedIndexedBean.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)

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