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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
83 private ClassLoader threadContextClassLoader = null;
84
85
86
87
88 @Override
89 protected void onSetUp() throws Exception {
90
91 threadContextClassLoader = Thread.currentThread().getContextClassLoader();
92 Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0],
93 this.getClass().getClassLoader()));
94
95
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
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
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
149
150 JavaMailSenderImpl mailSender = (JavaMailSenderImpl) applicationContext.getBean("mailSender");
151 mailSender.setPort(2525);
152 mailSender.setHost("localhost");
153 }
154
155
156
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",
185 "/WEB-INF/applicationContext*.xml"};
186 }
187 }