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;
20  
21  import org.apache.myfaces.mc.test.core.mock.MockMyFacesClient;
22  import org.apache.myfaces.mc.test.core.mock.ServletMockContainer;
23  import java.io.IOException;
24  
25  import javax.faces.application.Application;
26  import javax.faces.context.ExternalContext;
27  import javax.faces.context.FacesContext;
28  import javax.servlet.ServletRequestEvent;
29  
30  import org.apache.myfaces.test.mock.MockHttpServletRequest;
31  import org.apache.myfaces.test.mock.MockHttpServletResponse;
32  import org.apache.myfaces.test.mock.MockHttpSession;
33  import org.apache.myfaces.test.mock.MockHttpSessionProxy;
34  
35  /**
36   * <p>Abstract JUnit test case base class, with method to setup/teardown a request.
37   * It helps to create tests that involve multiple requests like client submits, or
38   * tests that involve more control over the lifecycle.</p>
39   * 
40   * 
41   * @author Leonardo Uribe
42   *
43   */
44  public abstract class AbstractMyFacesRequestTestCase extends AbstractMyFacesTestCase
45      implements ServletMockContainer
46  {
47      
48      @Override
49      public void setUp() throws Exception
50      {
51          super.setUp();
52          
53      }
54  
55      @Override
56      public void tearDown() throws Exception
57      {
58          endRequest();
59          session = null;
60          lastSession = null;
61          if (client != null)
62          {
63              client.setTestCase(null);
64          }
65          client = null;
66          super.tearDown();
67      }
68  
69      protected void setupRequest()
70      {
71          setupRequest(null);
72      }
73  
74      protected void setupRequest(String pathInfo)
75      {
76          if (pathInfo == null)
77          {
78              setupRequest(null, null);
79          }
80          else
81          {
82              int queryIndex = pathInfo.indexOf('?');
83              if (queryIndex >= 0) 
84              {
85                  setupRequest(pathInfo.substring(0,queryIndex), pathInfo.substring(queryIndex+1));
86              }
87              else
88              {
89                  setupRequest(pathInfo, null);
90              }
91          }
92      }
93  
94      protected void setupRequest(String pathInfo, String query)
95      {
96          if (request != null)
97          {
98              //tear down previous request
99              endRequest();
100         }
101         request = lastSession == null ? 
102             new MockHttpServletRequest() : new MockHttpServletRequest(lastSession);
103         request.setServletContext(servletContext);
104         requestInitializedCalled = false;
105         if (session == null)
106         {
107             session = new MockHttpSessionProxy(servletContext, request);
108         }
109         else
110         {
111             session.setRequest(request);
112         }
113         session.setServletContext(servletContext);
114         response = new MockHttpServletResponse();
115         //TODO check if this is correct
116         request.setPathElements(getContextPath(), getServletPath(), pathInfo, query);
117 
118         facesContext = facesContextFactory.getFacesContext(
119             servletContext, request, response, lifecycle);
120         externalContext = facesContext.getExternalContext();
121         application = facesContext.getApplication();
122         if (client != null)
123         {
124             client.apply(request);
125             client.reset(facesContext);
126         }
127         else
128         {
129             client = createClient();
130         }
131     }
132     
133     protected MockMyFacesClient createClient()
134     {
135         return new MockMyFacesClient(facesContext, this);
136     }
137 
138     /**
139      * This method call startViewRequest(viewId) and doRequestInitialized()
140      */
141     public final void startViewRequest(String viewId)
142     {
143         setupRequest(viewId);
144         doRequestInitialized();
145     }
146     
147     /**
148      * This method call startViewRequest(null) and doRequestInitialized()
149      */
150     public final void startRequest()
151     {
152         startViewRequest(null);
153         doRequestInitialized();
154     }
155     
156     public void doRequestInitialized()
157     {
158         if (!requestInitializedCalled)
159         {
160             webContainer.requestInitialized(new ServletRequestEvent(servletContext, request));
161             requestInitializedCalled = true;
162         }
163     }
164     
165     /**
166      * This method call doRequestDestroyed() and then tearDownRequest(). 
167      */
168     public final void endRequest()
169     {
170         doRequestDestroyed();
171         tearDownRequest();
172     }
173     
174     public void doRequestDestroyed()
175     {
176         if (request != null)
177         {
178             lastSession = (MockHttpSession) request.getSession(false);
179             webContainer.requestDestroyed(new ServletRequestEvent(servletContext, request));
180         }
181     }
182     
183     protected void tearDownRequest()
184     {
185         if (facesContext != null)
186         {
187             facesContext.release();
188         }
189         facesContext = null;
190         externalContext = null;
191         application = null;
192         
193         response = null;
194         request = null;
195         //session = null;
196     }
197     
198     protected String getContextPath()
199     {
200         return "/test";
201     }
202     
203     protected String getServletPath()
204     {
205         return "/faces";
206     }
207 
208     public void processLifecycleExecute()
209     {
210         processLifecycleExecute(facesContext);
211     }
212 
213     public void processLifecycleRender()
214     {
215         processLifecycleRender(facesContext);
216     }
217     
218     public void processLifecycleExecuteAndRender()
219     {
220         processLifecycleExecute();
221         renderResponse();
222     }
223 
224     public void restoreView()
225     {
226         restoreView(facesContext);
227     }
228     
229     public void applyRequestValues()
230     {
231         applyRequestValues(facesContext);
232     }
233 
234     public void processValidations()
235     {
236         processValidations(facesContext);
237     }
238 
239     public void updateModelValues()
240     {
241         updateModelValues(facesContext);
242 
243     }
244 
245     public void invokeApplication()
246     {
247         invokeApplication(facesContext);
248     }
249     
250     public void renderResponse()
251     {
252         renderResponse(facesContext);
253     }
254     
255     public void processRemainingExecutePhases()
256     {
257         processRemainingExecutePhases(facesContext);
258     }
259 
260     public void processRemainingPhases()
261     {
262         processRemainingPhases(facesContext);
263     }
264     
265     public void executeBeforeRender()
266     {
267         executeBeforeRender(facesContext);
268     }
269     
270     public void executeViewHandlerRender()
271     {
272         executeViewHandlerRender(facesContext);
273     }
274         
275     public void executeBuildViewCycle()
276     {
277         executeBuildViewCycle(facesContext);
278     }
279     
280     public void executeAfterRender()
281     {
282         executeAfterRender(facesContext);
283     }
284     
285     public String getRenderedContent() throws IOException
286     {
287         return getRenderedContent(facesContext);
288     }
289     
290     protected MockMyFacesClient client = null;
291     
292     // Servlet objects 
293     protected MockHttpServletRequest request = null;
294     protected boolean requestInitializedCalled = false;
295     protected MockHttpServletResponse response = null;
296     protected MockHttpSessionProxy session = null;
297     protected MockHttpSession lastSession = null;
298     
299     protected Application application = null;
300     protected ExternalContext externalContext = null;
301     protected FacesContext facesContext = null;
302 
303     @Override
304     public MockHttpServletRequest getRequest()
305     {
306         return request;
307     }
308 
309     @Override
310     public MockHttpServletResponse getResponse()
311     {
312         return response;
313     }
314 
315     @Override
316     public FacesContext getFacesContext()
317     {
318         return facesContext;
319     }
320 }