1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
package org.kuali.rice.test; |
14 | |
|
15 | |
import java.io.IOException; |
16 | |
import java.util.ArrayList; |
17 | |
import java.util.HashSet; |
18 | |
import java.util.LinkedList; |
19 | |
import java.util.List; |
20 | |
import java.util.ListIterator; |
21 | |
import java.util.Properties; |
22 | |
import java.util.Set; |
23 | |
|
24 | |
import javax.xml.namespace.QName; |
25 | |
|
26 | |
import org.apache.commons.lang.StringUtils; |
27 | |
import org.apache.log4j.Logger; |
28 | |
import org.apache.log4j.PropertyConfigurator; |
29 | |
import org.junit.After; |
30 | |
import org.junit.Before; |
31 | |
import org.kuali.rice.core.api.config.property.Config; |
32 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
33 | |
import org.kuali.rice.core.api.lifecycle.BaseLifecycle; |
34 | |
import org.kuali.rice.core.api.lifecycle.Lifecycle; |
35 | |
import org.kuali.rice.core.impl.config.property.JAXBConfigImpl; |
36 | |
import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader; |
37 | |
import org.kuali.rice.test.data.PerSuiteUnitTestData; |
38 | |
import org.kuali.rice.test.lifecycles.PerSuiteDataLoaderLifecycle; |
39 | |
import org.springframework.core.io.FileSystemResourceLoader; |
40 | |
import org.springframework.core.io.Resource; |
41 | |
import org.springframework.core.io.ResourceLoader; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public abstract class RiceTestCase extends BaseRiceTestCase { |
53 | |
|
54 | 0 | private static final Logger LOG = Logger.getLogger(RiceTestCase.class); |
55 | |
|
56 | |
private static final String ALT_LOG4J_CONFIG_LOCATION_PROP = "alt.log4j.config.location"; |
57 | |
private static final String DEFAULT_LOG4J_CONFIG = "classpath:rice-testharness-default-log4j.properties"; |
58 | |
protected static final String DEFAULT_TEST_HARNESS_SPRING_BEANS = "classpath:TestHarnessSpringBeans.xml"; |
59 | 0 | protected static boolean SUITE_LIFE_CYCLES_RAN = false; |
60 | 0 | protected static boolean SUITE_LIFE_CYCLES_FAILED = false; |
61 | |
protected static String failedSuiteTestName; |
62 | |
|
63 | 0 | protected List<Lifecycle> perTestLifeCycles = new LinkedList<Lifecycle>(); |
64 | |
|
65 | 0 | protected List<Lifecycle> suiteLifeCycles = new LinkedList<Lifecycle>(); |
66 | |
|
67 | 0 | private static Set<String> perSuiteDataLoaderLifecycleNamesRun = new HashSet<String>(); |
68 | |
|
69 | 0 | private List<String> reports = new ArrayList<String>(); |
70 | |
|
71 | |
private SpringResourceLoader testHarnessSpringResourceLoader; |
72 | |
|
73 | |
@Before |
74 | |
public void setUp() throws Exception { |
75 | |
try { |
76 | 0 | configureLogging(); |
77 | 0 | logBeforeRun(); |
78 | |
|
79 | 0 | final long initTime = System.currentTimeMillis(); |
80 | |
|
81 | 0 | setUpInternal(); |
82 | |
|
83 | 0 | report("Time to start all Lifecycles: " + (System.currentTimeMillis() - initTime)); |
84 | 0 | } catch (Throwable e) { |
85 | 0 | e.printStackTrace(); |
86 | 0 | tearDown(); |
87 | 0 | throw new RuntimeException(e); |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
protected void setUpInternal() throws Exception { |
98 | 0 | assertNotNull(getModuleName()); |
99 | 0 | setModuleName(getModuleName()); |
100 | 0 | setBaseDirSystemProperty(getModuleName()); |
101 | |
|
102 | 0 | this.perTestLifeCycles = getPerTestLifecycles(); |
103 | 0 | this.suiteLifeCycles = getSuiteLifecycles(); |
104 | |
|
105 | 0 | if (SUITE_LIFE_CYCLES_FAILED) { |
106 | 0 | fail("Suite Lifecycles startup failed on test " + failedSuiteTestName + "!!! Please see logs for details."); |
107 | |
} |
108 | 0 | if (!SUITE_LIFE_CYCLES_RAN) { |
109 | |
try { |
110 | 0 | startLifecycles(this.suiteLifeCycles); |
111 | 0 | SUITE_LIFE_CYCLES_RAN = true; |
112 | 0 | } catch (Throwable e) { |
113 | 0 | e.printStackTrace(); |
114 | 0 | SUITE_LIFE_CYCLES_RAN = false; |
115 | 0 | SUITE_LIFE_CYCLES_FAILED = true; |
116 | 0 | failedSuiteTestName = getFullTestName(); |
117 | 0 | tearDown(); |
118 | 0 | stopLifecycles(this.suiteLifeCycles); |
119 | 0 | throw new RuntimeException(e); |
120 | 0 | } |
121 | |
} |
122 | |
|
123 | 0 | startSuiteDataLoaderLifecycles(); |
124 | |
|
125 | 0 | startLifecycles(this.perTestLifeCycles); |
126 | |
|
127 | 0 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
protected void startSuiteDataLoaderLifecycles() throws Exception { |
137 | 0 | List<Class> classes = TestUtilities.getHierarchyClassesToHandle(getClass(), new Class[] { PerSuiteUnitTestData.class }, perSuiteDataLoaderLifecycleNamesRun); |
138 | 0 | for (Class c: classes) { |
139 | 0 | new PerSuiteDataLoaderLifecycle(c).start(); |
140 | 0 | perSuiteDataLoaderLifecycleNamesRun.add(c.getName()); |
141 | |
} |
142 | 0 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
protected void setBaseDirSystemProperty(String moduleBaseDir) { |
149 | 0 | if (System.getProperty("basedir") == null) { |
150 | 0 | System.setProperty("basedir", System.getProperty("user.dir") + "/" + moduleBaseDir); |
151 | |
} |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
protected String getBaseDir() { |
158 | 0 | return System.getProperty("basedir"); |
159 | |
} |
160 | |
|
161 | |
protected void setModuleName(String moduleName) { |
162 | 0 | if (System.getProperty("module.name") == null) { |
163 | 0 | System.setProperty("module.name", moduleName); |
164 | |
} |
165 | 0 | } |
166 | |
|
167 | |
@After |
168 | |
public void tearDown() throws Exception { |
169 | |
|
170 | 0 | ThreadMonitor.tearDown(60000); |
171 | 0 | stopLifecycles(this.perTestLifeCycles); |
172 | 0 | logAfterRun(); |
173 | 0 | } |
174 | |
|
175 | |
protected void logBeforeRun() { |
176 | 0 | LOG.info("##############################################################"); |
177 | 0 | LOG.info("# Starting test " + getFullTestName() + "..."); |
178 | 0 | LOG.info("# " + dumpMemory()); |
179 | 0 | LOG.info("##############################################################"); |
180 | 0 | } |
181 | |
|
182 | |
protected void logAfterRun() { |
183 | 0 | LOG.info("##############################################################"); |
184 | 0 | LOG.info("# ...finished test " + getFullTestName()); |
185 | 0 | LOG.info("# " + dumpMemory()); |
186 | 0 | for (final String report : this.reports) { |
187 | 0 | LOG.info("# " + report); |
188 | |
} |
189 | 0 | LOG.info("##############################################################\n\n\n"); |
190 | 0 | } |
191 | |
|
192 | |
protected String getFullTestName() { |
193 | 0 | return getClass().getSimpleName() + "." + getName(); |
194 | |
} |
195 | |
|
196 | |
protected void configureLogging() throws IOException { |
197 | 0 | ResourceLoader resourceLoader = new FileSystemResourceLoader(); |
198 | 0 | String altLog4jConfigLocation = System.getProperty(ALT_LOG4J_CONFIG_LOCATION_PROP); |
199 | 0 | Resource log4jConfigResource = null; |
200 | 0 | if (!StringUtils.isEmpty(altLog4jConfigLocation)) { |
201 | 0 | log4jConfigResource = resourceLoader.getResource(altLog4jConfigLocation); |
202 | |
} |
203 | 0 | if (log4jConfigResource == null || !log4jConfigResource.exists()) { |
204 | 0 | System.out.println("Alternate Log4j config resource does not exist! " + altLog4jConfigLocation); |
205 | 0 | System.out.println("Using default log4j configuration: " + DEFAULT_LOG4J_CONFIG); |
206 | 0 | log4jConfigResource = resourceLoader.getResource(DEFAULT_LOG4J_CONFIG); |
207 | |
} else { |
208 | 0 | System.out.println("Using alternate log4j configuration at: " + altLog4jConfigLocation); |
209 | |
} |
210 | 0 | Properties p = new Properties(); |
211 | 0 | p.load(log4jConfigResource.getInputStream()); |
212 | 0 | PropertyConfigurator.configure(p); |
213 | 0 | } |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
protected void startLifecycles(List<Lifecycle> lifecycles) throws Exception { |
219 | 0 | for (Lifecycle lifecycle : lifecycles) { |
220 | 0 | lifecycle.start(); |
221 | |
} |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
protected void stopLifecycles(List<Lifecycle> lifecycles) throws Exception { |
229 | 0 | final ListIterator<Lifecycle> iter = lifecycles.listIterator(); |
230 | 0 | while (iter.hasNext()) { |
231 | 0 | iter.next(); |
232 | |
} |
233 | 0 | while (iter.hasPrevious()) { |
234 | 0 | final Lifecycle lifeCycle = iter.previous(); |
235 | |
try { |
236 | 0 | if (lifeCycle == null) { |
237 | 0 | LOG.warn("Attempted to stop a null lifecycle"); |
238 | |
} else { |
239 | 0 | if (lifeCycle.isStarted()) { |
240 | 0 | lifeCycle.stop(); |
241 | |
} |
242 | |
} |
243 | 0 | } catch (Exception e) { |
244 | 0 | LOG.warn("Failed to shutdown one of the lifecycles!", e); |
245 | 0 | } |
246 | 0 | } |
247 | 0 | } |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
protected List<Lifecycle> getSuiteLifecycles() { |
253 | 0 | List<Lifecycle> lifecycles = new LinkedList<Lifecycle>(); |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | 0 | lifecycles.add(new BaseLifecycle() { |
259 | |
public void start() throws Exception { |
260 | 0 | Config config = getTestHarnessConfig(); |
261 | 0 | ConfigContext.init(config); |
262 | 0 | super.start(); |
263 | 0 | } |
264 | |
}); |
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | 0 | lifecycles.add(getTestHarnessSpringResourceLoader()); |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | 0 | lifecycles.add(new BaseLifecycle() { |
276 | |
public void start() throws Exception { |
277 | 0 | TestHarnessServiceLocator.setContext(getTestHarnessSpringResourceLoader().getContext()); |
278 | 0 | super.start(); |
279 | 0 | } |
280 | |
}); |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | 0 | lifecycles.add(new ClearDatabaseLifecycle()); |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | 0 | lifecycles.add(new BaseLifecycle() { |
291 | |
public void start() throws Exception { |
292 | 0 | loadSuiteTestData(); |
293 | 0 | super.start(); |
294 | 0 | } |
295 | |
}); |
296 | |
|
297 | 0 | Lifecycle loadApplicationLifecycle = getLoadApplicationLifecycle(); |
298 | 0 | if (loadApplicationLifecycle != null) { |
299 | 0 | lifecycles.add(loadApplicationLifecycle); |
300 | |
} |
301 | 0 | return lifecycles; |
302 | |
} |
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
protected Lifecycle getLoadApplicationLifecycle() { |
311 | |
|
312 | 0 | return null; |
313 | |
} |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
protected List<Lifecycle> getPerTestLifecycles() { |
319 | 0 | List<Lifecycle> lifecycles = new LinkedList<Lifecycle>(); |
320 | 0 | if (getClass().isAnnotationPresent(TransactionalTest.class)) { |
321 | 0 | String transactionManagerName = getClass().getAnnotation(TransactionalTest.class).transactionManager(); |
322 | 0 | TransactionalLifecycle transactionalLifecycle = new TransactionalLifecycle(transactionManagerName); |
323 | 0 | lifecycles.add(transactionalLifecycle); |
324 | |
} |
325 | 0 | lifecycles.add(getPerTestDataLoaderLifecycle()); |
326 | 0 | lifecycles.add(new BaseLifecycle() { |
327 | |
public void start() throws Exception { |
328 | 0 | loadPerTestData(); |
329 | 0 | super.start(); |
330 | 0 | } |
331 | |
}); |
332 | 0 | return lifecycles; |
333 | |
} |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
protected void loadSuiteTestData() throws Exception { |
339 | |
|
340 | 0 | } |
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
protected void loadPerTestData() throws Exception { |
346 | |
|
347 | 0 | } |
348 | |
|
349 | |
protected void report(final String report) { |
350 | 0 | this.reports.add(report); |
351 | 0 | } |
352 | |
|
353 | |
protected String dumpMemory() { |
354 | 0 | final long total = Runtime.getRuntime().totalMemory(); |
355 | 0 | final long free = Runtime.getRuntime().freeMemory(); |
356 | 0 | final long max = Runtime.getRuntime().maxMemory(); |
357 | 0 | return "[Memory] max: " + max + ", total: " + total + ", free: " + free; |
358 | |
} |
359 | |
|
360 | |
public SpringResourceLoader getTestHarnessSpringResourceLoader() { |
361 | 0 | if (testHarnessSpringResourceLoader == null) { |
362 | 0 | testHarnessSpringResourceLoader = new SpringResourceLoader(new QName("TestHarnessSpringContext"), getTestHarnessSpringBeansLocation(), null); |
363 | |
} |
364 | 0 | return testHarnessSpringResourceLoader; |
365 | |
} |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
protected String getTestHarnessSpringBeansLocation() { |
373 | 0 | return DEFAULT_TEST_HARNESS_SPRING_BEANS; |
374 | |
} |
375 | |
|
376 | |
protected Config getTestHarnessConfig() throws Exception { |
377 | 0 | Config config = new JAXBConfigImpl(getConfigLocations(), System.getProperties()); |
378 | 0 | config.parseConfig(); |
379 | 0 | return config; |
380 | |
} |
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
protected List<String> getConfigLocations() { |
388 | 0 | List<String> configLocations = new ArrayList<String>(); |
389 | 0 | configLocations.add(getRiceMasterDefaultConfigFile()); |
390 | 0 | configLocations.add(getModuleTestConfigLocation()); |
391 | 0 | return configLocations; |
392 | |
} |
393 | |
|
394 | |
protected String getModuleTestConfigLocation() { |
395 | 0 | return "classpath:META-INF/" + getModuleName().toLowerCase() + "-test-config.xml"; |
396 | |
} |
397 | |
|
398 | |
protected String getRiceMasterDefaultConfigFile() { |
399 | 0 | return "classpath:META-INF/test-config-defaults.xml"; |
400 | |
} |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
protected abstract String getModuleName(); |
408 | |
|
409 | |
} |