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