KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > DispatcherServletTests


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

16
17 package org.springframework.web.servlet;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import javax.servlet.Servlet JavaDoc;
23 import javax.servlet.ServletConfig JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.springframework.beans.MutablePropertyValues;
32 import org.springframework.beans.PropertyValue;
33 import org.springframework.beans.TestBean;
34 import org.springframework.context.support.DefaultMessageSourceResolvable;
35 import org.springframework.mock.web.MockHttpServletRequest;
36 import org.springframework.mock.web.MockHttpServletResponse;
37 import org.springframework.mock.web.MockServletConfig;
38 import org.springframework.mock.web.MockServletContext;
39 import org.springframework.validation.BindException;
40 import org.springframework.web.bind.EscapedErrors;
41 import org.springframework.web.context.WebApplicationContext;
42 import org.springframework.web.context.support.StaticWebApplicationContext;
43 import org.springframework.web.multipart.MaxUploadSizeExceededException;
44 import org.springframework.web.multipart.MultipartHttpServletRequest;
45 import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
46 import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
47 import org.springframework.web.servlet.mvc.BaseCommandController;
48 import org.springframework.web.servlet.mvc.Controller;
49 import org.springframework.web.servlet.support.RequestContext;
50 import org.springframework.web.servlet.support.RequestContextUtils;
51 import org.springframework.web.servlet.theme.AbstractThemeResolver;
52 import org.springframework.web.util.UrlPathHelper;
53
54 /**
55  * @author Rod Johnson
56  * @author Juergen Hoeller
57  */

