1 2 17 18 package org.springframework.web.servlet.mvc; 19 20 import java.util.Properties ; 21 22 import javax.servlet.RequestDispatcher ; 23 import javax.servlet.Servlet ; 24 import javax.servlet.ServletConfig ; 25 import javax.servlet.ServletContext ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.ServletResponse ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import junit.framework.TestCase; 32 import org.easymock.MockControl; 33 34 import org.springframework.mock.web.MockHttpServletRequest; 35 import org.springframework.mock.web.MockHttpServletResponse; 36 import org.springframework.web.context.support.StaticWebApplicationContext; 37 import org.springframework.web.servlet.ModelAndView; 38 import org.springframework.web.util.UrlPathHelper; 39 40 44 public class ControllerTests extends TestCase { 45 46 public void testParameterizableViewController() throws Exception { 47 String viewName = "viewName"; 48 ParameterizableViewController pvc = new ParameterizableViewController(); 49 pvc.setViewName(viewName); 50 pvc.initApplicationContext(); 51 ModelAndView mv = pvc.handleRequest(new MockHttpServletRequest("GET", "foo.html"), null); 53 assertTrue("model has no data", mv.getModel().size() == 0); 54 assertTrue("model has correct viewname", mv.getViewName().equals(viewName)); 55 assertTrue("getViewName matches", pvc.getViewName().equals(viewName)); 56 } 57 58 public void testParameterizableViewControllerWithPropertyNotSet() { 59 ParameterizableViewController pvc = new ParameterizableViewController(); 60 try { 61 pvc.initApplicationContext(); 62 fail("should require viewName property to be set"); 63 } 64 catch (IllegalArgumentException ex){ 65 assertTrue("meaningful exception message", ex.getMessage().indexOf("viewName") != -1); 67 } 68 } 69 70 public void testServletForwardingController() throws Exception { 71 ServletForwardingController sfc = new ServletForwardingController(); 72 sfc.setServletName("action"); 73 doTestServletForwardingController(sfc, false); 74 } 75 76 public void testServletForwardingControllerWithInclude() throws Exception { 77 ServletForwardingController sfc = new ServletForwardingController(); 78 sfc.setServletName("action"); 79 doTestServletForwardingController(sfc, true); 80 } 81 82 public void testServletForwardingControllerWithBeanName() throws Exception { 83 ServletForwardingController sfc = new ServletForwardingController(); 84 sfc.setBeanName("action"); 85 doTestServletForwardingController(sfc, false); 86 } 87 88 private void doTestServletForwardingController(ServletForwardingController sfc, boolean include) 89 throws Exception { 90 91 MockControl requestControl = MockControl.createControl(HttpServletRequest .class); 92 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 93 MockControl responseControl = MockControl.createControl(HttpServletResponse .class); 94 HttpServletResponse response = (HttpServletResponse ) responseControl.getMock(); 95 MockControl contextControl = MockControl.createControl(ServletContext .class); 96 ServletContext context = (ServletContext ) contextControl.getMock(); 97 MockControl dispatcherControl = MockControl.createControl(RequestDispatcher .class); 98 RequestDispatcher dispatcher = (RequestDispatcher ) dispatcherControl.getMock(); 99 100 request.getMethod(); 101 requestControl.setReturnValue("GET", 1); 102 context.getNamedDispatcher("action"); 103 contextControl.setReturnValue(dispatcher, 1); 104 if (include) { 105 request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE); 106 requestControl.setReturnValue("somePath", 1); 107 dispatcher.include(request, response); 108 dispatcherControl.setVoidCallable(1); 109 } 110 else { 111 request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE); 112 requestControl.setReturnValue(null, 1); 113 dispatcher.forward(request, response); 114 dispatcherControl.setVoidCallable(1); 115 } 116 requestControl.replay(); 117 contextControl.replay(); 118 dispatcherControl.replay(); 119 120 StaticWebApplicationContext sac = new StaticWebApplicationContext(); 121 sac.setServletContext(context); 122 sfc.setApplicationContext(sac); 123 assertNull(sfc.handleRequest(request, response)); 124 125 requestControl.verify(); 126 contextControl.verify(); 127 dispatcherControl.verify(); 128 } 129 130 public void testServletWrappingController() throws Exception { 131 HttpServletRequest request = new MockHttpServletRequest("GET", "/somePath"); 132 HttpServletResponse response = new MockHttpServletResponse(); 133 134 ServletWrappingController swc = new ServletWrappingController(); 135 swc.setServletClass(TestServlet.class); 136 swc.setServletName("action"); 137 Properties props = new Properties (); 138 props.setProperty("config", "myValue"); 139 swc.setInitParameters(props); 140 141 swc.afterPropertiesSet(); 142 assertNotNull(TestServlet.config); 143 assertEquals("action", TestServlet.config.getServletName()); 144 assertEquals("myValue", TestServlet.config.getInitParameter("config")); 145 assertNull(TestServlet.request); 146 assertFalse(TestServlet.destroyed); 147 148 assertNull(swc.handleRequest(request, response)); 149 assertEquals(request, TestServlet.request); 150 assertEquals(response, TestServlet.response); 151 assertFalse(TestServlet.destroyed); 152 153 swc.destroy(); 154 assertTrue(TestServlet.destroyed); 155 } 156 157 public void testServletWrappingControllerWithBeanName() throws Exception { 158 HttpServletRequest request = new MockHttpServletRequest("GET", "/somePath"); 159 HttpServletResponse response = new MockHttpServletResponse(); 160 161 ServletWrappingController swc = new ServletWrappingController(); 162 swc.setServletClass(TestServlet.class); 163 swc.setBeanName("action"); 164 165 swc.afterPropertiesSet(); 166 assertNotNull(TestServlet.config); 167 assertEquals("action", TestServlet.config.getServletName()); 168 assertNull(TestServlet.request); 169 assertFalse(TestServlet.destroyed); 170 171 assertNull(swc.handleRequest(request, response)); 172 assertEquals(request, TestServlet.request); 173 assertEquals(response, TestServlet.response); 174 assertFalse(TestServlet.destroyed); 175 176 swc.destroy(); 177 assertTrue(TestServlet.destroyed); 178 } 179 180 181 public static class TestServlet implements Servlet { 182 183 private static ServletConfig config; 184 private static ServletRequest request; 185 private static ServletResponse response; 186 private static boolean destroyed; 187 188 public TestServlet() { 189 config = null; 190 request = null; 191 response = null; 192 destroyed = false; 193 } 194 195 public void init(ServletConfig servletConfig) { 196 config = servletConfig; 197 } 198 199 public ServletConfig getServletConfig() { 200 return config; 201 } 202 203 public void service(ServletRequest servletRequest, ServletResponse servletResponse) { 204 request = servletRequest; 205 response = servletResponse; 206 } 207 208 public String getServletInfo() { 209 return "TestServlet"; 210 } 211 212 public void destroy() { 213 destroyed = true; 214 } 215 } 216 217 } 218 | Popular Tags |