Search in sources :

Example 21 with BeanWrapper

use of cn.taketoday.beans.BeanWrapper in project today-framework 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 22 with BeanWrapper

use of cn.taketoday.beans.BeanWrapper in project today-framework by TAKETODAY.

the class CustomEditorTests method testConversionToOldCollections.

@Test
void testConversionToOldCollections() throws PropertyVetoException {
    OldCollectionsBean tb = new OldCollectionsBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(Vector.class, new CustomCollectionEditor(Vector.class));
    bw.registerCustomEditor(Hashtable.class, new CustomMapEditor(Hashtable.class));
    bw.setPropertyValue("vector", new String[] { "a", "b" });
    assertThat(tb.getVector().size()).isEqualTo(2);
    assertThat(tb.getVector().get(0)).isEqualTo("a");
    assertThat(tb.getVector().get(1)).isEqualTo("b");
    bw.setPropertyValue("hashtable", Collections.singletonMap("foo", "bar"));
    assertThat(tb.getHashtable().size()).isEqualTo(1);
    assertThat(tb.getHashtable().get("foo")).isEqualTo("bar");
}
Also used : BeanWrapper(cn.taketoday.beans.BeanWrapper) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) Hashtable(java.util.Hashtable) Vector(java.util.Vector) Test(org.junit.jupiter.api.Test)

Example 23 with BeanWrapper

use of cn.taketoday.beans.BeanWrapper in project today-framework 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 24 with BeanWrapper

use of cn.taketoday.beans.BeanWrapper in project today-framework by TAKETODAY.

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

Example 25 with BeanWrapper

use of cn.taketoday.beans.BeanWrapper in project today-framework by TAKETODAY.

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(cn.taketoday.beans.BeanWrapper) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Aggregations

BeanWrapper (cn.taketoday.beans.BeanWrapper)69 Test (org.junit.jupiter.api.Test)63 BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)60 NumberTestBean (cn.taketoday.beans.NumberTestBean)42 BooleanTestBean (cn.taketoday.beans.BooleanTestBean)40 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)37 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)37 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)37 PropertyEditorSupport (java.beans.PropertyEditorSupport)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28 PropertyValues (cn.taketoday.beans.PropertyValues)25 BigDecimal (java.math.BigDecimal)8 BeansException (cn.taketoday.beans.BeansException)6 NumberFormat (java.text.NumberFormat)6 PropertyValue (cn.taketoday.beans.PropertyValue)4 BigInteger (java.math.BigInteger)4 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)3 Environment (cn.taketoday.core.env.Environment)2 ResourceEditor (cn.taketoday.core.io.ResourceEditor)2 ResourceLoader (cn.taketoday.core.io.ResourceLoader)2