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

For more information, please explore the Attic.

View Javadoc

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          super(name);
87      }
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          threadContextClassLoader = Thread.currentThread().getContextClassLoader();
100         Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0],
101                 this.getClass().getClassLoader()));
102 
103         // Set up Servlet API Objects
104         servletContext = new MockServletContext();
105         config = new MockServletConfig(servletContext);
106         session = new MockHttpSession();
107         session.setServletContext(servletContext);
108         request = new MockHttpServletRequest(session);
109         request.setServletContext(servletContext);
110         response = new MockHttpServletResponse();
111 
112         // Set up JSF API Objects
113         FactoryFinder.releaseFactories();
114         FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
115         "org.apache.shale.test.mock.MockApplicationFactory");
116         FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY,
117         "org.apache.shale.test.mock.MockFacesContextFactory");
118         FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY,
119         "org.apache.shale.test.mock.MockLifecycleFactory");
120         FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY,
121         "org.apache.shale.test.mock.MockRenderKitFactory");
122 
123         externalContext =
124             new MockExternalContext(servletContext, request, response);
125         lifecycleFactory = (MockLifecycleFactory)
126         FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
127         lifecycle = (MockLifecycle)
128         lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
129         facesContextFactory = (MockFacesContextFactory)
130         FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
131         facesContext = (MockFacesContext)
132         facesContextFactory.getFacesContext(servletContext,
133                 request,
134                 response,
135                 lifecycle);
136         externalContext = (MockExternalContext) facesContext.getExternalContext();
137         UIViewRoot root = new UIViewRoot();
138         root.setViewId("/viewId");
139         root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
140         facesContext.setViewRoot(root);
141         ApplicationFactory applicationFactory = (ApplicationFactory)
142           FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
143         application = (MockApplication) applicationFactory.getApplication();
144         facesContext.setApplication(application);
145         RenderKitFactory renderKitFactory = (RenderKitFactory)
146         FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
147         renderKit = new MockRenderKit();
148         renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT, renderKit);
149 
150     }
151 
152 
153     /***
154      * <p>Tear down instance variables required by this test case.</p>
155      */
156     protected void tearDown() throws Exception {
157 
158         application = null;
159         config = null;
160         externalContext = null;
161         facesContext.release();
162         facesContext = null;
163         lifecycle = null;
164         lifecycleFactory = null;
165         renderKit = null;
166         request = null;
167         response = null;
168         servletContext = null;
169         session = null;
170         FactoryFinder.releaseFactories();
171 
172         Thread.currentThread().setContextClassLoader(threadContextClassLoader);
173         threadContextClassLoader = null;
174 
175     }
176 
177 
178     // ------------------------------------------------------ Instance Variables
179 
180 
181     // Mock object instances for our tests
182     protected MockApplication         application = null;
183     protected MockServletConfig       config = null;
184     protected MockExternalContext     externalContext = null;
185     protected MockFacesContext        facesContext = null;
186     protected MockFacesContextFactory facesContextFactory = null;
187     protected MockLifecycle           lifecycle = null;
188     protected MockLifecycleFactory    lifecycleFactory = null;
189     protected MockRenderKit           renderKit = null;
190     protected MockHttpServletRequest  request = null;
191     protected MockHttpServletResponse response = null;
192     protected MockServletContext      servletContext = null;
193     protected MockHttpSession         session = null;
194 
195     // Thread context class loader saved and restored after each test
196     private ClassLoader threadContextClassLoader = null;
197 
198 }