Coverage Report - org.apache.myfaces.mc.test.core.mock.MockMyFacesClient
 
Classes in this File Line Coverage Branch Coverage Complexity
MockMyFacesClient
0%
0/125
0%
0/52
2.476
MockMyFacesClient$1
0%
0/5
0%
0/4
2.476
 
 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.mock;
 20  
 
 21  
 import java.util.HashMap;
 22  
 import java.util.Map;
 23  
 import javax.faces.FacesException;
 24  
 
 25  
 import javax.faces.component.UICommand;
 26  
 import javax.faces.component.UIComponent;
 27  
 import javax.faces.component.UIForm;
 28  
 import javax.faces.component.UIInput;
 29  
 import javax.faces.component.html.HtmlCommandButton;
 30  
 import javax.faces.component.visit.VisitCallback;
 31  
 import javax.faces.component.visit.VisitContext;
 32  
 import javax.faces.component.visit.VisitResult;
 33  
 import javax.faces.context.FacesContext;
 34  
 import javax.faces.render.ResponseStateManager;
 35  
 import javax.servlet.http.Cookie;
 36  
 import javax.servlet.http.HttpServletRequest;
 37  
 import javax.servlet.http.HttpServletResponse;
 38  
 import org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase;
 39  
 
 40  
 import org.apache.myfaces.shared.renderkit.RendererUtils;
 41  
 import org.apache.myfaces.test.mock.MockHttpServletRequest;
 42  
 import org.apache.myfaces.test.mock.MockHttpServletResponse;
 43  
 import org.junit.Assert;
 44  
 
 45  
 /**
 46  
  * Client that keep track and "translate" the commands done in a JSF component.
 47  
  * It simulates the effect of a browser, but without execute any javascript
 48  
  * or check the html output. If that level of detail is required, use an
 49  
  * in-container alternative like Arquillian and others. This strategy is designed
 50  
  * for server-side testing. 
 51  
  * 
 52  
  * @author Leonardo Uribe
 53  
  *
 54  
  */
 55  0
 public class MockMyFacesClient
 56  
 {
 57  0
     private Map<String, String> parameters = new HashMap<String, String>();
 58  0
     private Map<String, Cookie> cookies = new HashMap<String, Cookie>();
 59  0
     private Map<String, Object> headers = new HashMap<String, Object>();
 60  
     
 61  
     // It has sense the client has a reference over the test, because 
 62  
     // after all this class encapsulate some automatic operations
 63  
     private ServletMockContainer testCase;
 64  
     
 65  
     public MockMyFacesClient(FacesContext facesContext, ServletMockContainer testCase)
 66  0
     {
 67  0
         this.testCase = testCase;
 68  0
     }
 69  
     
 70  
     public void processRedirect()
 71  
     {
 72  0
         HttpServletResponse response = testCase.getResponse();
 73  0
         HttpServletRequest request = testCase.getRequest();
 74  0
         if (response.getStatus() == HttpServletResponse.SC_FOUND)
 75  
         {
 76  0
             testCase.processRemainingPhases(); //testCase.processRemainingPhases();
 77  0
             testCase.endRequest();
 78  0
             String location = response.getHeader("Location");
 79  0
             String contextPath = request.getContextPath() == null ? "" : request.getContextPath();
 80  0
             String servletPath = request.getServletPath() == null ? "" : request.getServletPath();
 81  0
             int cpi = location.indexOf(contextPath);
 82  0
             int spi = cpi < 0 ? -1 : location.indexOf(servletPath, cpi+contextPath.length());
 83  
             String viewId;
 84  0
             if (spi >= 0)
 85  
             {
 86  0
                 viewId = location.substring(spi+servletPath.length());
 87  
             }
 88  
             else
 89  
             {
 90  0
                 viewId = location;
 91  
             }
 92  0
             testCase.startViewRequest(viewId);
 93  0
         }
 94  
         else
 95  
         {
 96  0
             Assert.fail("Expected redirect not found");
 97  
         }
 98  0
     }
 99  
     
 100  
     public void inputText(UIInput input, String text)
 101  
     {
 102  0
         parameters.put(input.getClientId(), text);
 103  0
     }
 104  
     
 105  
     public void inputText(String clientId, String text)
 106  
     {
 107  0
         UIComponent input = getTestCase().getFacesContext().
 108  
             getViewRoot().findComponent(clientId);
 109  0
         if (input == null)
 110  
         {
 111  0
             throw new FacesException("input with clientId:"+clientId+" not found");
 112  
         }
 113  
         else
 114  
         {
 115  0
             parameters.put(input.getClientId(), text);
 116  
         }
 117  0
     }
 118  
     
 119  
     /**
 120  
      * Simulate a submit, processing the remaining phases and setting up the new request.
 121  
      * It delegates to client.submit, where the necessary data is gathered to be applied
 122  
      * later on client.apply method.
 123  
      * 
 124  
      * @param component
 125  
      * @throws Exception
 126  
      */
 127  
     public void submit(UIComponent component)
 128  
     {
 129  0
         testCase.processRemainingPhases();
 130  0
         this.internalSubmit((UICommand)component);
 131  0
         String viewId = testCase.getFacesContext().getViewRoot().getViewId();
 132  0
         testCase.endRequest();
 133  0
         testCase.startViewRequest(viewId);
 134  0
     }
 135  
     
 136  
     public void submit(String clientId)
 137  
     {
 138  0
         UIComponent button = getTestCase().getFacesContext().
 139  
             getViewRoot().findComponent(clientId);
 140  0
         if (button == null)
 141  
         {
 142  0
             throw new FacesException("button with clientId:"+clientId+" not found");
 143  
         }
 144  
         else
 145  
         {
 146  0
             submit(button);
 147  
         }
 148  0
     }
 149  
     
 150  
     protected void internalSubmit(UICommand command)
 151  
     {
 152  0
         if (command instanceof HtmlCommandButton)
 153  
         {
 154  0
             final FacesContext facesContext = testCase.getFacesContext();
 155  0
             UIForm form = getParentForm(command);
 156  0
             VisitContext visitContext = VisitContext.createVisitContext(facesContext);
 157  0
             form.visitTree(visitContext, new VisitCallback(){
 158  
 
 159  
                 public VisitResult visit(VisitContext context,
 160  
                         UIComponent target)
 161  
                 {
 162  0
                     if (target instanceof UIInput)
 163  
                     {
 164  0
                         if (!parameters.containsKey(target.getClientId(facesContext)))
 165  
                         {
 166  0
                             parameters.put(target.getClientId(facesContext), 
 167  
                                 RendererUtils.getStringValue(facesContext, target));
 168  
                         }
 169  
                     }
 170  0
                     return VisitResult.ACCEPT;
 171  
                 }
 172  
                 
 173  
             });
 174  0
             parameters.put(form.getClientId(facesContext)+"_SUBMIT", "1");
 175  0
             Object value = command.getValue();
 176  0
             parameters.put(command.getClientId(), value == null ? "" : value.toString());
 177  
             
 178  0
             applyStateFromPreviousRequest();
 179  
             /*
 180  
             parameters.put(ResponseStateManager.VIEW_STATE_PARAM, 
 181  
                 facesContext.getApplication().getStateManager().getViewState(facesContext));
 182  
             if (facesContext.getExternalContext().getClientWindow() != null)
 183  
             {
 184  
                 parameters.put(ResponseStateManager.CLIENT_WINDOW_URL_PARAM, 
 185  
                 facesContext.getExternalContext().getClientWindow().getId());
 186  
             }
 187  
             MockHttpServletResponse response = (MockHttpServletResponse) 
 188  
                 facesContext.getExternalContext().getResponse(); 
 189  
             Cookie cookie = response.getCookie("oam.Flash.RENDERMAP.TOKEN");
 190  
             getCookies().put("oam.Flash.RENDERMAP.TOKEN", cookie);*/
 191  
         }
 192  0
     }
 193  
     
 194  
     public void ajax(UIComponent source, String event, String execute, String render, boolean submit) throws Exception
 195  
     {
 196  0
         ajax(source, event, execute, render, submit, false);
 197  0
     }
 198  
             
 199  
     public void ajax(UIComponent source, String event, String execute, String render, 
 200  
         boolean submit, boolean resetValues)
 201  
     {
 202  0
         testCase.processRemainingPhases();
 203  0
         this.internalAjax(source, event, execute, render, submit, resetValues);
 204  0
         String viewId = testCase.getFacesContext().getViewRoot().getViewId();
 205  0
         testCase.endRequest();
 206  0
         testCase.startViewRequest(viewId);
 207  0
     }
 208  
     
 209  
     protected void internalAjax(UIComponent source, String event, String execute, String render, 
 210  
         boolean submit, boolean resetValues)
 211  
     {
 212  0
         final FacesContext facesContext = testCase.getFacesContext();
 213  0
         parameters.put("javax.faces.partial.ajax", "true");
 214  0
         parameters.put("javax.faces.behavior.event", event);
 215  0
         parameters.put("javax.faces.partial.event", "action".equals(event) ? "click" : event);
 216  0
         applyStateFromPreviousRequest();
 217  
         //parameters.put(ResponseStateManager.VIEW_STATE_PARAM, 
 218  
         //    facesContext.getApplication().getStateManager().getViewState(facesContext));
 219  0
         parameters.put("javax.faces.source", source.getClientId(facesContext));
 220  0
         if (execute == null)
 221  
         {
 222  0
             parameters.put("javax.faces.partial.execute", source.getClientId(facesContext));
 223  
         }
 224  
         else
 225  
         {
 226  0
             parameters.put("javax.faces.partial.execute", execute);
 227  
         }
 228  0
         if (render != null)
 229  
         {
 230  0
             parameters.put("javax.faces.partial.render", render);
 231  
         }
 232  
         
 233  0
         if (submit)
 234  
         {
 235  0
             parameters.put(source.getClientId(facesContext)+"_SUBMIT", "1");
 236  0
             parameters.put(source.getClientId(facesContext), source.getClientId(facesContext));
 237  
         }
 238  
         
 239  0
         if (resetValues)
 240  
         {
 241  0
             parameters.put("javax.faces.partial.resetValues", "true");
 242  
         }
 243  
         
 244  0
         headers.put("Faces-Request", "partial/ajax");
 245  0
         headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
 246  0
     }
 247  
     
 248  
     protected void applyStateFromPreviousRequest()
 249  
     {
 250  0
         FacesContext facesContext = testCase.getFacesContext();
 251  
         
 252  0
         parameters.put(ResponseStateManager.VIEW_STATE_PARAM, 
 253  
             facesContext.getApplication().getStateManager().getViewState(facesContext));
 254  0
         if (facesContext.getExternalContext().getClientWindow() != null)
 255  
         {
 256  0
             parameters.put(ResponseStateManager.CLIENT_WINDOW_URL_PARAM, 
 257  
                 facesContext.getExternalContext().getClientWindow().getId());
 258  
         }
 259  
         
 260  0
         applyCookiesFromPreviousRequest();
 261  0
     }
 262  
     
 263  
     protected void applyCookiesFromPreviousRequest()
 264  
     {
 265  0
         MockHttpServletResponse response = (MockHttpServletResponse) testCase.getResponse();
 266  0
         if (response.getCookies() != null && !response.getCookies().isEmpty())
 267  
         {
 268  0
             for (Map.Entry<String, Cookie> entry : response.getCookies().entrySet())
 269  
             {
 270  0
                 getCookies().put(entry.getKey(), entry.getValue());
 271  0
             }
 272  
         }
 273  0
     }
 274  
     
 275  
     public Map<String, String> getParameters()
 276  
     {
 277  0
         return parameters;
 278  
     }
 279  
     
 280  
     public Map<String, Object> getHeaders()
 281  
     {
 282  0
         return headers;
 283  
     }
 284  
     
 285  
     public Map<String, Cookie> getCookies()
 286  
     {
 287  0
         return cookies;
 288  
     }
 289  
     
 290  
     private UIForm getParentForm(UIComponent component)
 291  
     {
 292  0
         UIComponent parent = component.getParent();
 293  0
         while ( parent != null)
 294  
         {
 295  0
             if (parent instanceof UIForm)
 296  
             {
 297  0
                 return (UIForm) parent;
 298  
             }
 299  0
             parent = parent.getParent();
 300  
         }
 301  0
         return null;
 302  
     }
 303  
     
 304  
     /**
 305  
      * Apply all params, headers and cookies into the request.
 306  
      * 
 307  
      * @param request
 308  
      */
 309  
     public void apply(MockHttpServletRequest request)
 310  
     {
 311  0
         Map<String, String> inputFields = getParameters();
 312  0
         for (Map.Entry<String, String> entry : inputFields.entrySet())
 313  
         {
 314  0
             request.addParameter(entry.getKey(), entry.getValue());
 315  0
         }
 316  0
         Map<String, Object> headerFields = getHeaders();
 317  0
         for (Map.Entry<String, Object> entry : headerFields.entrySet())
 318  
         {
 319  0
             if (entry.getValue() instanceof String)
 320  
             {
 321  0
                 request.addHeader(entry.getKey(), (String) entry.getValue());
 322  
             }
 323  0
             else if (entry.getValue() instanceof Integer)
 324  
             {
 325  0
                 request.addIntHeader(entry.getKey(), (Integer) entry.getValue());
 326  
             }
 327  0
             else if (entry.getValue() instanceof java.util.Date)
 328  
             {
 329  0
                 request.addDateHeader(entry.getKey(), ((java.util.Date) entry.getValue()).getTime());
 330  
             }
 331  0
         }
 332  0
         Map<String, Cookie> cookies = getCookies();
 333  0
         for (Map.Entry<String, Cookie> entry : cookies.entrySet())
 334  
         {
 335  0
             request.addCookie(entry.getValue());
 336  0
         }
 337  0
     }
 338  
     
 339  
     public void reset(FacesContext facesContext)
 340  
     {
 341  0
         this.parameters.clear();
 342  0
         this.headers.clear();
 343  0
         this.cookies.clear();
 344  0
     }
 345  
 
 346  
     public ServletMockContainer getTestCase()
 347  
     {
 348  0
         return testCase;
 349  
     }
 350  
 
 351  
     public void setTestCase(AbstractMyFacesRequestTestCase testCase)
 352  
     {
 353  0
         this.testCase = testCase;
 354  0
     }
 355  
 }