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