use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class DataBinderTests method testCustomEditorForPrimitiveProperty.
@Test
public void testCustomEditorForPrimitiveProperty() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(int.class, "age", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new Integer(99));
}
@Override
public String getAsText() {
return "argh";
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("age", "");
binder.bind(pvs);
assertEquals("argh", binder.getBindingResult().getFieldValue("age"));
assertEquals(99, tb.getAge());
}
use of java.beans.PropertyEditorSupport 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 java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class DataBinderTests method testCustomEditorForAllStringProperties.
@Test
public void testCustomEditorForAllStringProperties() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(String.class, null, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("prefix" + text);
}
@Override
public String getAsText() {
return ((String) getValue()).substring(6);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "value");
pvs.add("touchy", "value");
binder.bind(pvs);
binder.getBindingResult().rejectValue("name", "someCode", "someMessage");
binder.getBindingResult().rejectValue("touchy", "someCode", "someMessage");
assertEquals("value", binder.getBindingResult().getFieldValue("name"));
assertEquals("prefixvalue", binder.getBindingResult().getFieldError("name").getRejectedValue());
assertEquals("prefixvalue", tb.getName());
assertEquals("value", binder.getBindingResult().getFieldValue("touchy"));
assertEquals("prefixvalue", binder.getBindingResult().getFieldError("touchy").getRejectedValue());
assertEquals("prefixvalue", tb.getTouchy());
}
use of java.beans.PropertyEditorSupport 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 java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class DataBinderTests method testBindToStringArrayWithComponentEditor.
@Test
public void testBindToStringArrayWithComponentEditor() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("X" + text);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("stringArray", new String[] { "a1", "b2" });
binder.bind(pvs);
assertTrue(!binder.getBindingResult().hasErrors());
assertEquals(2, tb.getStringArray().length);
assertEquals("Xa1", tb.getStringArray()[0]);
assertEquals("Xb2", tb.getStringArray()[1]);
}
Aggregations