diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/CancellableFormControllerTests.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/CancellableFormControllerTests.java deleted file mode 100644 index 804d13489e..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/CancellableFormControllerTests.java +++ /dev/null @@ -1,236 +0,0 @@ -/* - * Copyright 2002-2007 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.beans.TestBean; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.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() { - 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; - - public boolean supports(Class clazz) { - return TestBean.class.isAssignableFrom(clazz); - } - public void validate(Object target, Errors errors) { - this.invoked = true; - TestBean tb = (TestBean) target; - if (tb.getAge() < 25) { - errors.rejectValue("age", "TOO_YOUNG"); - } - } - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/CommandControllerTests.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/CommandControllerTests.java deleted file mode 100644 index 1b757c9314..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/CommandControllerTests.java +++ /dev/null @@ -1,510 +0,0 @@ -/* - * Copyright 2002-2005 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.beans.TestBean; -import org.springframework.beans.propertyeditors.CustomDateEditor; -import org.springframework.beans.propertyeditors.CustomNumberEditor; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.mock.web.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(new Long(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(new Long(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 { - 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() { - 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() { - 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() { - 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(new Long(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() { - 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() { - 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() { - 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() { - 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() { - 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() { - protected Object getCommand(HttpServletRequest request) throws Exception { - return new TestBean("original", 99); - } - 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"); - } - - 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); - } - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/ControllerTests.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/ControllerTests.java deleted file mode 100644 index d54fc6ab3e..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/ControllerTests.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright 2002-2006 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.util.Properties; - -import javax.servlet.RequestDispatcher; -import javax.servlet.Servlet; -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import junit.framework.TestCase; -import org.easymock.MockControl; - -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.web.context.support.StaticWebApplicationContext; -import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.util.WebUtils; - -/** - * @author Rod Johnson - * @author Juergen Hoeller - */ -public class ControllerTests extends TestCase { - - public void testParameterizableViewController() throws Exception { - String viewName = "viewName"; - ParameterizableViewController pvc = new ParameterizableViewController(); - pvc.setViewName(viewName); - pvc.initApplicationContext(); - // We don't care about the params. - ModelAndView mv = pvc.handleRequest(new MockHttpServletRequest("GET", "foo.html"), null); - assertTrue("model has no data", mv.getModel().size() == 0); - assertTrue("model has correct viewname", mv.getViewName().equals(viewName)); - assertTrue("getViewName matches", pvc.getViewName().equals(viewName)); - } - - public void testParameterizableViewControllerWithPropertyNotSet() { - ParameterizableViewController pvc = new ParameterizableViewController(); - try { - pvc.initApplicationContext(); - fail("should require viewName property to be set"); - } - catch (IllegalArgumentException ex){ - // expected - assertTrue("meaningful exception message", ex.getMessage().indexOf("viewName") != -1); - } - } - - public void testServletForwardingController() throws Exception { - ServletForwardingController sfc = new ServletForwardingController(); - sfc.setServletName("action"); - doTestServletForwardingController(sfc, false); - } - - public void testServletForwardingControllerWithInclude() throws Exception { - ServletForwardingController sfc = new ServletForwardingController(); - sfc.setServletName("action"); - doTestServletForwardingController(sfc, true); - } - - public void testServletForwardingControllerWithBeanName() throws Exception { - ServletForwardingController sfc = new ServletForwardingController(); - sfc.setBeanName("action"); - doTestServletForwardingController(sfc, false); - } - - private void doTestServletForwardingController(ServletForwardingController sfc, boolean include) - throws Exception { - - MockControl requestControl = MockControl.createControl(HttpServletRequest.class); - HttpServletRequest request = (HttpServletRequest) requestControl.getMock(); - MockControl responseControl = MockControl.createControl(HttpServletResponse.class); - HttpServletResponse response = (HttpServletResponse) responseControl.getMock(); - MockControl contextControl = MockControl.createControl(ServletContext.class); - ServletContext context = (ServletContext) contextControl.getMock(); - MockControl dispatcherControl = MockControl.createControl(RequestDispatcher.class); - RequestDispatcher dispatcher = (RequestDispatcher) dispatcherControl.getMock(); - - request.getMethod(); - requestControl.setReturnValue("GET", 1); - context.getNamedDispatcher("action"); - contextControl.setReturnValue(dispatcher, 1); - if (include) { - request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE); - requestControl.setReturnValue("somePath", 1); - dispatcher.include(request, response); - dispatcherControl.setVoidCallable(1); - } - else { - request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE); - requestControl.setReturnValue(null, 1); - dispatcher.forward(request, response); - dispatcherControl.setVoidCallable(1); - } - requestControl.replay(); - contextControl.replay(); - dispatcherControl.replay(); - - StaticWebApplicationContext sac = new StaticWebApplicationContext(); - sac.setServletContext(context); - sfc.setApplicationContext(sac); - assertNull(sfc.handleRequest(request, response)); - - requestControl.verify(); - contextControl.verify(); - dispatcherControl.verify(); - } - - public void testServletWrappingController() throws Exception { - HttpServletRequest request = new MockHttpServletRequest("GET", "/somePath"); - HttpServletResponse response = new MockHttpServletResponse(); - - ServletWrappingController swc = new ServletWrappingController(); - swc.setServletClass(TestServlet.class); - swc.setServletName("action"); - Properties props = new Properties(); - props.setProperty("config", "myValue"); - swc.setInitParameters(props); - - swc.afterPropertiesSet(); - assertNotNull(TestServlet.config); - assertEquals("action", TestServlet.config.getServletName()); - assertEquals("myValue", TestServlet.config.getInitParameter("config")); - assertNull(TestServlet.request); - assertFalse(TestServlet.destroyed); - - assertNull(swc.handleRequest(request, response)); - assertEquals(request, TestServlet.request); - assertEquals(response, TestServlet.response); - assertFalse(TestServlet.destroyed); - - swc.destroy(); - assertTrue(TestServlet.destroyed); - } - - public void testServletWrappingControllerWithBeanName() throws Exception { - HttpServletRequest request = new MockHttpServletRequest("GET", "/somePath"); - HttpServletResponse response = new MockHttpServletResponse(); - - ServletWrappingController swc = new ServletWrappingController(); - swc.setServletClass(TestServlet.class); - swc.setBeanName("action"); - - swc.afterPropertiesSet(); - assertNotNull(TestServlet.config); - assertEquals("action", TestServlet.config.getServletName()); - assertNull(TestServlet.request); - assertFalse(TestServlet.destroyed); - - assertNull(swc.handleRequest(request, response)); - assertEquals(request, TestServlet.request); - assertEquals(response, TestServlet.response); - assertFalse(TestServlet.destroyed); - - swc.destroy(); - assertTrue(TestServlet.destroyed); - } - - - public static class TestServlet implements Servlet { - - private static ServletConfig config; - private static ServletRequest request; - private static ServletResponse response; - private static boolean destroyed; - - public TestServlet() { - config = null; - request = null; - response = null; - destroyed = false; - } - - public void init(ServletConfig servletConfig) { - config = servletConfig; - } - - public ServletConfig getServletConfig() { - return config; - } - - public void service(ServletRequest servletRequest, ServletResponse servletResponse) { - request = servletRequest; - response = servletResponse; - } - - public String getServletInfo() { - return "TestServlet"; - } - - public void destroy() { - destroyed = true; - } - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/FormControllerTests.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/FormControllerTests.java deleted file mode 100644 index dd73591080..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/FormControllerTests.java +++ /dev/null @@ -1,646 +0,0 @@ -/* - * Copyright 2002-2007 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.beans.IndexedTestBean; -import org.springframework.beans.TestBean; -import org.springframework.context.support.StaticApplicationContext; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.mock.web.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 - */ -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() { - 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() { - 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() { - protected boolean isFormChangeRequest(HttpServletRequest request) { - return (request.getParameter("formChange") != null); - } - - protected void onFormChange(HttpServletRequest request, HttpServletResponse response, Object command) { - assertNotNull("Command should not be null", command); - assertEquals("Incorrect command class", TestBean.class, command.getClass()); - ((TestBean)command).setMyFloat(myFloat); - } - }; - mc.setFormView(formView); - mc.setSuccessView(successView); - - MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo.html"); - request.addParameter("name", "Rod"); - request.addParameter("age", "99"); - request.addParameter("formChange", "true"); - HttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = mc.handleRequest(request, response); - assertTrue("returned correct view name: expected '" + formView + "', not '" + mv.getViewName() + "'", - mv.getViewName().equals(formView)); - - TestBean person = (TestBean) mv.getModel().get(mc.getCommandName()); - assertTrue("model is non null", person != null); - assertTrue("bean name bound ok", person.getName().equals("Rod")); - assertTrue("bean age is 99", person.getAge() == 99); - assertEquals("Command property myFloat not updated in onFormChange", myFloat, person.getMyFloat()); - Errors errors = (Errors) mv.getModel().get(BindException.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"; - - public boolean supports(Class clazz) { return true; } - - public void validate(Object obj, Errors errors) { - TestBean tb = (TestBean) obj; - if (tb.getName() == null || "".equals(tb.getName())) - errors.rejectValue("name", "needname", null, "need name"); - else if (tb.getName().length() < 5) - errors.rejectValue("name", TOOSHORT, null, "need full name"); - } - } - - - private static class TestValidator2 implements Validator { - - public static String TOOSHORT = "tooshort"; - - public boolean supports(Class clazz) { return true; } - - public void validate(Object obj, Errors errors) { - errors.reject("test", "testmessage"); - } - } - - - private static class TestController extends SimpleFormController { - - public static String BEAN_NAME = "person"; - - public static int DEFAULT_AGE = 52; - - public TestController() { - setCommandClass(TestBean.class); - setCommandName(BEAN_NAME); - } - - protected Object formBackingObject(HttpServletRequest request) throws ServletException { - TestBean person = new TestBean(); - person.setAge(DEFAULT_AGE); - return person; - } - - protected boolean isFormChangeRequest(HttpServletRequest request) { - return (request.getParameter("formChange") != null); - } - } - - - private static class TestControllerWithCustomOnSubmit extends TestController { - - protected ModelAndView onSubmit(Object command) throws Exception { - return new ModelAndView("mySuccess"); - } - } - - - private static class RefController extends SimpleFormController { - - final String NUMBERS_ATT = "NUMBERS"; - - static final int[] NUMBERS = { 1, 2, 3, 4 }; - - int refDataCount; - - public RefController() { - setCommandClass(TestBean.class); - } - - protected Map referenceData(HttpServletRequest request) { - ++refDataCount; - Map m = new HashMap(); - m.put(NUMBERS_ATT, NUMBERS); - return m; - } - - public void invokeWebSpecificStuff() { - getTempDir(); - } - } - - - public static class BooleanBindingFormController extends AbstractFormController { - - protected ModelAndView processFormSubmission - (HttpServletRequest req, HttpServletResponse resp, Object command, BindException errors) throws Exception { - ModelAndView mav = new ModelAndView(); - mav.addObject("command", command); - return mav; - } - - 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; - } - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/UrlFilenameViewControllerTests.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/UrlFilenameViewControllerTests.java deleted file mode 100644 index ed45d97f82..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/UrlFilenameViewControllerTests.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright 2002-2007 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 junit.framework.TestCase; - -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.util.AntPathMatcher; -import org.springframework.util.PathMatcher; -import org.springframework.web.servlet.HandlerMapping; -import org.springframework.web.servlet.ModelAndView; - -/** - * @author Juergen Hoeller - * @author Rick Evans - * @since 14.09.2005 - */ -public class UrlFilenameViewControllerTests extends TestCase { - - private PathMatcher pathMatcher = new AntPathMatcher(); - - - public void testWithPlainFilename() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("index", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - public void testWithFilenamePlusExtension() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("index", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - public void testWithPrefixAndSuffix() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - ctrl.setPrefix("mypre_"); - ctrl.setSuffix("_mysuf"); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("mypre_index_mysuf", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - public void testWithPrefix() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - ctrl.setPrefix("mypre_"); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("mypre_index", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - public void testWithSuffix() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - ctrl.setSuffix("_mysuf"); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("index_mysuf", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - public void testMultiLevel() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("docs/cvs/commit", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - public void testMultiLevelWithMapping() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html"); - exposePathInMapping(request, "/docs/**"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("cvs/commit", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - public void testMultiLevelMappingWithFallback() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html"); - exposePathInMapping(request, "/docs/cvs/commit.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("docs/cvs/commit", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - public void testWithContextMapping() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/docs/cvs/commit.html"); - request.setContextPath("/myapp"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("docs/cvs/commit", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - public void testSettingPrefixToNullCausesEmptyStringToBeUsed() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - ctrl.setPrefix(null); - assertNotNull("When setPrefix(..) is called with a null argument, the empty string value must be used instead.", ctrl.getPrefix()); - assertEquals("When setPrefix(..) is called with a null argument, the empty string value must be used instead.", "", ctrl.getPrefix()); - } - - public void testSettingSuffixToNullCausesEmptyStringToBeUsed() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - ctrl.setSuffix(null); - assertNotNull("When setSuffix(..) is called with a null argument, the empty string value must be used instead.", ctrl.getSuffix()); - assertEquals("When setSuffix(..) is called with a null argument, the empty string value must be used instead.", "", ctrl.getSuffix()); - } - - /** - * This is the expected behavior, and it now has a test to prove it. - * http://opensource.atlassian.com/projects/spring/browse/SPR-2789 - */ - public void testNestedPathisUsedAsViewName_InBreakingChangeFromSpring12Line() throws Exception { - UrlFilenameViewController ctrl = new UrlFilenameViewController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/products/view.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = ctrl.handleRequest(request, response); - assertEquals("products/view", mv.getViewName()); - assertTrue(mv.getModel().isEmpty()); - } - - private void exposePathInMapping(MockHttpServletRequest request, String mapping) { - String pathInMapping = this.pathMatcher.extractPathWithinPattern(mapping, request.getRequestURI()); - request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathInMapping); - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java deleted file mode 100644 index e23a379f2b..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright 2002-2006 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.util.List; -import java.util.Properties; - -import junit.framework.TestCase; - -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.test.AssertThrows; -import org.springframework.web.servlet.support.WebContentGenerator; - -/** - * @author Rick Evans - */ -public class WebContentInterceptorTests extends TestCase { - - private MockHttpServletRequest request; - - private MockHttpServletResponse response; - - - protected void setUp() throws Exception { - request = new MockHttpServletRequest(); - request.setMethod(WebContentGenerator.METHOD_GET); - response = new MockHttpServletResponse(); - } - - - public void testPreHandleSetsCacheSecondsOnMatchingRequest() throws Exception { - WebContentInterceptor interceptor = new WebContentInterceptor(); - interceptor.setCacheSeconds(10); - - interceptor.preHandle(request, response, null); - - List expiresHeaders = response.getHeaders("Expires"); - assertNotNull("'Expires' header not set (must be) : null", expiresHeaders); - assertTrue("'Expires' header not set (must be) : empty", expiresHeaders.size() > 0); - List cacheControlHeaders = response.getHeaders("Cache-Control"); - assertNotNull("'Cache-Control' header not set (must be) : null", cacheControlHeaders); - assertTrue("'Cache-Control' header not set (must be) : empty", cacheControlHeaders.size() > 0); - } - - public void testPreHandleSetsCacheSecondsOnMatchingRequestWithCustomCacheMapping() throws Exception { - Properties mappings = new Properties(); - mappings.setProperty("**/*handle.vm", "-1"); - - WebContentInterceptor interceptor = new WebContentInterceptor(); - interceptor.setCacheSeconds(10); - interceptor.setCacheMappings(mappings); - - request.setRequestURI("http://localhost:7070/example/adminhandle.vm"); - interceptor.preHandle(request, response, null); - - List expiresHeaders = response.getHeaders("Expires"); - assertTrue("'Expires' header set (must not be) : empty", expiresHeaders.size() == 0); - List cacheControlHeaders = response.getHeaders("Cache-Control"); - assertTrue("'Cache-Control' header set (must not be) : empty", cacheControlHeaders.size() == 0); - - request.setRequestURI("http://localhost:7070/example/bingo.html"); - interceptor.preHandle(request, response, null); - - expiresHeaders = response.getHeaders("Expires"); - assertNotNull("'Expires' header not set (must be) : null", expiresHeaders); - assertTrue("'Expires' header not set (must be) : empty", expiresHeaders.size() > 0); - cacheControlHeaders = response.getHeaders("Cache-Control"); - assertNotNull("'Cache-Control' header not set (must be) : null", cacheControlHeaders); - assertTrue("'Cache-Control' header not set (must be) : empty", cacheControlHeaders.size() > 0); - } - - public void testPreHandleSetsCacheSecondsOnMatchingRequestWithNoCaching() throws Exception { - WebContentInterceptor interceptor = new WebContentInterceptor(); - interceptor.setCacheSeconds(0); - - interceptor.preHandle(request, response, null); - - List expiresHeaders = response.getHeaders("Expires"); - assertNotNull("'Expires' header not set (must be) : null", expiresHeaders); - assertTrue("'Expires' header not set (must be) : empty", expiresHeaders.size() > 0); - List cacheControlHeaders = response.getHeaders("Cache-Control"); - assertNotNull("'Cache-Control' header not set (must be) : null", cacheControlHeaders); - assertTrue("'Cache-Control' header not set (must be) : empty", cacheControlHeaders.size() > 0); - } - - public void testPreHandleSetsCacheSecondsOnMatchingRequestWithCachingDisabled() throws Exception { - WebContentInterceptor interceptor = new WebContentInterceptor(); - interceptor.setCacheSeconds(-1); - - interceptor.preHandle(request, response, null); - - List expiresHeaders = response.getHeaders("Expires"); - assertTrue("'Expires' header set (must not be) : empty", expiresHeaders.size() == 0); - List cacheControlHeaders = response.getHeaders("Cache-Control"); - assertTrue("'Cache-Control' header set (must not be) : empty", cacheControlHeaders.size() == 0); - } - - public void testSetPathMatcherToNull() throws Exception { - new AssertThrows(IllegalArgumentException.class) { - public void test() throws Exception { - WebContentInterceptor interceptor = new WebContentInterceptor(); - interceptor.setPathMatcher(null); - } - }.runTest(); - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/WizardFormControllerTests.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/WizardFormControllerTests.java deleted file mode 100644 index b8e35f0267..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/WizardFormControllerTests.java +++ /dev/null @@ -1,428 +0,0 @@ -/* - * Copyright 2002-2007 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.beans.TestBean; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.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 - */ -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() { - protected String getFormSessionAttributeName() { - return "myFormAttr"; - } - 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() { - protected String getFormSessionAttributeName(HttpServletRequest request) { - return "myFormAttr" + request.getParameter("formAttr"); - } - 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"}); - } - - protected Map referenceData(HttpServletRequest request, int page) throws Exception { - assertEquals(new Integer(page), request.getAttribute("target")); - return super.referenceData(request, page); - } - - protected boolean suppressValidation(HttpServletRequest request, Object command) { - return (request.getParameter("formChange") != null); - } - - 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"); - } - } - - 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); - } - - 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); - } - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/AdminController.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/AdminController.java deleted file mode 100644 index 9050e21440..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/AdminController.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2002-2008 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.annotation; - -import org.springframework.stereotype.Controller; - -/** - * @author Juergen Hoeller - */ -@Controller -public class AdminController { - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/BuyForm.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/BuyForm.java deleted file mode 100644 index 2ec6c6dd0d..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/BuyForm.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2002-2008 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.annotation; - -import org.springframework.stereotype.Controller; - -/** - * @author Juergen Hoeller - */ -@Controller -public class BuyForm { - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/IndexController.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/IndexController.java deleted file mode 100644 index ebbfbdc33f..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/IndexController.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2002-2008 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.annotation; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.servlet.ModelAndView; - -/** - * @author Juergen Hoeller - */ -@Controller -public class IndexController { - - @RequestMapping - public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { - return new ModelAndView("indexView"); - } - -} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/MethodNameDispatchingController.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/MethodNameDispatchingController.java deleted file mode 100644 index 709d7422d5..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/MethodNameDispatchingController.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2002-2008 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.annotation; - -import java.io.IOException; - -import javax.servlet.http.HttpServletResponse; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; - -/** - * @author Juergen Hoeller - */ -@Controller -@RequestMapping("/*.do") -public class MethodNameDispatchingController { - - @RequestMapping - public void myHandle(HttpServletResponse response) throws IOException { - response.getWriter().write("myView"); - } - - @RequestMapping - public void myOtherHandle(HttpServletResponse response) throws IOException { - response.getWriter().write("myOtherView"); - } - - @RequestMapping(method = RequestMethod.POST) - public void myLangHandle(HttpServletResponse response) throws IOException { - response.getWriter().write("myLangView"); - } - - @RequestMapping(method = RequestMethod.POST) - public void mySurpriseHandle(HttpServletResponse response) throws IOException { - response.getWriter().write("mySurpriseView"); - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/WelcomeController.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/WelcomeController.java deleted file mode 100644 index 5b1febae29..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/WelcomeController.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2002-2006 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.annotation; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.servlet.ModelAndView; - -/** - * @author Juergen Hoeller - */ -@Controller -public class WelcomeController { - - @RequestMapping - public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { - return new ModelAndView("welcomeView"); - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/class-mapping.xml b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/class-mapping.xml deleted file mode 100644 index bd73342dc4..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/annotation/class-mapping.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/multiaction/MultiActionControllerTests.java b/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/multiaction/MultiActionControllerTests.java deleted file mode 100644 index ddf7bd4451..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/web/servlet/mvc/multiaction/MultiActionControllerTests.java +++ /dev/null @@ -1,720 +0,0 @@ -/* - * Copyright 2002-2008 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.multiaction; - -import java.io.IOException; -import java.sql.SQLException; -import java.util.HashMap; -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.beans.FatalBeanException; -import org.springframework.beans.TestBean; -import org.springframework.context.ApplicationContextException; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.mock.web.MockHttpSession; -import org.springframework.web.HttpSessionRequiredException; -import org.springframework.web.bind.ServletRequestBindingException; -import org.springframework.web.servlet.ModelAndView; - -/** - * @author Rod Johnson - * @author Juergen Hoeller - * @author Colin Sampaleanu - * @author Rob Harrop - * @author Sam Brannen - */ -public class MultiActionControllerTests extends TestCase { - - public void testDefaultInternalPathMethodNameResolver() throws Exception { - doDefaultTestInternalPathMethodNameResolver("/foo.html", "foo"); - doDefaultTestInternalPathMethodNameResolver("/foo/bar.html", "bar"); - doDefaultTestInternalPathMethodNameResolver("/bugal.xyz", "bugal"); - doDefaultTestInternalPathMethodNameResolver("/x/y/z/q/foo.html", "foo"); - doDefaultTestInternalPathMethodNameResolver("qqq.q", "qqq"); - } - - private void doDefaultTestInternalPathMethodNameResolver(String in, String expected) throws Exception { - MultiActionController rc = new MultiActionController(); - HttpServletRequest request = new MockHttpServletRequest("GET", in); - String actual = rc.getMethodNameResolver().getHandlerMethodName(request); - assertEquals("Wrong method name resolved", expected, actual); - } - - public void testCustomizedInternalPathMethodNameResolver() throws Exception { - doTestCustomizedInternalPathMethodNameResolver("/foo.html", "my", null, "myfoo"); - doTestCustomizedInternalPathMethodNameResolver("/foo/bar.html", null, "Handler", "barHandler"); - doTestCustomizedInternalPathMethodNameResolver("/Bugal.xyz", "your", "Method", "yourBugalMethod"); - } - - private void doTestCustomizedInternalPathMethodNameResolver(String in, String prefix, String suffix, String expected) - throws Exception { - - MultiActionController rc = new MultiActionController(); - InternalPathMethodNameResolver resolver = new InternalPathMethodNameResolver(); - if (prefix != null) { - resolver.setPrefix(prefix); - } - if (suffix != null) { - resolver.setSuffix(suffix); - } - rc.setMethodNameResolver(resolver); - HttpServletRequest request = new MockHttpServletRequest("GET", in); - String actual = rc.getMethodNameResolver().getHandlerMethodName(request); - assertEquals("Wrong method name resolved", expected, actual); - } - - public void testParameterMethodNameResolver() throws NoSuchRequestHandlingMethodException { - ParameterMethodNameResolver mnr = new ParameterMethodNameResolver(); - - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html"); - request.addParameter("action", "bar"); - assertEquals("bar", mnr.getHandlerMethodName(request)); - - request = new MockHttpServletRequest("GET", "/foo.html"); - try { - mnr.getHandlerMethodName(request); - fail("Should have thrown NoSuchRequestHandlingMethodException"); - } - catch (NoSuchRequestHandlingMethodException expected) { - } - - request = new MockHttpServletRequest("GET", "/foo.html"); - request.addParameter("action", ""); - try { - mnr.getHandlerMethodName(request); - fail("Should have thrown NoSuchRequestHandlingMethodException"); - } - catch (NoSuchRequestHandlingMethodException expected) { - } - - request = new MockHttpServletRequest("GET", "/foo.html"); - request.addParameter("action", " "); - try { - mnr.getHandlerMethodName(request); - fail("Should have thrown NoSuchRequestHandlingMethodException"); - } - catch (NoSuchRequestHandlingMethodException expected) { - } - } - - public void testParameterMethodNameResolverWithCustomParamName() throws NoSuchRequestHandlingMethodException { - ParameterMethodNameResolver mnr = new ParameterMethodNameResolver(); - mnr.setParamName("myparam"); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html"); - request.addParameter("myparam", "bar"); - assertEquals("bar", mnr.getHandlerMethodName(request)); - } - - public void testParameterMethodNameResolverWithParamNames() throws NoSuchRequestHandlingMethodException { - ParameterMethodNameResolver resolver = new ParameterMethodNameResolver(); - resolver.setDefaultMethodName("default"); - resolver.setMethodParamNames(new String[] { "hello", "spring", "colin" }); - Properties logicalMappings = new Properties(); - logicalMappings.setProperty("hello", "goodbye"); - logicalMappings.setProperty("nina", "colin"); - resolver.setLogicalMappings(logicalMappings); - - // verify default handler - MockHttpServletRequest request = new MockHttpServletRequest(); - request.addParameter("this will not match anything", "whatever"); - assertEquals("default", resolver.getHandlerMethodName(request)); - - // verify first resolution strategy (action=method) - request = new MockHttpServletRequest(); - request.addParameter("action", "reset"); - assertEquals("reset", resolver.getHandlerMethodName(request)); - // this one also tests logical mapping - request = new MockHttpServletRequest(); - request.addParameter("action", "nina"); - assertEquals("colin", resolver.getHandlerMethodName(request)); - - // now validate second resolution strategy (parameter existence) - // this also tests logical mapping - request = new MockHttpServletRequest(); - request.addParameter("hello", "whatever"); - assertEquals("goodbye", resolver.getHandlerMethodName(request)); - - request = new MockHttpServletRequest(); - request.addParameter("spring", "whatever"); - assertEquals("spring", resolver.getHandlerMethodName(request)); - - request = new MockHttpServletRequest(); - request.addParameter("hello", "whatever"); - request.addParameter("spring", "whatever"); - assertEquals("goodbye", resolver.getHandlerMethodName(request)); - - request = new MockHttpServletRequest(); - request.addParameter("colin", "whatever"); - request.addParameter("spring", "whatever"); - assertEquals("spring", resolver.getHandlerMethodName(request)); - - // validate image button handling - request = new MockHttpServletRequest(); - request.addParameter("spring.x", "whatever"); - assertEquals("spring", resolver.getHandlerMethodName(request)); - - request = new MockHttpServletRequest(); - request.addParameter("hello.x", "whatever"); - request.addParameter("spring", "whatever"); - assertEquals("goodbye", resolver.getHandlerMethodName(request)); - } - - public void testParameterMethodNameResolverWithDefaultMethodName() throws NoSuchRequestHandlingMethodException { - ParameterMethodNameResolver mnr = new ParameterMethodNameResolver(); - mnr.setDefaultMethodName("foo"); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html"); - request.addParameter("action", "bar"); - assertEquals("bar", mnr.getHandlerMethodName(request)); - request = new MockHttpServletRequest("GET", "/foo.html"); - assertEquals("foo", mnr.getHandlerMethodName(request)); - } - - public void testInvokesCorrectMethod() throws Exception { - TestMaController mc = new TestMaController(); - HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); - HttpServletResponse response = new MockHttpServletResponse(); - Properties p = new Properties(); - p.put("/welcome.html", "welcome"); - PropertiesMethodNameResolver mnr = new PropertiesMethodNameResolver(); - mnr.setMappings(p); - mc.setMethodNameResolver(mnr); - - ModelAndView mv = mc.handleRequest(request, response); - assertTrue("Invoked welcome method", mc.wasInvoked("welcome")); - assertTrue("view name is welcome", mv.getViewName().equals("welcome")); - assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); - - mc = new TestMaController(); - request = new MockHttpServletRequest("GET", "/subdir/test.html"); - response = new MockHttpServletResponse(); - mv = mc.handleRequest(request, response); - assertTrue("Invoked test method", mc.wasInvoked("test")); - assertTrue("view name is subdir_test", mv.getViewName().equals("test")); - assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); - } - - public void testPathMatching() throws Exception { - TestMaController mc = new TestMaController(); - HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); - HttpServletResponse response = new MockHttpServletResponse(); - Properties p = new Properties(); - p.put("/welc*.html", "welcome"); - PropertiesMethodNameResolver mn = new PropertiesMethodNameResolver(); - mn.setMappings(p); - mc.setMethodNameResolver(mn); - - ModelAndView mv = mc.handleRequest(request, response); - assertTrue("Invoked welcome method", mc.wasInvoked("welcome")); - assertTrue("view name is welcome", mv.getViewName().equals("welcome")); - assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); - - mc = new TestMaController(); - mc.setMethodNameResolver(mn); - request = new MockHttpServletRequest("GET", "/nomatch"); - response = new MockHttpServletResponse(); - try { - mv = mc.handleRequest(request, response); - } - catch (Exception expected) { - } - assertFalse("Not invoking welcome method", mc.wasInvoked("welcome")); - assertTrue("No method invoked", mc.getInvokedMethods() == 0); - } - - public void testInvokesCorrectMethodOnDelegate() throws Exception { - MultiActionController mac = new MultiActionController(); - TestDelegate d = new TestDelegate(); - mac.setDelegate(d); - HttpServletRequest request = new MockHttpServletRequest("GET", "/test.html"); - HttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = mac.handleRequest(request, response); - assertTrue("view name is test", mv.getViewName().equals("test")); - assertTrue("Delegate was invoked", d.invoked); - } - - public void testInvokesCorrectMethodWithSession() throws Exception { - TestMaController mc = new TestMaController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/inSession.html"); - request.setSession(new MockHttpSession(null)); - HttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = mc.handleRequest(request, response); - assertTrue("Invoked inSession method", mc.wasInvoked("inSession")); - assertTrue("view name is welcome", mv.getViewName().equals("inSession")); - assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); - - request = new MockHttpServletRequest("GET", "/inSession.html"); - response = new MockHttpServletResponse(); - try { - - mc.handleRequest(request, response); - fail("Must have rejected request without session"); - } - catch (ServletException expected) { - } - } - - public void testInvokesCommandMethodNoSession() throws Exception { - TestMaController mc = new TestMaController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/commandNoSession.html"); - request.addParameter("name", "rod"); - request.addParameter("age", "32"); - HttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = mc.handleRequest(request, response); - assertTrue("Invoked commandNoSession method", mc.wasInvoked("commandNoSession")); - assertTrue("view name is commandNoSession", mv.getViewName().equals("commandNoSession")); - assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); - } - - public void testInvokesCommandMethodWithSession() throws Exception { - TestMaController mc = new TestMaController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/commandInSession.html"); - request.addParameter("name", "rod"); - request.addParameter("age", "32"); - - request.setSession(new MockHttpSession(null)); - HttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mv = mc.handleRequest(request, response); - assertTrue("Invoked commandInSession method", mc.wasInvoked("commandInSession")); - assertTrue("view name is commandInSession", mv.getViewName().equals("commandInSession")); - assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); - - request = new MockHttpServletRequest("GET", "/commandInSession.html"); - response = new MockHttpServletResponse(); - try { - - mc.handleRequest(request, response); - fail("Must have rejected request without session"); - } - catch (ServletException expected) { - } - } - - public void testSessionRequiredCatchable() throws Exception { - HttpServletRequest request = new MockHttpServletRequest("GET", "/testSession.html"); - HttpServletResponse response = new MockHttpServletResponse(); - TestMaController contr = new TestSessionRequiredController(); - try { - contr.handleRequest(request, response); - fail("Should have thrown exception"); - } - catch (HttpSessionRequiredException ex) { - // assertTrue("session required", ex.equals(t)); - } - request = new MockHttpServletRequest("GET", "/testSession.html"); - response = new MockHttpServletResponse(); - contr = new TestSessionRequiredExceptionHandler(); - ModelAndView mv = contr.handleRequest(request, response); - assertTrue("Name is ok", mv.getViewName().equals("handle(SRE)")); - } - - private void testExceptionNoHandler(TestMaController mc, Throwable t) throws Exception { - HttpServletRequest request = new MockHttpServletRequest("GET", "/testException.html"); - request.setAttribute(TestMaController.THROWABLE_ATT, t); - HttpServletResponse response = new MockHttpServletResponse(); - try { - mc.handleRequest(request, response); - fail("Should have thrown exception"); - } - catch (Throwable ex) { - assertTrue(ex.equals(t)); - } - } - - private void testExceptionNoHandler(Throwable t) throws Exception { - testExceptionNoHandler(new TestMaController(), t); - } - - public void testExceptionNoHandler() throws Exception { - testExceptionNoHandler(new Exception()); - - // must go straight through - testExceptionNoHandler(new ServletException()); - - // subclass of servlet exception - testExceptionNoHandler(new ServletRequestBindingException("foo")); - testExceptionNoHandler(new RuntimeException()); - testExceptionNoHandler(new Error()); - } - - public void testLastModifiedDefault() throws Exception { - TestMaController mc = new TestMaController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); - long lastMod = mc.getLastModified(request); - assertTrue("default last modified is -1", lastMod == -1L); - } - - public void testLastModifiedWithMethod() throws Exception { - LastModController mc = new LastModController(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); - long lastMod = mc.getLastModified(request); - assertTrue("last modified with method is > -1", lastMod == mc.getLastModified(request)); - } - - private ModelAndView testHandlerCaughtException(TestMaController mc, Throwable t) throws Exception { - HttpServletRequest request = new MockHttpServletRequest("GET", "/testException.html"); - request.setAttribute(TestMaController.THROWABLE_ATT, t); - HttpServletResponse response = new MockHttpServletResponse(); - return mc.handleRequest(request, response); - } - - public void testHandlerCaughtException() throws Exception { - TestMaController mc = new TestExceptionHandler(); - ModelAndView mv = testHandlerCaughtException(mc, new Exception()); - assertNotNull("ModelAndView must not be null", mv); - assertTrue("mv name is handle(Exception)", "handle(Exception)".equals(mv.getViewName())); - assertTrue("Invoked correct method", mc.wasInvoked("handle(Exception)")); - - // WILL GET RUNTIME EXCEPTIONS TOO - testExceptionNoHandler(mc, new Error()); - - mc = new TestServletExceptionHandler(); - mv = testHandlerCaughtException(mc, new ServletException()); - assertTrue(mv.getViewName().equals("handle(ServletException)")); - assertTrue("Invoke correct method", mc.wasInvoked("handle(ServletException)")); - - mv = testHandlerCaughtException(mc, new ServletRequestBindingException("foo")); - assertTrue(mv.getViewName().equals("handle(ServletException)")); - assertTrue("Invoke correct method", mc.wasInvoked("handle(ServletException)")); - - // Check it doesn't affect unknown exceptions - testExceptionNoHandler(mc, new RuntimeException()); - testExceptionNoHandler(mc, new Error()); - testExceptionNoHandler(mc, new SQLException()); - testExceptionNoHandler(mc, new Exception()); - - mc = new TestRuntimeExceptionHandler(); - mv = testHandlerCaughtException(mc, new RuntimeException()); - assertTrue(mv.getViewName().equals("handle(RTE)")); - assertTrue("Invoke correct method", mc.wasInvoked("handle(RTE)")); - mv = testHandlerCaughtException(mc, new FatalBeanException(null, null)); - assertTrue(mv.getViewName().equals("handle(RTE)")); - assertTrue("Invoke correct method", mc.wasInvoked("handle(RTE)")); - - testExceptionNoHandler(mc, new SQLException()); - testExceptionNoHandler(mc, new Exception()); - } - - public void testHandlerReturnsMap() throws Exception { - Map model = new HashMap(); - model.put("message", "Hello World!"); - - MultiActionController mac = new ModelOnlyMultiActionController(model); - - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mav = mac.handleRequest(request, response); - - assertNotNull("ModelAndView cannot be null", mav); - assertFalse("ModelAndView should not have a view", mav.hasView()); - assertEquals(model, mav.getModel()); - } - - public void testExceptionHandlerReturnsMap() throws Exception { - Map model = new HashMap(); - - MultiActionController mac = new ModelOnlyMultiActionController(model); - - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mav = mac.handleRequest(request, response); - - assertNotNull("ModelAndView cannot be null", mav); - assertFalse("ModelAndView should not have a view", mav.hasView()); - assertTrue(model.containsKey("exception")); - } - - public void testCannotCallExceptionHandlerDirectly() throws Exception { - Map model = new HashMap(); - - MultiActionController mac = new ModelOnlyMultiActionController(model); - - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/handleIllegalStateException.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mav = mac.handleRequest(request, response); - assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); - } - - public void testHandlerReturnsVoid() throws Exception { - MultiActionController mac = new VoidMultiActionController(); - - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mav = mac.handleRequest(request, response); - - assertNull("ModelAndView must be null", mav); - } - - public void testExceptionHandlerReturnsVoid() throws Exception { - MultiActionController mac = new VoidMultiActionController(); - - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mav = mac.handleRequest(request, response); - - assertNull("ModelAndView must be null", mav); - assertEquals("exception", response.getContentAsString()); - } - - public void testHandlerReturnsString() throws Exception { - MultiActionController mac = new StringMultiActionController(); - - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mav = mac.handleRequest(request, response); - - assertNotNull("ModelAndView cannot be null", mav); - assertTrue("ModelAndView must have a view", mav.hasView()); - assertEquals("Verifying view name", "welcomeString", mav.getViewName()); - } - - public void testExceptionHandlerReturnsString() throws Exception { - MultiActionController mac = new StringMultiActionController(); - - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html"); - MockHttpServletResponse response = new MockHttpServletResponse(); - ModelAndView mav = mac.handleRequest(request, response); - - assertNotNull("ModelAndView cannot be null", mav); - assertTrue("ModelAndView must have a view", mav.hasView()); - assertEquals("Verifying view name", "handleIllegalStateExceptionString", mav.getViewName()); - } - - - /** No error handlers */ - public static class TestMaController extends MultiActionController { - - public static final String THROWABLE_ATT = "throwable"; - - /** Method name -> object */ - protected Map invoked = new HashMap(); - - public void clear() { - this.invoked.clear(); - } - - public ModelAndView welcome(HttpServletRequest request, HttpServletResponse response) { - this.invoked.put("welcome", Boolean.TRUE); - return new ModelAndView("welcome"); - } - - public ModelAndView commandNoSession(HttpServletRequest request, HttpServletResponse response, TestBean command) { - this.invoked.put("commandNoSession", Boolean.TRUE); - - String pname = request.getParameter("name"); - String page = request.getParameter("age"); - // ALLOW FOR NULL - if (pname == null) { - assertTrue("name null", command.getName() == null); - } - else { - assertTrue("name param set", pname.equals(command.getName())); - } - // if (page == null) - // assertTrue("age default", command.getAge() == 0); - // else - // assertTrue("age set", command.getName().equals(pname)); - // assertTrue("a", - // command.getAge().equals(request.getParameter("name"))); - return new ModelAndView("commandNoSession"); - } - - public ModelAndView inSession(HttpServletRequest request, HttpServletResponse response, HttpSession session) { - this.invoked.put("inSession", Boolean.TRUE); - assertTrue("session non null", session != null); - return new ModelAndView("inSession"); - } - - public ModelAndView commandInSession(HttpServletRequest request, HttpServletResponse response, - HttpSession session, TestBean command) { - this.invoked.put("commandInSession", Boolean.TRUE); - assertTrue("session non null", session != null); - return new ModelAndView("commandInSession"); - } - - public ModelAndView test(HttpServletRequest request, HttpServletResponse response) { - this.invoked.put("test", Boolean.TRUE); - return new ModelAndView("test"); - } - - public ModelAndView testException(HttpServletRequest request, HttpServletResponse response) throws Throwable { - this.invoked.put("testException", Boolean.TRUE); - Throwable t = (Throwable) request.getAttribute(THROWABLE_ATT); - if (t != null) { - throw t; - } - else { - return new ModelAndView("no throwable"); - } - } - - public boolean wasInvoked(String method) { - return this.invoked.get(method) != null; - } - - public int getInvokedMethods() { - return this.invoked.size(); - } - } - - - public static class TestDelegate { - - boolean invoked; - - public ModelAndView test(HttpServletRequest request, HttpServletResponse response) { - this.invoked = true; - return new ModelAndView("test"); - } - } - - - public static class TestExceptionHandler extends TestMaController { - - public ModelAndView handleAnyException(HttpServletRequest request, HttpServletResponse response, Exception ex) { - this.invoked.put("handle(Exception)", Boolean.TRUE); - return new ModelAndView("handle(Exception)"); - } - } - - - public static class TestRuntimeExceptionHandler extends TestMaController { - - public ModelAndView handleRuntimeProblem(HttpServletRequest request, HttpServletResponse response, - RuntimeException ex) { - this.invoked.put("handle(RTE)", Boolean.TRUE); - return new ModelAndView("handle(RTE)"); - } - } - - - public static class TestSessionRequiredController extends TestMaController { - - public ModelAndView testSession(HttpServletRequest request, HttpServletResponse response, HttpSession sess) { - return null; - } - } - - - /** Extends previous to handle exception */ - public static class TestSessionRequiredExceptionHandler extends TestSessionRequiredController { - - public ModelAndView handleServletException(HttpServletRequest request, HttpServletResponse response, - HttpSessionRequiredException ex) { - this.invoked.put("handle(SRE)", Boolean.TRUE); - return new ModelAndView("handle(SRE)"); - } - } - - public static class TestServletExceptionHandler extends TestMaController { - - public ModelAndView handleServletException(HttpServletRequest request, HttpServletResponse response, - ServletException ex) { - this.invoked.put("handle(ServletException)", Boolean.TRUE); - return new ModelAndView("handle(ServletException)"); - } - } - - - public static class LastModController extends MultiActionController { - - public static final String THROWABLE_ATT = "throwable"; - - /** Method name -> object */ - protected HashMap invoked = new HashMap(); - - public void clear() { - this.invoked.clear(); - } - - public ModelAndView welcome(HttpServletRequest request, HttpServletResponse response) { - this.invoked.put("welcome", Boolean.TRUE); - return new ModelAndView("welcome"); - } - - /** Always says content is up to date */ - public long welcomeLastModified(HttpServletRequest request) { - return 1111L; - } - } - - - public static class ModelOnlyMultiActionController extends MultiActionController { - - private final Map model; - - public ModelOnlyMultiActionController(Map model) throws ApplicationContextException { - this.model = model; - } - - public Map welcome(HttpServletRequest request, HttpServletResponse response) { - return this.model; - } - - public Map index(HttpServletRequest request, HttpServletResponse response) { - throw new IllegalStateException(); - } - - public Map handleIllegalStateException(HttpServletRequest request, HttpServletResponse response, - IllegalStateException ex) { - this.model.put("exception", ex); - return this.model; - } - } - - - public static class VoidMultiActionController extends MultiActionController { - - public void welcome(HttpServletRequest request, HttpServletResponse response) { - } - - public void index(HttpServletRequest request, HttpServletResponse response) { - throw new IllegalStateException(); - } - - public void handleIllegalStateException(HttpServletRequest request, HttpServletResponse response, - IllegalStateException ex) throws IOException { - response.getWriter().write("exception"); - } - } - - - public static class StringMultiActionController extends MultiActionController { - - public String welcome(HttpServletRequest request, HttpServletResponse response) { - return "welcomeString"; - } - - public String index(HttpServletRequest request, HttpServletResponse response) { - throw new IllegalStateException(); - } - - public String handleIllegalStateException(HttpServletRequest request, HttpServletResponse response, - IllegalStateException ex) throws IOException { - return "handleIllegalStateExceptionString"; - } - } - -}