use of org.springframework.tests.sample.beans.IndexedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testDirectBindingToIndexedField.
@Test
public void testDirectBindingToIndexedField() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0]", "a");
binder.bind(pvs);
Errors errors = binder.getBindingResult();
errors.rejectValue("array[0]", "NOT_ROD", "are you sure you're not Rod?");
errors.rejectValue("map[key1]", "NOT_ROD", "are you sure you're not Rod?");
errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
assertEquals("arraya", errors.getFieldValue("array[0]"));
assertEquals(1, errors.getFieldErrorCount("array[0]"));
assertEquals("NOT_ROD", errors.getFieldError("array[0]").getCode());
assertEquals("NOT_ROD.tb.array[0]", errors.getFieldError("array[0]").getCodes()[0]);
assertEquals("NOT_ROD.tb.array", errors.getFieldError("array[0]").getCodes()[1]);
assertEquals("NOT_ROD.array[0]", errors.getFieldError("array[0]").getCodes()[2]);
assertEquals("NOT_ROD.array", errors.getFieldError("array[0]").getCodes()[3]);
assertEquals("NOT_ROD.org.springframework.tests.sample.beans.DerivedTestBean", errors.getFieldError("array[0]").getCodes()[4]);
assertEquals("NOT_ROD", errors.getFieldError("array[0]").getCodes()[5]);
assertEquals("arraya", errors.getFieldValue("array[0]"));
assertEquals(1, errors.getFieldErrorCount("map[key1]"));
assertEquals("NOT_ROD", errors.getFieldError("map[key1]").getCode());
assertEquals("NOT_ROD.tb.map[key1]", errors.getFieldError("map[key1]").getCodes()[0]);
assertEquals("NOT_ROD.tb.map", errors.getFieldError("map[key1]").getCodes()[1]);
assertEquals("NOT_ROD.map[key1]", errors.getFieldError("map[key1]").getCodes()[2]);
assertEquals("NOT_ROD.map", errors.getFieldError("map[key1]").getCodes()[3]);
assertEquals("NOT_ROD.org.springframework.tests.sample.beans.TestBean", errors.getFieldError("map[key1]").getCodes()[4]);
assertEquals("NOT_ROD", errors.getFieldError("map[key1]").getCodes()[5]);
assertEquals(1, errors.getFieldErrorCount("map[key0]"));
assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCode());
assertEquals("NOT_NULL.tb.map[key0]", errors.getFieldError("map[key0]").getCodes()[0]);
assertEquals("NOT_NULL.tb.map", errors.getFieldError("map[key0]").getCodes()[1]);
assertEquals("NOT_NULL.map[key0]", errors.getFieldError("map[key0]").getCodes()[2]);
assertEquals("NOT_NULL.map", errors.getFieldError("map[key0]").getCodes()[3]);
assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCodes()[4]);
}
use of org.springframework.tests.sample.beans.IndexedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testBindingToNestedIndexedField.
@Test
public 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?");
assertEquals(1, errors.getFieldErrorCount("array[0].nestedIndexedBean.list[0].name"));
assertEquals("NOT_ROD", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCode());
assertEquals("NOT_ROD.tb.array[0].nestedIndexedBean.list[0].name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[0]);
assertEquals("NOT_ROD.tb.array[0].nestedIndexedBean.list.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[1]);
assertEquals("NOT_ROD.tb.array.nestedIndexedBean.list.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[2]);
assertEquals("NOT_ROD.array[0].nestedIndexedBean.list[0].name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[3]);
assertEquals("NOT_ROD.array[0].nestedIndexedBean.list.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[4]);
assertEquals("NOT_ROD.array.nestedIndexedBean.list.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[5]);
assertEquals("NOT_ROD.name", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[6]);
assertEquals("NOT_ROD.java.lang.String", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[7]);
assertEquals("NOT_ROD", errors.getFieldError("array[0].nestedIndexedBean.list[0].name").getCodes()[8]);
}
use of org.springframework.tests.sample.beans.IndexedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testBindingNullToEmptyCollection.
@Test
public 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);
assertTrue(tb.getSet() instanceof TreeSet);
assertTrue(tb.getSet().isEmpty());
}
use of org.springframework.tests.sample.beans.IndexedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testBindingStringArrayToIntegerSet.
@Test
public void testBindingStringArrayToIntegerSet() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class) {
@Override
protected Object convertElement(Object element) {
return new Integer(element.toString());
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("set", new String[] { "10", "20", "30" });
binder.bind(pvs);
assertEquals(tb.getSet(), binder.getBindingResult().getFieldValue("set"));
assertTrue(tb.getSet() instanceof TreeSet);
assertEquals(3, tb.getSet().size());
assertTrue(tb.getSet().contains(new Integer(10)));
assertTrue(tb.getSet().contains(new Integer(20)));
assertTrue(tb.getSet().contains(new Integer(30)));
pvs = new MutablePropertyValues();
pvs.add("set", null);
binder.bind(pvs);
assertNull(tb.getSet());
}
use of org.springframework.tests.sample.beans.IndexedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testBindingWithResortedList.
@Test
public void testBindingWithResortedList() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
MutablePropertyValues pvs = new MutablePropertyValues();
TestBean tb1 = new TestBean("tb1", 99);
TestBean tb2 = new TestBean("tb2", 99);
pvs.add("list[0]", tb1);
pvs.add("list[1]", tb2);
binder.bind(pvs);
assertEquals(tb1.getName(), binder.getBindingResult().getFieldValue("list[0].name"));
assertEquals(tb2.getName(), binder.getBindingResult().getFieldValue("list[1].name"));
tb.getList().set(0, tb2);
tb.getList().set(1, tb1);
assertEquals(tb2.getName(), binder.getBindingResult().getFieldValue("list[0].name"));
assertEquals(tb1.getName(), binder.getBindingResult().getFieldValue("list[1].name"));
}
Aggregations