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.trinidadbuild.test;
20  
21  import java.io.IOException;
22  
23  import java.util.List;
24  
25  import javax.faces.FactoryFinder;
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.UIViewRoot;
28  import javax.faces.context.FacesContext;
29  import javax.faces.render.RenderKit;
30  import javax.faces.render.RenderKitFactory;
31  
32  import org.apache.shale.test.jmock.AbstractJmockJsfTestCase;
33  import org.apache.shale.test.mock.MockFacesContext;
34  import org.apache.shale.test.mock.MockRenderKitFactory;
35  import org.jmock.Mock;
36  
37  /**
38   * Base class for JavaServer Faces unit tests.
39   * Acts as a wrapper class to <code>AbstractJsfTestCase</code>
40   * 
41   */
42  public class FacesTestCase extends AbstractJmockJsfTestCase
43  {
44    /**
45     * Creates a new FacesTestCase.
46     *
47     * @param testName  the unit test name
48     */
49    public FacesTestCase(
50      String testName)
51    {
52      super(testName);
53    }
54  
55    @Override
56    protected void setUp() throws Exception
57    {
58      super.setUp();
59      FacesContext oldFacesContext = facesContext;
60      UIViewRoot oldViewRoot = oldFacesContext.getViewRoot();
61      oldFacesContext.release();
62      facesContext = new MockFacesContext12(externalContext,
63                                            lifecycle,
64                                            application);
65      facesContext.setViewRoot(oldViewRoot);
66      facesContext.setApplication(application);
67  
68      facesContext.getViewRoot().setRenderKitId("org.apache.myfaces.trinidad.core"); 
69      RenderKitFactory renderKitFactory = (RenderKitFactory)
70      FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
71      Mock mockRenderKitty = mock(RenderKit.class);
72      RenderKit renderKit = (RenderKit) mockRenderKitty.proxy();
73      _mockRenderKit = new MockRenderKitWrapper(mockRenderKitty, renderKit);
74      renderKitFactory.addRenderKit("org.apache.myfaces.trinidad.core", renderKit);
75    }
76  
77    @Override
78    protected void tearDown() throws Exception
79    {
80      super.tearDown();
81    }
82  
83    /**
84     * Configures the FactoryFinder RenderKitFactory implementation class.
85     *
86     * @param renderkitFactoryClass  the render kit factory class
87     */
88    protected RenderKitFactory setupRenderKitFactory(
89      Class<? extends RenderKitFactory> renderkitFactoryClass)
90    {
91  
92      FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY,
93                               renderkitFactoryClass.getName());
94      return (RenderKitFactory)
95        FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
96    }
97  
98    /**
99     * Configures the FactoryFinder MockRenderKitFactory implementation class.
100    * @TODO rename the class.
101    * @TODO move to Shale.
102    */
103   protected MockRenderKitFactory setupMockRenderKitFactory()
104   {
105     return (MockRenderKitFactory)
106       setupRenderKitFactory(MockRenderKitFactory.class);
107   }
108 
109 
110   /**
111    * Renders the component tree.
112    *
113    * @param context    the faces context
114    * @param component  the component tree to render
115    *
116    * @throws IOException  when the render fails
117    */
118   @SuppressWarnings("unchecked")
119   protected void doRenderResponse(
120     FacesContext context,
121     UIComponent  component) throws IOException
122   {
123     component.encodeBegin(context);
124     if (component.getRendersChildren())
125     {
126       component.encodeChildren(context);
127     }
128     else
129     {
130       for(UIComponent child : (List<UIComponent>)component.getChildren())
131       {
132         doRenderResponse(context, child);
133       }
134     }
135     component.encodeEnd(context);
136   }
137 
138   public void setCurrentContext(
139       FacesContext context)
140   {
141     TestFacesContext.setCurrentInstance(context);
142   }
143   
144   private MockRenderKitWrapper _mockRenderKit = null;
145   
146   public MockRenderKitWrapper getMockRenderKitWrapper()
147   {
148     return _mockRenderKit;
149   }
150 
151   /**
152    * @todo add this code to MockFacesContext
153    */
154   public static abstract class TestFacesContext extends MockFacesContext
155   {
156     public static void setCurrentInstance(
157       FacesContext context)
158     {
159       FacesContext.setCurrentInstance(context);
160     }
161   }
162 }