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