use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testEditorForNestedIndexedField.
@Test
void testEditorForNestedIndexedField() {
IndexedTestBean tb = new IndexedTestBean();
tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(String.class, "array.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);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
binder.bind(pvs);
assertThat(((TestBean) tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName()).isEqualTo("listtest1");
assertThat(((TestBean) tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName()).isEqualTo("listtest2");
assertThat(binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name")).isEqualTo("test1");
assertThat(binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name")).isEqualTo("test2");
}
use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testBindingNullToEmptyCollection.
@Test
void testBindingNullToEmptyCollection() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class, true));
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("set", null);
binder.bind(pvs);
boolean condition = tb.getSet() instanceof TreeSet;
assertThat(condition).isTrue();
assertThat(tb.getSet().isEmpty()).isTrue();
}
Aggregations