1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.unit;
17
18 import java.io.IOException;
19 import java.lang.reflect.Field;
20
21 import javax.servlet.FilterChain;
22 import javax.servlet.ServletException;
23 import javax.servlet.ServletRequest;
24 import javax.servlet.ServletResponse;
25
26 import org.seasar.cubby.action.ActionResult;
27 import org.seasar.cubby.action.Forward;
28 import org.seasar.cubby.action.Redirect;
29 import org.seasar.cubby.controller.ActionProcessor;
30 import org.seasar.cubby.routing.InternalForwardInfo;
31 import org.seasar.cubby.routing.Router;
32 import org.seasar.framework.beans.util.Beans;
33 import org.seasar.framework.mock.servlet.MockHttpServletRequest;
34 import org.seasar.framework.mock.servlet.MockHttpServletResponse;
35 import org.seasar.framework.unit.S2TigerTestCase;
36 import org.seasar.framework.util.ClassUtil;
37 import org.seasar.framework.util.StringUtil;
38
39
40
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 public abstract class CubbyTestCase extends S2TigerTestCase {
117
118
119 private Router router;
120
121
122 private final MockFilterChain filterChain = new MockFilterChain();
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 protected ActionResult processAction(final String orginalPath)
160 throws Exception {
161 routing(orginalPath);
162 return actionProcessor
163 .process(getRequest(), getResponse(), filterChain);
164 }
165
166
167
168
169
170
171
172
173 @SuppressWarnings( { "unchecked", "deprecation" })
174 protected String routing(final String orginalPath) {
175 final MockHttpServletRequest request = this.getServletContext()
176 .createRequest(orginalPath);
177 final MockHttpServletResponse response = this.getResponse();
178 final InternalForwardInfo internalForwardInfo = router.routing(request,
179 response);
180 if (internalForwardInfo == null) {
181 fail(orginalPath + " could not mapping to action");
182 }
183 final String internalForwardPath = internalForwardInfo
184 .getInternalForwardPath();
185 final MockHttpServletRequest internalForwardRequest = this
186 .getServletContext().createRequest(internalForwardPath);
187 Beans.copy(internalForwardRequest, getRequest()).execute();
188 final Field servletPathField = ClassUtil.getDeclaredField(getRequest()
189 .getClass(), "servletPath");
190 servletPathField.setAccessible(true);
191 try {
192 servletPathField.set(getRequest(), internalForwardRequest
193 .getServletPath());
194 } catch (final Exception ex) {
195 throw new RuntimeException(ex);
196 }
197
198 if (StringUtil.isNotBlank(internalForwardRequest.getQueryString())) {
199 getRequest().getParameterMap().putAll(
200 javax.servlet.http.HttpUtils.parseQueryString(getRequest()
201 .getQueryString()));
202 }
203 return internalForwardPath;
204 }
205
206
207
208
209
210
211 private static class MockFilterChain implements FilterChain {
212
213
214
215 public void doFilter(final ServletRequest request,
216 final ServletResponse response) throws IOException,
217 ServletException {
218 }
219 }
220
221 }