Search in sources :

Example 11 with BeanWrapperImpl

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

the class CustomEditorTests method testIndexedPropertiesWithListPropertyEditor.

@Test
void testIndexedPropertiesWithListPropertyEditor() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            List<TestBean> result = new ArrayList<>();
            result.add(new TestBean("list" + text, 99));
            setValue(result);
        }
    });
    bw.setPropertyValue("list", "1");
    assertThat(((TestBean) bean.getList().get(0)).getName()).isEqualTo("list1");
    bw.setPropertyValue("list[0]", "test");
    assertThat(bean.getList().get(0)).isEqualTo("test");
}
Also used : IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) 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) ArrayList(java.util.ArrayList) List(java.util.List) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 12 with BeanWrapperImpl

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

the class CustomEditorTests method testDefaultBooleanEditorForWrapperType.

@Test
void testDefaultBooleanEditorForWrapperType() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("bool2", "true");
    assertThat(Boolean.TRUE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "false");
    assertThat(Boolean.FALSE.equals(bw.getPropertyValue("bool2"))).as("Correct bool2 value").isTrue();
    boolean condition3 = !tb.getBool2();
    assertThat(condition3).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "on");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "off");
    boolean condition2 = !tb.getBool2();
    assertThat(condition2).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "yes");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "no");
    boolean condition1 = !tb.getBool2();
    assertThat(condition1).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "1");
    assertThat(tb.getBool2().booleanValue()).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "0");
    boolean condition = !tb.getBool2();
    assertThat(condition).as("Correct bool2 value").isTrue();
    bw.setPropertyValue("bool2", "");
    assertThat(tb.getBool2()).as("Correct bool2 value").isNull();
}
Also used : BooleanTestBean(cn.taketoday.beans.BooleanTestBean) BeanWrapper(cn.taketoday.beans.BeanWrapper) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Example 13 with BeanWrapperImpl

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

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(cn.taketoday.beans.BeanWrapper) NumberTestBean(cn.taketoday.beans.NumberTestBean) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) BigDecimal(java.math.BigDecimal) NumberFormat(java.text.NumberFormat) Test(org.junit.jupiter.api.Test)

Example 14 with BeanWrapperImpl

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

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(cn.taketoday.beans.testfixture.beans.IndexedTestBean) 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 15 with BeanWrapperImpl

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

the class CustomEditorTests method testUninitializedArrayPropertyWithCustomEditor.

@Test
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);
    assertThat(bean.getList().get(0)).isEqualTo(tb);
    assertThat(bw.findCustomEditor(int.class, "list.age")).isEqualTo(pe);
    assertThat(bw.findCustomEditor(null, "list.age")).isEqualTo(pe);
    assertThat(bw.findCustomEditor(int.class, "list[0].age")).isEqualTo(pe);
    assertThat(bw.findCustomEditor(null, "list[0].age")).isEqualTo(pe);
}
Also used : IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) 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) PropertyEditor(java.beans.PropertyEditor) 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