| 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.impl.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 |
|
@author |
| 50 |
|
@since |
| 51 |
|
|
|
|
|
| 0% |
Uncovered Elements: 180 (180) |
Complexity: 47 |
Complexity Density: 0.38 |
|
| 52 |
|
public abstract class RiceTestCase extends BaseRiceTestCase { |
| 53 |
|
|
| 54 |
|
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 |
|
protected static boolean SUITE_LIFE_CYCLES_RAN = false; |
| 60 |
|
protected static boolean SUITE_LIFE_CYCLES_FAILED = false; |
| 61 |
|
protected static String failedSuiteTestName; |
| 62 |
|
|
| 63 |
|
protected List<Lifecycle> perTestLifeCycles = new LinkedList<Lifecycle>(); |
| 64 |
|
|
| 65 |
|
protected List<Lifecycle> suiteLifeCycles = new LinkedList<Lifecycle>(); |
| 66 |
|
|
| 67 |
|
private static Set<String> perSuiteDataLoaderLifecycleNamesRun = new HashSet<String>(); |
| 68 |
|
|
| 69 |
|
private List<String> reports = new ArrayList<String>(); |
| 70 |
|
|
| 71 |
|
private SpringResourceLoader testHarnessSpringResourceLoader; |
| 72 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 73 |
0
|
@Before... |
| 74 |
|
public void setUp() throws Exception { |
| 75 |
0
|
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 |
|
} catch (Throwable e) { |
| 85 |
0
|
e.printStackTrace(); |
| 86 |
0
|
tearDown(); |
| 87 |
0
|
throw new RuntimeException(e); |
| 88 |
|
} |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 4 |
Complexity Density: 0.2 |
|
| 97 |
0
|
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 |
0
|
try { |
| 110 |
0
|
startLifecycles(this.suiteLifeCycles); |
| 111 |
0
|
SUITE_LIFE_CYCLES_RAN = true; |
| 112 |
|
} 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 |
|
} |
| 121 |
|
} |
| 122 |
|
|
| 123 |
0
|
startSuiteDataLoaderLifecycles(); |
| 124 |
|
|
| 125 |
0
|
startLifecycles(this.perTestLifeCycles); |
| 126 |
|
|
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
@throws |
| 135 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 136 |
0
|
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 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 148 |
0
|
protected void setBaseDirSystemProperty(String moduleBaseDir) {... |
| 149 |
0
|
if (System.getProperty("basedir") == null) { |
| 150 |
0
|
System.setProperty("basedir", System.getProperty("user.dir") + "/" + moduleBaseDir); |
| 151 |
|
} |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 157 |
0
|
protected String getBaseDir() {... |
| 158 |
0
|
return System.getProperty("basedir"); |
| 159 |
|
} |
| 160 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 161 |
0
|
protected void setModuleName(String moduleName) {... |
| 162 |
0
|
if (System.getProperty("module.name") == null) { |
| 163 |
0
|
System.setProperty("module.name", moduleName); |
| 164 |
|
} |
| 165 |
|
} |
| 166 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 167 |
0
|
@After... |
| 168 |
|
public void tearDown() throws Exception { |
| 169 |
|
|
| 170 |
0
|
ThreadMonitor.tearDown(60000); |
| 171 |
0
|
stopLifecycles(this.perTestLifeCycles); |
| 172 |
0
|
logAfterRun(); |
| 173 |
|
} |
| 174 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 175 |
0
|
protected void logBeforeRun() {... |
| 176 |
0
|
LOG.info("##############################################################"); |
| 177 |
0
|
LOG.info("# Starting test " + getFullTestName() + "..."); |
| 178 |
0
|
LOG.info("# " + dumpMemory()); |
| 179 |
0
|
LOG.info("##############################################################"); |
| 180 |
|
} |
| 181 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 182 |
0
|
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 |
|
} |
| 191 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 192 |
0
|
protected String getFullTestName() {... |
| 193 |
0
|
return getClass().getSimpleName() + "." + getName(); |
| 194 |
|
} |
| 195 |
|
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
| 196 |
0
|
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 |
|
} |
| 214 |
|
|
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 218 |
0
|
protected void startLifecycles(List<Lifecycle> lifecycles) throws Exception {... |
| 219 |
0
|
for (Lifecycle lifecycle : lifecycles) { |
| 220 |
0
|
lifecycle.start(); |
| 221 |
|
} |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 6 |
Complexity Density: 0.55 |
|
| 228 |
0
|
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 |
0
|
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 |
|
} catch (Exception e) { |
| 244 |
0
|
LOG.warn("Failed to shutdown one of the lifecycles!", e); |
| 245 |
|
} |
| 246 |
|
} |
| 247 |
|
} |
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
| 252 |
0
|
protected List<Lifecycle> getSuiteLifecycles() {... |
| 253 |
0
|
List<Lifecycle> lifecycles = new LinkedList<Lifecycle>(); |
| 254 |
|
|
| 255 |
|
|
| 256 |
|
|
| 257 |
|
|
| 258 |
0
|
lifecycles.add(new BaseLifecycle() { |
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 259 |
0
|
public void start() throws Exception {... |
| 260 |
0
|
Config config = getTestHarnessConfig(); |
| 261 |
0
|
ConfigContext.init(config); |
| 262 |
0
|
super.start(); |
| 263 |
|
} |
| 264 |
|
}); |
| 265 |
|
|
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
0
|
lifecycles.add(getTestHarnessSpringResourceLoader()); |
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
0
|
lifecycles.add(new BaseLifecycle() { |
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 276 |
0
|
public void start() throws Exception {... |
| 277 |
0
|
TestHarnessServiceLocator.setContext(getTestHarnessSpringResourceLoader().getContext()); |
| 278 |
0
|
super.start(); |
| 279 |
|
} |
| 280 |
|
}); |
| 281 |
|
|
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
0
|
lifecycles.add(new ClearDatabaseLifecycle()); |
| 286 |
|
|
| 287 |
|
|
| 288 |
|
|
| 289 |
|
|
| 290 |
0
|
lifecycles.add(new BaseLifecycle() { |
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 291 |
0
|
public void start() throws Exception {... |
| 292 |
0
|
loadSuiteTestData(); |
| 293 |
0
|
super.start(); |
| 294 |
|
} |
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 310 |
0
|
protected Lifecycle getLoadApplicationLifecycle() {... |
| 311 |
|
|
| 312 |
0
|
return null; |
| 313 |
|
} |
| 314 |
|
|
| 315 |
|
|
| 316 |
|
@return |
| 317 |
|
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 318 |
0
|
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() { |
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 327 |
0
|
public void start() throws Exception {... |
| 328 |
0
|
loadPerTestData(); |
| 329 |
0
|
super.start(); |
| 330 |
|
} |
| 331 |
|
}); |
| 332 |
0
|
return lifecycles; |
| 333 |
|
} |
| 334 |
|
|
| 335 |
|
|
| 336 |
|
|
| 337 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 338 |
0
|
protected void loadSuiteTestData() throws Exception {... |
| 339 |
|
|
| 340 |
|
} |
| 341 |
|
|
| 342 |
|
|
| 343 |
|
|
| 344 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 345 |
0
|
protected void loadPerTestData() throws Exception {... |
| 346 |
|
|
| 347 |
|
} |
| 348 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 349 |
0
|
protected void report(final String report) {... |
| 350 |
0
|
this.reports.add(report); |
| 351 |
|
} |
| 352 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 353 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 360 |
0
|
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 |
|
@return |
| 371 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 372 |
0
|
protected String getTestHarnessSpringBeansLocation() {... |
| 373 |
0
|
return DEFAULT_TEST_HARNESS_SPRING_BEANS; |
| 374 |
|
} |
| 375 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 376 |
0
|
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 |
|
@return |
| 386 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 387 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 394 |
0
|
protected String getModuleTestConfigLocation() {... |
| 395 |
0
|
return "classpath:META-INF/" + getModuleName().toLowerCase() + "-test-config.xml"; |
| 396 |
|
} |
| 397 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 398 |
0
|
protected String getRiceMasterDefaultConfigFile() {... |
| 399 |
0
|
return "classpath:META-INF/test-config-defaults.xml"; |
| 400 |
|
} |
| 401 |
|
|
| 402 |
|
|
| 403 |
|
|
| 404 |
|
|
| 405 |
|
@return |
| 406 |
|
|
| 407 |
|
protected abstract String getModuleName(); |
| 408 |
|
|
| 409 |
|
} |