1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.unit; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.ATTR_ROUTINGS; |
19 | |
|
20 | |
import java.lang.reflect.Field; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.List; |
24 | |
import java.util.Map; |
25 | |
import java.util.Map.Entry; |
26 | |
|
27 | |
import org.seasar.cubby.action.ActionResult; |
28 | |
import org.seasar.cubby.action.Forward; |
29 | |
import org.seasar.cubby.action.Redirect; |
30 | |
import org.seasar.cubby.controller.ActionProcessor; |
31 | |
import org.seasar.cubby.controller.ActionResultWrapper; |
32 | |
import org.seasar.cubby.controller.ThreadContext; |
33 | |
import org.seasar.cubby.routing.InternalForwardInfo; |
34 | |
import org.seasar.cubby.routing.Router; |
35 | |
import org.seasar.framework.beans.util.Beans; |
36 | |
import org.seasar.framework.mock.servlet.MockHttpServletRequest; |
37 | |
import org.seasar.framework.mock.servlet.MockHttpServletResponse; |
38 | |
import org.seasar.framework.unit.S2TigerTestCase; |
39 | |
import org.seasar.framework.util.ClassUtil; |
40 | |
import org.seasar.framework.util.StringUtil; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | 2 | public abstract class CubbyTestCase extends S2TigerTestCase { |
120 | |
|
121 | |
|
122 | |
private Router router; |
123 | |
|
124 | |
|
125 | |
private ActionProcessor actionProcessor; |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public static void assertPathEquals( |
138 | |
final Class<? extends ActionResult> resultClass, |
139 | |
final String expectedPath, final ActionResult actualResult) { |
140 | 0 | assertPathEquals(resultClass, expectedPath, actualResult, "UTF-8"); |
141 | 0 | } |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public static void assertPathEquals( |
156 | |
final Class<? extends ActionResult> resultClass, |
157 | |
final String expectedPath, final ActionResult actualResult, |
158 | |
final String characterEncoding) { |
159 | 0 | assertEquals("ActionResultの型をチェック", resultClass, actualResult |
160 | |
.getClass()); |
161 | 0 | if (actualResult instanceof Forward) { |
162 | 0 | assertEquals("パスのチェック", expectedPath, Forward.class.cast( |
163 | |
actualResult).getPath(characterEncoding)); |
164 | 0 | } else if (actualResult instanceof Redirect) { |
165 | 0 | assertEquals("パスのチェック", expectedPath, Redirect.class.cast( |
166 | |
actualResult).getPath(characterEncoding)); |
167 | |
} |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
protected ActionResult processAction(final String originalPath) |
180 | |
throws Exception { |
181 | 1 | final MockHttpServletRequest request = getRequest(); |
182 | 1 | setServletPath(request, originalPath); |
183 | 1 | final MockHttpServletResponse response = getResponse(); |
184 | 1 | routing(request, response); |
185 | 1 | setupThreadContext(); |
186 | 1 | final ActionResultWrapper actionResultWrapper = actionProcessor |
187 | |
.process(request, response); |
188 | 1 | if (actionResultWrapper == null) { |
189 | 0 | return null; |
190 | |
} |
191 | 1 | return actionResultWrapper.getActionResult(); |
192 | |
} |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
protected String routing(final MockHttpServletRequest request, |
205 | |
final MockHttpServletResponse response) { |
206 | 1 | final InternalForwardInfo internalForwardInfo = router.routing(request, |
207 | |
response); |
208 | 1 | if (internalForwardInfo == null) { |
209 | 0 | fail(request.getServletPath() + " could not mapping to action"); |
210 | |
} |
211 | 1 | final String internalForwardPath = internalForwardInfo |
212 | |
.getInternalForwardPath(); |
213 | 1 | final MockHttpServletRequest internalForwardRequest = this |
214 | |
.getServletContext().createRequest(internalForwardPath); |
215 | 1 | request.setAttribute(ATTR_ROUTINGS, internalForwardInfo |
216 | |
.getOnSubmitRoutings()); |
217 | 1 | request.setAttribute("javax.servlet.forward.request_uri", request |
218 | |
.getRequestURI()); |
219 | 1 | request.setAttribute("javax.servlet.forward.context_path", request |
220 | |
.getContextPath()); |
221 | 1 | request.setAttribute("javax.servlet.forward.servlet_path", request |
222 | |
.getServletPath()); |
223 | 1 | request.setAttribute("javax.servlet.forward.path_info", request |
224 | |
.getPathInfo()); |
225 | 1 | request.setAttribute("javax.servlet.forward.query_string", request |
226 | |
.getQueryString()); |
227 | 1 | final String servletPath = internalForwardRequest.getServletPath(); |
228 | 1 | setServletPath(request, servletPath); |
229 | 1 | request.setQueryString(internalForwardRequest.getQueryString()); |
230 | 1 | if (StringUtil.isNotBlank(internalForwardRequest.getQueryString())) { |
231 | 0 | final Map<String, List<String>> pathParameters = parseQueryString(internalForwardRequest |
232 | |
.getQueryString()); |
233 | 0 | for (final Entry<String, List<String>> entry : pathParameters |
234 | |
.entrySet()) { |
235 | 0 | final String name = entry.getKey(); |
236 | 0 | for (final String value : entry.getValue()) { |
237 | 0 | request.addParameter(name, value); |
238 | |
} |
239 | 0 | } |
240 | |
} |
241 | 1 | return internalForwardPath; |
242 | |
} |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
private static void setServletPath(final MockHttpServletRequest request, |
253 | |
final String servletPath) { |
254 | 2 | final Field servletPathField = ClassUtil.getDeclaredField(request |
255 | |
.getClass(), "servletPath"); |
256 | 2 | servletPathField.setAccessible(true); |
257 | |
try { |
258 | 2 | servletPathField.set(request, servletPath); |
259 | 0 | } catch (final Exception ex) { |
260 | 0 | throw new RuntimeException(ex); |
261 | 2 | } |
262 | 2 | } |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
protected void setupThreadContext() { |
268 | 2 | ThreadContext.setRequest(getRequest()); |
269 | 2 | } |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
private Map<String, List<String>> parseQueryString(final String queryString) { |
279 | 0 | final Map<String, List<String>> params = new HashMap<String, List<String>>(); |
280 | 0 | final String[] tokens = queryString.split("&"); |
281 | 0 | for (final String token : tokens) { |
282 | 0 | final String[] param = token.split("="); |
283 | 0 | final String name = param[0]; |
284 | 0 | final String value = param[1]; |
285 | |
final List<String> values; |
286 | 0 | if (params.containsKey(name)) { |
287 | 0 | values = params.get(name); |
288 | |
} else { |
289 | 0 | values = new ArrayList<String>(); |
290 | 0 | params.put(name, values); |
291 | |
} |
292 | 0 | values.add(value); |
293 | |
} |
294 | 0 | return params; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
@Deprecated |
305 | |
@SuppressWarnings( { "unchecked" }) |
306 | |
protected String routing(final String orginalPath) { |
307 | 0 | final MockHttpServletRequest originalRequest = this.getServletContext() |
308 | |
.createRequest(orginalPath); |
309 | 0 | final MockHttpServletResponse response = this.getResponse(); |
310 | 0 | final InternalForwardInfo internalForwardInfo = router.routing( |
311 | |
originalRequest, response); |
312 | 0 | if (internalForwardInfo == null) { |
313 | 0 | fail(orginalPath + " could not mapping to action"); |
314 | |
} |
315 | 0 | final String internalForwardPath = internalForwardInfo |
316 | |
.getInternalForwardPath(); |
317 | 0 | final MockHttpServletRequest internalForwardRequest = this |
318 | |
.getServletContext().createRequest(internalForwardPath); |
319 | 0 | final MockHttpServletRequest request = getRequest(); |
320 | 0 | request.setAttribute(ATTR_ROUTINGS, internalForwardInfo |
321 | |
.getOnSubmitRoutings()); |
322 | 0 | Beans.copy(internalForwardRequest, request).execute(); |
323 | 0 | final Field servletPathField = ClassUtil.getDeclaredField(request |
324 | |
.getClass(), "servletPath"); |
325 | 0 | servletPathField.setAccessible(true); |
326 | |
try { |
327 | 0 | servletPathField.set(request, internalForwardRequest |
328 | |
.getServletPath()); |
329 | 0 | } catch (final Exception ex) { |
330 | 0 | throw new RuntimeException(ex); |
331 | 0 | } |
332 | 0 | request.setQueryString(internalForwardRequest.getQueryString()); |
333 | 0 | if (StringUtil.isNotBlank(internalForwardRequest.getQueryString())) { |
334 | 0 | request.getParameterMap().putAll( |
335 | |
javax.servlet.http.HttpUtils.parseQueryString(request |
336 | |
.getQueryString())); |
337 | |
} |
338 | 0 | return internalForwardPath; |
339 | |
} |
340 | |
|
341 | |
} |