assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName());
}
@Test
public void testIndexedPropertiesWithDirectAccessAndSpecificPropertyEditors() {
IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(TestBean.class, "array[0]", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("array0" + text, 99));
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "array[1]", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("array1" + text, 99));
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "list[0]", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("list0" + text, 99));
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "list[1]", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("list1" + text, 99));
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "map[key1]", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("mapkey1" + text, 99));
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "map[key2]", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("mapkey2" + text, 99));
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
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);
assertEquals("array0a", bean.getArray()[0].getName());
assertEquals("array1b", bean.getArray()[1].getName());
assertEquals("list0c", ((TestBean) bean.getList().get(0)).getName());
assertEquals("list1d", ((TestBean) bean.getList().get(1)).getName());
assertEquals("mapkey1e", ((TestBean) bean.getMap().get("key1")).getName());
assertEquals("mapkey2f", ((TestBean) bean.getMap().get("key2")).getName());
}