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 java.io.IOException;
22  
23  import javax.faces.application.Application;
24  import javax.faces.component.UICommand;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIInput;
27  import javax.faces.context.ExternalContext;
28  import javax.faces.context.FacesContext;
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  
34  /**
35   * <p>Abstract JUnit test case base class, with method to setup/teardown a request.
36   * It helps to create tests that involve multiple requests like client submits, or
37   * tests that involve more control over the lifecycle.</p>
38   * 
39   * 
40   * @author Leonardo Uribe
41   *
42   */
43  public abstract class AbstractMyFacesRequestTestCase extends AbstractMyFacesTestCase
44  {
45      
46      @Override
47      public void setUp() throws Exception
48      {
49          super.setUp();
50          
51      }
52  
53      @Override
54      public void tearDown() throws Exception
55      {
56          tearDownRequest();
57          session = null;
58          if (client != null)
59          {
60              client.setTestCase(null);
61          }
62          client = null;
63          super.tearDown();
64      }
65  
66      protected void setupRequest() throws Exception
67      {
68          setupRequest(null);
69      }
70  
71      protected void setupRequest(String pathInfo) throws Exception
72      {
73          if (pathInfo == null)
74          {
75              setupRequest(null, null);
76          }
77          else
78          {
79              int queryIndex = pathInfo.indexOf("?");
80              if (queryIndex >= 0) 
81              {
82                  setupRequest(pathInfo.substring(0,queryIndex), pathInfo.substring(queryIndex+1));
83              }
84              else
85              {
86                  setupRequest(pathInfo, null);
87              }
88          }
89      }
90      
91      protected void setupRequest(String pathInfo, String query) throws Exception
92      {
93          session = (session == null) ? new MockHttpSession() : session;
94          session.setServletContext(servletContext);
95          request = new MockHttpServletRequest(session);
96          request.setServletContext(servletContext);
97          response = new MockHttpServletResponse();
98          //TODO check if this is correct
99          request.setPathElements(getContextPath(), getServletPath(), pathInfo, query);
100 
101         facesContext = facesContextFactory.getFacesContext(servletContext, request, response, lifecycle);
102         externalContext = facesContext.getExternalContext();
103         application = facesContext.getApplication();
104         if (client != null)
105         {
106             client.apply(request);
107         }
108         //Reset client
109         client = createClient();
110     }
111     
112     protected MockMyFacesClient createClient()
113     {
114         return new MockMyFacesClient(facesContext, this);
115     }
116     
117     protected void tearDownRequest()
118     {
119         if (facesContext != null)
120         {
121             facesContext.release();
122         }
123         facesContext = null;
124         externalContext = null;
125         application = null;
126         
127         response = null;
128         request = null;
129         //session = null;
130     }
131     
132     protected String getContextPath()
133     {
134         return "/test";
135     }
136     
137     protected String getServletPath()
138     {
139         return "/faces";
140     }
141 
142     protected void processLifecycleExecute() throws Exception
143     {
144         processLifecycleExecute(facesContext);
145     }
146     
147     protected void processLifecycleExecuteAndRender() throws Exception
148     {
149         processLifecycleExecute();
150         processRender();
151     }
152 
153     protected void processRestoreViewPhase() throws Exception
154     {
155         processRestoreViewPhase(facesContext);
156     }
157     
158     protected void processApplyRequestValuesPhase() throws Exception
159     {
160         processApplyRequestValuesPhase(facesContext);
161     }
162 
163     protected void processValidationsPhase() throws Exception
164     {
165         processValidationsPhase(facesContext);
166     }
167 
168     protected void processUpdateModelPhase() throws Exception
169     {
170         processUpdateModelPhase(facesContext);
171 
172     }
173     
174     protected void processInvokeApplicationPhase() throws Exception
175     {
176         processInvokeApplicationPhase(facesContext);
177     }
178     
179     protected void processRender() throws Exception
180     {
181         processRender(facesContext);
182     }
183     
184     protected void processRemainingExecutePhases() throws Exception
185     {
186         processRemainingExecutePhases(facesContext);
187     }
188 
189     protected void processRemainingPhases() throws Exception
190     {
191         processRemainingPhases(facesContext);
192     }
193     
194     protected String getRenderedContent() throws IOException
195     {
196         return getRenderedContent(facesContext);
197     }
198     
199     protected void inputText(UIComponent input, String text)
200     {
201         client.inputText((UIInput)input, text);
202     }
203     
204     /**
205      * Simulate a submit, processing the remaining phases and setting up the new request.
206      * It delegates to client.submit, where the necessary data is gathered to be applied
207      * later on client.apply method.
208      * 
209      * @param component
210      * @throws Exception
211      */
212     protected void submit(UIComponent component) throws Exception
213     {
214         client.submit(component);
215         /*
216         processRemainingPhases();
217         client.submit((UICommand)component);
218         String viewId = facesContext.getViewRoot().getViewId();
219         tearDownRequest();
220         setupRequest(viewId);
221         */
222     }
223     
224     protected MockMyFacesClient client = null;
225     
226     // Servlet objects 
227     protected MockHttpServletRequest request = null;
228     protected MockHttpServletResponse response = null;
229     protected MockHttpSession session = null;
230     
231     protected Application application = null;
232     protected ExternalContext externalContext = null;
233     protected FacesContext facesContext = null;
234 
235 }