KickJava   Java API By Example, From Geeks To Geeks.

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


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.mock.web.MockHttpServletRequest;
22 import org.springframework.mock.web.MockServletContext;
23 import org.springframework.web.context.ConfigurableWebApplicationContext;
24 import org.springframework.web.context.support.XmlWebApplicationContext;
25 import org.springframework.web.servlet.HandlerExecutionChain;
26 import org.springframework.web.servlet.HandlerMapping;
27
28 /**
29  * @author Alef Arendsen
30  */

31 public class PathMatchingUrlHandlerMappingTests extends TestCase {
32
33     public static final String JavaDoc CONF = "/org/springframework/web/servlet/handler/map3.xml";
34
35     private HandlerMapping hm;
36
37     private ConfigurableWebApplicationContext wac;
38
39     public void setUp() throws Exception JavaDoc {
40         MockServletContext sc = new MockServletContext("");
41         wac = new XmlWebApplicationContext();
42         wac.setServletContext(sc);
43         wac.setConfigLocations(new String JavaDoc[] {CONF});
44         wac.refresh();
45         hm = (HandlerMapping) wac.getBean("urlMapping");
46     }
47
48     public void testRequestsWithHandlers() throws Exception JavaDoc {
49         Object JavaDoc bean = wac.getBean("mainController");
50
51         MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html");
52         HandlerExecutionChain hec = hm.getHandler(req);
53         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
54
55         req = new MockHttpServletRequest("GET", "/show.html");
56         hec = hm.getHandler(req);
57         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
58
59         req = new MockHttpServletRequest("GET", "/bookseats.html");
60         hec = hm.getHandler(req);
61         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
62     }
63
64     public void testActualPathMatching() throws Exception JavaDoc {
65         // there a couple of mappings defined with which we can test the
66
// path matching, let's do that...
67

68         Object JavaDoc bean = wac.getBean("mainController");
69         Object JavaDoc defaultBean = wac.getBean("starController");
70
71         // testing some normal behavior
72
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/pathmatchingTest.html");
73         HandlerExecutionChain hec = hm.getHandler(req);
74         assertTrue("Handler is null", hec != null);
75         assertTrue("Handler is correct bean", hec.getHandler() == bean);
76
77         // no match, no forward slash included
78
req = new MockHttpServletRequest("GET", "welcome.html");
79         hec = hm.getHandler(req);
80         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
81
82
83         // testing some ????? behavior
84
req = new MockHttpServletRequest("GET", "/pathmatchingAA.html");
85         hec = hm.getHandler(req);
86         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
87
88         // testing some ????? behavior
89
req = new MockHttpServletRequest("GET", "/pathmatchingA.html");
90         hec = hm.getHandler(req);
91         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
92
93         // testing some ????? behavior
94
req = new MockHttpServletRequest("GET", "/administrator/pathmatching.html");
95         hec = hm.getHandler(req);
96         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
97
98         // testing simple /**/behavior
99
req = new MockHttpServletRequest("GET", "/administrator/test/pathmatching.html");
100         hec = hm.getHandler(req);
101         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
102
103         // this should not match because of the administratorT
104
req = new MockHttpServletRequest("GET", "/administratort/pathmatching.html");
105         hec = hm.getHandler(req);
106         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
107
108         // this should match because of *.jsp
109
req = new MockHttpServletRequest("GET", "/bla.jsp");
110         hec = hm.getHandler(req);
111         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
112
113         // this as well, because there's a **/in there as well
114
req = new MockHttpServletRequest("GET", "/testing/bla.jsp");
115         hec = hm.getHandler(req);
116         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
117
118         // should match because because exact pattern is there
119
req = new MockHttpServletRequest("GET", "/administrator/another/bla.xml");
120         hec = hm.getHandler(req);
121         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
122
123         // should not match, because there's not .gif extension in there
124
req = new MockHttpServletRequest("GET", "/administrator/another/bla.gif");
125         hec = hm.getHandler(req);
126         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
127
128         // should match because there testlast* in there
129
req = new MockHttpServletRequest("GET", "/administrator/test/testlastbit");
130         hec = hm.getHandler(req);
131         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
132
133         // but this not, because it's testlast and not testla
134
req = new MockHttpServletRequest("GET", "/administrator/test/testla");
135         hec = hm.getHandler(req);
136         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
137
138         req = new MockHttpServletRequest("GET", "/administrator/testing/longer/bla");
139         hec = hm.getHandler(req);
140         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
141
142         req = new MockHttpServletRequest("GET", "/administrator/testing/longer/test.jsp");
143         hec = hm.getHandler(req);
144         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
145
146         req = new MockHttpServletRequest("GET", "/administrator/testing/longer2/notmatching/notmatching");
147         hec = hm.getHandler(req);
148         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
149
150         req = new MockHttpServletRequest("GET", "/shortpattern/testing/toolong");
151         hec = hm.getHandler(req);
152         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
153
154         req = new MockHttpServletRequest("GET", "/XXpathXXmatching.html");
155         hec = hm.getHandler(req);
156         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
157
158         req = new MockHttpServletRequest("GET", "/pathXXmatching.html");
159         hec = hm.getHandler(req);
160         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
161
162         req = new MockHttpServletRequest("GET", "/XpathXXmatching.html");
163         hec = hm.getHandler(req);
164         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
165
166         req = new MockHttpServletRequest("GET", "/XXpathmatching.html");
167         hec = hm.getHandler(req);
168         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
169
170         req = new MockHttpServletRequest("GET", "/show12.html");
171         hec = hm.getHandler(req);
172         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
173
174         req = new MockHttpServletRequest("GET", "/show123.html");
175         hec = hm.getHandler(req);
176         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
177
178         req = new MockHttpServletRequest("GET", "/show1.html");
179         hec = hm.getHandler(req);
180         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
181
182         req = new MockHttpServletRequest("GET", "/reallyGood-test-is-this.jpeg");
183         hec = hm.getHandler(req);
184         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
185
186         req = new MockHttpServletRequest("GET", "/reallyGood-tst-is-this.jpeg");
187         hec = hm.getHandler(req);
188         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
189
190         req = new MockHttpServletRequest("GET", "/testing/test.jpeg");
191         hec = hm.getHandler(req);
192         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
193
194         req = new MockHttpServletRequest("GET", "/testing/test.jpg");
195         hec = hm.getHandler(req);
196         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
197
198         req = new MockHttpServletRequest("GET", "/anotherTest");
199         hec = hm.getHandler(req);
200         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
201
202         req = new MockHttpServletRequest("GET", "/stillAnotherTest");
203         hec = hm.getHandler(req);
204         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
205
206         // there outofpattern*yeah in the pattern, so this should fail
207
req = new MockHttpServletRequest("GET", "/outofpattern*ye");
208         hec = hm.getHandler(req);
209         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
210
211         req = new MockHttpServletRequest("GET", "/test't est/path'm atching.html");
212         hec = hm.getHandler(req);
213         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
214
215         req = new MockHttpServletRequest("GET", "/test%26t%20est/path%26m%20atching.html");
216         hec = hm.getHandler(req);
217         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
218     }
219
220     public void testDefaultMapping() throws Exception JavaDoc {
221         Object JavaDoc bean = wac.getBean("starController");
222         MockHttpServletRequest req = new MockHttpServletRequest("GET", "/goggog.html");
223         HandlerExecutionChain hec = hm.getHandler(req);
224         assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
225     }
226
227 }
228
Popular Tags