use of cn.taketoday.beans.BeanWrapper in project today-framework 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");
}
use of cn.taketoday.beans.BeanWrapper in project today-framework by TAKETODAY.
the class CustomEditorTests method testCustomEditorForSingleNestedProperty.
@Test
void testCustomEditorForSingleNestedProperty() {
TestBean tb = new TestBean();
tb.setSpouse(new TestBean());
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(String.class, "spouse.name", 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("value");
assertThat(tb.getTouchy()).isEqualTo("value");
}
use of cn.taketoday.beans.BeanWrapper in project today-framework 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.BeanWrapper in project today-framework by TAKETODAY.
the class CustomEditorTests method testComplexObject.
@Test
void testComplexObject() {
TestBean tb = new TestBean();
String newName = "Rod";
String tbString = "Kerry_34";
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
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();
}
use of cn.taketoday.beans.BeanWrapper in project today-framework by TAKETODAY.
the class CustomEditorTests method testCustomEditorForSingleProperty.
@Test
void testCustomEditorForSingleProperty() {
TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(String.class, "name", 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("value");
assertThat(tb.getTouchy()).isEqualTo("value");
}
Aggregations