use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.
the class CustomEditorTests method testArrayToStringConversion.
@Test
void testArrayToStringConversion() throws PropertyVetoException {
TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("-" + text + "-");
}
});
bw.setPropertyValue("name", new String[] { "a", "b" });
assertThat(tb.getName()).isEqualTo("-a,b-");
}
use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.
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 org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.
the class CustomEditorTests method testIndexedPropertiesWithCustomEditorForProperty.
@Test
void testIndexedPropertiesWithCustomEditorForProperty() {
IndexedTestBean bean = new IndexedTestBean(false);
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(String.class, "array.name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("array" + text);
}
});
bw.registerCustomEditor(String.class, "list.name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("list" + text);
}
});
bw.registerCustomEditor(String.class, "map.name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("map" + text);
}
});
bean.populate();
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"));
assertThat(tb0.getName()).isEqualTo("name0");
assertThat(tb1.getName()).isEqualTo("name1");
assertThat(tb2.getName()).isEqualTo("name2");
assertThat(tb3.getName()).isEqualTo("name3");
assertThat(tb4.getName()).isEqualTo("name4");
assertThat(tb5.getName()).isEqualTo("name5");
assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("name0");
assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("name1");
assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("name2");
assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("name3");
assertThat(bw.getPropertyValue("map[key1].name")).isEqualTo("name4");
assertThat(bw.getPropertyValue("map[key2].name")).isEqualTo("name5");
assertThat(bw.getPropertyValue("map['key1'].name")).isEqualTo("name4");
assertThat(bw.getPropertyValue("map[\"key2\"].name")).isEqualTo("name5");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0].name", "name5");
pvs.add("array[1].name", "name4");
pvs.add("list[0].name", "name3");
pvs.add("list[1].name", "name2");
pvs.add("map[key1].name", "name1");
pvs.add("map['key2'].name", "name0");
bw.setPropertyValues(pvs);
assertThat(tb0.getName()).isEqualTo("arrayname5");
assertThat(tb1.getName()).isEqualTo("arrayname4");
assertThat(tb2.getName()).isEqualTo("listname3");
assertThat(tb3.getName()).isEqualTo("listname2");
assertThat(tb4.getName()).isEqualTo("mapname1");
assertThat(tb5.getName()).isEqualTo("mapname0");
assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("arrayname5");
assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("arrayname4");
assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("listname3");
assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("listname2");
assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("mapname1");
assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("mapname0");
}
use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.
the class CustomEditorTests method testCharacterEditor.
@Test
void testCharacterEditor() {
CharBean cb = new CharBean();
BeanWrapper bw = new BeanWrapperImpl(cb);
bw.setPropertyValue("myChar", Character.valueOf('c'));
assertThat(cb.getMyChar()).isEqualTo('c');
bw.setPropertyValue("myChar", "c");
assertThat(cb.getMyChar()).isEqualTo('c');
bw.setPropertyValue("myChar", "\u0041");
assertThat(cb.getMyChar()).isEqualTo('A');
bw.setPropertyValue("myChar", "\\u0022");
assertThat(cb.getMyChar()).isEqualTo('"');
CharacterEditor editor = new CharacterEditor(false);
editor.setAsText("M");
assertThat(editor.getAsText()).isEqualTo("M");
}
use of org.springframework.beans.BeanWrapper in project spring-framework by spring-projects.
the class CustomEditorTests method testCharacterEditorWithAllowEmpty.
@Test
void testCharacterEditorWithAllowEmpty() {
CharBean cb = new CharBean();
BeanWrapper bw = new BeanWrapperImpl(cb);
bw.registerCustomEditor(Character.class, new CharacterEditor(true));
bw.setPropertyValue("myCharacter", 'c');
assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf('c'));
bw.setPropertyValue("myCharacter", "c");
assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf('c'));
bw.setPropertyValue("myCharacter", "\u0041");
assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf('A'));
bw.setPropertyValue("myCharacter", " ");
assertThat(cb.getMyCharacter()).isEqualTo(Character.valueOf(' '));
bw.setPropertyValue("myCharacter", "");
assertThat(cb.getMyCharacter()).isNull();
}
Aggregations