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 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 assertPathEquals(resultClass, expectedPath, actualResult, "UTF-8");
141 }
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 assertEquals("ActionResultの型をチェック", resultClass, actualResult
160 .getClass());
161 if (actualResult instanceof Forward) {
162 assertEquals("パスのチェック", expectedPath, Forward.class.cast(
163 actualResult).getPath(characterEncoding));
164 } else if (actualResult instanceof Redirect) {
165 assertEquals("パスのチェック", expectedPath, Redirect.class.cast(
166 actualResult).getPath(characterEncoding));
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179 protected ActionResult processAction(final String originalPath)
180 throws Exception {
181 final MockHttpServletRequest request = getRequest();
182 setServletPath(request, originalPath);
183 final MockHttpServletResponse response = getResponse();
184 routing(request, response);
185 setupThreadContext();
186 final ActionResultWrapper actionResultWrapper = actionProcessor
187 .process(request, response);
188 if (actionResultWrapper == null) {
189 return null;
190 }
191 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 final InternalForwardInfo internalForwardInfo = router.routing(request,
207 response);
208 if (internalForwardInfo == null) {
209 fail(request.getServletPath() + " could not mapping to action");
210 }
211 final String internalForwardPath = internalForwardInfo
212 .getInternalForwardPath();
213 final MockHttpServletRequest internalForwardRequest = this
214 .getServletContext().createRequest(internalForwardPath);
215 request.setAttribute(ATTR_ROUTINGS, internalForwardInfo
216 .getOnSubmitRoutings());
217 request.setAttribute("javax.servlet.forward.request_uri", request
218 .getRequestURI());
219 request.setAttribute("javax.servlet.forward.context_path", request
220 .getContextPath());
221 request.setAttribute("javax.servlet.forward.servlet_path", request
222 .getServletPath());
223 request.setAttribute("javax.servlet.forward.path_info", request
224 .getPathInfo());
225 request.setAttribute("javax.servlet.forward.query_string", request
226 .getQueryString());
227 final String servletPath = internalForwardRequest.getServletPath();
228 setServletPath(request, servletPath);
229 request.setQueryString(internalForwardRequest.getQueryString());
230 if (StringUtil.isNotBlank(internalForwardRequest.getQueryString())) {
231 final Map<String, List<String>> pathParameters = parseQueryString(internalForwardRequest
232 .getQueryString());
233 for (final Entry<String, List<String>> entry : pathParameters
234 .entrySet()) {
235 final String name = entry.getKey();
236 for (final String value : entry.getValue()) {
237 request.addParameter(name, value);
238 }
239 }
240 }
241 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 final Field servletPathField = ClassUtil.getDeclaredField(request
255 .getClass(), "servletPath");
256 servletPathField.setAccessible(true);
257 try {
258 servletPathField.set(request, servletPath);
259 } catch (final Exception ex) {
260 throw new RuntimeException(ex);
261 }
262 }
263
264
265
266
267 protected void setupThreadContext() {
268 ThreadContext.setRequest(getRequest());
269 }
270
271
272
273
274
275
276
277
278 private Map<String, List<String>> parseQueryString(final String queryString) {
279 final Map<String, List<String>> params = new HashMap<String, List<String>>();
280 final String[] tokens = queryString.split("&");
281 for (final String token : tokens) {
282 final String[] param = token.split("=");
283 final String name = param[0];
284 final String value = param[1];
285 final List<String> values;
286 if (params.containsKey(name)) {
287 values = params.get(name);
288 } else {
289 values = new ArrayList<String>();
290 params.put(name, values);
291 }
292 values.add(value);
293 }
294 return params;
295 }
296
297
298
299
300
301
302
303
304 @Deprecated
305 @SuppressWarnings( { "unchecked" })
306 protected String routing(final String orginalPath) {
307 final MockHttpServletRequest originalRequest = this.getServletContext()
308 .createRequest(orginalPath);
309 final MockHttpServletResponse response = this.getResponse();
310 final InternalForwardInfo internalForwardInfo = router.routing(
311 originalRequest, response);
312 if (internalForwardInfo == null) {
313 fail(orginalPath + " could not mapping to action");
314 }
315 final String internalForwardPath = internalForwardInfo
316 .getInternalForwardPath();
317 final MockHttpServletRequest internalForwardRequest = this
318 .getServletContext().createRequest(internalForwardPath);
319 final MockHttpServletRequest request = getRequest();
320 request.setAttribute(ATTR_ROUTINGS, internalForwardInfo
321 .getOnSubmitRoutings());
322 Beans.copy(internalForwardRequest, request).execute();
323 final Field servletPathField = ClassUtil.getDeclaredField(request
324 .getClass(), "servletPath");
325 servletPathField.setAccessible(true);
326 try {
327 servletPathField.set(request, internalForwardRequest
328 .getServletPath());
329 } catch (final Exception ex) {
330 throw new RuntimeException(ex);
331 }
332 request.setQueryString(internalForwardRequest.getQueryString());
333 if (StringUtil.isNotBlank(internalForwardRequest.getQueryString())) {
334 request.getParameterMap().putAll(
335 javax.servlet.http.HttpUtils.parseQueryString(request
336 .getQueryString()));
337 }
338 return internalForwardPath;
339 }
340
341 }