📄 databindertests.java
字号:
public void setAsText(String text) throws IllegalArgumentException {
setValue("list" + text);
}
public String getAsText() {
return ((String) getValue()).substring(4);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("array[0].nestedIndexedBean.list[0].name", "test1");
pvs.addPropertyValue("array[1].nestedIndexedBean.list[1].name", "test2");
binder.bind(pvs);
assertEquals("listtest1", ((TestBean) tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
assertEquals("test2", ((TestBean) tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
assertEquals("test1", binder.getErrors().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
assertEquals("test2", binder.getErrors().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
public void testDirectBindingToIndexedField() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("array[0]", "a");
binder.bind(pvs);
Errors errors = binder.getErrors();
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.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.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]);
}
public void testDirectBindingToEmptyIndexedFieldWithRegisteredSpecificEditor() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, "map[key0]", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
Errors errors = binder.getErrors();
errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
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]);
// This next code is only generated because of the registered editor, using the
// registered type of the editor as guess for the content type of the collection.
assertEquals("NOT_NULL.org.springframework.beans.TestBean", errors.getFieldError("map[key0]").getCodes()[4]);
assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCodes()[5]);
}
public void testDirectBindingToEmptyIndexedFieldWithRegisteredGenericEditor() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
Errors errors = binder.getErrors();
errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
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]);
// This next code is only generated because of the registered editor, using the
// registered type of the editor as guess for the content type of the collection.
assertEquals("NOT_NULL.org.springframework.beans.TestBean", errors.getFieldError("map[key0]").getCodes()[4]);
assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCodes()[5]);
}
public void testCustomEditorWithSubclass() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("array[0]", "a");
binder.bind(pvs);
Errors errors = binder.getErrors();
errors.rejectValue("array[0]", "NOT_ROD", "are you sure you're not Rod?");
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.beans.DerivedTestBean", errors.getFieldError("array[0]").getCodes()[4]);
assertEquals("NOT_ROD", errors.getFieldError("array[0]").getCodes()[5]);
assertEquals("arraya", errors.getFieldValue("array[0]"));
}
public void testBindToStringArrayWithArrayEditor() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(String[].class, "stringArray", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(StringUtils.delimitedListToStringArray(text, "-"));
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("stringArray", "a1-b2");
binder.bind(pvs);
assertTrue(!binder.getErrors().hasErrors());
assertEquals(2, tb.getStringArray().length);
assertEquals("a1", tb.getStringArray()[0]);
assertEquals("b2", tb.getStringArray()[1]);
}
public void testBindToStringArrayWithComponentEditor() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue("X" + text);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("stringArray", new String[] {"a1", "b2"});
binder.bind(pvs);
assertTrue(!binder.getErrors().hasErrors());
assertEquals(2, tb.getStringArray().length);
assertEquals("Xa1", tb.getStringArray()[0]);
assertEquals("Xb2", tb.getStringArray()[1]);
}
public void testBindingErrors() {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("age", "32x"));
binder.bind(pvs);
Errors errors = binder.getErrors();
FieldError ageError = errors.getFieldError("age");
assertEquals("typeMismatch", ageError.getCode());
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("org.springframework.validation.messages1");
String msg = messageSource.getMessage(ageError, Locale.getDefault());
assertEquals("Field age did not have correct type", msg);
messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("org.springframework.validation.messages2");
msg = messageSource.getMessage(ageError, Locale.getDefault());
assertEquals("Field Age did not have correct type", msg);
messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("org.springframework.validation.messages3");
msg = messageSource.getMessage(ageError, Locale.getDefault());
assertEquals("Field Person Age did not have correct type", msg);
}
public void testAddAllErrors() {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("age", "32x"));
binder.bind(pvs);
Errors errors = binder.getErrors();
BindException errors2 = new BindException(rod, "person");
errors.rejectValue("name", "badName");
errors.addAllErrors(errors2);
FieldError ageError = errors.getFieldError("age");
assertEquals("typeMismatch", ageError.getCode());
FieldError nameError = errors.getFieldError("name");
assertEquals("badName", nameError.getCode());
}
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.addPropertyValue("list[0]", tb1);
pvs.addPropertyValue("list[1]", tb2);
binder.bind(pvs);
assertEquals(tb1.getName(), binder.getErrors().getFieldValue("list[0].name"));
assertEquals(tb2.getName(), binder.getErrors().getFieldValue("list[1].name"));
tb.getList().set(0, tb2);
tb.getList().set(1, tb1);
assertEquals(tb2.getName(), binder.getErrors().getFieldValue("list[0].name"));
assertEquals(tb1.getName(), binder.getErrors().getFieldValue("list[1].name"));
}
public void testRejectWithoutDefaultMessage() throws Exception {
TestBean tb = new TestBean();
tb.setName("myName");
tb.setAge(99);
BindException ex = new BindException(tb, "tb");
ex.reject("invalid");
ex.rejectValue("age", "invalidField");
StaticMessageSource ms = new StaticMessageSource();
ms.addMessage("invalid", Locale.US, "general error");
ms.addMessage("invalidField", Locale.US, "invalid field");
assertEquals("general error", ms.getMessage(ex.getGlobalError(), Locale.US));
assertEquals("invalid field", ms.getMessage(ex.getFieldError("age"), Locale.US));
}
public void testBindExceptionSerializable() throws Exception {
SerializablePerson tb = new SerializablePerson();
tb.setName("myName");
tb.setAge(99);
BindException ex = new BindException(tb, "tb");
ex.reject("invalid", "someMessage");
ex.rejectValue("age", "invalidField", "someMessage");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(ex);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
BindException ex2 = (BindException) ois.readObject();
assertTrue(ex2.hasGlobalErrors());
assertEquals("invalid", ex2.getGlobalError().getCode());
assertTrue(ex2.hasFieldErrors("age"));
assertEquals("invalidField", ex2.getFieldError("age").getCode());
assertEquals(new Integer(99), ex2.getFieldValue("age"));
ex2.rejectValue("name", "invalidField", "someMessage");
assertTrue(ex2.hasFieldErrors("name"));
assertEquals("invalidField", ex2.getFieldError("name").getCode());
assertEquals("myName", ex2.getFieldValue("name"));
}
private static class TestBeanValidator implements Validator {
public boolean supports(Class clazz) {
return TestBean.class.isAssignableFrom(clazz);
}
public void validate(Object obj, Errors errors) {
TestBean tb = (TestBean) obj;
if (tb.getAge() < 32) {
errors.rejectValue("age", "TOO_YOUNG", null, "simply too young");
}
if (tb.getAge() % 2 == 0) {
errors.rejectValue("age", "AGE_NOT_ODD", null, "your age isn't odd");
}
if (tb.getName() == null || !tb.getName().equals("Rod")) {
errors.rejectValue("name", "NOT_ROD", "are you sure you're not Rod?");
}
if (tb.getTouchy() == null || !tb.getTouchy().equals(tb.getName())) {
errors.reject("NAME_TOUCHY_MISMATCH", "name and touchy do not match");
}
if (tb.getAge() == 0) {
errors.reject("GENERAL_ERROR", new String[]{"arg"}, "msg");
}
}
}
private static class SpouseValidator implements Validator {
public boolean supports(Class clazz) {
return TestBean.class.isAssignableFrom(clazz);
}
public void validate(Object obj, Errors errors) {
TestBean tb = (TestBean) obj;
if (tb.getAge() < 32) {
errors.rejectValue("age", "TOO_YOUNG", null, "simply too young");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -