Search in sources :

Example 6 with BeanWrapperImpl

use of cn.taketoday.beans.BeanWrapperImpl in project today-infrastructure by TAKETODAY.

the class CustomEditorTests method testDefaultBooleanEditorForPrimitiveType.

@Test
void testDefaultBooleanEditorForPrimitiveType() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("bool1", "true");
    assertThat(Boolean.TRUE.equals(bw.getPropertyValue("bool1"))).as("Correct bool1 value").isTrue();
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "false");
    assertThat(Boolean.FALSE.equals(bw.getPropertyValue("bool1"))).as("Correct bool1 value").isTrue();
    boolean condition4 = !tb.isBool1();
    assertThat(condition4).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "  true  ");
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "  false  ");
    boolean condition3 = !tb.isBool1();
    assertThat(condition3).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "on");
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "off");
    boolean condition2 = !tb.isBool1();
    assertThat(condition2).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "yes");
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "no");
    boolean condition1 = !tb.isBool1();
    assertThat(condition1).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "1");
    assertThat(tb.isBool1()).as("Correct bool1 value").isTrue();
    bw.setPropertyValue("bool1", "0");
    boolean condition = !tb.isBool1();
    assertThat(condition).as("Correct bool1 value").isTrue();
    assertThatExceptionOfType(BeansException.class).isThrownBy(() -> bw.setPropertyValue("bool1", "argh"));
}
Also used : BooleanTestBean(cn.taketoday.beans.BooleanTestBean) BeanWrapper(cn.taketoday.beans.BeanWrapper) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) BeansException(cn.taketoday.beans.BeansException) Test(org.junit.jupiter.api.Test)

Example 7 with BeanWrapperImpl

use of cn.taketoday.beans.BeanWrapperImpl in project today-infrastructure by TAKETODAY.

the class CustomEditorTests method testCustomEditorForAllStringProperties.

@Test
void testCustomEditorForAllStringProperties() {
    TestBean tb = new TestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, 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("prefixvalue");
    assertThat(tb.getTouchy()).isEqualTo("prefixvalue");
}
Also used : BeanWrapper(cn.taketoday.beans.BeanWrapper) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BooleanTestBean(cn.taketoday.beans.BooleanTestBean) NumberTestBean(cn.taketoday.beans.NumberTestBean) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 8 with BeanWrapperImpl

use of cn.taketoday.beans.BeanWrapperImpl in project today-infrastructure by TAKETODAY.

the class CustomEditorTests method testComplexObjectWithOldValueAccess.

@Test
void testComplexObjectWithOldValueAccess() {
    TestBean tb = new TestBean();
    String newName = "Rod";
    String tbString = "Kerry_34";
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setExtractOldValueForEditor(true);
    bw.registerCustomEditor(ITestBean.class, new OldValueAccessingTestBeanEditor());
    PropertyValues pvs = new PropertyValues();
    pvs.add(new PropertyValue("age", 55));
    pvs.add(new PropertyValue("name", newName));
    pvs.add(new PropertyValue("touchy", "valid"));
    pvs.add(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();
    ITestBean spouse = tb.getSpouse();
    bw.setPropertyValues(pvs);
    assertThat(tb.getSpouse()).as("Should have remained same object").isSameAs(spouse);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BooleanTestBean(cn.taketoday.beans.BooleanTestBean) NumberTestBean(cn.taketoday.beans.NumberTestBean) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Example 9 with BeanWrapperImpl

use of cn.taketoday.beans.BeanWrapperImpl in project today-infrastructure by TAKETODAY.

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();
        }
    });
    PropertyValues pvs = new PropertyValues();
    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(cn.taketoday.beans.testfixture.beans.IndexedTestBean) BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BooleanTestBean(cn.taketoday.beans.BooleanTestBean) NumberTestBean(cn.taketoday.beans.NumberTestBean) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 10 with BeanWrapperImpl

use of cn.taketoday.beans.BeanWrapperImpl in project today-infrastructure by TAKETODAY.

the class CustomEditorTests method testCustomEditorForAllNestedStringProperties.

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

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("prefix" + text);
        }
    });
    bw.setPropertyValue("spouse.name", "value");
    bw.setPropertyValue("touchy", "value");
    assertThat(bw.getPropertyValue("spouse.name")).isEqualTo("prefixvalue");
    assertThat(tb.getSpouse().getName()).isEqualTo("prefixvalue");
    assertThat(bw.getPropertyValue("touchy")).isEqualTo("prefixvalue");
    assertThat(tb.getTouchy()).isEqualTo("prefixvalue");
}
Also used : BeanWrapper(cn.taketoday.beans.BeanWrapper) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BooleanTestBean(cn.taketoday.beans.BooleanTestBean) NumberTestBean(cn.taketoday.beans.NumberTestBean) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Aggregations

BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)72 Test (org.junit.jupiter.api.Test)64 BeanWrapper (cn.taketoday.beans.BeanWrapper)60 NumberTestBean (cn.taketoday.beans.NumberTestBean)42 BooleanTestBean (cn.taketoday.beans.BooleanTestBean)40 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)34 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)34 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)34 PropertyEditorSupport (java.beans.PropertyEditorSupport)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28 PropertyValues (cn.taketoday.beans.PropertyValues)18 BeansException (cn.taketoday.beans.BeansException)8 BeanProperty (cn.taketoday.beans.BeanProperty)6 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)6 BigDecimal (java.math.BigDecimal)6 NumberFormat (java.text.NumberFormat)6 PropertyValue (cn.taketoday.beans.PropertyValue)4 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)4 BeanDefinitionStoreException (cn.taketoday.beans.factory.BeanDefinitionStoreException)4 InjectionPoint (cn.taketoday.beans.factory.InjectionPoint)4