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