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");
}
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();
}
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));
}
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");
}
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);
}
Aggregations