📄 abstractjsfspringcontexttests.java
字号:
/* * Copyright 2002-2004 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package de.mindmatters.faces.spring.test;import java.util.Enumeration;import java.util.Properties;import javax.faces.FactoryFinder;import javax.faces.component.UIViewRoot;import javax.faces.context.FacesContext;import javax.faces.context.FacesContextFactory;import javax.faces.lifecycle.Lifecycle;import javax.faces.lifecycle.LifecycleFactory;import javax.faces.render.RenderKitFactory;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.i18n.LocaleContextHolder;import org.springframework.mock.web.MockHttpServletRequest;import org.springframework.mock.web.MockHttpServletResponse;import org.springframework.mock.web.MockServletContext;import org.springframework.test.AbstractDependencyInjectionSpringContextTests;import org.springframework.util.ObjectUtils;import org.springframework.web.context.ConfigurableWebApplicationContext;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import de.mindmatters.faces.context.FacesContextImpl;import de.mindmatters.faces.context.ServletExternalContextFake;import de.mindmatters.faces.spring.context.ContextLoader;/** * Convenient superclass for <strong>integration</strong> tests depending on a * JSF-Spring context hierarchy. The test instance itself is populated by * Dependency Injection. * * <p> * This test mocks the servlet-api and builds up a fully working JSF * implementation based upon any production environment you choose (e.g. MyFaces * with the help of the {@link MyFacesStartupDriver} or SunRI with * {@link JsfRIStartupDriver}). * </p> * * <p> * Subclasses have to implement {@link #getResourceBasePath()} which should * point to an 'unzipped' WAR. All relevant context files will be automatically * loaded and you are able to test your production setup or build up 'fake' web * applications for testing your managed beans. * </p> * * @author Andreas Kuhrwahl * */public abstract class AbstractJsfSpringContextTests extends AbstractDependencyInjectionSpringContextTests { static { Thread shutdownHook = new Thread() { public void run() { FactoryFinder.releaseFactories(); } }; Runtime.getRuntime().addShutdownHook(shutdownHook); } /** The request mock. */ private HttpServletRequest request; /** The response mock. */ private HttpServletResponse response; /** The scopes. */ private ServletRequestAttributes requestAttributes; /** The used startup driver. */ private static JsfStartupDriver startupDriver = new JsfStartupDriver() { public void start(final ServletContext ctx) { } public void stop(final ServletContext ctx) { } }; /** * Default constructor - populating protected variables via dependency * injection is turned on. */ public AbstractJsfSpringContextTests() { super(); setPopulateProtectedVariables(true); } /** * {@inheritDoc} */ protected final void prepareTestInstance() throws Exception { FacesContext contextFake = new FacesContextImpl( new ServletExternalContextFake(getWebApplicationContext() .getServletContext())); try { super.prepareTestInstance(); } finally { contextFake.release(); } } /** * {@inheritDoc} */ protected final ConfigurableApplicationContext loadContextLocations( final String[] locations) throws Exception { ServletContext servletContext = createServletContext(); JsfEnvironment environment = loadJsfEnvironment(servletContext); synchronized (startupDriver) { JsfStartupDriver currentDriver = startupDriver, driverToUse = environment .loadJsfImplementation(servletContext); currentDriver.stop(servletContext); driverToUse.start(servletContext); startupDriver = driverToUse; } FacesContext contextFake = new FacesContextImpl( new ServletExternalContextFake(servletContext)); try { ConfigurableWebApplicationContext context = environment .loadContext(servletContext); return context; } finally { contextFake.release(); } } /** * Creates the ServletContext mock implementation - default is * {@link MockServletContext}. * * @return the mock */ protected ServletContext createServletContext() { MockServletContext context = new MockServletContext( getResourceBasePath()); Properties props = System.getProperties(); for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); context.addInitParameter(name, System.getProperty(name)); } return context; } /** * Loads the {@link JsfEnvironment} of the particular test case. * * @param servletContext * the current {@link ServletContext} * @return the current JsfEnvironment * @throws Exception * in case of errors */ protected JsfEnvironment loadJsfEnvironment( final ServletContext servletContext) throws Exception { return new JsfEnvironment() { public ConfigurableWebApplicationContext loadContext( final ServletContext servletContext) throws Exception { return AbstractJsfSpringContextTests.this .loadContext(servletContext); } public JsfStartupDriver loadJsfImplementation( final ServletContext servletContext) throws Exception { return AbstractJsfSpringContextTests.this .loadJsfImplementation(servletContext); } }; } /** * Loads the * {@link de.mindmatters.faces.spring.context.FacesWebApplicationContext} by * default - with the parent loaded by * {@link #loadParentContext(ServletContext)}. * * @param servletContext * the current {@link ServletContext} * @return the JSF {@link WebApplicationContext} * @throws Exception * in case of errors */ protected ConfigurableWebApplicationContext loadContext( final ServletContext servletContext) throws Exception { ConfigurableWebApplicationContext parent = loadParentContext(servletContext); parent.registerShutdownHook(); return (ConfigurableWebApplicationContext) new ContextLoader() .initWebApplicationContext(servletContext); } /** * Loads the current JSF implementation startup driver used for * configuration and initialization purposes. * * @param servletContext * the current {@link ServletContext} * @return the used {@link JsfStartupDriver} * @throws Exception * in case of errors */ protected JsfStartupDriver loadJsfImplementation( final ServletContext servletContext) throws Exception { return new MyFacesStartupDriver(); } /** * {@inheritDoc} */ protected final String contextKeyString(final Object contextKey) { return ObjectUtils.nullSafeToString(getResourceBasePath()); } /** * Loads the parent {@link WebApplicationContext} - the * {@link org.springframework.web.context.ContextLoader} is used for loading * and initialization by default. * * @param servletContext * the current {@link ServletContext} * @return the parent {@link WebApplicationContext} * @throws Exception * in case of errors */ protected ConfigurableWebApplicationContext loadParentContext( final ServletContext servletContext) throws Exception { return (ConfigurableWebApplicationContext) new org.springframework.web.context.ContextLoader() .initWebApplicationContext(servletContext); } /** * @return the {@link ServletContext} mock implementation */ protected ServletContext getServletContext() { return getWebApplicationContext().getServletContext(); } /** * {@inheritDoc} */ protected final String[] getConfigLocations() { return null; } /** * @return the path to an 'unzipped' WAR to test */ protected abstract String getResourceBasePath(); /** * {@inheritDoc} */ protected void onSetUp() throws Exception { this.request = new MockHttpServletRequest(getServletContext()); this.response = new MockHttpServletResponse(); LocaleContextHolder.setLocale(request.getLocale()); this.requestAttributes = new ServletRequestAttributes(this.request); RequestContextHolder.setRequestAttributes(this.requestAttributes); LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder .getFactory(FactoryFinder.LIFECYCLE_FACTORY); Lifecycle lifecycle = lifecycleFactory .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE); FacesContextFactory facesContextFactory = (FacesContextFactory) FactoryFinder .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); FacesContext facesContext = facesContextFactory.getFacesContext( getServletContext(), request, response, lifecycle); UIViewRoot root = new UIViewRoot(); root.setViewId("/viewId"); root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT); facesContext.setViewRoot(root); } /** * {@inheritDoc} */ protected void onTearDown() throws Exception { FacesContext.getCurrentInstance().release(); this.requestAttributes.requestCompleted(); RequestContextHolder.setRequestAttributes(null); LocaleContextHolder.setLocale(null); this.request = null; this.response = null; } /** * @return the root {@link WebApplicationContext} */ protected WebApplicationContext getWebApplicationContext() { try { return (WebApplicationContext) this.applicationContext; } catch (Exception ex) { throw new RuntimeException(ex); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -