001// Generated by delombok at Sun Jul 30 17:21:56 UTC 2023
002package de.cuioss.test.jsf.util;
003
004import java.net.URL;
005import java.net.URLClassLoader;
006import javax.faces.FactoryFinder;
007import javax.faces.application.Application;
008import javax.faces.application.ApplicationFactory;
009import javax.faces.component.UIViewRoot;
010import javax.faces.lifecycle.LifecycleFactory;
011import javax.faces.render.RenderKitFactory;
012import org.apache.myfaces.test.config.ResourceBundleVarNames;
013import org.apache.myfaces.test.mock.MockExternalContext;
014import org.apache.myfaces.test.mock.MockFacesContext;
015import org.apache.myfaces.test.mock.MockFacesContextFactory;
016import org.apache.myfaces.test.mock.MockHttpServletRequest;
017import org.apache.myfaces.test.mock.MockHttpServletResponse;
018import org.apache.myfaces.test.mock.MockHttpSession;
019import org.apache.myfaces.test.mock.MockRenderKit;
020import org.apache.myfaces.test.mock.MockServletConfig;
021import org.apache.myfaces.test.mock.lifecycle.MockLifecycle;
022import org.apache.myfaces.test.mock.lifecycle.MockLifecycleFactory;
023import de.cuioss.test.jsf.mocks.CuiMockHttpServletRequest;
024import de.cuioss.test.jsf.mocks.CuiMockHttpSession;
025import de.cuioss.test.jsf.mocks.CuiMockSearchExpressionContextFactory;
026import de.cuioss.test.jsf.mocks.CuiMockServletContext;
027import de.cuioss.test.jsf.mocks.CuiMockUIViewRoot;
028
029/**
030 * Replacement for MyFaces
031 * {@link org.apache.myfaces.test.base.junit4.AbstractJsfTestCase}, where the
032 * code is initially taken from
033 *
034 * @author Oliver Wolff
035 */
036public class JsfRuntimeSetup {
037    private Application application = null;
038    private MockServletConfig config = null;
039    private MockExternalContext externalContext = null;
040    private MockFacesContext facesContext = null;
041    private MockFacesContextFactory facesContextFactory = null;
042    private MockLifecycle lifecycle = null;
043    private MockLifecycleFactory lifecycleFactory = null;
044    private MockRenderKit renderKit = null;
045    private MockHttpServletRequest request = null;
046    private MockHttpServletResponse response = null;
047    private CuiMockServletContext servletContext = null;
048    private MockHttpSession session = null;
049    // Thread context class loader saved and restored after each test
050    private ClassLoader threadContextClassLoader = null;
051    private boolean classLoaderSet = false;
052
053    /**
054     * <p>
055     * Set up instance variables required by this test case.
056     * </p>
057     */
058    public void setUp() {
059        // Set up a new thread context class loader
060        setUpClassloader();
061        // Set up Servlet API Objects
062        setUpServletObjects();
063        // Set up JSF API Objects
064        FactoryFinder.releaseFactories();
065        setFactories();
066        setUpJSFObjects();
067    }
068
069    /**
070     * <p>
071     * Tear down instance variables required by this test case.
072     * </p>
073     */
074    public void tearDown() {
075        application = null;
076        config = null;
077        externalContext = null;
078        if (facesContext != null) {
079            facesContext.release();
080        }
081        facesContext = null;
082        lifecycle = null;
083        lifecycleFactory = null;
084        renderKit = null;
085        request = null;
086        response = null;
087        servletContext = null;
088        session = null;
089        FactoryFinder.releaseFactories();
090        ResourceBundleVarNames.resetNames();
091        tearDownClassloader();
092    }
093
094    // owolff: No problem in test context
095    /**
096     * Set up the thread context classloader. JSF uses the this classloader in order
097     * to find related factory classes and other resources, but in some selected
098     * cases, the default classloader cannot be properly set.
099     */
100    @SuppressWarnings("resource")
101    private void setUpClassloader() {
102        threadContextClassLoader = Thread.currentThread().getContextClassLoader();
103        Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0], this.getClass().getClassLoader()));
104        classLoaderSet = true;
105    }
106
107    /**
108     * <p>
109     * Setup JSF object used for the test. By default it calls to the following
110     * methods in this order:
111     * </p>
112     *
113     * <ul>
114     * <li><code>setUpExternalContext();</code></li>
115     * <li><code>setUpLifecycle();</code></li>
116     * <li><code>setUpFacesContext();</code></li>
117     * <li><code>setUpView();</code></li>
118     * <li><code>setUpApplication();</code></li>
119     * <li><code>setUpRenderKit();</code></li>
120     * </ul>
121     */
122    private void setUpJSFObjects() {
123        setUpExternalContext();
124        setUpLifecycle();
125        setUpFacesContext();
126        setUpView();
127        setUpApplication();
128        setUpRenderKit();
129    }
130
131    /**
132     * <p>
133     * Setup servlet objects that will be used for the test:
134     * </p>
135     *
136     * <ul>
137     * <li><code>config</code> (<code>MockServletConfig</code>)</li>
138     * <li><code>servletContext</code> (<code>MockServletContext</code>)</li>
139     * <li><code>request</code> (<code>CuiMockHttpServletRequest</code></li>
140     * <li><code>response</code> (<code>MockHttpServletResponse</code>)</li>
141     * <li><code>session</code> (<code>CuiMockHttpSession</code>)</li>
142     * </ul>
143     */
144    private void setUpServletObjects() {
145        servletContext = new CuiMockServletContext();
146        config = new MockServletConfig(servletContext);
147        session = new CuiMockHttpSession(servletContext);
148        request = new CuiMockHttpServletRequest();
149        request.setHttpSession(session);
150        request.setServletContext(servletContext);
151        response = new MockHttpServletResponse();
152    }
153
154    /**
155     * <p>
156     * Set JSF factories using FactoryFinder method setFactory.
157     * </p>
158     */
159    private void setFactories() {
160        FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, "org.apache.myfaces.test.mock.MockApplicationFactory");
161        FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY, "org.apache.myfaces.test.mock.MockFacesContextFactory");
162        FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY, "org.apache.myfaces.test.mock.lifecycle.MockLifecycleFactory");
163        FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY, "org.apache.myfaces.test.mock.MockRenderKitFactory");
164        FactoryFinder.setFactory(FactoryFinder.EXCEPTION_HANDLER_FACTORY, "org.apache.myfaces.test.mock.MockExceptionHandlerFactory");
165        FactoryFinder.setFactory(FactoryFinder.PARTIAL_VIEW_CONTEXT_FACTORY, "org.apache.myfaces.test.mock.MockPartialViewContextFactory");
166        FactoryFinder.setFactory(FactoryFinder.VISIT_CONTEXT_FACTORY, "org.apache.myfaces.test.mock.visit.MockVisitContextFactory");
167        FactoryFinder.setFactory(FactoryFinder.CLIENT_WINDOW_FACTORY, "org.apache.myfaces.test.mock.MockClientWindowFactory");
168        // Cui Extensions
169        FactoryFinder.setFactory(FactoryFinder.SEARCH_EXPRESSION_CONTEXT_FACTORY, CuiMockSearchExpressionContextFactory.class.getName());
170    }
171
172    /**
173     * Setup the <code>externalContext</code> variable, using the servlet variables
174     * already initialized.
175     */
176    private void setUpExternalContext() {
177        externalContext = new MockExternalContext(servletContext, request, response);
178    }
179
180    /**
181     * Setup the <code>lifecycle</code> and <code>lifecycleFactory</code> variables.
182     */
183    private void setUpLifecycle() {
184        lifecycleFactory = (MockLifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
185        lifecycle = (MockLifecycle) lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
186    }
187
188    /**
189     * Setup the <code>facesContextFactory</code> and <code>facesContext</code>
190     * variable. Before end, by default it override <code>externalContext</code>
191     * variable from the value retrieved from facesContext.getExternalContext(),
192     * because sometimes it is possible facesContext overrides externalContext
193     * internally.
194     */
195    private void setUpFacesContext() {
196        facesContextFactory = (MockFacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
197        facesContext = (MockFacesContext) facesContextFactory.getFacesContext(servletContext, request, response, lifecycle);
198        if (facesContext.getExternalContext() != null) {
199            externalContext = (MockExternalContext) facesContext.getExternalContext();
200        }
201    }
202
203    /**
204     * By default, create an instance of UIViewRoot, set its viewId as "/viewId" and
205     * assign it to the current facesContext.
206     */
207    private void setUpView() {
208        UIViewRoot root = new CuiMockUIViewRoot();
209        root.setViewId("/viewId");
210        root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
211        facesContext.setViewRoot(root);
212    }
213
214    /**
215     * Setup the <code>application</code> variable and before the end by default it
216     * is assigned to the <code>facesContext</code> variable, calling
217     * <code>facesContext.setApplication(application)</code>
218     */
219    private void setUpApplication() {
220        var applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
221        application = applicationFactory.getApplication();
222        facesContext.setApplication(application);
223    }
224
225    /**
226     * Setup the <code>renderKit</code> variable. This is a good place to use
227     * <code>ConfigParser</code> to register converters, validators, components or
228     * renderkits.
229     */
230    private void setUpRenderKit() {
231        var renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
232        renderKit = new MockRenderKit();
233        renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT, renderKit);
234    }
235
236    private void tearDownClassloader() {
237        if (classLoaderSet) {
238            Thread.currentThread().setContextClassLoader(threadContextClassLoader);
239            threadContextClassLoader = null;
240            classLoaderSet = false;
241        }
242    }
243
244    @java.lang.SuppressWarnings("all")
245    @lombok.Generated
246    public Application getApplication() {
247        return this.application;
248    }
249
250    @java.lang.SuppressWarnings("all")
251    @lombok.Generated
252    public void setApplication(final Application application) {
253        this.application = application;
254    }
255
256    @java.lang.SuppressWarnings("all")
257    @lombok.Generated
258    public MockServletConfig getConfig() {
259        return this.config;
260    }
261
262    @java.lang.SuppressWarnings("all")
263    @lombok.Generated
264    public void setConfig(final MockServletConfig config) {
265        this.config = config;
266    }
267
268    @java.lang.SuppressWarnings("all")
269    @lombok.Generated
270    public MockExternalContext getExternalContext() {
271        return this.externalContext;
272    }
273
274    @java.lang.SuppressWarnings("all")
275    @lombok.Generated
276    public void setExternalContext(final MockExternalContext externalContext) {
277        this.externalContext = externalContext;
278    }
279
280    @java.lang.SuppressWarnings("all")
281    @lombok.Generated
282    public MockFacesContext getFacesContext() {
283        return this.facesContext;
284    }
285
286    @java.lang.SuppressWarnings("all")
287    @lombok.Generated
288    public void setFacesContext(final MockFacesContext facesContext) {
289        this.facesContext = facesContext;
290    }
291
292    @java.lang.SuppressWarnings("all")
293    @lombok.Generated
294    public MockFacesContextFactory getFacesContextFactory() {
295        return this.facesContextFactory;
296    }
297
298    @java.lang.SuppressWarnings("all")
299    @lombok.Generated
300    public void setFacesContextFactory(final MockFacesContextFactory facesContextFactory) {
301        this.facesContextFactory = facesContextFactory;
302    }
303
304    @java.lang.SuppressWarnings("all")
305    @lombok.Generated
306    public MockLifecycle getLifecycle() {
307        return this.lifecycle;
308    }
309
310    @java.lang.SuppressWarnings("all")
311    @lombok.Generated
312    public void setLifecycle(final MockLifecycle lifecycle) {
313        this.lifecycle = lifecycle;
314    }
315
316    @java.lang.SuppressWarnings("all")
317    @lombok.Generated
318    public MockLifecycleFactory getLifecycleFactory() {
319        return this.lifecycleFactory;
320    }
321
322    @java.lang.SuppressWarnings("all")
323    @lombok.Generated
324    public void setLifecycleFactory(final MockLifecycleFactory lifecycleFactory) {
325        this.lifecycleFactory = lifecycleFactory;
326    }
327
328    @java.lang.SuppressWarnings("all")
329    @lombok.Generated
330    public MockRenderKit getRenderKit() {
331        return this.renderKit;
332    }
333
334    @java.lang.SuppressWarnings("all")
335    @lombok.Generated
336    public void setRenderKit(final MockRenderKit renderKit) {
337        this.renderKit = renderKit;
338    }
339
340    @java.lang.SuppressWarnings("all")
341    @lombok.Generated
342    public MockHttpServletRequest getRequest() {
343        return this.request;
344    }
345
346    @java.lang.SuppressWarnings("all")
347    @lombok.Generated
348    public void setRequest(final MockHttpServletRequest request) {
349        this.request = request;
350    }
351
352    @java.lang.SuppressWarnings("all")
353    @lombok.Generated
354    public MockHttpServletResponse getResponse() {
355        return this.response;
356    }
357
358    @java.lang.SuppressWarnings("all")
359    @lombok.Generated
360    public void setResponse(final MockHttpServletResponse response) {
361        this.response = response;
362    }
363
364    @java.lang.SuppressWarnings("all")
365    @lombok.Generated
366    public CuiMockServletContext getServletContext() {
367        return this.servletContext;
368    }
369
370    @java.lang.SuppressWarnings("all")
371    @lombok.Generated
372    public void setServletContext(final CuiMockServletContext servletContext) {
373        this.servletContext = servletContext;
374    }
375
376    @java.lang.SuppressWarnings("all")
377    @lombok.Generated
378    public MockHttpSession getSession() {
379        return this.session;
380    }
381
382    @java.lang.SuppressWarnings("all")
383    @lombok.Generated
384    public void setSession(final MockHttpSession session) {
385        this.session = session;
386    }
387}