📄 customeditortests.java
字号:
TestBean tb0 = bean.getArray()[0];
TestBean tb1 = bean.getArray()[1];
TestBean tb2 = ((TestBean) bean.getList().get(0));
TestBean tb3 = ((TestBean) bean.getList().get(1));
TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
tb0.setNestedIndexedBean(new IndexedTestBean());
tb1.setNestedIndexedBean(new IndexedTestBean());
tb2.setNestedIndexedBean(new IndexedTestBean());
tb3.setNestedIndexedBean(new IndexedTestBean());
tb4.setNestedIndexedBean(new IndexedTestBean());
tb5.setNestedIndexedBean(new IndexedTestBean());
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(String.class, "array[0].nestedIndexedBean.array[0].name", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue("array" + text);
}
});
bw.registerCustomEditor(String.class, "list.nestedIndexedBean.list[1].name", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue("list" + text);
}
});
bw.registerCustomEditor(String.class, "map[key1].nestedIndexedBean.map.name", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue("map" + text);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("array[0].nestedIndexedBean.array[0].name", "name5");
pvs.addPropertyValue("array[1].nestedIndexedBean.array[1].name", "name4");
pvs.addPropertyValue("list[0].nestedIndexedBean.list[0].name", "name3");
pvs.addPropertyValue("list[1].nestedIndexedBean.list[1].name", "name2");
pvs.addPropertyValue("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
pvs.addPropertyValue("map['key2'].nestedIndexedBean.map[key2].name", "name0");
bw.setPropertyValues(pvs);
assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName());
assertEquals("name4", tb1.getNestedIndexedBean().getArray()[1].getName());
assertEquals("name3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName());
assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName());
assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName());
assertEquals("name0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
}
public void testIndexedPropertiesWithDirectAccessAndPropertyEditors() {
IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("array" + text, 99));
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "list", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("list" + text, 99));
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("map" + text, 99));
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("array[0]", "a");
pvs.addPropertyValue("array[1]", "b");
pvs.addPropertyValue("list[0]", "c");
pvs.addPropertyValue("list[1]", "d");
pvs.addPropertyValue("map[key1]", "e");
pvs.addPropertyValue("map['key2']", "f");
bw.setPropertyValues(pvs);
assertEquals("arraya", bean.getArray()[0].getName());
assertEquals("arrayb", bean.getArray()[1].getName());
assertEquals("listc", ((TestBean) bean.getList().get(0)).getName());
assertEquals("listd", ((TestBean) bean.getList().get(1)).getName());
assertEquals("mape", ((TestBean) bean.getMap().get("key1")).getName());
assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName());
}
public void testIndexedPropertiesWithDirectAccessAndSpecificPropertyEditors() {
IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(TestBean.class, "array[0]", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("array0" + text, 99));
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "array[1]", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("array1" + text, 99));
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "list[0]", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("list0" + text, 99));
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "list[1]", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("list1" + text, 99));
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "map[key1]", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("mapkey1" + text, 99));
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
bw.registerCustomEditor(TestBean.class, "map[key2]", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean("mapkey2" + text, 99));
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("array[0]", "a");
pvs.addPropertyValue("array[1]", "b");
pvs.addPropertyValue("list[0]", "c");
pvs.addPropertyValue("list[1]", "d");
pvs.addPropertyValue("map[key1]", "e");
pvs.addPropertyValue("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());
}
public void testIndexedPropertiesWithListPropertyEditor() {
IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
List result = new ArrayList();
result.add(new TestBean("list" + text, 99));
setValue(result);
}
});
bw.setPropertyValue("list", "1");
assertEquals("list1", ((TestBean) bean.getList().get(0)).getName());
bw.setPropertyValue("list[0]", "test");
assertEquals("test", bean.getList().get(0));
}
public 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);
assertEquals(tb, bean.getList().get(0));
assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
assertEquals(pe, bw.findCustomEditor(null, "list.age"));
assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age"));
assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
}
public void testArrayToArrayConversion() throws PropertyVetoException {
IndexedTestBean tb = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean(text, 99));
}
});
bw.setPropertyValue("array", new String[]{"a", "b"});
assertEquals(2, tb.getArray().length);
assertEquals("a", tb.getArray()[0].getName());
assertEquals("b", tb.getArray()[1].getName());
}
public void testArrayToStringConversion() throws PropertyVetoException {
TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue("-" + text + "-");
}
});
bw.setPropertyValue("name", new String[]{"a", "b"});
assertEquals("-a,b-", tb.getName());
}
private static class TestBeanEditor extends PropertyEditorSupport {
public void setAsText(String text) {
TestBean tb = new TestBean();
StringTokenizer st = new StringTokenizer(text, "_");
tb.setName(st.nextToken());
tb.setAge(Integer.parseInt(st.nextToken()));
setValue(tb);
}
}
private static class OldValueAccessingTestBeanEditor extends PropertyEditorSupport {
public void setAsText(String text) {
TestBean tb = new TestBean();
StringTokenizer st = new StringTokenizer(text, "_");
tb.setName(st.nextToken());
tb.setAge(Integer.parseInt(st.nextToken()));
if (!tb.equals(getValue())) {
setValue(tb);
}
}
}
private static class PrimitiveArrayBean {
private byte[] byteArray;
private char[] charArray;
public byte[] getByteArray() {
return byteArray;
}
public void setByteArray(byte[] byteArray) {
this.byteArray = byteArray;
}
public char[] getCharArray() {
return charArray;
}
public void setCharArray(char[] charArray) {
this.charArray = charArray;
}
}
private static class CharBean {
private char myChar;
private Character myCharacter;
public char getMyChar() {
return myChar;
}
public void setMyChar(char myChar) {
this.myChar = myChar;
}
public Character getMyCharacter() {
return myCharacter;
}
public void setMyCharacter(Character myCharacter) {
this.myCharacter = myCharacter;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -