Search in sources :

Example 11 with BeanWrapper

use of cn.taketoday.beans.BeanWrapper 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 12 with BeanWrapper

use of cn.taketoday.beans.BeanWrapper 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 13 with BeanWrapper

use of cn.taketoday.beans.BeanWrapper 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)

Example 14 with BeanWrapper

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

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

Example 15 with BeanWrapper

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

the class CustomEditorTests method testCharArrayPropertyEditor.

@Test
void testCharArrayPropertyEditor() {
    PrimitiveArrayBean bean = new PrimitiveArrayBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.setPropertyValue("charArray", "myvalue");
    assertThat(new String(bean.getCharArray())).isEqualTo("myvalue");
}
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