KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.ServletException JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.context.ApplicationContextException;
24 import org.springframework.mock.web.MockHttpServletRequest;
25 import org.springframework.mock.web.MockServletContext;
26 import org.springframework.web.context.ConfigurableWebApplicationContext;
27 import org.springframework.web.context.support.XmlWebApplicationContext;
28 import org.springframework.web.servlet.HandlerExecutionChain;
29 import org.springframework.web.servlet.HandlerMapping;
30
31 /**
32  * @author Rod Johnson
33  * @author Juergen Hoeller
34  */

35 public class BeanNameUrlHandlerMappingTests extends TestCase {
36
37     public static final String JavaDoc CONF = "/org/springframework/web/servlet/handler/map1.xml";
38     
39     private ConfigurableWebApplicationContext wac;
40
41     public void setUp() throws Exception JavaDoc {
42         MockServletContext sc = new MockServletContext("");
43         wac = new XmlWebApplicationContext();
44         wac.setServletContext(sc);
45         wac.setConfigLocations(new String JavaDoc[] {CONF});
46         wac.refresh();
47     }
48
49     public void testRequestsWithoutHandlers() throws Exception JavaDoc {
50         HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
51
52         MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/nonsense.html");
53         req.setContextPath("/myapp");
54         Object JavaDoc h = hm.getHandler(req);
55         assertTrue("Handler is null", h == null);
56
57         req = new MockHttpServletRequest("GET", "/foo/bar/baz.html");
58         h = hm.getHandler(req);
59         assertTrue("Handler is null", h == null);
60     }
61
62     public void testRequestsWithSubPaths() throws Exception JavaDoc {
63         HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
64         Object JavaDoc bean = wac.getBean("godCtrl");
65
66         MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/welcome.html");
67         HandlerExecutionChain hec = hm.getHandler(req);
68         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
69
70         req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html");
71         req.setContextPath("/myapp");
72         hec = hm.getHandler(req);
73         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
74         
75         req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html");
76         req.setContextPath("/myapp");
77         req.setServletPath("/mypath/welcome.html");
78         hec = hm.getHandler(req);
79         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
80
81         req = new MockHttpServletRequest("GET", "/myapp/myservlet/mypath/welcome.html");
82         req.setContextPath("/myapp");
83         req.setServletPath("/myservlet");
84         hec = hm.getHandler(req);
85         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
86
87         req = new MockHttpServletRequest("GET", "/myapp/myapp/mypath/welcome.html");
88         req.setContextPath("/myapp");
89         req.setServletPath("/myapp");
90         hec = hm.getHandler(req);
91         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
92
93         req = new MockHttpServletRequest("GET", "/mypath/show.html");
94         hec = hm.getHandler(req);
95         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
96
97         req = new MockHttpServletRequest("GET", "/mypath/bookseats.html");
98         hec = hm.getHandler(req);
99         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
100     }
101     
102     public void testRequestsWithFullPaths() throws Exception JavaDoc {
103         BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping();
104         hm.setAlwaysUseFullPath(true);
105         hm.setApplicationContext(wac);
106         Object JavaDoc bean = wac.getBean("godCtrl");
107
108         MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/welcome.html");
109         HandlerExecutionChain hec = hm.getHandler(req);
110         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
111
112         req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html");
113         req.setContextPath("/myapp");
114         hec = hm.getHandler(req);
115         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
116
117         req = new MockHttpServletRequest("GET", "/mypath/welcome.html");
118         req.setContextPath("");
119         req.setServletPath("/mypath");
120         hec = hm.getHandler(req);
121         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
122
123         req = new MockHttpServletRequest("GET", "/Myapp/mypath/welcome.html");
124         req.setContextPath("/myapp");
125         req.setServletPath("/mypath");
126         hec = hm.getHandler(req);
127         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
128     }
129
130     public void testAsteriskMatches() throws Exception JavaDoc {
131         HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
132         Object JavaDoc bean = wac.getBean("godCtrl");
133
134         MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html");
135         HandlerExecutionChain hec = hm.getHandler(req);
136         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
137
138         req = new MockHttpServletRequest("GET", "/mypath/testarossa");
139         hec = hm.getHandler(req);
140         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
141
142         req = new MockHttpServletRequest("GET", "/mypath/tes");
143         hec = hm.getHandler(req);
144         assertTrue("Handler is correct bean", hec == null);
145     }
146
147     public void testOverlappingMappings() throws Exception JavaDoc {
148         BeanNameUrlHandlerMapping hm = (BeanNameUrlHandlerMapping) wac.getBean("handlerMapping");
149         Object JavaDoc anotherHandler = new Object JavaDoc();
150         hm.registerHandler("/mypath/testaross*", anotherHandler);
151         Object JavaDoc bean = wac.getBean("godCtrl");
152
153         MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html");
154         HandlerExecutionChain hec = hm.getHandler(req);
155         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
156
157         req = new MockHttpServletRequest("GET", "/mypath/testarossa");
158         hec = hm.getHandler(req);
159         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == anotherHandler);
160
161         req = new MockHttpServletRequest("GET", "/mypath/tes");
162         hec = hm.getHandler(req);
163         assertTrue("Handler is correct bean", hec == null);
164     }
165
166     public void testDoubleMappings() throws ServletException JavaDoc {
167         BeanNameUrlHandlerMapping hm = (BeanNameUrlHandlerMapping) wac.getBean("handlerMapping");
168         try {
169             hm.registerHandler("/mypath/welcome.html", new Object JavaDoc());
170             fail("Should have thrown ApplicationContextException");
171         }
172         catch (ApplicationContextException ex) {
173             // expected
174
}
175     }
176
177 }
178
Popular Tags