View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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          super(testClass);
50      }
51      
52      @Override
53      public void setUp(Object testInstance)
54      {
55          super.setUp(testInstance);
56          
57      }
58  
59      @Override
60      public void tearDown()
61      {
62          endRequest();
63          session = null;
64          lastSession = null;
65          if (client != null)
66          {
67              client.setTestCase(null);
68          }
69          client = null;
70          super.tearDown();
71      }
72  
73      protected void setupRequest()
74      {
75          setupRequest(null);
76      }
77  
78      protected void setupRequest(String pathInfo)
79      {
80          if (pathInfo == null)
81          {
82              setupRequest(null, null);
83          }
84          else
85          {
86              int queryIndex = pathInfo.indexOf("?");
87              if (queryIndex >= 0) 
88              {
89                  setupRequest(pathInfo.substring(0,queryIndex), pathInfo.substring(queryIndex+1));
90              }
91              else
92              {
93                  setupRequest(pathInfo, null);
94              }
95          }
96      }
97  
98      protected void setupRequest(String pathInfo, String query)
99      {
100         if (request != null)
101         {
102             //tear down previous request
103             endRequest();
104         }
105         request = lastSession == null ? 
106             new MockHttpServletRequest() : new MockHttpServletRequest(lastSession);
107         request.setServletContext(servletContext);
108         requestInitializedCalled = false;
109         if (session == null)
110         {
111             session = new MockHttpSessionProxy(servletContext, request);
112         }
113         else
114         {
115             session.setRequest(request);
116         }
117         session.setServletContext(servletContext);
118         response = new MockHttpServletResponse();
119         //TODO check if this is correct
120         request.setPathElements(getContextPath(), getServletPath(), pathInfo, query);
121 
122         facesContext = facesContextFactory.getFacesContext(
123             servletContext, request, response, lifecycle);
124         externalContext = facesContext.getExternalContext();
125         application = facesContext.getApplication();
126         if (client != null)
127         {
128             client.apply(request);
129             client.reset(facesContext);
130         }
131         else
132         {
133             client = createClient();
134         }
135     }
136     
137     protected MockMyFacesClient createClient()
138     {
139         return new MockMyFacesClient(facesContext, this);
140     }
141 
142     /**
143      * This method call startViewRequest(viewId) and doRequestInitialized()
144      */
145     public final void startViewRequest(String viewId)
146     {
147         setupRequest(viewId);
148         doRequestInitialized();
149     }
150     
151     /**
152      * This method call startViewRequest(null) and doRequestInitialized()
153      */
154     public final void startRequest()
155     {
156         startViewRequest(null);
157         doRequestInitialized();
158     }
159     
160     public void doRequestInitialized()
161     {
162         if (!requestInitializedCalled)
163         {
164             List<FrameworkMethod> beforeRequestMethods = testClass.getAnnotatedMethods(BeforeRequest.class);
165             if (beforeRequestMethods != null && !beforeRequestMethods.isEmpty())
166             {
167                 for (FrameworkMethod fm : beforeRequestMethods)
168                 {
169                     try
170                     {
171                         fm.invokeExplosively(testInstance);
172                     }
173                     catch (Throwable ex)
174                     {
175                         throw new FacesException(ex);
176                     }
177                 }
178             }
179             
180             webContainer.requestInitialized(new ServletRequestEvent(servletContext, request));
181             requestInitializedCalled = true;
182         }
183     }
184     
185     /**
186      * This method call doRequestDestroyed() and then tearDownRequest(). 
187      */
188     public final void endRequest()
189     {
190         doRequestDestroyed();
191         tearDownRequest();
192     }
193     
194     public void doRequestDestroyed()
195     {
196         if (request != null)
197         {
198             List<FrameworkMethod> afterRequestMethods = testClass.getAnnotatedMethods(AfterRequest.class);
199             if (afterRequestMethods != null && !afterRequestMethods.isEmpty())
200             {
201                 for (FrameworkMethod fm : afterRequestMethods)
202                 {
203                     try
204                     {
205                         fm.invokeExplosively(testInstance);
206                     }
207                     catch (Throwable ex)
208                     {
209                         throw new FacesException(ex);
210                     }
211                 }
212             }
213             lastSession = (MockHttpSession) request.getSession(false);
214             webContainer.requestDestroyed(new ServletRequestEvent(servletContext, request));
215         }
216     }
217     
218     protected void tearDownRequest()
219     {
220         if (facesContext != null)
221         {
222             facesContext.release();
223         }
224         facesContext = null;
225         externalContext = null;
226         application = null;
227         
228         response = null;
229         request = null;
230         //session = null;
231     }
232     
233     protected String getContextPath()
234     {
235         TestConfig testConfig = getTestJavaClass().getAnnotation(TestConfig.class);
236         if (testConfig != null)
237         {
238             return testConfig.contextPath();
239         }
240         return "/test";
241     }
242     
243     protected String getServletPath()
244     {
245         TestConfig testConfig = getTestJavaClass().getAnnotation(TestConfig.class);
246         if (testConfig != null)
247         {
248             return testConfig.servletPath();
249         }
250         return "/faces";
251     }
252 
253     public void processLifecycleExecute()
254     {
255         processLifecycleExecute(facesContext);
256     }
257 
258     public void processLifecycleRender()
259     {
260         processLifecycleRender(facesContext);
261     }
262     
263     public void processLifecycleExecuteAndRender()
264     {
265         processLifecycleExecute();
266         renderResponse();
267     }
268 
269     public void restoreView()
270     {
271         restoreView(facesContext);
272     }
273     
274     public void applyRequestValues()
275     {
276         applyRequestValues(facesContext);
277     }
278 
279     public void processValidations()
280     {
281         processValidations(facesContext);
282     }
283 
284     public void updateModelValues()
285     {
286         updateModelValues(facesContext);
287 
288     }
289 
290     public void invokeApplication()
291     {
292         invokeApplication(facesContext);
293     }
294     
295     public void renderResponse()
296     {
297         renderResponse(facesContext);
298     }
299     
300     public void processRemainingExecutePhases()
301     {
302         processRemainingExecutePhases(facesContext);
303     }
304 
305     public void processRemainingPhases()
306     {
307         processRemainingPhases(facesContext);
308     }
309     
310     public void executeBeforeRender()
311     {
312         executeBeforeRender(facesContext);
313     }
314     
315     public void executeViewHandlerRender()
316     {
317         executeViewHandlerRender(facesContext);
318     }
319         
320     public void executeBuildViewCycle()
321     {
322         executeBuildViewCycle(facesContext);
323     }
324     
325     public void executeAfterRender()
326     {
327         executeAfterRender(facesContext);
328     }
329     
330     public String getRenderedContent() throws IOException
331     {
332         return getRenderedContent(facesContext);
333     }
334     
335     protected MockMyFacesClient client = null;
336     
337     // Servlet objects 
338     protected MockHttpServletRequest request = null;
339     protected boolean requestInitializedCalled = false;
340     protected MockHttpServletResponse response = null;
341     protected MockHttpSessionProxy session = null;
342     protected MockHttpSession lastSession = null;
343     
344     protected Application application = null;
345     protected ExternalContext externalContext = null;
346     protected FacesContext facesContext = null;
347 
348     @Override
349     public MockHttpServletRequest getRequest()
350     {
351         return request;
352     }
353 
354     @Override
355     public MockHttpServletResponse getResponse()
356     {
357         return response;
358     }
359 
360     @Override
361     public FacesContext getFacesContext()
362     {
363         return facesContext;
364     }
365 
366     public MockMyFacesClient getClient()
367     {
368         return client;
369     }
370 
371     public Application getApplication()
372     {
373         return application;
374     }
375 
376     public ExternalContext getExternalContext()
377     {
378         return externalContext;
379     }
380 
381 }