use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class CustomEditorTests method testIndexedPropertiesWithCustomEditorForType.
@Test
void testIndexedPropertiesWithCustomEditorForType() {
IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("prefix" + text);
}
});
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("prefixname5");
assertThat(tb1.getName()).isEqualTo("prefixname4");
assertThat(tb2.getName()).isEqualTo("prefixname3");
assertThat(tb3.getName()).isEqualTo("prefixname2");
assertThat(tb4.getName()).isEqualTo("prefixname1");
assertThat(tb5.getName()).isEqualTo("prefixname0");
assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("prefixname5");
assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("prefixname4");
assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("prefixname3");
assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("prefixname2");
assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("prefixname1");
assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("prefixname0");
}
use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class CustomEditorTests method testNestedIndexedPropertiesWithIndexedCustomEditorForProperty.
@Test
void testNestedIndexedPropertiesWithIndexedCustomEditorForProperty() {
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[0].nestedIndexedBean.array[0].name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("array" + text);
}
});
bw.registerCustomEditor(String.class, "list.nestedIndexedBean.list[1].name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("list" + text);
}
});
bw.registerCustomEditor(String.class, "map[key1].nestedIndexedBean.map.name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("map" + text);
}
});
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("name4");
assertThat(((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName()).isEqualTo("name3");
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("name0");
}
use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class CustomEditorTests method testIndexedPropertiesWithListPropertyEditor.
@Test
void testIndexedPropertiesWithListPropertyEditor() {
IndexedTestBean bean = new IndexedTestBean();
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
List<TestBean> result = new ArrayList<>();
result.add(new TestBean("list" + text, 99));
setValue(result);
}
});
bw.setPropertyValue("list", "1");
assertThat(((TestBean) bean.getList().get(0)).getName()).isEqualTo("list1");
bw.setPropertyValue("list[0]", "test");
assertThat(bean.getList().get(0)).isEqualTo("test");
}
use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class CustomEditorTests method testIndexedPropertiesWithDirectAccessAndSpecificPropertyEditors.
@Test
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);
assertThat(bean.getArray()[0].getName()).isEqualTo("array0a");
assertThat(bean.getArray()[1].getName()).isEqualTo("array1b");
assertThat(((TestBean) bean.getList().get(0)).getName()).isEqualTo("list0c");
assertThat(((TestBean) bean.getList().get(1)).getName()).isEqualTo("list1d");
assertThat(((TestBean) bean.getMap().get("key1")).getName()).isEqualTo("mapkey1e");
assertThat(((TestBean) bean.getMap().get("key2")).getName()).isEqualTo("mapkey2f");
}
use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testBindingToNestedIndexedField.
@Test
void testBindingToNestedIndexedField() {
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);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0].nestedIndexedBean.list[0].name", "a");
binder.bind(pvs);
Errors errors = binder.getBindingResult();
errors.rejectValue("array[0].nestedIndexedBean.list[0].name", "NOT_ROD", "are you sure you're not Rod?");
assertThat(errors.getFieldErrorCount("array[0].nestedIndexedBean.list[0].name")).isEqualTo(1);
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCode()).isEqualTo("NOT_ROD");
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[0]).isEqualTo("NOT_ROD.tb.array[0].nestedIndexedBean.list[0].name");
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[1]).isEqualTo("NOT_ROD.tb.array[0].nestedIndexedBean.list.name");
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[2]).isEqualTo("NOT_ROD.tb.array.nestedIndexedBean.list.name");
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[3]).isEqualTo("NOT_ROD.array[0].nestedIndexedBean.list[0].name");
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[4]).isEqualTo("NOT_ROD.array[0].nestedIndexedBean.list.name");
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[5]).isEqualTo("NOT_ROD.array.nestedIndexedBean.list.name");
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[6]).isEqualTo("NOT_ROD.name");
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[7]).isEqualTo("NOT_ROD.java.lang.String");
assertThat(errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[8]).isEqualTo("NOT_ROD");
}
Aggregations