Search in sources :

Example 21 with Statement

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();
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Failure(org.junit.runner.notification.Failure) Result(org.junit.runner.Result) RunListener(org.junit.runner.notification.RunListener)

Example 22 with Statement

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);
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) Failure(org.junit.runner.notification.Failure)

Example 23 with Statement

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();
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) AnnotatedElement(java.lang.reflect.AnnotatedElement) ThreadLeakGroup(com.carrotsearch.randomizedtesting.annotations.ThreadLeakGroup) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) MultipleFailureException(org.junit.runners.model.MultipleFailureException) UncaughtException(com.carrotsearch.randomizedtesting.RandomizedRunner.UncaughtException) Failure(org.junit.runner.notification.Failure)

Example 24 with Statement

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);
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) URLClassLoader(java.net.URLClassLoader) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) BeforeClass(org.junit.BeforeClass) TestClass(org.junit.runners.model.TestClass) AfterClass(org.junit.AfterClass) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Sandbox(org.robolectric.internal.bytecode.Sandbox)

Example 25 with Statement

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();
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement)

Aggregations

Statement (org.junit.runners.model.Statement)129 Test (org.junit.Test)22 FrameworkMethod (org.junit.runners.model.FrameworkMethod)15 Method (java.lang.reflect.Method)10 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)10 Fail (org.junit.internal.runners.statements.Fail)9 TestRule (org.junit.rules.TestRule)7 Description (org.junit.runner.Description)7 MethodRule (org.junit.rules.MethodRule)6 MultipleFailureException (org.junit.runners.model.MultipleFailureException)6 ExecutorService (java.util.concurrent.ExecutorService)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ReflectiveCallable (org.junit.internal.runners.model.ReflectiveCallable)5 ArrayList (java.util.ArrayList)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 EmptyStatement (com.hazelcast.util.EmptyStatement)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TestExecutorService (org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService)3 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)3 LoggerContextRule (org.apache.logging.log4j.junit.LoggerContextRule)3