use of org.junit.runners.model.Statement in project randomizedtesting by randomizedtesting.
the class RandomizedRunner method runSuite.
/**
* Test execution logic for the entire suite, executing under designated
* {@link RunnerThreadGroup}.
*/
private void runSuite(final RandomizedContext context, final RunNotifier notifier) {
final Result result = new Result();
final RunListener accounting = result.createListener();
notifier.addListener(accounting);
final Randomness classRandomness = runnerRandomness.clone(Thread.currentThread());
context.push(classRandomness);
try {
// Check for automatically hookable listeners.
subscribeListeners(notifier);
// Fire a synthetic "suite started" event.
for (RunListener r : autoListeners) {
try {
r.testRunStarted(suiteDescription);
} catch (Throwable e) {
logger.log(Level.SEVERE, "Panic: RunListener hook shouldn't throw exceptions.", e);
}
}
// Filter out test candidates to see if there's anything left.
// If not, don't bother running class hooks.
final List<TestCandidate> tests = getFilteredTestCandidates(notifier);
if (!tests.isEmpty()) {
Map<TestCandidate, Boolean> ignored = determineIgnoredTests(tests);
if (ignored.size() == tests.size()) {
// All tests ignored, ignore class hooks but report all the ignored tests.
for (TestCandidate c : tests) {
if (ignored.get(c)) {
reportAsIgnored(notifier, groupEvaluator, c);
}
}
} else {
ThreadLeakControl threadLeakControl = new ThreadLeakControl(notifier, this);
Statement s = runTestsStatement(threadLeakControl.notifier(), tests, ignored, threadLeakControl);
s = withClassBefores(s);
s = withClassAfters(s);
s = withClassRules(s);
s = withCloseContextResources(s, LifecycleScope.SUITE);
s = threadLeakControl.forSuite(s, suiteDescription);
try {
s.evaluate();
} catch (Throwable t) {
t = augmentStackTrace(t, runnerRandomness);
if (t instanceof AssumptionViolatedException) {
// Fire assumption failure before method ignores. (GH-103).
notifier.fireTestAssumptionFailed(new Failure(suiteDescription, t));
// see Rants#RANT_3
for (final TestCandidate c : tests) {
notifier.fireTestIgnored(c.description);
}
} else {
fireTestFailure(notifier, suiteDescription, t);
}
}
}
}
} catch (Throwable t) {
notifier.fireTestFailure(new Failure(suiteDescription, t));
}
// Fire a synthetic "suite ended" event and unsubscribe listeners.
for (RunListener r : autoListeners) {
try {
r.testRunFinished(result);
} catch (Throwable e) {
logger.log(Level.SEVERE, "Panic: RunListener hook shouldn't throw exceptions.", e);
}
}
// Final cleanup.
notifier.removeListener(accounting);
unsubscribeListeners(notifier);
context.popAndDestroy();
}
use of org.junit.runners.model.Statement in project randomizedtesting by randomizedtesting.
the class RandomizedRunner method runSingleTest.
/**
* Runs a single test in the "master" test thread.
*/
void runSingleTest(final RunNotifier notifier, final TestCandidate c, final ThreadLeakControl threadLeakControl) {
notifier.fireTestStarted(c.description);
try {
// Get the test instance.
final Object instance = c.instanceProvider.newInstance();
// Collect rules and execute wrapped method.
Statement s = new Statement() {
public void evaluate() throws Throwable {
invoke(c.method, instance);
}
};
s = wrapExpectedExceptions(s, c);
s = wrapBeforeAndAfters(s, c, instance);
s = wrapMethodRules(s, c, instance);
s = withCloseContextResources(s, LifecycleScope.TEST);
s = threadLeakControl.forTest(s, c);
s.evaluate();
} catch (Throwable e) {
e = augmentStackTrace(e);
if (e instanceof AssumptionViolatedException) {
notifier.fireTestAssumptionFailed(new Failure(c.description, e));
} else {
fireTestFailure(notifier, c.description, e);
}
} finally {
notifier.fireTestFinished(c.description);
}
}
use of org.junit.runners.model.Statement in project randomizedtesting by randomizedtesting.
the class ThreadLeakControl method forSuite.
/**
* A {@link Statement} for wrapping suite-level execution.
*/
Statement forSuite(final Statement s, final Description suiteDescription) {
final Class<?> suiteClass = RandomizedContext.current().getTargetClass();
final int timeout = determineTimeout(suiteClass);
return new Statement() {
@Override
public void evaluate() throws Throwable {
checkZombies();
threadLeakGroup = firstAnnotated(ThreadLeakGroup.class, suiteClass, DefaultAnnotationValues.class);
final List<Throwable> errors = new ArrayList<Throwable>();
suiteFilters = instantiateFilters(errors, suiteClass);
MultipleFailureException.assertEmpty(errors);
final StatementRunner sr = new StatementRunner(s);
final boolean timedOut = forkTimeoutingTask(sr, timeout, errors);
synchronized (notifierLock) {
if (timedOut) {
// Mark as timed out so that we don't do any checks in any currently running test
suiteTimedOut.set(true);
// Flush streams so that we don't get warning outputs before sysout buffers.
flushStreams();
// Emit a warning.
LOGGER.warning("Suite execution timed out: " + suiteDescription + formatThreadStacksFull());
// mark subNotifier as dead (no longer passing events).
subNotifier.pleaseStop();
}
}
if (timedOut) {
// complete subNotifier state in case in the middle of a test.
if (subNotifier.testInProgress != null) {
targetNotifier.fireTestFailure(new Failure(subNotifier.testInProgress, RandomizedRunner.augmentStackTrace(emptyStack(new Exception("Test abandoned because suite timeout was reached.")))));
targetNotifier.fireTestFinished(subNotifier.testInProgress);
}
// throw suite failure (timeout).
errors.add(RandomizedRunner.augmentStackTrace(emptyStack(new Exception("Suite timeout exceeded (>= " + timeout + " msec)."))));
}
final AnnotatedElement[] chain = { suiteClass, DefaultAnnotationValues.class };
List<Throwable> threadLeakErrors = timedOut ? new ArrayList<Throwable>() : errors;
checkThreadLeaks(refilter(expectedSuiteState, suiteFilters), threadLeakErrors, LifecycleScope.SUITE, suiteDescription, chain);
processUncaught(errors, runner.handler.getUncaughtAndClear());
MultipleFailureException.assertEmpty(errors);
}
@SuppressForbidden("Legitimate use of syserr.")
private void flushStreams() {
System.out.flush();
System.err.flush();
}
};
}
use of org.junit.runners.model.Statement in project robolectric by robolectric.
the class SandboxTestRunner method methodBlock.
protected Statement methodBlock(final FrameworkMethod method) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Sandbox sandbox = getSandbox(method);
// Configure shadows *BEFORE* setting the ClassLoader. This is necessary because
// creating the ShadowMap loads all ShadowProviders via ServiceLoader and this is
// not available once we install the Robolectric class loader.
configureShadows(method, sandbox);
final ClassLoader priorContextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(sandbox.getRobolectricClassLoader());
//noinspection unchecked
Class bootstrappedTestClass = sandbox.bootstrappedClass(getTestClass().getJavaClass());
HelperTestRunner helperTestRunner = getHelperTestRunner(bootstrappedTestClass);
helperTestRunner.frameworkMethod = method;
final Method bootstrappedMethod;
try {
//noinspection unchecked
bootstrappedMethod = bootstrappedTestClass.getMethod(method.getMethod().getName());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
try {
// Only invoke @BeforeClass once per class
invokeBeforeClass(bootstrappedTestClass);
beforeTest(sandbox, method, bootstrappedMethod);
final Statement statement = helperTestRunner.methodBlock(new FrameworkMethod(bootstrappedMethod));
// todo: this try/finally probably isn't right -- should mimic RunAfters? [xw]
try {
statement.evaluate();
} finally {
afterTest(method, bootstrappedMethod);
}
} finally {
Thread.currentThread().setContextClassLoader(priorContextClassLoader);
finallyAfterTest(method);
}
}
};
}
use of org.junit.runners.model.Statement in project spring-boot by spring-projects.
the class CliTester method apply.
@Override
public Statement apply(final Statement base, final Description description) {
final Statement statement = CliTester.this.outputCapture.apply(new RunLauncherStatement(base), description);
return new Statement() {
@Override
public void evaluate() throws Throwable {
Assume.assumeTrue("Not running sample integration tests because integration profile not active", System.getProperty("spring.profiles.active", "integration").contains("integration"));
statement.evaluate();
}
};
}
Aggregations