KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > ControllerTests


1
2 /*
3  * Copyright 2002-2005 the original author or authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.springframework.web.servlet.mvc;
19
20 import java.util.Properties JavaDoc;
21
22 import javax.servlet.RequestDispatcher JavaDoc;
23 import javax.servlet.Servlet JavaDoc;
24 import javax.servlet.ServletConfig JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.ServletResponse JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
41  * @author Rod Johnson
42  * @author Juergen Hoeller
43  */

44 public class ControllerTests extends TestCase {
45
46     public void testParameterizableViewController() throws Exception JavaDoc {
47         String JavaDoc viewName = "viewName";
48         ParameterizableViewController pvc = new ParameterizableViewController();
49         pvc.setViewName(viewName);
50         pvc.initApplicationContext();
51         // We don't care about the params.
52
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 JavaDoc ex){
65             // expected
66
assertTrue("meaningful exception message", ex.getMessage().indexOf("viewName") != -1);
67         }
68     }
69
70     public void testServletForwardingController() throws Exception JavaDoc {
71         ServletForwardingController sfc = new ServletForwardingController();
72         sfc.setServletName("action");
73         doTestServletForwardingController(sfc, false);
74     }
75
76     public void testServletForwardingControllerWithInclude() throws Exception JavaDoc {
77         ServletForwardingController sfc = new ServletForwardingController();
78         sfc.setServletName("action");
79         doTestServletForwardingController(sfc, true);
80     }
81
82     public void testServletForwardingControllerWithBeanName() throws Exception JavaDoc {
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 JavaDoc {
90
91         MockControl requestControl = MockControl.createControl(HttpServletRequest JavaDoc.class);
92         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
93         MockControl responseControl = MockControl.createControl(HttpServletResponse JavaDoc.class);
94         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) responseControl.getMock();
95         MockControl contextControl = MockControl.createControl(ServletContext JavaDoc.class);
96         ServletContext JavaDoc context = (ServletContext JavaDoc) contextControl.getMock();
97         MockControl dispatcherControl = MockControl.createControl(RequestDispatcher JavaDoc.class);
98         RequestDispatcher JavaDoc dispatcher = (RequestDispatcher JavaDoc) 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 JavaDoc {
131         HttpServletRequest JavaDoc request = new MockHttpServletRequest("GET", "/somePath");
132         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
133
134         ServletWrappingController swc = new ServletWrappingController();
135         swc.setServletClass(TestServlet.class);
136         swc.setServletName("action");
137         Properties JavaDoc props = new Properties JavaDoc();
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 JavaDoc {
158         HttpServletRequest JavaDoc request = new MockHttpServletRequest("GET", "/somePath");
159         HttpServletResponse JavaDoc 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 JavaDoc {
182
183         private static ServletConfig JavaDoc config;
184         private static ServletRequest JavaDoc request;
185         private static ServletResponse JavaDoc 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 JavaDoc servletConfig) {
196             config = servletConfig;
197         }
198
199         public ServletConfig JavaDoc getServletConfig() {
200             return config;
201         }
202
203         public void service(ServletRequest JavaDoc servletRequest, ServletResponse JavaDoc servletResponse) {
204             request = servletRequest;
205             response = servletResponse;
206         }
207
208         public String JavaDoc getServletInfo() {
209             return "TestServlet";
210         }
211
212         public void destroy() {
213             destroyed = true;
214         }
215     }
216
217 }
218
Popular Tags