1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.mc.test.core.runner; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.List; |
23 | |
import javax.faces.FacesException; |
24 | |
import javax.faces.application.Application; |
25 | |
import javax.faces.context.ExternalContext; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.servlet.ServletRequestEvent; |
28 | |
import org.apache.myfaces.mc.test.core.mock.MockMyFacesClient; |
29 | |
import org.apache.myfaces.mc.test.core.mock.ServletMockContainer; |
30 | |
import org.apache.myfaces.mc.test.core.annotation.AfterRequest; |
31 | |
import org.apache.myfaces.mc.test.core.annotation.BeforeRequest; |
32 | |
import org.apache.myfaces.mc.test.core.annotation.TestConfig; |
33 | |
import org.apache.myfaces.test.mock.MockHttpServletRequest; |
34 | |
import org.apache.myfaces.test.mock.MockHttpServletResponse; |
35 | |
import org.apache.myfaces.test.mock.MockHttpSession; |
36 | |
import org.apache.myfaces.test.mock.MockHttpSessionProxy; |
37 | |
import org.junit.runners.model.FrameworkMethod; |
38 | |
import org.junit.runners.model.TestClass; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class AbstractJsfRequestTestContainer extends AbstractJsfTestContainer |
44 | |
implements ServletMockContainer |
45 | |
{ |
46 | |
|
47 | |
public AbstractJsfRequestTestContainer(TestClass testClass) |
48 | |
{ |
49 | 0 | super(testClass); |
50 | 0 | } |
51 | |
|
52 | |
@Override |
53 | |
public void setUp(Object testInstance) |
54 | |
{ |
55 | 0 | super.setUp(testInstance); |
56 | |
|
57 | 0 | } |
58 | |
|
59 | |
@Override |
60 | |
public void tearDown() |
61 | |
{ |
62 | 0 | endRequest(); |
63 | 0 | session = null; |
64 | 0 | lastSession = null; |
65 | 0 | if (client != null) |
66 | |
{ |
67 | 0 | client.setTestCase(null); |
68 | |
} |
69 | 0 | client = null; |
70 | 0 | super.tearDown(); |
71 | 0 | } |
72 | |
|
73 | |
protected void setupRequest() |
74 | |
{ |
75 | 0 | setupRequest(null); |
76 | 0 | } |
77 | |
|
78 | |
protected void setupRequest(String pathInfo) |
79 | |
{ |
80 | 0 | if (pathInfo == null) |
81 | |
{ |
82 | 0 | setupRequest(null, null); |
83 | |
} |
84 | |
else |
85 | |
{ |
86 | 0 | int queryIndex = pathInfo.indexOf("?"); |
87 | 0 | if (queryIndex >= 0) |
88 | |
{ |
89 | 0 | setupRequest(pathInfo.substring(0,queryIndex), pathInfo.substring(queryIndex+1)); |
90 | |
} |
91 | |
else |
92 | |
{ |
93 | 0 | setupRequest(pathInfo, null); |
94 | |
} |
95 | |
} |
96 | 0 | } |
97 | |
|
98 | |
protected void setupRequest(String pathInfo, String query) |
99 | |
{ |
100 | 0 | if (request != null) |
101 | |
{ |
102 | |
|
103 | 0 | endRequest(); |
104 | |
} |
105 | 0 | request = lastSession == null ? |
106 | |
new MockHttpServletRequest() : new MockHttpServletRequest(lastSession); |
107 | 0 | request.setServletContext(servletContext); |
108 | 0 | requestInitializedCalled = false; |
109 | 0 | if (session == null) |
110 | |
{ |
111 | 0 | session = new MockHttpSessionProxy(servletContext, request); |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | 0 | session.setRequest(request); |
116 | |
} |
117 | 0 | session.setServletContext(servletContext); |
118 | 0 | response = new MockHttpServletResponse(); |
119 | |
|
120 | 0 | request.setPathElements(getContextPath(), getServletPath(), pathInfo, query); |
121 | |
|
122 | 0 | facesContext = facesContextFactory.getFacesContext( |
123 | |
servletContext, request, response, lifecycle); |
124 | 0 | externalContext = facesContext.getExternalContext(); |
125 | 0 | application = facesContext.getApplication(); |
126 | 0 | if (client != null) |
127 | |
{ |
128 | 0 | client.apply(request); |
129 | 0 | client.reset(facesContext); |
130 | |
} |
131 | |
else |
132 | |
{ |
133 | 0 | client = createClient(); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
protected MockMyFacesClient createClient() |
138 | |
{ |
139 | 0 | return new MockMyFacesClient(facesContext, this); |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public final void startViewRequest(String viewId) |
146 | |
{ |
147 | 0 | setupRequest(viewId); |
148 | 0 | doRequestInitialized(); |
149 | 0 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public final void startRequest() |
155 | |
{ |
156 | 0 | startViewRequest(null); |
157 | 0 | doRequestInitialized(); |
158 | 0 | } |
159 | |
|
160 | |
public void doRequestInitialized() |
161 | |
{ |
162 | 0 | if (!requestInitializedCalled) |
163 | |
{ |
164 | 0 | List<FrameworkMethod> beforeRequestMethods = testClass.getAnnotatedMethods(BeforeRequest.class); |
165 | 0 | if (beforeRequestMethods != null && !beforeRequestMethods.isEmpty()) |
166 | |
{ |
167 | 0 | for (FrameworkMethod fm : beforeRequestMethods) |
168 | |
{ |
169 | |
try |
170 | |
{ |
171 | 0 | fm.invokeExplosively(testInstance); |
172 | |
} |
173 | 0 | catch (Throwable ex) |
174 | |
{ |
175 | 0 | throw new FacesException(ex); |
176 | 0 | } |
177 | 0 | } |
178 | |
} |
179 | |
|
180 | 0 | webContainer.requestInitialized(new ServletRequestEvent(servletContext, request)); |
181 | 0 | requestInitializedCalled = true; |
182 | |
} |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public final void endRequest() |
189 | |
{ |
190 | 0 | doRequestDestroyed(); |
191 | 0 | tearDownRequest(); |
192 | 0 | } |
193 | |
|
194 | |
public void doRequestDestroyed() |
195 | |
{ |
196 | 0 | if (request != null) |
197 | |
{ |
198 | 0 | List<FrameworkMethod> afterRequestMethods = testClass.getAnnotatedMethods(AfterRequest.class); |
199 | 0 | if (afterRequestMethods != null && !afterRequestMethods.isEmpty()) |
200 | |
{ |
201 | 0 | for (FrameworkMethod fm : afterRequestMethods) |
202 | |
{ |
203 | |
try |
204 | |
{ |
205 | 0 | fm.invokeExplosively(testInstance); |
206 | |
} |
207 | 0 | catch (Throwable ex) |
208 | |
{ |
209 | 0 | throw new FacesException(ex); |
210 | 0 | } |
211 | 0 | } |
212 | |
} |
213 | 0 | lastSession = (MockHttpSession) request.getSession(false); |
214 | 0 | webContainer.requestDestroyed(new ServletRequestEvent(servletContext, request)); |
215 | |
} |
216 | 0 | } |
217 | |
|
218 | |
protected void tearDownRequest() |
219 | |
{ |
220 | 0 | if (facesContext != null) |
221 | |
{ |
222 | 0 | facesContext.release(); |
223 | |
} |
224 | 0 | facesContext = null; |
225 | 0 | externalContext = null; |
226 | 0 | application = null; |
227 | |
|
228 | 0 | response = null; |
229 | 0 | request = null; |
230 | |
|
231 | 0 | } |
232 | |
|
233 | |
protected String getContextPath() |
234 | |
{ |
235 | 0 | TestConfig testConfig = getTestJavaClass().getAnnotation(TestConfig.class); |
236 | 0 | if (testConfig != null) |
237 | |
{ |
238 | 0 | return testConfig.contextPath(); |
239 | |
} |
240 | 0 | return "/test"; |
241 | |
} |
242 | |
|
243 | |
protected String getServletPath() |
244 | |
{ |
245 | 0 | TestConfig testConfig = getTestJavaClass().getAnnotation(TestConfig.class); |
246 | 0 | if (testConfig != null) |
247 | |
{ |
248 | 0 | return testConfig.servletPath(); |
249 | |
} |
250 | 0 | return "/faces"; |
251 | |
} |
252 | |
|
253 | |
public void processLifecycleExecute() |
254 | |
{ |
255 | 0 | processLifecycleExecute(facesContext); |
256 | 0 | } |
257 | |
|
258 | |
public void processLifecycleRender() |
259 | |
{ |
260 | 0 | processLifecycleRender(facesContext); |
261 | 0 | } |
262 | |
|
263 | |
public void processLifecycleExecuteAndRender() |
264 | |
{ |
265 | 0 | processLifecycleExecute(); |
266 | 0 | renderResponse(); |
267 | 0 | } |
268 | |
|
269 | |
public void restoreView() |
270 | |
{ |
271 | 0 | restoreView(facesContext); |
272 | 0 | } |
273 | |
|
274 | |
public void applyRequestValues() |
275 | |
{ |
276 | 0 | applyRequestValues(facesContext); |
277 | 0 | } |
278 | |
|
279 | |
public void processValidations() |
280 | |
{ |
281 | 0 | processValidations(facesContext); |
282 | 0 | } |
283 | |
|
284 | |
public void updateModelValues() |
285 | |
{ |
286 | 0 | updateModelValues(facesContext); |
287 | |
|
288 | 0 | } |
289 | |
|
290 | |
public void invokeApplication() |
291 | |
{ |
292 | 0 | invokeApplication(facesContext); |
293 | 0 | } |
294 | |
|
295 | |
public void renderResponse() |
296 | |
{ |
297 | 0 | renderResponse(facesContext); |
298 | 0 | } |
299 | |
|
300 | |
public void processRemainingExecutePhases() |
301 | |
{ |
302 | 0 | processRemainingExecutePhases(facesContext); |
303 | 0 | } |
304 | |
|
305 | |
public void processRemainingPhases() |
306 | |
{ |
307 | 0 | processRemainingPhases(facesContext); |
308 | 0 | } |
309 | |
|
310 | |
public void executeBeforeRender() |
311 | |
{ |
312 | 0 | executeBeforeRender(facesContext); |
313 | 0 | } |
314 | |
|
315 | |
public void executeViewHandlerRender() |
316 | |
{ |
317 | 0 | executeViewHandlerRender(facesContext); |
318 | 0 | } |
319 | |
|
320 | |
public void executeBuildViewCycle() |
321 | |
{ |
322 | 0 | executeBuildViewCycle(facesContext); |
323 | 0 | } |
324 | |
|
325 | |
public void executeAfterRender() |
326 | |
{ |
327 | 0 | executeAfterRender(facesContext); |
328 | 0 | } |
329 | |
|
330 | |
public String getRenderedContent() throws IOException |
331 | |
{ |
332 | 0 | return getRenderedContent(facesContext); |
333 | |
} |
334 | |
|
335 | 0 | protected MockMyFacesClient client = null; |
336 | |
|
337 | |
|
338 | 0 | protected MockHttpServletRequest request = null; |
339 | 0 | protected boolean requestInitializedCalled = false; |
340 | 0 | protected MockHttpServletResponse response = null; |
341 | 0 | protected MockHttpSessionProxy session = null; |
342 | 0 | protected MockHttpSession lastSession = null; |
343 | |
|
344 | 0 | protected Application application = null; |
345 | 0 | protected ExternalContext externalContext = null; |
346 | 0 | protected FacesContext facesContext = null; |
347 | |
|
348 | |
@Override |
349 | |
public MockHttpServletRequest getRequest() |
350 | |
{ |
351 | 0 | return request; |
352 | |
} |
353 | |
|
354 | |
@Override |
355 | |
public MockHttpServletResponse getResponse() |
356 | |
{ |
357 | 0 | return response; |
358 | |
} |
359 | |
|
360 | |
@Override |
361 | |
public FacesContext getFacesContext() |
362 | |
{ |
363 | 0 | return facesContext; |
364 | |
} |
365 | |
|
366 | |
public MockMyFacesClient getClient() |
367 | |
{ |
368 | 0 | return client; |
369 | |
} |
370 | |
|
371 | |
public Application getApplication() |
372 | |
{ |
373 | 0 | return application; |
374 | |
} |
375 | |
|
376 | |
public ExternalContext getExternalContext() |
377 | |
{ |
378 | 0 | return externalContext; |
379 | |
} |
380 | |
|
381 | |
} |