📄 formcontrollertests.java
字号:
mv.getViewName().equals(formView));
TestBean person = (TestBean) mv.getModel().get(TestController.BEAN_NAME);
assertTrue("model is non null", person != null);
// yes, but it was rejected after binding by the validator
assertTrue("bean name bound ok", person.getName().equals(name));
assertTrue("bean age is default", person.getAge() == TestController.DEFAULT_AGE);
Errors errors = (Errors) mv.getModel().get(BindException.ERROR_KEY_PREFIX + mc.getCommandName());
assertTrue("errors returned in model", errors != null);
assertTrue("3 errors", errors.getErrorCount() == 3);
FieldError fe = errors.getFieldError("age");
assertTrue("Saved invalid value", fe.getRejectedValue().equals(age));
assertTrue("Correct field", fe.getField().equals("age"));
// raised by first validator
fe = errors.getFieldError("name");
assertTrue("Saved invalid value", fe.getRejectedValue().equals(name));
assertTrue("Correct field", fe.getField().equals("name"));
assertTrue("Correct validation code: expected '" +TestValidator.TOOSHORT + "', not '"
+ fe.getCode() + "'", fe.getCode().equals(TestValidator.TOOSHORT));
// raised by second validator
ObjectError oe = errors.getGlobalError();
assertEquals("test", oe.getCode());
assertEquals("testmessage", oe.getDefaultMessage());
}
public void testSessionController() throws Exception {
String formView = "f";
String successView = "s";
TestController mc = new TestController();
mc.setFormView(formView);
mc.setSuccessView(successView);
mc.setSessionForm(true);
// first request: GET form
HttpServletRequest request1 = new MockHttpServletRequest("GET", "/welcome.html");
HttpServletResponse response1 = new MockHttpServletResponse();
ModelAndView mv1 = mc.handleRequest(request1, response1);
assertTrue("returned correct view name", mv1.getViewName().equals(formView));
TestBean person = (TestBean) mv1.getModel().get(TestController.BEAN_NAME);
assertTrue("model is non null", person != null);
assertTrue("Bean age default ok", person.getAge() == TestController.DEFAULT_AGE);
// second request, using same session: POST submit
MockHttpServletRequest request2 = new MockHttpServletRequest("POST", "/welcome.html");
request2.setSession(request1.getSession(false));
HttpServletResponse response2 = new MockHttpServletResponse();
ModelAndView mv2 = mc.handleRequest(request2, response2);
assertTrue("returned correct view name", mv2.getViewName().equals(successView));
TestBean person2 = (TestBean) mv2.getModel().get(TestController.BEAN_NAME);
assertTrue("model is same object", person == person2);
}
public void testDefaultInvalidSubmit() throws Exception {
String formView = "f";
String successView = "s";
TestController mc = new TestController();
mc.setFormView(formView);
mc.setSuccessView(successView);
mc.setSessionForm(true);
// invalid request: POST submit
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/welcome.html");
HttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
assertTrue("returned correct view name", mv.getViewName().equals(successView));
TestBean person = (TestBean) mv.getModel().get(TestController.BEAN_NAME);
assertTrue("model is non null", person != null);
}
public void testSpecialInvalidSubmit() throws Exception {
String formView = "f";
String successView = "s";
TestController mc = new TestController() {
protected ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throw new ServletException("invalid submit");
}
};
mc.setFormView(formView);
mc.setSuccessView(successView);
mc.setSessionForm(true);
// invalid request: POST submit
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/welcome.html");
HttpServletResponse response = new MockHttpServletResponse();
try {
mc.handleRequest(request, response);
fail("Should have thrown ServletException");
}
catch (ServletException ex) {
// expected
}
}
public void testSubmitWithIndexedProperties() throws Exception {
String formView = "fred";
String successView = "tony";
SimpleFormController mc = new SimpleFormController();
mc.setCommandClass(IndexedTestBean.class);
mc.setFormView(formView);
mc.setSuccessView(successView);
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo.html");
request.addParameter("array[0].name", "name3");
request.addParameter("array[1].age", "name2");
request.addParameter("list[0].name", "name1");
request.addParameter("list[1].age", "name0");
request.addParameter("list[2]", "listobj");
request.addParameter("map[key1]", "mapobj1");
request.addParameter("map[key3]", "mapobj2");
HttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
assertTrue("returned correct view name: expected '" + formView + "', not '" + mv.getViewName() + "'",
mv.getViewName().equals(formView));
IndexedTestBean bean = (IndexedTestBean) mv.getModel().get(mc.getCommandName());
assertTrue("model is non null", bean != null);
assertEquals("name3", bean.getArray()[0].getName());
assertEquals("name1", ((TestBean) bean.getList().get(0)).getName());
Errors errors = (Errors) mv.getModel().get(BindException.ERROR_KEY_PREFIX + mc.getCommandName());
assertTrue("errors returned in model", errors != null);
assertTrue("2 errors", errors.getErrorCount() == 2);
FieldError fe1 = errors.getFieldError("array[1].age");
assertTrue("Saved invalid value", fe1.getRejectedValue().equals("name2"));
assertTrue("Correct field", fe1.getField().equals("array[1].age"));
FieldError fe2 = errors.getFieldError("list[1].age");
assertTrue("Saved invalid value", fe2.getRejectedValue().equals("name0"));
assertTrue("Correct field", fe2.getField().equals("list[1].age"));
assertEquals("listobj", bean.getList().get(2));
assertEquals("mapobj1", bean.getMap().get("key1"));
assertEquals("mapobj2", bean.getMap().get("key3"));
}
public void testFormChangeRequest() throws Exception {
String formView = "fred";
String successView = "tony";
final Float myFloat = new Float("123.45");
TestController mc = new TestController() {
protected boolean isFormChangeRequest(HttpServletRequest request) {
return (request.getParameter("formChange") != null);
}
protected void onFormChange(HttpServletRequest request, HttpServletResponse response, Object command) {
assertNotNull("Command should not be null", command);
assertEquals("Incorrect command class", TestBean.class, command.getClass());
((TestBean)command).setMyFloat(myFloat);
}
};
mc.setFormView(formView);
mc.setSuccessView(successView);
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo.html");
request.addParameter("name", "Rod");
request.addParameter("age", "99");
request.addParameter("formChange", "true");
HttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = mc.handleRequest(request, response);
assertTrue("returned correct view name: expected '" + formView + "', not '" + mv.getViewName() + "'",
mv.getViewName().equals(formView));
TestBean person = (TestBean) mv.getModel().get(mc.getCommandName());
assertTrue("model is non null", person != null);
assertTrue("bean name bound ok", person.getName().equals("Rod"));
assertTrue("bean age is 99", person.getAge() == 99);
assertEquals("Command property myFloat not updated in onFormChange", myFloat, person.getMyFloat());
Errors errors = (Errors) mv.getModel().get(BindException.ERROR_KEY_PREFIX + mc.getCommandName());
assertTrue("errors returned in model", errors != null);
assertTrue("No errors", errors.getErrorCount() == 0);
}
public void testFormControllerInWebApplicationContext() {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.setServletContext(new MockServletContext());
RefController mc = new RefController();
mc.setApplicationContext(ctx);
try {
mc.invokeWebSpecificStuff();
}
catch (IllegalStateException ex) {
fail("Shouldn't have thrown exception: " + ex.getMessage());
}
}
public void testFormControllerInNonWebApplicationContext() {
StaticApplicationContext ctx = new StaticApplicationContext();
RefController mc = new RefController();
mc.setApplicationContext(ctx);
try {
mc.invokeWebSpecificStuff();
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}
private static class TestValidator implements Validator {
public static String TOOSHORT = "tooshort";
public boolean supports(Class clazz) { return true; }
public void validate(Object obj, Errors errors) {
TestBean tb = (TestBean) obj;
if (tb.getName() == null || "".equals(tb.getName()))
errors.rejectValue("name", "needname", null, "need name");
else if (tb.getName().length() < 5)
errors.rejectValue("name", TOOSHORT, null, "need full name");
}
}
private static class TestValidator2 implements Validator {
public static String TOOSHORT = "tooshort";
public boolean supports(Class clazz) { return true; }
public void validate(Object obj, Errors errors) {
errors.reject("test", "testmessage");
}
}
private static class TestController extends SimpleFormController {
public static String BEAN_NAME = "person";
public static int DEFAULT_AGE = 52;
public TestController() {
setCommandClass(TestBean.class);
setCommandName(BEAN_NAME);
}
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
TestBean person = new TestBean();
person.setAge(DEFAULT_AGE);
return person;
}
protected boolean isFormChangeRequest(HttpServletRequest request) {
return (request.getParameter("formChange") != null);
}
}
private static class TestControllerWithCustomOnSubmit extends TestController {
protected ModelAndView onSubmit(Object command) throws Exception {
return new ModelAndView("mySuccess");
}
}
private static class RefController extends SimpleFormController {
final String NUMBERS_ATT = "NUMBERS";
static final int[] NUMBERS = { 1, 2, 3, 4 };
int refDataCount;
public RefController() {
setCommandClass(TestBean.class);
}
protected Map referenceData(HttpServletRequest request) {
++refDataCount;
Map m = new HashMap();
m.put(NUMBERS_ATT, NUMBERS);
return m;
}
public void invokeWebSpecificStuff() {
getTempDir();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -