2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

Coverage Report - org.apache.shale.test.base.AbstractJsfTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJsfTestCase
100%
62/62
N/A
1
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to you under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.shale.test.base;
 19  
 
 20  
 import java.net.URL;
 21  
 import java.net.URLClassLoader;
 22  
 
 23  
 import javax.faces.FactoryFinder;
 24  
 import javax.faces.application.ApplicationFactory;
 25  
 import javax.faces.component.UIViewRoot;
 26  
 import javax.faces.lifecycle.LifecycleFactory;
 27  
 import javax.faces.render.RenderKitFactory;
 28  
 
 29  
 import junit.framework.TestCase;
 30  
 
 31  
 import org.apache.shale.test.mock.MockApplication;
 32  
 import org.apache.shale.test.mock.MockExternalContext;
 33  
 import org.apache.shale.test.mock.MockFacesContext;
 34  
 import org.apache.shale.test.mock.MockFacesContextFactory;
 35  
 import org.apache.shale.test.mock.MockHttpServletRequest;
 36  
 import org.apache.shale.test.mock.MockHttpServletResponse;
 37  
 import org.apache.shale.test.mock.MockHttpSession;
 38  
 import org.apache.shale.test.mock.MockLifecycle;
 39  
 import org.apache.shale.test.mock.MockLifecycleFactory;
 40  
 import org.apache.shale.test.mock.MockRenderKit;
 41  
 import org.apache.shale.test.mock.MockServletConfig;
 42  
 import org.apache.shale.test.mock.MockServletContext;
 43  
 
 44  
 /**
 45  
  * <p>Abstract JUnit test case base class, which sets up the JavaServer Faces
 46  
  * mock object environment for a particular simulated request.  The following
 47  
  * protected variables are initialized in the <code>setUp()</code> method, and
 48  
  * cleaned up in the <code>tearDown()</code> method:</p>
 49  
  * <ul>
 50  
  * <li><code>application</code> (<code>MockApplication</code>)</li>
 51  
  * <li><code>config</code> (<code>MockServletConfig</code>)</li>
 52  
  * <li><code>externalContext</code> (<code>MockExternalContext</code>)</li>
 53  
  * <li><code>facesContext</code> (<code>MockFacesContext</code>)</li>
 54  
  * <li><code>lifecycle</code> (<code>MockLifecycle</code>)</li>
 55  
  * <li><code>request</code> (<code>MockHttpServletRequest</code></li>
 56  
  * <li><code>response</code> (<code>MockHttpServletResponse</code>)</li>
 57  
  * <li><code>servletContext</code> (<code>MockServletContext</code>)</li>
 58  
  * <li><code>session</code> (<code>MockHttpSession</code>)</li>
 59  
  * </ul>
 60  
  *
 61  
  * <p>In addition, appropriate factory classes will have been registered with
 62  
  * <code>javax.faces.FactoryFinder</code> for <code>Application</code> and
 63  
  * <code>RenderKit</code> instances.  The created <code>FacesContext</code>
 64  
  * instance will also have been registered in the apppriate thread local
 65  
  * variable, to simulate what a servlet container would do.</p>
 66  
  *
 67  
  * <p><strong>WARNING</strong> - If you choose to subclass this class, be sure
 68  
  * your <code>setUp()</code> and <code>tearDown()</code> methods call
 69  
  * <code>super.setUp()</code> and <code>super.tearDown()</code> respectively,
 70  
  * and that you implement your own <code>suite()</code> method that exposes
 71  
  * the test methods for your test case.</p>
 72  
  */
 73  
 
 74  
 public abstract class AbstractJsfTestCase extends TestCase {
 75  
 
 76  
 
 77  
     // ------------------------------------------------------------ Constructors
 78  
 
 79  
 
 80  
     /**
 81  
      * <p>Construct a new instance of this test case.</p>
 82  
      *
 83  
      * @param name Name of this test case
 84  
      */
 85  
     public AbstractJsfTestCase(String name) {
 86  36
         super(name);
 87  36
     }
 88  
 
 89  
 
 90  
     // ---------------------------------------------------- Overall Test Methods
 91  
 
 92  
 
 93  
     /**
 94  
      * <p>Set up instance variables required by this test case.</p>
 95  
      */
 96  
     protected void setUp() throws Exception {
 97  
 
 98  
         // Set up a new thread context class loader
 99  18
         threadContextClassLoader = Thread.currentThread().getContextClassLoader();
 100  18
         Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0],
 101  
                 this.getClass().getClassLoader()));
 102  
 
 103  
         // Set up Servlet API Objects
 104  18
         servletContext = new MockServletContext();
 105  18
         config = new MockServletConfig(servletContext);
 106  18
         session = new MockHttpSession();
 107  18
         session.setServletContext(servletContext);
 108  18
         request = new MockHttpServletRequest(session);
 109  18
         request.setServletContext(servletContext);
 110  18
         response = new MockHttpServletResponse();
 111  
 
 112  
         // Set up JSF API Objects
 113  18
         FactoryFinder.releaseFactories();
 114  18
         FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
 115  
         "org.apache.shale.test.mock.MockApplicationFactory");
 116  18
         FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY,
 117  
         "org.apache.shale.test.mock.MockFacesContextFactory");
 118  18
         FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY,
 119  
         "org.apache.shale.test.mock.MockLifecycleFactory");
 120  18
         FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY,
 121  
         "org.apache.shale.test.mock.MockRenderKitFactory");
 122  
 
 123  18
         externalContext =
 124  
             new MockExternalContext(servletContext, request, response);
 125  18
         lifecycleFactory = (MockLifecycleFactory)
 126  
         FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
 127  18
         lifecycle = (MockLifecycle)
 128  
         lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
 129  18
         facesContextFactory = (MockFacesContextFactory)
 130  
         FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
 131  18
         facesContext = (MockFacesContext)
 132  
         facesContextFactory.getFacesContext(servletContext,
 133  
                 request,
 134  
                 response,
 135  
                 lifecycle);
 136  18
         externalContext = (MockExternalContext) facesContext.getExternalContext();
 137  18
         UIViewRoot root = new UIViewRoot();
 138  18
         root.setViewId("/viewId");
 139  18
         root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
 140  18
         facesContext.setViewRoot(root);
 141  18
         ApplicationFactory applicationFactory = (ApplicationFactory)
 142  
           FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
 143  18
         application = (MockApplication) applicationFactory.getApplication();
 144  18
         facesContext.setApplication(application);
 145  18
         RenderKitFactory renderKitFactory = (RenderKitFactory)
 146  
         FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
 147  18
         renderKit = new MockRenderKit();
 148  18
         renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT, renderKit);
 149  
 
 150  18
     }
 151  
 
 152  
 
 153  
     /**
 154  
      * <p>Tear down instance variables required by this test case.</p>
 155  
      */
 156  
     protected void tearDown() throws Exception {
 157  
 
 158  18
         application = null;
 159  18
         config = null;
 160  18
         externalContext = null;
 161  18
         facesContext.release();
 162  18
         facesContext = null;
 163  18
         lifecycle = null;
 164  18
         lifecycleFactory = null;
 165  18
         renderKit = null;
 166  18
         request = null;
 167  18
         response = null;
 168  18
         servletContext = null;
 169  18
         session = null;
 170  18
         FactoryFinder.releaseFactories();
 171  
 
 172  18
         Thread.currentThread().setContextClassLoader(threadContextClassLoader);
 173  18
         threadContextClassLoader = null;
 174  
 
 175  18
     }
 176  
 
 177  
 
 178  
     // ------------------------------------------------------ Instance Variables
 179  
 
 180  
 
 181  
     // Mock object instances for our tests
 182  36
     protected MockApplication         application = null;
 183  36
     protected MockServletConfig       config = null;
 184  36
     protected MockExternalContext     externalContext = null;
 185  36
     protected MockFacesContext        facesContext = null;
 186  36
     protected MockFacesContextFactory facesContextFactory = null;
 187  36
     protected MockLifecycle           lifecycle = null;
 188  36
     protected MockLifecycleFactory    lifecycleFactory = null;
 189  36
     protected MockRenderKit           renderKit = null;
 190  36
     protected MockHttpServletRequest  request = null;
 191  36
     protected MockHttpServletResponse response = null;
 192  36
     protected MockServletContext      servletContext = null;
 193  36
     protected MockHttpSession         session = null;
 194  
 
 195  
     // Thread context class loader saved and restored after each test
 196  36
     private ClassLoader threadContextClassLoader = null;
 197  
 
 198  
 }