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