View Javadoc

1   package org.appfuse.webapp.action;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.apache.shale.test.mock.MockApplication;
6   import org.apache.shale.test.mock.MockExternalContext;
7   import org.apache.shale.test.mock.MockFacesContext;
8   import org.apache.shale.test.mock.MockFacesContextFactory;
9   import org.apache.shale.test.mock.MockHttpServletRequest;
10  import org.apache.shale.test.mock.MockHttpServletResponse;
11  import org.apache.shale.test.mock.MockHttpSession;
12  import org.apache.shale.test.mock.MockLifecycle;
13  import org.apache.shale.test.mock.MockLifecycleFactory;
14  import org.apache.shale.test.mock.MockRenderKit;
15  import org.apache.shale.test.mock.MockServletConfig;
16  import org.apache.shale.test.mock.MockServletContext;
17  import org.appfuse.Constants;
18  import org.springframework.mail.javamail.JavaMailSenderImpl;
19  import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
20  
21  import javax.faces.FactoryFinder;
22  import javax.faces.application.ApplicationFactory;
23  import javax.faces.component.UIViewRoot;
24  import javax.faces.lifecycle.LifecycleFactory;
25  import javax.faces.render.RenderKitFactory;
26  import java.net.URL;
27  import java.net.URLClassLoader;
28  import java.util.Locale;
29  import java.util.List;
30  import java.util.ArrayList;
31  
32  /**
33   * <p>Abstract JUnit test case base class, which sets up the JavaServer Faces
34   * mock object environment for a particular simulated request.  The following
35   * protected variables are initialized in the <code>setUp()</code> method, and
36   * cleaned up in the <code>tearDown()</code> method:</p>
37   * <ul>
38   * <li><code>application</code> (<code>MockApplication</code>)</li>
39   * <li><code>config</code> (<code>MockServletConfig</code>)</li>
40   * <li><code>externalContext</code> (<code>MockExternalContext</code>)</li>
41   * <li><code>facesContext</code> (<code>MockFacesContext</code>)</li>
42   * <li><code>lifecycle</code> (<code>MockLifecycle</code>)</li>
43   * <li><code>request</code> (<code>MockHttpServletRequest</code></li>
44   * <li><code>response</code> (<code>MockHttpServletResponse</code>)</li>
45   * <li><code>servletContext</code> (<code>MockServletContext</code>)</li>
46   * <li><code>session</code> (<code>MockHttpSession</code>)</li>
47   * </ul>
48   * <p/>
49   * <p>In addition, appropriate factory classes will have been registered with
50   * <code>javax.faces.FactoryFinder</code> for <code>Application</code> and
51   * <code>RenderKit</code> instances.  The created <code>FacesContext</code>
52   * instance will also have been registered in the apppriate thread local
53   * variable, to simulate what a servlet container would do.</p>
54   * <p/>
55   * <p><strong>WARNING</strong> - If you choose to subclass this class, be sure
56   * your <code>onSetUp()</code> and <code>onTearDown()</code> methods call
57   * <code>super.setUp()</code> and <code>super.tearDown()</code> respectively,
58   * and that you implement your own <code>suite()</code> method that exposes
59   * the test methods for your test case.</p>
60   * <p/>
61   * <p><strong>NOTE:</strong> This class is a copy of Shale's AbstractJsfTestCase,
62   * except it extends Spring's AbstractTransactionalDataSourceSpringContextTests
63   * instead of JUnit's TestCase.</p>
64   */
65  public abstract class BasePageTestCase extends AbstractTransactionalDataSourceSpringContextTests {
66      protected final Log log = LogFactory.getLog(getClass());
67      protected static final String MESSAGES = Constants.BUNDLE_KEY;
68      
69      protected MockApplication application = null;
70      protected MockServletConfig config = null;
71      protected MockExternalContext externalContext = null;
72      protected MockFacesContext facesContext = null;
73      protected MockFacesContextFactory facesContextFactory = null;
74      protected MockLifecycle lifecycle = null;
75      protected MockLifecycleFactory lifecycleFactory = null;
76      protected MockRenderKit renderKit = null;
77      protected MockHttpServletRequest request = null;
78      protected MockHttpServletResponse response = null;
79      protected MockServletContext servletContext = null;
80      protected MockHttpSession session = null;
81  
82      // Thread context class loader saved and restored after each test
83      private ClassLoader threadContextClassLoader = null;
84  
85      /**
86       * <p>Set up instance variables required by this test case.</p>
87       */
88      @Override
89      protected void onSetUp() throws Exception {
90          // Set up a new thread context class loader
91          threadContextClassLoader = Thread.currentThread().getContextClassLoader();
92          Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0],
93                  this.getClass().getClassLoader()));
94  
95          // Set up Servlet API Objects
96          servletContext = new MockServletContext();
97          
98          config = new MockServletConfig(servletContext);
99          session = new MockHttpSession();
100         session.setServletContext(servletContext);
101         request = new MockHttpServletRequest(session);
102         request.setServletContext(servletContext);
103         request.setLocale(new Locale("en"));
104         response = new MockHttpServletResponse();
105 
106         // Set up JSF API Objects
107         FactoryFinder.releaseFactories();
108         FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
109                 "org.apache.shale.test.mock.MockApplicationFactory");
110         FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY,
111                 "org.apache.shale.test.mock.MockFacesContextFactory");
112         FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY,
113                 "org.apache.shale.test.mock.MockLifecycleFactory");
114         FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY,
115                 "org.apache.shale.test.mock.MockRenderKitFactory");
116 
117         externalContext = new MockExternalContext(servletContext, request, response);
118         lifecycleFactory = (MockLifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
119         lifecycle = (MockLifecycle) lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
120         facesContextFactory = (MockFacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
121         facesContext = (MockFacesContext)
122                 facesContextFactory.getFacesContext(servletContext, request, response, lifecycle);
123         
124         externalContext = (MockExternalContext) facesContext.getExternalContext();
125         UIViewRoot root = new UIViewRoot();
126         root.setViewId("/viewId");
127         root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
128         facesContext.setViewRoot(root);
129         ApplicationFactory applicationFactory = (ApplicationFactory)
130                 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
131         application = (MockApplication) applicationFactory.getApplication();
132         facesContext.setApplication(application);
133         RenderKitFactory renderKitFactory = (RenderKitFactory)
134                 FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
135         renderKit = new MockRenderKit();
136         renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT, renderKit);
137 
138         application.setMessageBundle(Constants.BUNDLE_KEY);
139 
140         // set supported locales
141         List<Locale> list = new ArrayList<Locale>();
142         list.add(new Locale("en"));
143         list.add(new Locale("fr"));
144         list.add(new Locale("de"));
145         list.add(new Locale("es"));
146         application.setSupportedLocales(list);
147 
148         // change the port on the mailSender so it doesn't conflict with an
149         // existing SMTP server on localhost
150         JavaMailSenderImpl mailSender = (JavaMailSenderImpl) applicationContext.getBean("mailSender");
151         mailSender.setPort(2525);
152         mailSender.setHost("localhost");
153     }
154 
155     /**
156      * <p>Tear down instance variables required by this test case.</p>
157      */
158     @Override
159     protected void onTearDown() throws Exception {
160         application = null;
161         config = null;
162         externalContext = null;
163         facesContext.release();
164         facesContext = null;
165         lifecycle = null;
166         lifecycleFactory = null;
167         renderKit = null;
168         request = null;
169         response = null;
170         servletContext = null;
171         session = null;
172         FactoryFinder.releaseFactories();
173 
174         Thread.currentThread().setContextClassLoader(threadContextClassLoader);
175         threadContextClassLoader = null;
176     }
177 
178     protected String[] getConfigLocations() {
179         setAutowireMode(AUTOWIRE_BY_NAME);
180         return new String[] {
181             "classpath:/applicationContext-resources.xml",
182             "classpath:/applicationContext-dao.xml",
183             "classpath:/applicationContext-service.xml",
184             "classpath*:/applicationContext.xml", // for modular archetypes
185             "/WEB-INF/applicationContext*.xml"};
186     }
187 }