KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > handler > SimpleUrlHandlerMappingTests


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.handler;
18
19 import junit.framework.TestCase;
20
21 import org.springframework.beans.FatalBeanException;
22 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
23 import org.springframework.mock.web.MockHttpServletRequest;
24 import org.springframework.mock.web.MockServletContext;
25 import org.springframework.web.context.support.XmlWebApplicationContext;
26 import org.springframework.web.servlet.HandlerExecutionChain;
27 import org.springframework.web.servlet.HandlerMapping;
28 import org.springframework.web.util.UrlPathHelper;
29
30 /**
31  * @author Rod Johnson
32  * @author Juergen Hoeller
33  */

34 public class SimpleUrlHandlerMappingTests extends TestCase {
35
36     public void testHandlerBeanNotFound() throws Exception JavaDoc {
37         MockServletContext sc = new MockServletContext("");
38         XmlWebApplicationContext root = new XmlWebApplicationContext();
39         root.setServletContext(sc);
40         root.setConfigLocations(new String JavaDoc[] {"/org/springframework/web/servlet/handler/map1.xml"});
41         root.refresh();
42         XmlWebApplicationContext wac = new XmlWebApplicationContext();
43         wac.setParent(root);
44         wac.setServletContext(sc);
45         wac.setNamespace("map2err");
46         wac.setConfigLocations(new String JavaDoc[] {"/org/springframework/web/servlet/handler/map2err.xml"});
47         try {
48             wac.refresh();
49             fail("Should have thrown NoSuchBeanDefinitionException");
50         }
51         catch (FatalBeanException ex) {
52             NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause();
53             assertEquals("mainControlle", nestedEx.getBeanName());
54         }
55     }
56
57     public void testUrlMappingWithUrlMap() throws Exception JavaDoc {
58         checkMappings("urlMapping");
59     }
60
61     public void testUrlMappingWithProps() throws Exception JavaDoc {
62         checkMappings("urlMappingWithProps");
63     }
64
65     private void checkMappings(String JavaDoc beanName) throws Exception JavaDoc {
66         MockServletContext sc = new MockServletContext("");
67         XmlWebApplicationContext wac = new XmlWebApplicationContext();
68         wac.setServletContext(sc);
69         wac.setConfigLocations(new String JavaDoc[] {"/org/springframework/web/servlet/handler/map2.xml"});
70         wac.refresh();
71         Object JavaDoc bean = wac.getBean("mainController");
72         Object JavaDoc otherBean = wac.getBean("otherController");
73         HandlerMapping hm = (HandlerMapping) wac.getBean(beanName);
74
75         MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html");
76         HandlerExecutionChain hec = hm.getHandler(req);
77         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
78
79         req = new MockHttpServletRequest("GET", "/welcome.x");
80         hec = hm.getHandler(req);
81         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
82
83         req = new MockHttpServletRequest("GET", "/");
84         req.setServletPath("/welcome.html");
85         hec = hm.getHandler(req);
86         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
87
88         req = new MockHttpServletRequest("GET", "/welcome.html");
89         req.setContextPath("/app");
90         hec = hm.getHandler(req);
91         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
92
93         req = new MockHttpServletRequest("GET", "/show.html");
94         hec = hm.getHandler(req);
95         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
96
97         req = new MockHttpServletRequest("GET", "/bookseats.html");
98         hec = hm.getHandler(req);
99         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
100
101         req = new MockHttpServletRequest("GET", "/original-welcome.html");
102         req.setAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE, "/welcome.html");
103         hec = hm.getHandler(req);
104         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
105
106         req = new MockHttpServletRequest("GET", "/original-show.html");
107         req.setAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE, "/show.html");
108         hec = hm.getHandler(req);
109         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
110
111         req = new MockHttpServletRequest("GET", "/original-bookseats.html");
112         req.setAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE, "/bookseats.html");
113         hec = hm.getHandler(req);
114         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
115     }
116
117 }
118
Popular Tags