Removed Spring MVC command/form controller class hierarchy
This commit is contained in:
@@ -60,7 +60,6 @@ import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
import org.springframework.web.servlet.mvc.Controller;
|
||||
import org.springframework.web.servlet.mvc.ParameterizableViewController;
|
||||
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.SimpleFormController;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
import org.springframework.web.servlet.theme.SessionThemeResolver;
|
||||
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
|
||||
@@ -111,7 +110,7 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext {
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.add(
|
||||
"mappings", "/form.do=formHandler\n/head.do=headController\n" +
|
||||
"mappings", "/head.do=headController\n" +
|
||||
"body.do=bodyController\n/noview*=noviewController\n/noview/simple*=noviewController");
|
||||
pvs.add("order", "1");
|
||||
registerSingleton("handlerMapping", SimpleUrlHandlerMapping.class, pvs);
|
||||
@@ -130,11 +129,6 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext {
|
||||
pvs.add("suffix", ".jsp");
|
||||
registerSingleton("viewResolver2", InternalResourceViewResolver.class, pvs);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.add("commandClass", "org.springframework.tests.sample.beans.TestBean");
|
||||
pvs.add("formView", "form");
|
||||
registerSingleton("formHandler", SimpleFormController.class, pvs);
|
||||
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.add("viewName", "form");
|
||||
registerSingleton("viewHandler", ParameterizableViewController.class, pvs);
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.web.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
@@ -31,7 +30,6 @@ import junit.framework.TestCase;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.DummyEnvironment;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
@@ -39,7 +37,6 @@ import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletConfig;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.web.bind.EscapedErrors;
|
||||
import org.springframework.web.context.ConfigurableWebEnvironment;
|
||||
import org.springframework.web.context.ServletConfigAwareBean;
|
||||
import org.springframework.web.context.ServletContextAwareBean;
|
||||
@@ -51,11 +48,8 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.mvc.BaseCommandController;
|
||||
import org.springframework.web.servlet.mvc.Controller;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
import org.springframework.web.servlet.theme.AbstractThemeResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
@@ -155,44 +149,6 @@ public class DispatcherServletTests extends TestCase {
|
||||
assertEquals(0, listener.counter);
|
||||
}
|
||||
|
||||
public void testFormRequest() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do");
|
||||
request.addPreferredLocale(Locale.CANADA);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
simpleDispatcherServlet.service(request, response);
|
||||
assertTrue("forwarded to form", "form".equals(response.getForwardedUrl()));
|
||||
DefaultMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[]{"test"});
|
||||
RequestContext rc = new RequestContext(request);
|
||||
|
||||
assertTrue("hasn't RequestContext attribute", request.getAttribute("rc") == null);
|
||||
assertTrue("Correct WebApplicationContext",
|
||||
RequestContextUtils.getWebApplicationContext(request) instanceof SimpleWebApplicationContext);
|
||||
assertTrue("Correct context path", rc.getContextPath().equals(request.getContextPath()));
|
||||
assertTrue("Correct locale", Locale.CANADA.equals(RequestContextUtils.getLocale(request)));
|
||||
assertTrue("Correct theme", AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME.equals(
|
||||
RequestContextUtils.getTheme(request).getName()));
|
||||
assertTrue("Correct message", "Canadian & test message".equals(rc.getMessage("test")));
|
||||
|
||||
assertTrue("Correct WebApplicationContext",
|
||||
rc.getWebApplicationContext() == simpleDispatcherServlet.getWebApplicationContext());
|
||||
assertTrue("Correct Errors",
|
||||
!(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME) instanceof EscapedErrors));
|
||||
assertTrue("Correct Errors",
|
||||
!(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, false) instanceof EscapedErrors));
|
||||
assertTrue("Correct Errors",
|
||||
rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, true) instanceof EscapedErrors);
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test"));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, false));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, true));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, false));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, true));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", "default"));
|
||||
assertEquals("Correct message", "default", rc.getMessage("testa", "default"));
|
||||
assertEquals("Correct message", "default &", rc.getMessage("testa", null, "default &", true));
|
||||
}
|
||||
|
||||
public void testParameterizableViewController() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/view.do");
|
||||
request.addUserRole("role1");
|
||||
@@ -227,51 +183,6 @@ public class DispatcherServletTests extends TestCase {
|
||||
assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(ServletException.class));
|
||||
}
|
||||
|
||||
public void testAnotherFormRequest() throws Exception {
|
||||
MockHttpServletRequest request =
|
||||
new MockHttpServletRequest(getServletContext(), "GET", "/form.do;jsessionid=xxx");
|
||||
request.addPreferredLocale(Locale.CANADA);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
complexDispatcherServlet.service(request, response);
|
||||
assertTrue("forwarded to form", "myform.jsp".equals(response.getForwardedUrl()));
|
||||
assertTrue("has RequestContext attribute", request.getAttribute("rc") != null);
|
||||
DefaultMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[]{"test"});
|
||||
|
||||
RequestContext rc = (RequestContext) request.getAttribute("rc");
|
||||
assertTrue("Not in HTML escaping mode", !rc.isDefaultHtmlEscape());
|
||||
assertTrue("Correct WebApplicationContext",
|
||||
rc.getWebApplicationContext() == complexDispatcherServlet.getWebApplicationContext());
|
||||
assertTrue("Correct context path", rc.getContextPath().equals(request.getContextPath()));
|
||||
assertTrue("Correct locale", Locale.CANADA.equals(rc.getLocale()));
|
||||
assertTrue("Correct Errors",
|
||||
!(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME) instanceof EscapedErrors));
|
||||
assertTrue("Correct Errors",
|
||||
!(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, false) instanceof EscapedErrors));
|
||||
assertTrue("Correct Errors",
|
||||
rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, true) instanceof EscapedErrors);
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test"));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, false));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, true));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, false));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, true));
|
||||
|
||||
rc.setDefaultHtmlEscape(true);
|
||||
assertTrue("Is in HTML escaping mode", rc.isDefaultHtmlEscape());
|
||||
assertTrue("Correct Errors", rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME) instanceof EscapedErrors);
|
||||
assertTrue("Correct Errors",
|
||||
!(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, false) instanceof EscapedErrors));
|
||||
assertTrue("Correct Errors",
|
||||
rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, true) instanceof EscapedErrors);
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test"));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, false));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, true));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, false));
|
||||
assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, true));
|
||||
}
|
||||
|
||||
public void testAnotherLocaleRequest() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
|
||||
request.addPreferredLocale(Locale.CANADA);
|
||||
@@ -568,7 +479,6 @@ public class DispatcherServletTests extends TestCase {
|
||||
request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do");
|
||||
response = new MockHttpServletResponse();
|
||||
complexDispatcherServlet.service(request, response);
|
||||
assertTrue("forwarded to form", "myform.jsp".equals(response.getForwardedUrl()));
|
||||
}
|
||||
|
||||
public void testNotDetectAllHandlerAdapters() throws ServletException, IOException {
|
||||
@@ -641,7 +551,6 @@ public class DispatcherServletTests extends TestCase {
|
||||
|
||||
request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
|
||||
simpleDispatcherServlet.service(request, response);
|
||||
assertTrue("forwarded to form", "form".equals(response.getIncludedUrl()));
|
||||
|
||||
assertEquals("value1", request.getAttribute("test1"));
|
||||
assertEquals("value2", request.getAttribute("test2"));
|
||||
@@ -663,12 +572,10 @@ public class DispatcherServletTests extends TestCase {
|
||||
|
||||
request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
|
||||
simpleDispatcherServlet.service(request, response);
|
||||
assertTrue("forwarded to form", "form".equals(response.getIncludedUrl()));
|
||||
|
||||
assertEquals("value1", request.getAttribute("test1"));
|
||||
assertEquals("value2", request.getAttribute("test2"));
|
||||
assertSame(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
|
||||
assertSame(command, request.getAttribute("command"));
|
||||
}
|
||||
|
||||
public void testNoCleanupAfterInclude() throws ServletException, IOException {
|
||||
@@ -685,12 +592,10 @@ public class DispatcherServletTests extends TestCase {
|
||||
request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
|
||||
simpleDispatcherServlet.setCleanupAfterInclude(false);
|
||||
simpleDispatcherServlet.service(request, response);
|
||||
assertTrue("forwarded to form", "form".equals(response.getIncludedUrl()));
|
||||
|
||||
assertEquals("value1", request.getAttribute("test1"));
|
||||
assertEquals("value2", request.getAttribute("test2"));
|
||||
assertSame(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
|
||||
assertNotSame(command, request.getAttribute("command"));
|
||||
}
|
||||
|
||||
public void testServletHandlerAdapter() throws Exception {
|
||||
|
||||
@@ -34,7 +34,6 @@ import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
import org.springframework.web.servlet.mvc.Controller;
|
||||
import org.springframework.web.servlet.mvc.LastModified;
|
||||
import org.springframework.web.servlet.mvc.SimpleFormController;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
import org.springframework.web.servlet.theme.AbstractThemeResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
@@ -52,7 +51,6 @@ public class SimpleWebApplicationContext extends StaticWebApplicationContext {
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.add("commandClass", "org.springframework.tests.sample.beans.TestBean");
|
||||
pvs.add("formView", "form");
|
||||
registerSingleton("/form.do", SimpleFormController.class, pvs);
|
||||
|
||||
registerSingleton("/locale.do", LocaleChecker.class);
|
||||
|
||||
|
||||
@@ -1,239 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.web.servlet.mvc;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class CancellableFormControllerTests extends TestCase {
|
||||
|
||||
public void testFormViewRequest() throws Exception {
|
||||
String formView = "theFormView";
|
||||
|
||||
TestController ctl = new TestController();
|
||||
ctl.setFormView(formView);
|
||||
ctl.setBindOnNewForm(true);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
String name = "Rob Harrop";
|
||||
int age = 23;
|
||||
|
||||
request.setMethod("GET");
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", "" + age);
|
||||
|
||||
ModelAndView mv = ctl.handleRequest(request, response);
|
||||
|
||||
assertEquals("Incorrect view name", formView, mv.getViewName());
|
||||
|
||||
TestBean command = (TestBean) mv.getModel().get(ctl.getCommandName());
|
||||
|
||||
testCommandIsBound(command, name, age);
|
||||
}
|
||||
|
||||
public void testFormSubmissionRequestWithoutCancel() throws Exception {
|
||||
String successView = "successView";
|
||||
|
||||
TestController ctl = new TestController();
|
||||
ctl.setSuccessView(successView);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
String name = "Rob Harrop";
|
||||
int age = 23;
|
||||
|
||||
request.setMethod("POST");
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", "" + age);
|
||||
|
||||
ModelAndView mv = ctl.handleRequest(request, response);
|
||||
|
||||
assertEquals("Incorrect view name", successView, mv.getViewName());
|
||||
|
||||
TestBean command = (TestBean) mv.getModel().get(ctl.getCommandName());
|
||||
|
||||
testCommandIsBound(command, name, age);
|
||||
}
|
||||
|
||||
public void testFormSubmissionWithErrors() throws Exception {
|
||||
String successView = "successView";
|
||||
String formView = "formView";
|
||||
|
||||
TestController ctl = new TestController();
|
||||
ctl.setSuccessView(successView);
|
||||
ctl.setFormView(formView);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
request.setMethod("POST");
|
||||
request.addParameter("name", "Rob Harrop");
|
||||
request.addParameter("age", "xxx23");
|
||||
|
||||
ModelAndView mv = ctl.handleRequest(request, response);
|
||||
assertEquals("Incorrect view name", formView, mv.getViewName());
|
||||
|
||||
Errors errors = (Errors) mv.getModel().get(BindException.MODEL_KEY_PREFIX + ctl.getCommandName());
|
||||
assertNotNull("No errors", errors);
|
||||
assertEquals(1, errors.getErrorCount());
|
||||
}
|
||||
|
||||
public void testFormSubmissionWithValidationError() throws Exception {
|
||||
String successView = "successView";
|
||||
String formView = "formView";
|
||||
|
||||
TestController ctl = new TestController();
|
||||
ctl.setSuccessView(successView);
|
||||
ctl.setFormView(formView);
|
||||
TestValidator val = new TestValidator();
|
||||
ctl.setValidator(val);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
request.setMethod("POST");
|
||||
request.addParameter("name", "Rob Harrop");
|
||||
request.addParameter("age", "23");
|
||||
|
||||
ModelAndView mv = ctl.handleRequest(request, response);
|
||||
assertEquals("Incorrect view name", formView, mv.getViewName());
|
||||
|
||||
Errors errors = (Errors) mv.getModel().get(BindException.MODEL_KEY_PREFIX + ctl.getCommandName());
|
||||
assertNotNull("No errors", errors);
|
||||
assertEquals(1, errors.getErrorCount());
|
||||
assertTrue(val.invoked);
|
||||
}
|
||||
|
||||
public void testCancelSubmission() throws Exception {
|
||||
String cancelView = "cancelView";
|
||||
String cancelParameterKey = "cancelRequest";
|
||||
|
||||
TestController ctl = new TestController();
|
||||
ctl.setCancelParamKey(cancelParameterKey);
|
||||
ctl.setCancelView(cancelView);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
request.setMethod("POST");
|
||||
request.addParameter("cancelRequest", "true");
|
||||
|
||||
ModelAndView mv = ctl.handleRequest(request, response);
|
||||
assertEquals("Incorrect view name", cancelView, mv.getViewName());
|
||||
}
|
||||
|
||||
public void testCancelSubmissionWithValidationError() throws Exception {
|
||||
String cancelView = "cancelView";
|
||||
String cancelParameterKey = "cancelRequest";
|
||||
|
||||
TestController ctl = new TestController();
|
||||
ctl.setCancelParamKey(cancelParameterKey);
|
||||
ctl.setCancelView(cancelView);
|
||||
TestValidator val = new TestValidator();
|
||||
ctl.setValidator(val);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
request.setMethod("POST");
|
||||
request.addParameter("name", "Rob Harrop");
|
||||
request.addParameter("age", "23");
|
||||
request.addParameter("cancelRequest", "true");
|
||||
|
||||
ModelAndView mv = ctl.handleRequest(request, response);
|
||||
assertEquals("Incorrect view name", cancelView, mv.getViewName());
|
||||
|
||||
assertFalse(val.invoked);
|
||||
}
|
||||
|
||||
public void testCancelSubmissionWithCustomModelParams() throws Exception {
|
||||
String cancelView = "cancelView";
|
||||
String cancelParameterKey = "cancelRequest";
|
||||
final String reason = "Because I wanted to";
|
||||
|
||||
TestController ctl = new TestController() {
|
||||
@Override
|
||||
protected ModelAndView onCancel(HttpServletRequest request, HttpServletResponse response, Object command) {
|
||||
return new ModelAndView(getCancelView(), "reason", reason);
|
||||
}
|
||||
};
|
||||
|
||||
ctl.setCancelParamKey(cancelParameterKey);
|
||||
ctl.setCancelView(cancelView);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
request.setMethod("POST");
|
||||
request.addParameter("cancelRequest", "true");
|
||||
|
||||
ModelAndView mv = ctl.handleRequest(request, response);
|
||||
assertEquals("Incorrect view name", cancelView, mv.getViewName());
|
||||
assertEquals("Model parameter reason not correct", reason, mv.getModel().get("reason"));
|
||||
}
|
||||
|
||||
private void testCommandIsBound(TestBean command, String name, int age) {
|
||||
assertNotNull("Command bean should not be null", command);
|
||||
assertEquals("Name not bound", name, command.getName());
|
||||
assertEquals("Age not bound", age, command.getAge());
|
||||
}
|
||||
|
||||
|
||||
private static class TestController extends CancellableFormController {
|
||||
|
||||
public TestController() {
|
||||
setCommandClass(TestBean.class);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TestValidator implements Validator {
|
||||
|
||||
private boolean invoked = false;
|
||||
|
||||
@Override
|
||||
public boolean supports(Class clazz) {
|
||||
return TestBean.class.isAssignableFrom(clazz);
|
||||
}
|
||||
@Override
|
||||
public void validate(Object target, Errors errors) {
|
||||
this.invoked = true;
|
||||
TestBean tb = (TestBean) target;
|
||||
if (tb.getAge() < 25) {
|
||||
errors.rejectValue("age", "TOO_YOUNG");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,522 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.web.servlet.mvc;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.beans.propertyeditors.CustomDateEditor;
|
||||
import org.springframework.beans.propertyeditors.CustomNumberEditor;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockHttpSession;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.ServletRequestDataBinder;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class CommandControllerTests extends TestCase {
|
||||
|
||||
public void testNoArgsNoErrors() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
assertTrue("returned correct view name", mv.getViewName().equals(request.getServletPath()));
|
||||
TestBean person = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("command and errors non null", person != null && errors != null);
|
||||
assertTrue("no errors", !errors.hasErrors());
|
||||
assertTrue("Correct caching", response.getHeader("Cache-Control") == null);
|
||||
assertTrue("Correct expires header", response.getHeader("Expires") == null);
|
||||
}
|
||||
|
||||
public void test2ArgsNoErrors() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
String name = "Rod";
|
||||
int age = 32;
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", "" + age);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
assertTrue("returned correct view name", mv.getViewName().equals(request.getServletPath()));
|
||||
TestBean person = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("command and errors non null", person != null && errors != null);
|
||||
assertTrue("no errors", !errors.hasErrors());
|
||||
assertTrue("command name bound ok", person.getName().equals(name));
|
||||
assertTrue("command age bound ok", person.getAge() == age);
|
||||
}
|
||||
|
||||
public void test2Args1Mismatch() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
String name = "Rod";
|
||||
String age = "32x";
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", age);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
assertTrue("returned correct view name", mv.getViewName().equals(request.getServletPath()));
|
||||
TestBean person = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("command and errors non null", person != null && errors != null);
|
||||
assertTrue("has 1 errors", errors.getErrorCount() == 1);
|
||||
assertTrue("command name bound ok", person.getName().equals(name));
|
||||
assertTrue("command age default", person.getAge() == new TestBean().getAge());
|
||||
assertTrue("has error on field age", errors.hasFieldErrors("age"));
|
||||
FieldError fe = errors.getFieldError("age");
|
||||
assertTrue("Saved invalid value", fe.getRejectedValue().equals(age));
|
||||
assertTrue("Correct field", fe.getField().equals("age"));
|
||||
}
|
||||
|
||||
public void testSupportedMethods() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
mc.setSupportedMethods(new String[] {"POST"});
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
try {
|
||||
mc.handleRequest(request, response);
|
||||
fail("Should have thrown ServletException");
|
||||
}
|
||||
catch (ServletException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testRequireSessionWithoutSession() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
mc.setRequireSession(true);
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
try {
|
||||
mc.handleRequest(request, response);
|
||||
fail("Should have thrown ServletException");
|
||||
}
|
||||
catch (ServletException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testRequireSessionWithSession() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
mc.setRequireSession(true);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
request.setSession(new MockHttpSession(null));
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
}
|
||||
|
||||
public void testNoCaching() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
mc.setCacheSeconds(0);
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("Correct expires header", response.getHeader("Expires").equals("1"));
|
||||
List cacheControl = response.getHeaders("Cache-Control");
|
||||
assertTrue("Correct cache control", cacheControl.contains("no-cache"));
|
||||
assertTrue("Correct cache control", cacheControl.contains("no-store"));
|
||||
}
|
||||
|
||||
public void testNoCachingWithoutExpires() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
mc.setCacheSeconds(0);
|
||||
mc.setUseExpiresHeader(false);
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("No expires header", response.getHeader("Expires") == null);
|
||||
List cacheControl = response.getHeaders("Cache-Control");
|
||||
assertTrue("Correct cache control", cacheControl.contains("no-cache"));
|
||||
assertTrue("Correct cache control", cacheControl.contains("no-store"));
|
||||
}
|
||||
|
||||
public void testNoCachingWithoutCacheControl() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
mc.setCacheSeconds(0);
|
||||
mc.setUseCacheControlHeader(false);
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("Correct expires header", response.getHeader("Expires").equals("1"));
|
||||
assertTrue("No cache control", response.getHeader("Cache-Control") == null);
|
||||
}
|
||||
|
||||
public void testCaching() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
mc.setCacheSeconds(10);
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("Correct expires header", response.getHeader("Expires") != null);
|
||||
assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=10"));
|
||||
}
|
||||
|
||||
public void testCachingWithoutExpires() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
mc.setCacheSeconds(10);
|
||||
mc.setUseExpiresHeader(false);
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("No expires header", response.getHeader("Expires") == null);
|
||||
assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=10"));
|
||||
}
|
||||
|
||||
public void testCachingWithoutCacheControl() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
mc.setCacheSeconds(10);
|
||||
mc.setUseCacheControlHeader(false);
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("Correct expires header", response.getHeader("Expires") != null);
|
||||
assertTrue("No cache control", response.getHeader("Cache-Control") == null);
|
||||
}
|
||||
|
||||
public void testCachingWithLastModified() throws Exception {
|
||||
class LastModifiedTestController extends TestController implements LastModified {
|
||||
@Override
|
||||
public long getLastModified(HttpServletRequest request) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
LastModifiedTestController mc = new LastModifiedTestController();
|
||||
mc.setCacheSeconds(10);
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("Correct expires header", response.getHeader("Expires") != null);
|
||||
assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=10, must-revalidate"));
|
||||
}
|
||||
|
||||
public void testCachingWithCustomCacheForSecondsCall() throws Exception {
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
|
||||
cacheForSeconds(response, 5);
|
||||
return super.handle(request, response, command, errors);
|
||||
}
|
||||
};
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("Correct expires header", response.getHeader("Expires") != null);
|
||||
assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=5"));
|
||||
}
|
||||
|
||||
public void testCachingWithCustomApplyCacheSecondsCall1() throws Exception {
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
|
||||
applyCacheSeconds(response, 5);
|
||||
return super.handle(request, response, command, errors);
|
||||
}
|
||||
};
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("Correct expires header", response.getHeader("Expires") != null);
|
||||
assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=5"));
|
||||
}
|
||||
|
||||
public void testCachingWithCustomApplyCacheSecondsCall2() throws Exception {
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
|
||||
applyCacheSeconds(response, 0);
|
||||
return super.handle(request, response, command, errors);
|
||||
}
|
||||
};
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("Correct expires header", response.getHeader("Expires").equals("1"));
|
||||
List cacheControl = response.getHeaders("Cache-Control");
|
||||
assertTrue("Correct cache control", cacheControl.contains("no-cache"));
|
||||
assertTrue("Correct cache control", cacheControl.contains("no-store"));
|
||||
}
|
||||
|
||||
public void testCachingWithCustomApplyCacheSecondsCall3() throws Exception {
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
|
||||
applyCacheSeconds(response, -1);
|
||||
return super.handle(request, response, command, errors);
|
||||
}
|
||||
};
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
mc.handleRequest(request, response);
|
||||
assertTrue("No expires header", response.getHeader("Expires") == null);
|
||||
assertTrue("No cache control", response.getHeader("Cache-Control") == null);
|
||||
}
|
||||
|
||||
public void testCustomDateEditorWithAllowEmpty() throws Exception {
|
||||
final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN);
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
|
||||
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
|
||||
}
|
||||
};
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("date", "1.5.2003");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
TestBean tb = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("No field error", !errors.hasFieldErrors("date"));
|
||||
assertTrue("Correct date property", df.parse("1.5.2003").equals(tb.getDate()));
|
||||
assertTrue("Correct date value", "01.05.2003".equals(errors.getFieldValue("date")));
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("date", "");
|
||||
response = new MockHttpServletResponse();
|
||||
mv = mc.handleRequest(request, response);
|
||||
tb = (TestBean) mv.getModel().get("command");
|
||||
errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("No field error", !errors.hasFieldErrors("date"));
|
||||
assertTrue("Correct date property", tb.getDate() == null);
|
||||
assertTrue("Correct date value", "".equals(errors.getFieldValue("date")));
|
||||
}
|
||||
|
||||
public void testCustomDateEditorWithoutAllowEmpty() throws Exception {
|
||||
final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN);
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
|
||||
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
|
||||
}
|
||||
};
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("date", "1.5.2003");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
TestBean tb = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("No field error", !errors.hasFieldErrors("date"));
|
||||
assertTrue("Correct date property", df.parse("1.5.2003").equals(tb.getDate()));
|
||||
assertTrue("Correct date value", "01.05.2003".equals(errors.getFieldValue("date")));
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("date", "");
|
||||
response = new MockHttpServletResponse();
|
||||
mv = mc.handleRequest(request, response);
|
||||
tb = (TestBean) mv.getModel().get("command");
|
||||
errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("Has field error", errors.hasFieldErrors("date"));
|
||||
assertTrue("Correct date property", tb.getDate() != null);
|
||||
assertTrue("Correct date value", errors.getFieldValue("date") != null);
|
||||
}
|
||||
|
||||
public void testCustomNumberEditorWithAllowEmpty() throws Exception {
|
||||
final NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
|
||||
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
|
||||
binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
|
||||
}
|
||||
};
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("myFloat", "5,1");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
TestBean tb = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("No field error", !errors.hasFieldErrors("myFloat"));
|
||||
assertTrue("Correct float property", (new Float(5.1)).equals(tb.getMyFloat()));
|
||||
assertTrue("Correct float value", "5,1".equals(errors.getFieldValue("myFloat")));
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("myFloat", "");
|
||||
response = new MockHttpServletResponse();
|
||||
mv = mc.handleRequest(request, response);
|
||||
tb = (TestBean) mv.getModel().get("command");
|
||||
errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("No field error", !errors.hasFieldErrors("myFloat"));
|
||||
assertTrue("Correct float property", tb.getMyFloat() == null);
|
||||
assertTrue("Correct float value", "".equals(errors.getFieldValue("myFloat")));
|
||||
}
|
||||
|
||||
public void testCustomNumberEditorWithoutAllowEmpty() throws Exception {
|
||||
final NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
|
||||
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
|
||||
binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, false));
|
||||
}
|
||||
};
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("myFloat", "5,1");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
TestBean tb = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("No field error", !errors.hasFieldErrors("myFloat"));
|
||||
assertTrue("Correct float property", (new Float(5.1)).equals(tb.getMyFloat()));
|
||||
assertTrue("Correct float value", "5,1".equals(errors.getFieldValue("myFloat")));
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("myFloat", "");
|
||||
response = new MockHttpServletResponse();
|
||||
mv = mc.handleRequest(request, response);
|
||||
tb = (TestBean) mv.getModel().get("command");
|
||||
errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("Has field error", errors.hasFieldErrors("myFloat"));
|
||||
assertTrue("Correct float property", tb.getMyFloat() != null);
|
||||
assertTrue("Correct float value", errors.getFieldValue("myFloat") != null);
|
||||
}
|
||||
|
||||
public void testResetEmptyStringField() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("_name", "visible");
|
||||
request.addParameter("name", "test");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
TestBean tb = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("Correct name property", "test".equals(tb.getName()));
|
||||
assertTrue("Correct name value", "test".equals(errors.getFieldValue("name")));
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("_name", "visible");
|
||||
request.addParameter("_someNonExistingField", "visible");
|
||||
mv = mc.handleRequest(request, response);
|
||||
tb = (TestBean) mv.getModel().get("command");
|
||||
errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("Correct name property", tb.getName() == null);
|
||||
assertTrue("Correct name value", errors.getFieldValue("name") == null);
|
||||
}
|
||||
|
||||
public void testResetEmptyBooleanField() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("_postProcessed", "visible");
|
||||
request.addParameter("postProcessed", "true");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
TestBean tb = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("Correct postProcessed property", tb.isPostProcessed());
|
||||
assertTrue("Correct postProcessed value", Boolean.TRUE.equals(errors.getFieldValue("postProcessed")));
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("_postProcessed", "visible");
|
||||
mv = mc.handleRequest(request, response);
|
||||
tb = (TestBean) mv.getModel().get("command");
|
||||
errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("Correct postProcessed property", !tb.isPostProcessed());
|
||||
assertTrue("Correct postProcessed value", Boolean.FALSE.equals(errors.getFieldValue("postProcessed")));
|
||||
}
|
||||
|
||||
public void testResetEmptyStringArrayField() throws Exception {
|
||||
TestController mc = new TestController();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("_stringArray", "visible");
|
||||
request.addParameter("stringArray", "value1");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
TestBean tb = (TestBean) mv.getModel().get("command");
|
||||
assertTrue("Correct stringArray property",
|
||||
tb.getStringArray() != null && "value1".equals(tb.getStringArray()[0]));
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("_stringArray", "visible");
|
||||
mv = mc.handleRequest(request, response);
|
||||
tb = (TestBean) mv.getModel().get("command");
|
||||
assertTrue("Correct stringArray property", tb.getStringArray() != null && tb.getStringArray().length == 0);
|
||||
}
|
||||
|
||||
public void testResetEmptyFieldsTurnedOff() throws Exception {
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected Object getCommand(HttpServletRequest request) throws Exception {
|
||||
return new TestBean("original", 99);
|
||||
}
|
||||
@Override
|
||||
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
|
||||
binder.setFieldMarkerPrefix(null);
|
||||
}
|
||||
};
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("_name", "visible");
|
||||
request.addParameter("name", "test");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
TestBean tb = (TestBean) mv.getModel().get("command");
|
||||
Errors errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("Correct name property", "test".equals(tb.getName()));
|
||||
assertTrue("Correct name value", "test".equals(errors.getFieldValue("name")));
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("_name", "true");
|
||||
mv = mc.handleRequest(request, response);
|
||||
tb = (TestBean) mv.getModel().get("command");
|
||||
errors = (Errors) mv.getModel().get("errors");
|
||||
assertTrue("Correct name property", "original".equals(tb.getName()));
|
||||
assertTrue("Correct name value", "original".equals(errors.getFieldValue("name")));
|
||||
}
|
||||
|
||||
|
||||
private static class TestController extends AbstractCommandController {
|
||||
|
||||
private TestController() {
|
||||
super(TestBean.class, "person");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
|
||||
Map m = new HashMap();
|
||||
assertTrue("Command not null", command != null);
|
||||
assertTrue("errors not null", errors != null);
|
||||
m.put("errors", errors);
|
||||
m.put("command", command);
|
||||
return new ModelAndView(request.getServletPath(), m);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,661 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.web.servlet.mvc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.tests.sample.beans.IndexedTestBean;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@Deprecated
|
||||
public class FormControllerTests extends TestCase {
|
||||
|
||||
public void testReferenceDataOnForm() throws Exception {
|
||||
String formView = "f";
|
||||
String successView = "s";
|
||||
|
||||
RefController mc = new RefController();
|
||||
mc.setFormView(formView);
|
||||
mc.setCommandName("tb");
|
||||
mc.setSuccessView(successView);
|
||||
mc.refDataCount = 0;
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
assertTrue("returned correct view name", mv.getViewName().equals(formView));
|
||||
|
||||
assertTrue("refDataCount == 1", mc.refDataCount == 1);
|
||||
|
||||
TestBean person = (TestBean) mv.getModel().get(mc.getCommandName());
|
||||
int[] numbers = (int[]) mv.getModel().get(mc.NUMBERS_ATT);
|
||||
assertTrue("model is non null", person != null);
|
||||
assertTrue("numbers is non null", numbers != null);
|
||||
}
|
||||
|
||||
public void testReferenceDataOnResubmit() throws Exception {
|
||||
String formView = "f";
|
||||
String successView = "s";
|
||||
|
||||
RefController mc = new RefController();
|
||||
mc.setFormView(formView);
|
||||
mc.setCommandName("tb");
|
||||
mc.setSuccessView(successView);
|
||||
mc.refDataCount = 0;
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/welcome.html");
|
||||
request.addParameter("age", "23x");
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
assertTrue("returned correct view name", mv.getViewName().equals(formView));
|
||||
assertTrue("has errors", mv.getModel().get(BindException.MODEL_KEY_PREFIX + mc.getCommandName()) != null);
|
||||
|
||||
assertTrue("refDataCount == 1", mc.refDataCount == 1);
|
||||
|
||||
TestBean person = (TestBean) mv.getModel().get(mc.getCommandName());
|
||||
int[] numbers = (int[]) mv.getModel().get(mc.NUMBERS_ATT);
|
||||
assertTrue("model is non null", person != null);
|
||||
assertTrue("numbers is non null", numbers != null);
|
||||
}
|
||||
|
||||
public void testForm() throws Exception {
|
||||
String formView = "f";
|
||||
String successView = "s";
|
||||
|
||||
TestController mc = new TestController();
|
||||
mc.setFormView(formView);
|
||||
mc.setSuccessView(successView);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("name", "rod");
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
assertTrue("returned correct view name", mv.getViewName().equals(formView));
|
||||
|
||||
TestBean person = (TestBean) mv.getModel().get(TestController.BEAN_NAME);
|
||||
assertTrue("model is non null", person != null);
|
||||
assertTrue("bean age default ok", person.getAge() == TestController.DEFAULT_AGE);
|
||||
assertTrue("name not set", person.getName() == null);
|
||||
}
|
||||
|
||||
public void testBindOnNewForm() throws Exception {
|
||||
String formView = "f";
|
||||
String successView = "s";
|
||||
final Integer someNumber = new Integer(12);
|
||||
|
||||
TestController mc = new TestController() {
|
||||
@Override
|
||||
protected void onBindOnNewForm(HttpServletRequest request, Object command) throws Exception {
|
||||
TestBean testBean = (TestBean)command;
|
||||
testBean.setSomeNumber(new Integer(12));
|
||||
}
|
||||
};
|
||||
mc.setFormView(formView);
|
||||
mc.setSuccessView(successView);
|
||||
mc.setBindOnNewForm(true);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html");
|
||||
request.addParameter("name", "rod");
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
assertEquals("returned correct view name", formView, mv.getViewName());
|
||||
|
||||
TestBean person = (TestBean) mv.getModel().get(TestController.BEAN_NAME);
|
||||
assertNotNull("model is non null", person);
|
||||
assertEquals("bean age default ok", person.getAge(), TestController.DEFAULT_AGE);
|
||||
assertEquals("name set", "rod", person.getName());
|
||||
assertEquals("Property [someNumber] not set in onBindOnNewForm callback", someNumber, person.getSomeNumber());
|
||||
}
|
||||
|
||||
public void testSubmitWithoutErrors() throws Exception {
|
||||
String formView = "f";
|
||||
String successView = "s";
|
||||
|
||||
TestController mc = new TestController();
|
||||
mc.setFormView(formView);
|
||||
mc.setSuccessView(successView);
|
||||
|
||||
String name = "Rod";
|
||||
int age = 32;
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/welcome.html");
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", "" + age);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
|
||||
assertEquals("returned correct view name", successView, mv.getViewName());
|
||||
TestBean person = (TestBean) mv.getModel().get(TestController.BEAN_NAME);
|
||||
Errors errors = (Errors) mv.getModel().get(BindException.MODEL_KEY_PREFIX + TestController.BEAN_NAME);
|
||||
assertTrue("model is non null", person != null);
|
||||
assertTrue("errors is non null", errors != null);
|
||||
assertTrue("bean name bound ok", person.getName().equals(name));
|
||||
assertTrue("bean age bound ok", person.getAge() == age);
|
||||
}
|
||||
|
||||
public void testSubmitWithoutValidation() throws Exception {
|
||||
String formView = "f";
|
||||
String successView = "s";
|
||||
|
||||
TestController mc = new TestController();
|
||||
mc.setFormView(formView);
|
||||
mc.setSuccessView(successView);
|
||||
|
||||
String name = "Rod";
|
||||
int age = 32;
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/welcome.html");
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", "" + age);
|
||||
request.addParameter("formChange", "true");
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
|
||||
assertEquals("returned correct view name", formView, mv.getViewName());
|
||||
TestBean person = (TestBean) mv.getModel().get(TestController.BEAN_NAME);
|
||||
Errors errors = (Errors) mv.getModel().get(BindException.MODEL_KEY_PREFIX + TestController.BEAN_NAME);
|
||||
assertTrue("model is non null", person != null);
|
||||
assertTrue("errors is non null", errors != null);
|
||||
assertTrue("bean name bound ok", person.getName().equals(name));
|
||||
assertTrue("bean age bound ok", person.getAge() == age);
|
||||
}
|
||||
|
||||
public void testSubmitWithCustomOnSubmit() throws Exception {
|
||||
String formView = "f";
|
||||
|
||||
TestControllerWithCustomOnSubmit mc = new TestControllerWithCustomOnSubmit();
|
||||
mc.setFormView(formView);
|
||||
|
||||
String name = "Rod";
|
||||
int age = 32;
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/welcome.html");
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", "" + age);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
assertEquals("returned correct view name", "mySuccess", mv.getViewName());
|
||||
assertTrue("no model", mv.getModel().isEmpty());
|
||||
}
|
||||
|
||||
public void testSubmitPassedByValidator() throws Exception {
|
||||
String formView = "f";
|
||||
String successView = "s";
|
||||
|
||||
TestController mc = new TestController();
|
||||
mc.setFormView(formView);
|
||||
mc.setSuccessView(successView);
|
||||
mc.setValidator(new TestValidator());
|
||||
|
||||
String name = "Roderick Johnson";
|
||||
int age = 32;
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/welcome.html");
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", "" + age);
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = mc.handleRequest(request, response);
|
||||
assertTrue("returned correct view name: expected '" + successView + "', not '" + mv.getViewName() + "'",
|
||||
mv.getViewName().equals(successView));
|
||||
|
||||
TestBean person = (TestBean) mv.getModel().get(TestController.BEAN_NAME);
|
||||
assertTrue("model is non null", person != null);
|
||||
assertTrue("bean name bound ok", person.getName().equals(name));
|
||||
assertTrue("bean age bound ok", person.getAge() == age);
|
||||
}
|
||||
|
||||
public void testSubmit1Mismatch() throws Exception {
|
||||
String formView = "fred";
|
||||
String successView = "tony";
|
||||
|
||||
TestController mc = new TestController();
|
||||
mc.setFormView(formView);
|
||||
mc.setSuccessView(successView);
|
||||
|
||||
String name = "Rod";
|
||||
String age = "xxx";
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo.html");
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", "" + age);
|
||||
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(name));
|
||||
assertTrue("bean age is default", person.getAge() == TestController.DEFAULT_AGE);
|
||||
Errors errors = (Errors) mv.getModel().get(BindException.MODEL_KEY_PREFIX + mc.getCommandName());
|
||||
assertTrue("errors returned in model", errors != null);
|
||||
assertTrue("One error", errors.getErrorCount() == 1);
|
||||
FieldError fe = errors.getFieldError("age");
|
||||
assertTrue("Saved invalid value", fe.getRejectedValue().equals(age));
|
||||
assertTrue("Correct field", fe.getField().equals("age"));
|
||||
}
|
||||
|
||||
public void testSubmit1Mismatch1Invalidated() throws Exception {
|
||||
String formView = "fred";
|
||||
String successView = "tony";
|
||||
|
||||
TestController mc = new TestController();
|
||||
mc.setFormView(formView);
|
||||
mc.setSuccessView(successView);
|
||||
mc.setValidators(new Validator[] {new TestValidator(), new TestValidator2()});
|
||||
|
||||
String name = "Rod";
|
||||
// will be rejected
|
||||
String age = "xxx";
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo.html");
|
||||
request.addParameter("name", name);
|
||||
request.addParameter("age", "" + age);
|
||||
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(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.MODEL_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() {
|
||||
@Override
|
||||
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.MODEL_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() {
|
||||
@Override
|
||||
protected boolean isFormChangeRequest(HttpServletRequest request) {
|
||||
return (request.getParameter("formChange") != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
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.MODEL_KEY_PREFIX + mc.getCommandName());
|
||||
assertTrue("errors returned in model", errors != null);
|
||||
assertTrue("No errors", errors.getErrorCount() == 0);
|
||||
}
|
||||
|
||||
public void testFormBindingOfNestedBooleans() throws Exception {
|
||||
BooleanBindingFormController controller = new BooleanBindingFormController();
|
||||
controller.setCommandClass(ListForm.class);
|
||||
MockHttpServletRequest req = new MockHttpServletRequest("POST", "/myurl");
|
||||
MockHttpServletResponse res = new MockHttpServletResponse();
|
||||
req.addParameter("oks[0].ok", "true");
|
||||
ModelAndView mav = controller.handleRequest(req, res);
|
||||
ListForm form = (ListForm) mav.getModelMap().get("command");
|
||||
Boolean ok = ((Ok) form.getOks().get(0)).getOk();
|
||||
assertNotNull(ok);
|
||||
}
|
||||
|
||||
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";
|
||||
|
||||
@Override
|
||||
public boolean supports(Class clazz) { return true; }
|
||||
|
||||
@Override
|
||||
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";
|
||||
|
||||
@Override
|
||||
public boolean supports(Class clazz) { return true; }
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
|
||||
TestBean person = new TestBean();
|
||||
person.setAge(DEFAULT_AGE);
|
||||
return person;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isFormChangeRequest(HttpServletRequest request) {
|
||||
return (request.getParameter("formChange") != null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TestControllerWithCustomOnSubmit extends TestController {
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map referenceData(HttpServletRequest request) {
|
||||
++refDataCount;
|
||||
Map m = new HashMap();
|
||||
m.put(NUMBERS_ATT, NUMBERS);
|
||||
return m;
|
||||
}
|
||||
|
||||
public void invokeWebSpecificStuff() {
|
||||
getTempDir();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class BooleanBindingFormController extends AbstractFormController {
|
||||
|
||||
@Override
|
||||
protected ModelAndView processFormSubmission
|
||||
(HttpServletRequest req, HttpServletResponse resp, Object command, BindException errors) throws Exception {
|
||||
ModelAndView mav = new ModelAndView();
|
||||
mav.addObject("command", command);
|
||||
return mav;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ModelAndView showForm(
|
||||
HttpServletRequest req, HttpServletResponse resp, BindException err) throws Exception {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class Ok {
|
||||
|
||||
private Boolean ok;
|
||||
|
||||
public Boolean getOk () {
|
||||
return ok;
|
||||
}
|
||||
|
||||
public void setOk(Boolean ok) {
|
||||
this.ok = ok;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ListForm {
|
||||
|
||||
private List oks = new ArrayList();
|
||||
|
||||
public ListForm () {
|
||||
for (int index = 0; index < 5; index++) {
|
||||
Ok ok = new Ok();
|
||||
oks.add(ok);
|
||||
}
|
||||
}
|
||||
|
||||
public List getOks() {
|
||||
return oks;
|
||||
}
|
||||
|
||||
public void setOks(List oks) {
|
||||
this.oks = oks;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,438 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.web.servlet.mvc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 29.04.2003
|
||||
*/
|
||||
@Deprecated
|
||||
public class WizardFormControllerTests extends TestCase {
|
||||
|
||||
public void testNoDirtyPageChange() throws Exception {
|
||||
AbstractWizardFormController wizard = new TestWizardController();
|
||||
wizard.setAllowDirtyBack(false);
|
||||
wizard.setAllowDirtyForward(false);
|
||||
wizard.setPageAttribute("currentPage");
|
||||
|
||||
assertTrue(wizard.getFormSessionAttributeName() != wizard.getPageSessionAttributeName());
|
||||
HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
|
||||
|
||||
Properties params = new Properties();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, null, 0, null, 0, "currentPage");
|
||||
// not allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("name", "myname");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_PAGE, "0");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
|
||||
// name set -> now allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("name", "myname");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
|
||||
// name set -> now allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("name", "myname");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1.x", "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
|
||||
// name set -> now allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("name", "myname");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1.y", "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
|
||||
// name set -> now allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("date", "not a date");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1.y", "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
|
||||
// name set -> now allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
|
||||
// not allowed to go to 0
|
||||
|
||||
params.clear();
|
||||
params.setProperty("age", "32");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_PAGE, "1");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
|
||||
performRequest(wizard, session, params, 0, "myname", 32, "currentPage");
|
||||
// age set -> now allowed to go to 0
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
|
||||
performRequest(wizard, session, params, -1, "myname", 32, null);
|
||||
}
|
||||
|
||||
public void testCustomSessionAttributes() throws Exception {
|
||||
AbstractWizardFormController wizard = new TestWizardController() {
|
||||
@Override
|
||||
protected String getFormSessionAttributeName() {
|
||||
return "myFormAttr";
|
||||
}
|
||||
@Override
|
||||
protected String getPageSessionAttributeName() {
|
||||
return "myPageAttr";
|
||||
}
|
||||
};
|
||||
wizard.setAllowDirtyBack(false);
|
||||
wizard.setAllowDirtyForward(false);
|
||||
wizard.setPageAttribute("currentPage");
|
||||
|
||||
HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
|
||||
assertTrue(session.getAttribute("myFormAttr") instanceof TestBean);
|
||||
assertEquals(new Integer(0), session.getAttribute("myPageAttr"));
|
||||
|
||||
Properties params = new Properties();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, null, 0, null, 0, "currentPage");
|
||||
// not allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("name", "myname");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_PAGE, "0");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
|
||||
// name set -> now allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("age", "32");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
|
||||
performRequest(wizard, session, params, -1, "myname", 32, "currentPage");
|
||||
}
|
||||
|
||||
public void testCustomRequestDependentSessionAttributes() throws Exception {
|
||||
AbstractWizardFormController wizard = new TestWizardController() {
|
||||
@Override
|
||||
protected String getFormSessionAttributeName(HttpServletRequest request) {
|
||||
return "myFormAttr" + request.getParameter("formAttr");
|
||||
}
|
||||
@Override
|
||||
protected String getPageSessionAttributeName(HttpServletRequest request) {
|
||||
return "myPageAttr" + request.getParameter("pageAttr");
|
||||
}
|
||||
};
|
||||
wizard.setAllowDirtyBack(false);
|
||||
wizard.setAllowDirtyForward(false);
|
||||
wizard.setPageAttribute("currentPage");
|
||||
|
||||
HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
|
||||
assertTrue(session.getAttribute("myFormAttr1") instanceof TestBean);
|
||||
assertEquals(new Integer(0), session.getAttribute("myPageAttr2"));
|
||||
|
||||
Properties params = new Properties();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, null, 0, null, 0, "currentPage");
|
||||
// not allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("name", "myname");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_PAGE, "0");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
|
||||
// name set -> now allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("age", "32");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
|
||||
performRequest(wizard, session, params, -1, "myname", 32, "currentPage");
|
||||
}
|
||||
|
||||
public void testDirtyBack() throws Exception {
|
||||
AbstractWizardFormController wizard = new TestWizardController();
|
||||
wizard.setAllowDirtyBack(true);
|
||||
wizard.setAllowDirtyForward(false);
|
||||
HttpSession session = performRequest(wizard, null, null, 0, null, 0, null);
|
||||
|
||||
Properties params = new Properties();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, params, 0, null, 0, null);
|
||||
// not allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty("name", "myname");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, null);
|
||||
// name set -> now allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
|
||||
performRequest(wizard, session, params, 0, "myname", 0, null);
|
||||
// dirty back -> allowed to go to 0
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, null);
|
||||
// finish while dirty -> show dirty page (1)
|
||||
|
||||
params.clear();
|
||||
params.setProperty("age", "32");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
|
||||
performRequest(wizard, session, params, -1, "myname", 32, null);
|
||||
// age set -> now allowed to finish
|
||||
}
|
||||
|
||||
public void testDirtyForward() throws Exception {
|
||||
AbstractWizardFormController wizard = new TestWizardController();
|
||||
wizard.setAllowDirtyBack(false);
|
||||
wizard.setAllowDirtyForward(true);
|
||||
HttpSession session = performRequest(wizard, null, null, 0, null, 0, null);
|
||||
|
||||
Properties params = new Properties();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, params, 1, null, 0, null);
|
||||
// dirty forward -> allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
|
||||
performRequest(wizard, session, params, 1, null, 0, null);
|
||||
// not allowed to go to 0
|
||||
|
||||
params.clear();
|
||||
params.setProperty("age", "32");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
|
||||
performRequest(wizard, session, params, 0, null, 32, null);
|
||||
// age set -> now allowed to go to 0
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
|
||||
performRequest(wizard, session, params, 0, null, 32, null);
|
||||
// finish while dirty -> show dirty page (0)
|
||||
|
||||
params.clear();
|
||||
params.setProperty("name", "myname");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH + ".x", "value");
|
||||
performRequest(wizard, session, params, -1, "myname", 32, null);
|
||||
// name set -> now allowed to finish
|
||||
}
|
||||
|
||||
public void testSubmitWithoutValidation() throws Exception {
|
||||
AbstractWizardFormController wizard = new TestWizardController();
|
||||
wizard.setAllowDirtyBack(false);
|
||||
wizard.setAllowDirtyForward(false);
|
||||
HttpSession session = performRequest(wizard, null, null, 0, null, 0, null);
|
||||
|
||||
Properties params = new Properties();
|
||||
params.setProperty("formChange", "true");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, params, 1, null, 0, null);
|
||||
// no validation -> allowed to go to 1
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
|
||||
performRequest(wizard, session, params, 1, null, 0, null);
|
||||
// not allowed to go to 0
|
||||
|
||||
params.clear();
|
||||
params.setProperty("age", "32");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
|
||||
performRequest(wizard, session, params, 0, null, 32, null);
|
||||
// age set -> now allowed to go to 0
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
|
||||
performRequest(wizard, session, params, 0, null, 32, null);
|
||||
// finish while dirty -> show dirty page (0)
|
||||
|
||||
params.clear();
|
||||
params.setProperty("name", "myname");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH + ".x", "value");
|
||||
performRequest(wizard, session, params, -1, "myname", 32, null);
|
||||
// name set -> now allowed to finish
|
||||
}
|
||||
|
||||
public void testCancel() throws Exception {
|
||||
AbstractWizardFormController wizard = new TestWizardController();
|
||||
HttpSession session = performRequest(wizard, null, null, 0, null, 0, null);
|
||||
Properties params = new Properties();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_CANCEL, "value");
|
||||
performRequest(wizard, session, params, -2, null, 0, null);
|
||||
|
||||
assertTrue(session.getAttribute(wizard.getFormSessionAttributeName()) == null);
|
||||
assertTrue(session.getAttribute(wizard.getPageSessionAttributeName()) == null);
|
||||
|
||||
session = performRequest(wizard, null, null, 0, null, 0, null);
|
||||
params = new Properties();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_CANCEL + ".y", "value");
|
||||
performRequest(wizard, session, params, -2, null, 0, null);
|
||||
}
|
||||
|
||||
public void testInvalidSubmit() throws Exception {
|
||||
AbstractWizardFormController wizard = new TestWizardController();
|
||||
wizard.setAllowDirtyBack(false);
|
||||
wizard.setAllowDirtyForward(false);
|
||||
wizard.setPageAttribute("currentPage");
|
||||
HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
|
||||
|
||||
Properties params = new Properties();
|
||||
params.setProperty("name", "myname");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
|
||||
performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
|
||||
|
||||
params.clear();
|
||||
params.setProperty("age", "32");
|
||||
params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
|
||||
performRequest(wizard, session, params, 0, "myname", 32, "currentPage");
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
|
||||
performRequest(wizard, session, params, -1, "myname", 32, null);
|
||||
|
||||
params.clear();
|
||||
params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
|
||||
performRequest(wizard, session, params, 0, null, 0, "currentPage");
|
||||
// returned to initial page of new wizard form
|
||||
}
|
||||
|
||||
private HttpSession performRequest(
|
||||
AbstractWizardFormController wizard, HttpSession session, Properties params,
|
||||
int target, String name, int age, String pageAttr) throws Exception {
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest((params != null ? "POST" : "GET"), "/wizard");
|
||||
request.addParameter("formAttr", "1");
|
||||
request.addParameter("pageAttr", "2");
|
||||
if (params != null) {
|
||||
for (Iterator it = params.keySet().iterator(); it.hasNext();) {
|
||||
String param = (String) it.next();
|
||||
request.addParameter(param, params.getProperty(param));
|
||||
}
|
||||
}
|
||||
request.setSession(session);
|
||||
request.setAttribute("target", new Integer(target));
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
ModelAndView mv = wizard.handleRequest(request, response);
|
||||
if (target >= 0) {
|
||||
assertTrue("Page " + target + " returned", ("page" + target).equals(mv.getViewName()));
|
||||
if (pageAttr != null) {
|
||||
assertTrue("Page attribute set", (new Integer(target)).equals(mv.getModel().get(pageAttr)));
|
||||
assertTrue("Correct model size", mv.getModel().size() == 3);
|
||||
}
|
||||
else {
|
||||
assertTrue("Correct model size", mv.getModel().size() == 2);
|
||||
}
|
||||
assertTrue(
|
||||
request.getSession().getAttribute(wizard.getFormSessionAttributeName(request)) instanceof TestBean);
|
||||
assertEquals(new Integer(target),
|
||||
request.getSession().getAttribute(wizard.getPageSessionAttributeName(request)));
|
||||
}
|
||||
else if (target == -1) {
|
||||
assertTrue("Success target returned", "success".equals(mv.getViewName()));
|
||||
assertTrue("Correct model size", mv.getModel().size() == 1);
|
||||
assertTrue(request.getSession().getAttribute(wizard.getFormSessionAttributeName(request)) == null);
|
||||
assertTrue(request.getSession().getAttribute(wizard.getPageSessionAttributeName(request)) == null);
|
||||
}
|
||||
else if (target == -2) {
|
||||
assertTrue("Cancel view returned", "cancel".equals(mv.getViewName()));
|
||||
assertTrue("Correct model size", mv.getModel().size() == 1);
|
||||
assertTrue(request.getSession().getAttribute(wizard.getFormSessionAttributeName(request)) == null);
|
||||
assertTrue(request.getSession().getAttribute(wizard.getPageSessionAttributeName(request)) == null);
|
||||
}
|
||||
TestBean tb = (TestBean) mv.getModel().get("tb");
|
||||
assertTrue("Has model", tb != null);
|
||||
assertTrue("Name is " + name, ObjectUtils.nullSafeEquals(name, tb.getName()));
|
||||
assertTrue("Age is " + age, tb.getAge() == age);
|
||||
Errors errors = (Errors) mv.getModel().get(BindException.MODEL_KEY_PREFIX + "tb");
|
||||
if (params != null && params.containsKey("formChange")) {
|
||||
assertNotNull(errors);
|
||||
assertFalse(errors.hasErrors());
|
||||
}
|
||||
return request.getSession(false);
|
||||
}
|
||||
|
||||
|
||||
private static class TestWizardController extends AbstractWizardFormController {
|
||||
|
||||
public TestWizardController() {
|
||||
setCommandClass(TestBean.class);
|
||||
setCommandName("tb");
|
||||
setPages(new String[] {"page0", "page1"});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map referenceData(HttpServletRequest request, int page) throws Exception {
|
||||
assertEquals(new Integer(page), request.getAttribute("target"));
|
||||
return super.referenceData(request, page);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean suppressValidation(HttpServletRequest request, Object command) {
|
||||
return (request.getParameter("formChange") != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void validatePage(Object command, Errors errors, int page) {
|
||||
TestBean tb = (TestBean) command;
|
||||
switch (page) {
|
||||
case 0:
|
||||
if (tb.getName() == null) {
|
||||
errors.rejectValue("name", "NAME_REQUIRED", null, "Name is required");
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (tb.getAge() == 0) {
|
||||
errors.rejectValue("age", "AGE_REQUIRED", null, "Age is required");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid page number");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ModelAndView processFinish(
|
||||
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
|
||||
throws ServletException, IOException {
|
||||
assertTrue(getCurrentPage(request) == 0 || getCurrentPage(request) == 1);
|
||||
return new ModelAndView("success", getCommandName(), command);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ModelAndView processCancel(
|
||||
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
|
||||
throws ServletException, IOException {
|
||||
assertTrue(getCurrentPage(request) == 0 || getCurrentPage(request) == 1);
|
||||
return new ModelAndView("cancel", getCommandName(), command);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -16,12 +16,17 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.mapping;
|
||||
|
||||
import org.springframework.web.servlet.mvc.SimpleFormController;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
*/
|
||||
@Deprecated
|
||||
public class BuyForm extends SimpleFormController {
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.mvc.Controller;
|
||||
|
||||
public class BuyForm implements Controller {
|
||||
|
||||
@Override
|
||||
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user