📄 validationtestsuite.java
字号:
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.validation;
import java.beans.PropertyEditorSupport;
import java.util.Iterator;
import java.util.Map;
import junit.framework.TestCase;
import org.springframework.beans.IndexedTestBean;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.TestBean;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
public class ValidationTestSuite extends TestCase {
public void testBindingNoErrors() throws Exception {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "Rod"));
pvs.addPropertyValue(new PropertyValue("age", new Integer(32)));
binder.bind(pvs);
binder.close();
assertTrue("changed name correctly", rod.getName().equals("Rod"));
assertTrue("changed age correctly", rod.getAge() == 32);
Map m = binder.getErrors().getModel();
assertTrue("There is one element in map", m.size() == 2);
TestBean tb = (TestBean) m.get("person");
assertTrue("Same object", tb.equals(rod));
}
public void testBindingWithErrors() throws Exception {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "Rod"));
pvs.addPropertyValue(new PropertyValue("age", "32x"));
binder.bind(pvs);
try {
binder.close();
fail("Should have thrown BindException");
}
catch (BindException ex) {
assertTrue("changed name correctly", rod.getName().equals("Rod"));
//assertTrue("changed age correctly", rod.getAge() == 32);
Map m = binder.getErrors().getModel();
//assertTrue("There are 3 element in map", m.size() == 1);
TestBean tb = (TestBean) m.get("person");
assertTrue("Same object", tb.equals(rod));
BindException be = (BindException) m.get(BindException.ERROR_KEY_PREFIX + "person");
assertTrue("Added itself to map", ex == be);
assertTrue(be.hasErrors());
assertTrue("Correct number of errors", be.getErrorCount() == 1);
assertTrue("Has age errors", be.hasFieldErrors("age"));
assertTrue("Correct number of age errors", be.getFieldErrorCount("age") == 1);
assertEquals("32x", binder.getErrors().getFieldValue("age"));
assertEquals("32x", binder.getErrors().getFieldError("age").getRejectedValue());
assertEquals(0, tb.getAge());
}
}
public void testBindingWithAllowedFields() throws Exception {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
binder.setAllowedFields(new String[] {"name", "myparam"});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "Rod"));
pvs.addPropertyValue(new PropertyValue("age", "32x"));
binder.bind(pvs);
binder.close();
assertTrue("changed name correctly", rod.getName().equals("Rod"));
assertTrue("did not change age", rod.getAge() == 0);
Map m = binder.getErrors().getModel();
assertTrue("There is one element in map", m.size() == 2);
TestBean tb = (TestBean) m.get("person");
assertTrue("Same object", tb.equals(rod));
}
public void testBindingWithAllowedFieldsUsingAsterisks() throws Exception {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
binder.setAllowedFields(new String[] {"nam*", "*ouchy"});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "Rod"));
pvs.addPropertyValue(new PropertyValue("touchy", "Rod"));
pvs.addPropertyValue(new PropertyValue("age", "32x"));
binder.bind(pvs);
binder.close();
assertTrue("changed name correctly", "Rod".equals(rod.getName()));
assertTrue("changed touchy correctly", "Rod".equals(rod.getTouchy()));
assertTrue("did not change age", rod.getAge() == 0);
Map m = binder.getErrors().getModel();
assertTrue("There is one element in map", m.size() == 2);
TestBean tb = (TestBean) m.get("person");
assertTrue("Same object", tb.equals(rod));
}
/**
* Tests for required field, both null, non-existing and empty strings
*/
public void testBindingWithRequiredFields() throws Exception {
TestBean alef = new TestBean();
DataBinder binder = new DataBinder(alef, "person");
binder.setRequiredFields(new String[] {"name", "touchy", "date"});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("touchy", ""));
pvs.addPropertyValue(new PropertyValue("name", null));
binder.bind(pvs);
BindException ex = binder.getErrors();
assertEquals("Wrong amount of errors", 3, ex.getErrorCount());
/*// for debugging purposes
assertTrue("Error available for name", ex.getFieldError("name") != null);
assertTrue("Error available for touchy", ex.getFieldError("touchy") != null);
assertTrue("Error available for date", ex.getFieldError("date") != null);
*/
assertEquals("required", ex.getFieldError("touchy").getCode());
assertEquals("required", ex.getFieldError("name").getCode());
assertEquals("required", ex.getFieldError("date").getCode());
}
public void testCustomEditorForSingleProperty() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue("prefix" + text);
}
public String getAsText() {
return ((String) getValue()).substring(6);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "value"));
pvs.addPropertyValue(new PropertyValue("touchy", "value"));
binder.bind(pvs);
binder.getErrors().rejectValue("name", "someCode", "someMessage");
binder.getErrors().rejectValue("touchy", "someCode", "someMessage");
assertEquals("", binder.getErrors().getNestedPath());
assertEquals("value", binder.getErrors().getFieldValue("name"));
assertEquals("prefixvalue", binder.getErrors().getFieldError("name").getRejectedValue());
assertEquals("prefixvalue", tb.getName());
assertEquals("value", binder.getErrors().getFieldValue("touchy"));
assertEquals("value", binder.getErrors().getFieldError("touchy").getRejectedValue());
assertEquals("value", tb.getTouchy());
}
public void testCustomEditorForPrimitiveProperty() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(int.class, "age", new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue(new Integer(99));
}
public String getAsText() {
return "argh";
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("age", ""));
binder.bind(pvs);
assertEquals("argh", binder.getErrors().getFieldValue("age"));
assertEquals(99, tb.getAge());
}
public void testCustomEditorForAllStringProperties() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(String.class, null, new PropertyEditorSupport() {
public void setAsText(String text) throws IllegalArgumentException {
setValue("prefix" + text);
}
public String getAsText() {
return ((String) getValue()).substring(6);
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "value"));
pvs.addPropertyValue(new PropertyValue("touchy", "value"));
binder.bind(pvs);
binder.getErrors().rejectValue("name", "someCode", "someMessage");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -