Coverage Report - org.apache.myfaces.mc.test.core.mock.MockDefaultViewDeclarationLanguage
 
Classes in this File Line Coverage Branch Coverage Complexity
MockDefaultViewDeclarationLanguage
0%
0/71
0%
0/12
2.077
 
 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.beans.BeanInfo;
 22  
 import java.io.IOException;
 23  
 
 24  
 import javax.faces.FacesException;
 25  
 import javax.faces.FactoryFinder;
 26  
 import javax.faces.application.Application;
 27  
 import javax.faces.application.Resource;
 28  
 import javax.faces.application.ViewHandler;
 29  
 import javax.faces.component.UIViewRoot;
 30  
 import javax.faces.context.ExternalContext;
 31  
 import javax.faces.context.FacesContext;
 32  
 import javax.faces.context.ResponseWriter;
 33  
 import javax.faces.render.RenderKit;
 34  
 import javax.faces.render.RenderKitFactory;
 35  
 import javax.faces.view.StateManagementStrategy;
 36  
 import javax.faces.view.ViewDeclarationLanguage;
 37  
 import javax.faces.view.ViewMetadata;
 38  
 import javax.servlet.http.HttpServletResponse;
 39  
 
 40  
 import org.apache.myfaces.shared.application.InvalidViewIdException;
 41  
 
 42  
 /**
 43  
  * It is a vdl that is just provided as fallback, to ensure if
 44  
  * viewHandler.createView(context, null) is called, an instance
 45  
  * is retrieved just like with jsp vdl, and it provides some methods
 46  
  * to hook a view instance for createView() and restoreView() methods.
 47  
  * 
 48  
  * In this way, it is possible to create view instance programatically
 49  
  * and simulate that it is generated by the vdl. 
 50  
  * 
 51  
  * @author Leonardo Uribe
 52  
  *
 53  
  */
 54  0
 public class MockDefaultViewDeclarationLanguage extends ViewDeclarationLanguage
 55  
 {
 56  
     public static final String DUMMY_VIEW_CREATE_HOOK = "oam.dummy.create.UIViewRoot";
 57  
     public static final String DUMMY_VIEW_RESTORE_HOOK = "oam.dummy.restore.UIViewRoot";
 58  
 
 59  
     @Override
 60  
     public void buildView(FacesContext context, UIViewRoot view)
 61  
             throws IOException
 62  
     {
 63  0
     }
 64  
 
 65  
     @Override
 66  
     public UIViewRoot createView(FacesContext context, String viewId)
 67  
     {
 68  0
         if (context.getAttributes().containsKey(DUMMY_VIEW_CREATE_HOOK))
 69  
         {
 70  0
             UIViewRoot root = (UIViewRoot) context.getAttributes().remove(DUMMY_VIEW_CREATE_HOOK);
 71  0
             Application application = context.getApplication();
 72  0
             ViewHandler handler = application.getViewHandler();
 73  0
             root.setLocale(handler.calculateLocale(context));
 74  0
             root.setRenderKitId(handler.calculateRenderKitId(context));
 75  0
             root.setViewId(viewId);
 76  0
             return root;
 77  
         }
 78  
         else
 79  
         {
 80  
             try
 81  
             {
 82  0
                 viewId = calculateViewId(context, viewId);
 83  
                 
 84  0
                 Application application = context.getApplication();
 85  
                 // Create a new UIViewRoot object instance using Application.createComponent(UIViewRoot.COMPONENT_TYPE).
 86  0
                 UIViewRoot newViewRoot = (UIViewRoot) application.createComponent(UIViewRoot.COMPONENT_TYPE);
 87  0
                 UIViewRoot oldViewRoot = context.getViewRoot();
 88  0
                 if (oldViewRoot == null)
 89  
                 {
 90  
                     // If not, this method must call calculateLocale() and calculateRenderKitId(), and store the results
 91  
                     // as the values of the locale and renderKitId, proeprties, respectively, of the newly created
 92  
                     // UIViewRoot.
 93  0
                     ViewHandler handler = application.getViewHandler();
 94  0
                     newViewRoot.setLocale(handler.calculateLocale(context));
 95  0
                     newViewRoot.setRenderKitId(handler.calculateRenderKitId(context));
 96  0
                 }
 97  
                 else
 98  
                 {
 99  
                     // If there is an existing UIViewRoot available on the FacesContext, 
 100  
                     //this method must copy its locale
 101  
                     // and renderKitId to this new view root
 102  0
                     newViewRoot.setLocale(oldViewRoot.getLocale());
 103  0
                     newViewRoot.setRenderKitId(oldViewRoot.getRenderKitId());
 104  
                 }
 105  
                 
 106  
                 // TODO: VALIDATE - The spec is silent on the following line, but I feel bad if I don't set it
 107  0
                 newViewRoot.setViewId(viewId);
 108  
     
 109  0
                 return newViewRoot;
 110  
             }
 111  0
             catch (InvalidViewIdException e)
 112  
             {
 113  
                 // If no viewId could be identified, or the viewId is exactly equal to the servlet mapping, 
 114  
                 // send the response error code SC_NOT_FOUND with a suitable message to the client.
 115  0
                 sendSourceNotFound(context, e.getMessage());
 116  
                 
 117  
                 // TODO: VALIDATE - Spec is silent on the return value when an error was sent
 118  0
                 return null;
 119  
             }
 120  
         }
 121  
     }
 122  
     
 123  
     /**
 124  
      * Calculates the effective view identifier for the specified raw view identifier.
 125  
      * 
 126  
      * @param context le current FacesContext
 127  
      * @param viewId the raw view identifier
 128  
      * 
 129  
      * @return the effective view identifier
 130  
      */
 131  
     protected String calculateViewId(FacesContext context, String viewId)
 132  
     {
 133  0
         if (viewId != null)
 134  
         {
 135  0
             throw new InvalidViewIdException();
 136  
         }
 137  0
         return null;
 138  
     }
 139  
     
 140  
     protected void sendSourceNotFound(FacesContext context, String message)
 141  
     {
 142  0
         HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
 143  
         try
 144  
         {
 145  0
             context.responseComplete();
 146  0
             response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
 147  
         }
 148  0
         catch (IOException ioe)
 149  
         {
 150  0
             throw new FacesException(ioe);
 151  0
         }
 152  0
     }
 153  
     
 154  
     /**
 155  
      * Hook the passed instance on UIViewRoot, storing into facesContext attribute map,
 156  
      * so the next call to createView() will return that value.
 157  
      * 
 158  
      * @param context
 159  
      * @param root
 160  
      */
 161  
     public static void hookCreateView(FacesContext context, UIViewRoot root)
 162  
     {
 163  0
         context.getAttributes().put(DUMMY_VIEW_CREATE_HOOK, root);
 164  0
     }
 165  
 
 166  
     /**
 167  
      * Hook the passed instance on UIViewRoot, storing into facesContext attribute map,
 168  
      * so the next call to createView() will return that value.
 169  
      * 
 170  
      * @param context
 171  
      * @param root
 172  
      */
 173  
     public static void hookRestoreView(FacesContext context, UIViewRoot root)
 174  
     {
 175  0
         context.getAttributes().put(DUMMY_VIEW_RESTORE_HOOK, root);
 176  0
     }
 177  
 
 178  
     @Override
 179  
     public BeanInfo getComponentMetadata(FacesContext context,
 180  
             Resource componentResource)
 181  
     {
 182  0
         return null;
 183  
     }
 184  
 
 185  
     @Override
 186  
     public Resource getScriptComponentResource(FacesContext context,
 187  
             Resource componentResource)
 188  
     {
 189  0
         return null;
 190  
     }
 191  
 
 192  
     @Override
 193  
     public StateManagementStrategy getStateManagementStrategy(
 194  
             FacesContext context, String viewId)
 195  
     {
 196  0
         return null;
 197  
     }
 198  
 
 199  
     @Override
 200  
     public ViewMetadata getViewMetadata(FacesContext context, String viewId)
 201  
     {
 202  0
         return null;
 203  
     }
 204  
 
 205  
     @Override
 206  
     public void renderView(FacesContext context, UIViewRoot view)
 207  
             throws IOException
 208  
     {
 209  0
         checkNull(context, "context");
 210  0
         checkNull(view, "view");
 211  
         
 212  0
         ExternalContext externalContext = context.getExternalContext();
 213  0
         ResponseWriter responseWriter = context.getResponseWriter();
 214  0
         if (responseWriter == null)
 215  
         {
 216  0
             RenderKitFactory renderFactory = (RenderKitFactory) 
 217  
                 FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
 218  0
             RenderKit renderKit = renderFactory.getRenderKit(context, view.getRenderKitId());
 219  
 
 220  0
             responseWriter = renderKit.createResponseWriter(externalContext.getResponseOutputWriter(), 
 221  
                     null, externalContext.getRequestCharacterEncoding());
 222  0
             context.setResponseWriter(responseWriter);
 223  
         }
 224  
 
 225  
         // Now we actually render the document
 226  
         // Call startDocument() on the ResponseWriter.
 227  0
         responseWriter.startDocument();
 228  
 
 229  0
         view.encodeAll(context);
 230  
         
 231  
         // Call endDocument() on the ResponseWriter
 232  0
         responseWriter.endDocument();
 233  
     
 234  0
         responseWriter.flush();
 235  0
     }
 236  
 
 237  
     @Override
 238  
     public UIViewRoot restoreView(FacesContext context, String viewId)
 239  
     {
 240  0
         checkNull(context, "context");
 241  
         //checkNull(viewId, "viewId");
 242  
 
 243  0
         if (context.getAttributes().containsKey(DUMMY_VIEW_RESTORE_HOOK))
 244  
         {
 245  0
             UIViewRoot root = (UIViewRoot) context.getAttributes().remove(DUMMY_VIEW_RESTORE_HOOK);
 246  0
             root.setViewId(viewId);
 247  0
             return root;
 248  
         }
 249  
         else
 250  
         {
 251  0
             Application application = context.getApplication();
 252  
             
 253  0
             ViewHandler applicationViewHandler = application.getViewHandler();
 254  
             
 255  0
             String renderKitId = applicationViewHandler.calculateRenderKitId(context);
 256  
     
 257  0
             UIViewRoot viewRoot = application.getStateManager().restoreView(context, viewId, renderKitId);
 258  
     
 259  0
             return viewRoot;
 260  
         }
 261  
     }
 262  
     
 263  
     protected void checkNull(final Object o, final String param)
 264  
     {
 265  0
         if (o == null)
 266  
         {
 267  0
             throw new NullPointerException(param + " can not be null.");
 268  
         }
 269  0
     }
 270  
 }