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