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

For more information, please explore the Attic.

Coverage Report - org.apache.shale.test.jmock.AbstractJmockJsfTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJmockJsfTestCase
0% 
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.jmock;
 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 org.apache.shale.test.mock.MockApplication;
 30  
 import org.apache.shale.test.mock.MockExternalContext;
 31  
 import org.apache.shale.test.mock.MockFacesContext;
 32  
 import org.apache.shale.test.mock.MockFacesContextFactory;
 33  
 import org.apache.shale.test.mock.MockHttpServletRequest;
 34  
 import org.apache.shale.test.mock.MockHttpServletResponse;
 35  
 import org.apache.shale.test.mock.MockHttpSession;
 36  
 import org.apache.shale.test.mock.MockLifecycle;
 37  
 import org.apache.shale.test.mock.MockLifecycleFactory;
 38  
 import org.apache.shale.test.mock.MockRenderKit;
 39  
 import org.apache.shale.test.mock.MockServletConfig;
 40  
 import org.apache.shale.test.mock.MockServletContext;
 41  
 import org.jmock.cglib.MockObjectTestCase;
 42  
 
 43  
 /**
 44  
  * <p>Abstract JMock test case base class, which sets up the JavaServer Faces
 45  
  * mock object environment for a particular simulated request.  The following
 46  
  * protected variables are initialized in the <code>setUp()</code> method, and
 47  
  * cleaned up in the <code>tearDown()</code> method:</p>
 48  
  * <ul>
 49  
  * <li><code>application</code> (<code>MockApplication</code>)</li>
 50  
  * <li><code>config</code> (<code>MockServletConfig</code>)</li>
 51  
  * <li><code>externalContext</code> (<code>MockExternalContext</code>)</li>
 52  
  * <li><code>facesContext</code> (<code>MockFacesContext</code>)</li>
 53  
  * <li><code>lifecycle</code> (<code>MockLifecycle</code>)</li>
 54  
  * <li><code>request</code> (<code>MockHttpServletRequest</code></li>
 55  
  * <li><code>response</code> (<code>MockHttpServletResponse</code>)</li>
 56  
  * <li><code>servletContext</code> (<code>MockServletContext</code>)</li>
 57  
  * <li><code>session</code> (<code>MockHttpSession</code>)</li>
 58  
  * </ul>
 59  
  *
 60  
  * <p>In addition, appropriate factory classes will have been registered with
 61  
  * <code>javax.faces.FactoryFinder</code> for <code>Application</code> and
 62  
  * <code>RenderKit</code> instances.  The created <code>FacesContext</code>
 63  
  * instance will also have been registered in the apppriate thread local
 64  
  * variable, to simulate what a servlet container would do.</p>
 65  
  *
 66  
  * <p><strong>WARNING</strong> - If you choose to subclass this class, be sure
 67  
  * your <code>setUp()</code> and <code>tearDown()</code> methods call
 68  
  * <code>super.setUp()</code> and <code>super.tearDown()</code> respectively,
 69  
  * and that you implement your own <code>suite()</code> method that exposes
 70  
  * the test methods for your test case.</p>
 71  
  */
 72  
 
 73  
 public abstract class AbstractJmockJsfTestCase extends MockObjectTestCase {
 74  
 
 75  
 
 76  
     // ------------------------------------------------------------ Constructors
 77  
 
 78  
 
 79  
     // Construct a new instance of this test case.
 80  0
     public AbstractJmockJsfTestCase(String name) {
 81  0
         setName(name);
 82  0
     }
 83  
 
 84  
 
 85  
     // ---------------------------------------------------- Overall Test Methods
 86  
 
 87  
 
 88  
     // Set up instance variables required by this test case.
 89  
     protected void setUp() throws Exception {
 90  
         
 91  
         // Set up a new thread context class loader
 92  0
         threadContextClassLoader = Thread.currentThread().getContextClassLoader();
 93  0
         Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0],
 94  
                 this.getClass().getClassLoader()));
 95  
         
 96  
         // Set up Servlet API Objects
 97  0
         servletContext = new MockServletContext();
 98  0
         config = new MockServletConfig(servletContext);
 99  0
         session = new MockHttpSession();
 100  0
         session.setServletContext(servletContext);
 101  0
         request = new MockHttpServletRequest(session);
 102  0
         request.setServletContext(servletContext);
 103  0
         response = new MockHttpServletResponse();
 104  
         
 105  
         // Set up JSF API Objects
 106  0
         FactoryFinder.releaseFactories();
 107  0
         FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
 108  
         "org.apache.shale.test.mock.MockApplicationFactory");
 109  0
         FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY,
 110  
         "org.apache.shale.test.mock.MockFacesContextFactory");
 111  0
         FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY,
 112  
         "org.apache.shale.test.mock.MockLifecycleFactory");
 113  0
         FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY,
 114  
         "org.apache.shale.test.mock.MockRenderKitFactory");
 115  
         
 116  0
         externalContext =
 117  
             new MockExternalContext(servletContext, request, response);
 118  0
         lifecycleFactory = (MockLifecycleFactory)
 119  
         FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
 120  0
         lifecycle = (MockLifecycle)
 121  
         lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
 122  0
         facesContextFactory = (MockFacesContextFactory)
 123  
         FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
 124  0
         facesContext = (MockFacesContext)
 125  
         facesContextFactory.getFacesContext(servletContext,
 126  
                 request,
 127  
                 response,
 128  
                 lifecycle);
 129  0
         externalContext = (MockExternalContext) facesContext.getExternalContext();
 130  0
         UIViewRoot root = new UIViewRoot();
 131  0
         root.setViewId("/viewId");
 132  0
         root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
 133  0
         facesContext.setViewRoot(root);
 134  0
         ApplicationFactory applicationFactory = (ApplicationFactory)
 135  
           FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
 136  0
         application = (MockApplication) applicationFactory.getApplication();
 137  0
         facesContext.setApplication(application);
 138  0
         RenderKitFactory renderKitFactory = (RenderKitFactory)
 139  
         FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
 140  0
         renderKit = new MockRenderKit();
 141  0
         renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT, renderKit);
 142  
         
 143  0
     }
 144  
 
 145  
     // Tear down instance variables required by this test case.
 146  
     protected void tearDown() throws Exception {
 147  
 
 148  0
         application = null;
 149  0
         config = null;
 150  0
         externalContext = null;
 151  0
         facesContext.release();
 152  0
         facesContext = null;
 153  0
         lifecycle = null;
 154  0
         lifecycleFactory = null;
 155  0
         renderKit = null;
 156  0
         request = null;
 157  0
         response = null;
 158  0
         servletContext = null;
 159  0
         session = null;
 160  0
         FactoryFinder.releaseFactories();
 161  
 
 162  0
         Thread.currentThread().setContextClassLoader(threadContextClassLoader);
 163  0
         threadContextClassLoader = null;
 164  
 
 165  0
     }
 166  
 
 167  
 
 168  
     // ------------------------------------------------------ Instance Variables
 169  
 
 170  
 
 171  
     // Mock object instances for our tests
 172  0
     protected MockApplication         application = null;
 173  0
     protected MockServletConfig       config = null;
 174  0
     protected MockExternalContext     externalContext = null;
 175  0
     protected MockFacesContext        facesContext = null;
 176  0
     protected MockFacesContextFactory facesContextFactory = null;
 177  0
     protected MockLifecycle           lifecycle = null;
 178  0
     protected MockLifecycleFactory    lifecycleFactory = null;
 179  0
     protected MockRenderKit           renderKit = null;
 180  0
     protected MockHttpServletRequest  request = null;
 181  0
     protected MockHttpServletResponse response = null;
 182  0
     protected MockServletContext      servletContext = null;
 183  0
     protected MockHttpSession         session = null;
 184  
 
 185  
     // Thread context class loader saved and restored after each test
 186  0
     private ClassLoader threadContextClassLoader = null;
 187  
 
 188  
 }