58 public class DispatcherServletTests extends TestCase {
59     
60     private static final String JavaDoc URL_KNOWN_ONLY_PARENT = "/knownOnlyToParent.do";
61
62     private MockServletConfig servletConfig;
63
64     private DispatcherServlet simpleDispatcherServlet;
65
66     private DispatcherServlet complexDispatcherServlet;
67
68     protected void setUp() throws ServletException JavaDoc {
69         servletConfig = new MockServletConfig(new MockServletContext(), "simple");
70         MockServletConfig complexConfig = new MockServletConfig(servletConfig.getServletContext(), "complex");
71         complexConfig.addInitParameter("publishContext", "false");
72         complexConfig.addInitParameter("class", "notWritable");
73         complexConfig.addInitParameter("unknownParam", "someValue");
74
75         simpleDispatcherServlet = new DispatcherServlet();
76         simpleDispatcherServlet.setContextClass(SimpleWebApplicationContext.class);
77         simpleDispatcherServlet.init(servletConfig);
78
79         complexDispatcherServlet = new DispatcherServlet();
80         complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
81         complexDispatcherServlet.setNamespace("test");
82         complexDispatcherServlet.addRequiredProperty("publishContext");
83         complexDispatcherServlet.init(complexConfig);
84     }
85
86     private ServletContext JavaDoc getServletContext() {
87         return servletConfig.getServletContext();
88     }
89
90     public void testDispatcherServlets() {
91         assertTrue("Correct namespace",
92                 ("simple" + FrameworkServlet.DEFAULT_NAMESPACE_SUFFIX).equals(simpleDispatcherServlet.getNamespace()));
93         assertTrue("Correct attribute",
94                 (FrameworkServlet.SERVLET_CONTEXT_PREFIX + "simple").equals(simpleDispatcherServlet.getServletContextAttributeName()));
95         assertTrue("Context published",
96                 simpleDispatcherServlet.getWebApplicationContext() ==
97                 getServletContext().getAttribute(FrameworkServlet.SERVLET_CONTEXT_PREFIX + "simple"));
98
99         assertTrue("Correct namespace", "test".equals(complexDispatcherServlet.getNamespace()));
100         assertTrue("Correct attribute",
101                 (FrameworkServlet.SERVLET_CONTEXT_PREFIX + "complex").equals(complexDispatcherServlet.getServletContextAttributeName()));
102         assertTrue("Context not published",
103                 getServletContext().getAttribute(FrameworkServlet.SERVLET_CONTEXT_PREFIX + "complex") == null);
104     }
105
106     public void testInvalidRequest() throws Exception JavaDoc {
107         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/invalid.do");
108         MockHttpServletResponse response = new MockHttpServletResponse();
109         simpleDispatcherServlet.service(request, response);
110         assertTrue("Not forwarded", response.getForwardedUrl() == null);
111         assertTrue("correct error code", response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
112     }
113
114     public void testRequestHandledEvent() throws Exception JavaDoc {
115         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
116         MockHttpServletResponse response = new MockHttpServletResponse();
117         complexDispatcherServlet.service(request, response);
118         ComplexWebApplicationContext.TestApplicationListener listener =
119                 (ComplexWebApplicationContext.TestApplicationListener)
120                 complexDispatcherServlet.getWebApplicationContext().getBean("testListener");
121         assertEquals(1, listener.counter);
122     }
123
124     public void testPublishEventsOff() throws Exception JavaDoc {
125         complexDispatcherServlet.setPublishEvents(false);
126         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
127         MockHttpServletResponse response = new MockHttpServletResponse();
128         complexDispatcherServlet.service(request, response);
129         ComplexWebApplicationContext.TestApplicationListener listener =
130                 (ComplexWebApplicationContext.TestApplicationListener)
131                 complexDispatcherServlet.getWebApplicationContext().getBean("testListener");
132         assertEquals(0, listener.counter);
133     }
134
135     public void testFormRequest() throws Exception JavaDoc {
136         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do");
137         request.addPreferredLocale(Locale.CANADA);
138         MockHttpServletResponse response = new MockHttpServletResponse();
139
140         simpleDispatcherServlet.service(request, response);
141         assertTrue("forwarded to form", "form".equals(response.getForwardedUrl()));
142         DefaultMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String JavaDoc[] {"test"});
143         RequestContext rc = new RequestContext(request);
144
145         assertTrue("hasn't RequestContext attribute", request.getAttribute("rc") == null);
146         assertTrue("Correct WebApplicationContext",
147                 RequestContextUtils.getWebApplicationContext(request) instanceof SimpleWebApplicationContext);
148         assertTrue("Correct context path", rc.getContextPath().equals(request.getContextPath()));
149         assertTrue("Correct locale", Locale.CANADA.equals(RequestContextUtils.getLocale(request)));
150         assertTrue("Correct theme",
151                 AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME.equals(RequestContextUtils.getTheme(request).getName()));
152         assertTrue("Correct message", "Canadian & test message".equals(rc.getMessage("test")));
153
154         assertTrue("Correct WebApplicationContext", rc.getWebApplicationContext() == simpleDispatcherServlet.getWebApplicationContext());
155         assertTrue("Correct Errors", !(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME) instanceof EscapedErrors));
156         assertTrue("Correct Errors", !(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, false) instanceof EscapedErrors));
157         assertTrue("Correct Errors", rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, true) instanceof EscapedErrors);
158         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test"));
159         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, false));
160         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, true));
161         assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable));
162         assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, false));
163         assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, true));
164         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", "default"));
165         assertEquals("Correct message", "default", rc.getMessage("testa", "default"));
166         assertEquals("Correct message", "default &", rc.getMessage("testa", null, "default &", true));
167     }
168
169     public void testParameterizableViewController() throws Exception JavaDoc {
170         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/view.do");
171         request.addUserRole("role1");
172         MockHttpServletResponse response = new MockHttpServletResponse();
173         complexDispatcherServlet.service(request, response);
174         assertTrue("forwarded to form", "myform.jsp".equals(response.getForwardedUrl()));
175     }
176
177     public void testHandlerInterceptorSuppressesView() throws Exception JavaDoc {
178         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/view.do");
179         request.addUserRole("role1");
180         request.addParameter("noView", "true");
181         MockHttpServletResponse response = new MockHttpServletResponse();
182         complexDispatcherServlet.service(request, response);
183         assertTrue("Not forwarded", response.getForwardedUrl() == null);
184     }
185
186     public void testLocaleRequest() throws Exception JavaDoc {
187         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
188         request.addPreferredLocale(Locale.CANADA);
189         MockHttpServletResponse response = new MockHttpServletResponse();
190         assertEquals(98, simpleDispatcherServlet.getLastModified(request));
191         simpleDispatcherServlet.service(request, response);
192         assertTrue("Not forwarded", response.getForwardedUrl() == null);
193     }
194
195     public void testUnknownRequest() throws Exception JavaDoc {
196         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do");
197         MockHttpServletResponse response = new MockHttpServletResponse();
198         complexDispatcherServlet.service(request, response);
199         assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl());
200         assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(ServletException JavaDoc.class));
201     }
202
203     public void testAnotherFormRequest() throws Exception JavaDoc {
204         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do;jsessionid=xxx");
205         request.addPreferredLocale(Locale.CANADA);
206         MockHttpServletResponse response = new MockHttpServletResponse();
207
208         complexDispatcherServlet.service(request, response);
209         assertTrue("forwarded to form", "myform.jsp".equals(response.getForwardedUrl()));
210         assertTrue("has RequestContext attribute", request.getAttribute("rc") != null);
211         DefaultMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String JavaDoc[] {"test"});
212
213         RequestContext rc = (RequestContext) request.getAttribute("rc");
214         assertTrue("Not in HTML escaping mode", !rc.isDefaultHtmlEscape());
215         assertTrue("Correct WebApplicationContext", rc.getWebApplicationContext() == complexDispatcherServlet.getWebApplicationContext());
216         assertTrue("Correct context path", rc.getContextPath().equals(request.getContextPath()));
217         assertTrue("Correct locale", Locale.CANADA.equals(rc.getLocale()));
218         assertTrue("Correct Errors", !(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME) instanceof EscapedErrors));
219         assertTrue("Correct Errors", !(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, false) instanceof EscapedErrors));
220         assertTrue("Correct Errors", rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, true) instanceof EscapedErrors);
221         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test"));
222         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, false));
223         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, true));
224         assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable));
225         assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, false));
226         assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, true));
227
228         rc.setDefaultHtmlEscape(true);
229         assertTrue("Is in HTML escaping mode", rc.isDefaultHtmlEscape());
230         assertTrue("Correct Errors", rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME) instanceof EscapedErrors);
231         assertTrue("Correct Errors", !(rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, false) instanceof EscapedErrors));
232         assertTrue("Correct Errors", rc.getErrors(BaseCommandController.DEFAULT_COMMAND_NAME, true) instanceof EscapedErrors);
233         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test"));
234         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, false));
235         assertEquals("Correct message", "Canadian & test message", rc.getMessage("test", null, true));
236         assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable));
237         assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, false));
238         assertEquals("Correct message", "Canadian & test message", rc.getMessage(resolvable, true));
239     }
240
241     public void testAnotherLocaleRequest() throws Exception JavaDoc {
242         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
243         request.addPreferredLocale(Locale.CANADA);
244         request.addUserRole("role1");
245         MockHttpServletResponse response = new MockHttpServletResponse();
246         assertEquals(99, complexDispatcherServlet.getLastModified(request));
247         complexDispatcherServlet.service(request, response);
248         assertTrue("Not forwarded", response.getForwardedUrl() == null);
249         assertTrue(request.getAttribute("test1") != null);
250         assertTrue(request.getAttribute("test1x") == null);
251         assertTrue(request.getAttribute("test1y") == null);
252         assertTrue(request.getAttribute("test2") != null);
253         assertTrue(request.getAttribute("test2x") == null);
254         assertTrue(request.getAttribute("test2y") == null);
255     }
256
257     public void testExistingMultipartRequest() throws Exception JavaDoc {
258         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
259         request.addPreferredLocale(Locale.CANADA);
260         request.addUserRole("role1");
261         MockHttpServletResponse response = new MockHttpServletResponse();
262         ComplexWebApplicationContext.MockMultipartResolver multipartResolver =
263                 (ComplexWebApplicationContext.MockMultipartResolver)
264             complexDispatcherServlet.getWebApplicationContext().getBean("multipartResolver");
265         MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
266         complexDispatcherServlet.service(multipartRequest, response);
267         //System.out.println(response.getForwardedUrl());
268
multipartResolver.cleanupMultipart(multipartRequest);
269         assertNotNull(request.getAttribute("cleanedUp"));
270     }
271
272     public void testMultipartResolutionFailed() throws Exception JavaDoc {
273         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
274         request.addPreferredLocale(Locale.CANADA);
275         request.addUserRole("role1");
276         request.setAttribute("fail", Boolean.TRUE);
277         MockHttpServletResponse response = new MockHttpServletResponse();
278         complexDispatcherServlet.service(request, response);
279         assertTrue("forwarded to failed", "failed0.jsp".equals(response.getForwardedUrl()));
280         assertEquals(200, response.getStatus());
281         assertTrue("correct exception",
282             request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE) instanceof MaxUploadSizeExceededException);
283     }
284
285     public void testHandlerInterceptorAbort() throws Exception JavaDoc {
286         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
287         request.addParameter("abort", "true");
288         request.addPreferredLocale(Locale.CANADA);
289         request.addUserRole("role1");
290         MockHttpServletResponse response = new MockHttpServletResponse();
291         complexDispatcherServlet.service(request, response);
292         assertTrue("Not forwarded", response.getForwardedUrl() == null);
293         assertTrue(request.getAttribute("test1") != null);
294         assertTrue(request.getAttribute("test1x") != null);
295         assertTrue(request.getAttribute("test1y") == null);
296         assertTrue(request.getAttribute("test2") == null);
297         assertTrue(request.getAttribute("test2x") == null);
298         assertTrue(request.getAttribute("test2y") == null);
299     }
300
301     public void testModelAndViewDefiningException() throws Exception JavaDoc {
302         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
303         request.addPreferredLocale(Locale.CANADA);
304         request.addUserRole("role1");
305         request.addParameter("fail", "yes");
306         MockHttpServletResponse response = new MockHttpServletResponse();
307         try {
308             complexDispatcherServlet.service(request, response);
309             assertEquals(200, response.getStatus());
310             assertTrue("forwarded to failed", "failed1.jsp".equals(response.getForwardedUrl()));
311         }
312         catch (ServletException JavaDoc ex) {
313             fail("Should not have thrown ServletException: " + ex.getMessage());
314         }
315     }
316
317     public void testSimpleMappingExceptionResolverWithSpecificHandler1() throws Exception JavaDoc {
318         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
319         request.addPreferredLocale(Locale.CANADA);
320         request.addUserRole("role1");
321         request.addParameter("access", "yes");
322         MockHttpServletResponse response = new MockHttpServletResponse();
323         complexDispatcherServlet.service(request, response);
324         assertEquals(200, response.getStatus());
325         assertEquals("forwarded to failed", "failed2.jsp", response.getForwardedUrl());
326         assertTrue("Exception exposed", request.getAttribute("exception") instanceof IllegalAccessException JavaDoc);
327     }
328
329     public void testSimpleMappingExceptionResolverWithSpecificHandler2() throws Exception JavaDoc {
330         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
331         request.addPreferredLocale(Locale.CANADA);
332         request.addUserRole("role1");
333         request.addParameter("servlet", "yes");
334         MockHttpServletResponse response = new MockHttpServletResponse();
335         complexDispatcherServlet.service(request, response);
336         assertEquals(200, response.getStatus());
337         assertEquals("forwarded to failed", "failed3.jsp", response.getForwardedUrl());
338         assertTrue("Exception exposed", request.getAttribute("exception") instanceof ServletException JavaDoc);
339     }
340
341     public void testSimpleMappingExceptionResolverWithAllHandlers1() throws Exception JavaDoc {
342         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/loc.do");
343         request.addPreferredLocale(Locale.CANADA);
344         request.addUserRole("role1");
345         request.addParameter("access", "yes");
346         MockHttpServletResponse response = new MockHttpServletResponse();
347         complexDispatcherServlet.service(request, response);
348         assertEquals(500, response.getStatus());
349         assertEquals("forwarded to failed", "failed1.jsp", response.getForwardedUrl());
350         assertTrue("Exception exposed", request.getAttribute("exception") instanceof IllegalAccessException JavaDoc);
351     }
352
353     public void testSimpleMappingExceptionResolverWithAllHandlers2() throws Exception JavaDoc {
354         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/loc.do");
355         request.addPreferredLocale(Locale.CANADA);
356         request.addUserRole("role1");
357         request.addParameter("servlet", "yes");
358         MockHttpServletResponse response = new MockHttpServletResponse();
359         complexDispatcherServlet.service(request, response);
360         assertEquals(500, response.getStatus());
361         assertEquals("forwarded to failed", "failed1.jsp", response.getForwardedUrl());
362         assertTrue("Exception exposed", request.getAttribute("exception") instanceof ServletException JavaDoc);
363     }
364
365     public void testSimpleMappingExceptionResolverWithDefaultErrorView() throws Exception JavaDoc {
366         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
367         request.addPreferredLocale(Locale.CANADA);
368         request.addUserRole("role1");
369         request.addParameter("exception", "yes");
370         MockHttpServletResponse response = new MockHttpServletResponse();
371         complexDispatcherServlet.service(request, response);
372         assertEquals(200, response.getStatus());
373         assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl());
374         assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(RuntimeException JavaDoc.class));
375     }
376
377     public void testLocaleChangeInterceptor1() throws Exception JavaDoc {
378         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
379         request.addPreferredLocale(Locale.GERMAN);
380         request.addUserRole("role2");
381         request.addParameter("locale", "en");
382         MockHttpServletResponse response = new MockHttpServletResponse();
383         complexDispatcherServlet.service(request, response);
384         assertEquals(200, response.getStatus());
385         assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl());
386         assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(ServletException JavaDoc.class));
387     }
388
389     public void testLocaleChangeInterceptor2() throws Exception JavaDoc {
390         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
391         request.addPreferredLocale(Locale.GERMAN);
392         request.addUserRole("role2");
393         request.addParameter("locale", "en");
394         request.addParameter("locale2", "en_CA");
395         MockHttpServletResponse response = new MockHttpServletResponse();
396         complexDispatcherServlet.service(request, response);
397         assertTrue("Not forwarded", response.getForwardedUrl() == null);
398     }
399
400     public void testThemeChangeInterceptor1() throws Exception JavaDoc {
401         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
402         request.addPreferredLocale(Locale.CANADA);
403         request.addUserRole("role1");
404         request.addParameter("theme", "mytheme");
405         MockHttpServletResponse response = new MockHttpServletResponse();
406         complexDispatcherServlet.service(request, response);
407         assertEquals(200, response.getStatus());
408         assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl());
409         assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(ServletException JavaDoc.class));
410     }
411
412     public void testThemeChangeInterceptor2() throws Exception JavaDoc {
413         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
414         request.addPreferredLocale(Locale.CANADA);
415         request.addUserRole("role1");
416         request.addParameter("theme", "mytheme");
417         request.addParameter("theme2", "theme");
418         MockHttpServletResponse response = new MockHttpServletResponse();
419         try {
420             complexDispatcherServlet.service(request, response);
421             assertTrue("Not forwarded", response.getForwardedUrl() == null);
422         }
423         catch (ServletException JavaDoc ex) {
424             fail("Should not have thrown ServletException: " + ex.getMessage());
425         }
426     }
427
428     public void testNotAuthorized() throws Exception JavaDoc {
429         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
430         request.addPreferredLocale(Locale.CANADA);
431         MockHttpServletResponse response = new MockHttpServletResponse();
432         try {
433             complexDispatcherServlet.service(request, response);
434             assertTrue("Correct response", response.getStatus() == HttpServletResponse.SC_FORBIDDEN);
435         }
436         catch (ServletException JavaDoc ex) {
437             fail("Should not have thrown ServletException: " + ex.getMessage());
438         }
439     }
440
441     public void testHeadMethodWithExplicitHandling() throws Exception JavaDoc {
442         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "HEAD", "/head.do");
443         MockHttpServletResponse response = new MockHttpServletResponse();
444         complexDispatcherServlet.service(request, response);
445         assertEquals(5, response.getContentLength());
446
447         request = new MockHttpServletRequest(getServletContext(), "GET", "/head.do");
448         response = new MockHttpServletResponse();
449         complexDispatcherServlet.service(request, response);
450         assertEquals("", response.getContentAsString());
451     }
452
453     public void testHeadMethodWithNoBodyResponse() throws Exception JavaDoc {
454         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "HEAD", "/body.do");
455         MockHttpServletResponse response = new MockHttpServletResponse();
456         complexDispatcherServlet.service(request, response);
457         assertEquals(4, response.getContentLength());
458
459         request = new MockHttpServletRequest(getServletContext(), "GET", "/body.do");
460         response = new MockHttpServletResponse();
461         complexDispatcherServlet.service(request, response);
462         assertEquals("body", response.getContentAsString());
463     }
464
465     public void testServletHandlerAdapter() throws Exception JavaDoc {
466         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/servlet.do");
467         MockHttpServletResponse response = new MockHttpServletResponse();
468         complexDispatcherServlet.service(request, response);
469         assertEquals("body", response.getContentAsString());
470
471         Servlet JavaDoc myServlet = (Servlet JavaDoc) complexDispatcherServlet.getWebApplicationContext().getBean("myServlet");
472         assertEquals("myServlet", myServlet.getServletConfig().getServletName());
473         assertEquals(getServletContext(), myServlet.getServletConfig().getServletContext());
474         complexDispatcherServlet.destroy();
475         assertNull(myServlet.getServletConfig());
476     }
477
478     public void testNotDetectAllHandlerMappings() throws ServletException JavaDoc, IOException JavaDoc {
479         DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
480         complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
481         complexDispatcherServlet.setNamespace("test");
482         complexDispatcherServlet.setDetectAllHandlerMappings(false);
483         complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
484
485         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do");
486         MockHttpServletResponse response = new MockHttpServletResponse();
487         complexDispatcherServlet.service(request, response);
488         assertTrue(response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
489     }
490
491     public void testHandlerNotMappedWithAutodetect() throws ServletException JavaDoc, IOException JavaDoc {
492         DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
493         // No parent
494
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
495         complexDispatcherServlet.setNamespace("test");
496         complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
497
498         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET",
499                 URL_KNOWN_ONLY_PARENT);
500         MockHttpServletResponse response = new MockHttpServletResponse();
501         complexDispatcherServlet.service(request, response);
502         assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
503     }
504     
505     public void testDetectHandlerMappingFromParent() throws ServletException JavaDoc, IOException JavaDoc {
506         // Create a parent context that includes a mapping
507
StaticWebApplicationContext parent = new StaticWebApplicationContext();
508         parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());
509         
510         MutablePropertyValues pvs = new MutablePropertyValues();
511         pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));
512         
513         parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
514         parent.refresh();
515
516         DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
517         // Will have parent
518
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
519         complexDispatcherServlet.setNamespace("test");
520         
521         ServletConfig JavaDoc config = new MockServletConfig(getServletContext(), "complex");
522         config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
523         complexDispatcherServlet.init(config);
524
525         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET",
526                 URL_KNOWN_ONLY_PARENT);
527         MockHttpServletResponse response = new MockHttpServletResponse();
528         complexDispatcherServlet.service(request, response);
529         
530         assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(),
531                 response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
532     }
533
534     public void testNotDetectAllHandlerExceptionResolvers() throws ServletException JavaDoc, IOException JavaDoc {
535         DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
536         complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
537         complexDispatcherServlet.setNamespace("test");
538         complexDispatcherServlet.setDetectAllHandlerExceptionResolvers(false);
539         complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
540
541         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do");
542         MockHttpServletResponse response = new MockHttpServletResponse();
543         try {
544             complexDispatcherServlet.service(request, response);
545             fail("Should have thrown ServletException");
546         }
547         catch (ServletException JavaDoc ex) {
548             // expected
549
assertTrue(ex.getMessage().indexOf("No adapter for handler") != -1);
550         }
551     }
552
553     public void testNotDetectAllViewResolvers() throws ServletException JavaDoc, IOException JavaDoc {
554         DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
555         complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
556         complexDispatcherServlet.setNamespace("test");
557         complexDispatcherServlet.setDetectAllViewResolvers(false);
558         complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
559
560         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do");
561         MockHttpServletResponse response = new MockHttpServletResponse();
562         try {
563             complexDispatcherServlet.service(request, response);
564             fail("Should have thrown ServletException");
565         }
566         catch (ServletException JavaDoc ex) {
567             // expected
568
assertTrue(ex.getMessage().indexOf("failed0") != -1);
569         }
570     }
571
572     public void testCleanupAfterIncludeWithRemove() throws ServletException JavaDoc, IOException JavaDoc {
573         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
574         MockHttpServletResponse response = new MockHttpServletResponse();
575
576         request.setAttribute("test1", "value1");
577         request.setAttribute("test2", "value2");
578         WebApplicationContext wac = new StaticWebApplicationContext();
579         request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
580
581         request.setAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE, "/form.do");
582         simpleDispatcherServlet.service(request, response);
583         assertTrue("forwarded to form", "form".equals(response.getIncludedUrl()));
584
585         assertEquals("value1", request.getAttribute("test1"));
586         assertEquals("value2", request.getAttribute("test2"));
587         assertEquals(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
588         assertNull(request.getAttribute("command"));
589     }
590
591     public void testCleanupAfterIncludeWithRestore() throws ServletException JavaDoc, IOException JavaDoc {
592         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
593         MockHttpServletResponse response = new MockHttpServletResponse();
594
595         request.setAttribute("test1", "value1");
596         request.setAttribute("test2", "value2");
597         WebApplicationContext wac = new StaticWebApplicationContext();
598         request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
599         TestBean command = new TestBean();
600         request.setAttribute("command", command);
601
602         request.setAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE, "/form.do");
603         simpleDispatcherServlet.service(request, response);
604         assertTrue("forwarded to form", "form".equals(response.getIncludedUrl()));
605
606         assertEquals("value1", request.getAttribute("test1"));
607         assertEquals("value2", request.getAttribute("test2"));
608         assertSame(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
609         assertSame(command, request.getAttribute("command"));
610     }
611
612     public void testNoCleanupAfterInclude() throws ServletException JavaDoc, IOException JavaDoc {
613         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
614         MockHttpServletResponse response = new MockHttpServletResponse();
615
616         request.setAttribute("test1", "value1");
617         request.setAttribute("test2", "value2");
618         WebApplicationContext wac = new StaticWebApplicationContext();
619         request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
620         TestBean command = new TestBean();
621         request.setAttribute("command", command);
622
623         request.setAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE, "/form.do");
624         simpleDispatcherServlet.setCleanupAfterInclude(false);
625         simpleDispatcherServlet.service(request, response);
626         assertTrue("forwarded to form", "form".equals(response.getIncludedUrl()));
627
628         assertEquals("value1", request.getAttribute("test1"));
629         assertEquals("value2", request.getAttribute("test2"));
630         assertSame(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
631         assertNotSame(command, request.getAttribute("command"));
632     }
633
634     public void testThrowawayController() throws Exception JavaDoc {
635         SimpleWebApplicationContext.TestThrowawayController.counter = 0;
636         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/throwaway.do");
637         request.addParameter("myInt", "5");
638         MockHttpServletResponse response = new MockHttpServletResponse();
639
640         simpleDispatcherServlet.service(request, response);
641         assertTrue("Correct response", "view5".equals(response.getForwardedUrl()));
642         assertEquals(1, SimpleWebApplicationContext.TestThrowawayController.counter);
643
644         simpleDispatcherServlet.service(request, response);
645         assertTrue("Correct response", "view5".equals(response.getForwardedUrl()));
646         assertEquals(2, SimpleWebApplicationContext.TestThrowawayController.counter);
647     }
648
649     public void testThrowawayControllerWithBindingFailure() throws Exception JavaDoc {
650         SimpleWebApplicationContext.TestThrowawayController.counter = 0;
651         MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/throwaway.do");
652         request.addParameter("myInt", "5x");
653         MockHttpServletResponse response = new MockHttpServletResponse();
654
655         try {
656             simpleDispatcherServlet.service(request, response);
657             fail("Should have thrown ServletException");
658         }
659         catch (ServletException JavaDoc ex) {
660             // expected
661
assertTrue(ex.getRootCause() instanceof BindException);
662             assertEquals(1, SimpleWebApplicationContext.TestThrowawayController.counter);
663         }
664     }
665
666     public void testWebApplicationContextLookup() {
667         MockServletContext servletContext = new MockServletContext();
668         MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/invalid.do");
669
670         try {
671             RequestContextUtils.getWebApplicationContext(request);
672             fail("Should have thrown IllegalStateException");
673         }
674         catch (IllegalStateException JavaDoc ex) {
675             // expected
676
}
677
678         try {
679             RequestContextUtils.getWebApplicationContext(request, servletContext);
680             fail("Should have thrown IllegalStateException");
681         }
682         catch (IllegalStateException JavaDoc ex) {
683             // expected
684
}
685
686         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
687                 new StaticWebApplicationContext());
688         try {
689             RequestContextUtils.getWebApplicationContext(request, servletContext);
690         }
691         catch (IllegalStateException JavaDoc ex) {
692             fail("Should not have thrown IllegalStateException: " + ex.getMessage());
693         }
694     }
695
696
697     public static class ControllerFromParent implements Controller {
698
699         public ModelAndView handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
700             return new ModelAndView(ControllerFromParent.class.getName());
701         }
702     }
703
704 }
705
Popular Tags