use of org.springframework.beans.BeanWrapperImpl 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.BeanWrapperImpl 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();
}
use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.
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();
}
});
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);
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 org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.
the class CustomEditorTests method testCustomNumberEditorWithAllowEmpty.
@Test
void testCustomNumberEditorWithAllowEmpty() {
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
NumberTestBean tb = new NumberTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, true));
bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true));
bw.setPropertyValue("long1", "5");
bw.setPropertyValue("long2", "6");
assertThat(Long.valueOf("5").equals(bw.getPropertyValue("long1"))).as("Correct long1 value").isTrue();
assertThat(tb.getLong1() == 5).as("Correct long1 value").isTrue();
assertThat(Long.valueOf("6").equals(bw.getPropertyValue("long2"))).as("Correct long2 value").isTrue();
assertThat(Long.valueOf("6").equals(tb.getLong2())).as("Correct long2 value").isTrue();
bw.setPropertyValue("long2", "");
assertThat(bw.getPropertyValue("long2") == null).as("Correct long2 value").isTrue();
assertThat(tb.getLong2() == null).as("Correct long2 value").isTrue();
assertThatExceptionOfType(BeansException.class).isThrownBy(() -> bw.setPropertyValue("long1", ""));
assertThat(bw.getPropertyValue("long1")).isEqualTo(5L);
assertThat(tb.getLong1()).isEqualTo(5);
}
use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.
the class CustomEditorTests method testNestedIndexedPropertiesWithCustomEditorForProperty.
@Test
void testNestedIndexedPropertiesWithCustomEditorForProperty() {
IndexedTestBean bean = new IndexedTestBean();
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.nestedIndexedBean.array.name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("array" + text);
}
@Override
public String getAsText() {
return ((String) getValue()).substring(5);
}
});
bw.registerCustomEditor(String.class, "list.nestedIndexedBean.list.name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("list" + text);
}
@Override
public String getAsText() {
return ((String) getValue()).substring(4);
}
});
bw.registerCustomEditor(String.class, "map.nestedIndexedBean.map.name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("map" + text);
}
@Override
public String getAsText() {
return ((String) getValue()).substring(4);
}
});
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].nestedIndexedBean.array[0].name")).isEqualTo("name0");
assertThat(bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")).isEqualTo("name1");
assertThat(bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")).isEqualTo("name2");
assertThat(bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")).isEqualTo("name3");
assertThat(bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name")).isEqualTo("name4");
assertThat(bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name")).isEqualTo("name5");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0].nestedIndexedBean.array[0].name", "name5");
pvs.add("array[1].nestedIndexedBean.array[1].name", "name4");
pvs.add("list[0].nestedIndexedBean.list[0].name", "name3");
pvs.add("list[1].nestedIndexedBean.list[1].name", "name2");
pvs.add("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
pvs.add("map['key2'].nestedIndexedBean.map[key2].name", "name0");
bw.setPropertyValues(pvs);
assertThat(tb0.getNestedIndexedBean().getArray()[0].getName()).isEqualTo("arrayname5");
assertThat(tb1.getNestedIndexedBean().getArray()[1].getName()).isEqualTo("arrayname4");
assertThat(((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName()).isEqualTo("listname3");
assertThat(((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName()).isEqualTo("listname2");
assertThat(((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName()).isEqualTo("mapname1");
assertThat(((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName()).isEqualTo("mapname0");
assertThat(bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name")).isEqualTo("arrayname5");
assertThat(bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")).isEqualTo("arrayname4");
assertThat(bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")).isEqualTo("listname3");
assertThat(bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")).isEqualTo("listname2");
assertThat(bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name")).isEqualTo("mapname1");
assertThat(bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name")).isEqualTo("mapname0");
}
Aggregations