Search in sources :

Example 81 with Runner

use of org.junit.runner.Runner in project xwiki-platform by xwiki.

the class XWikiExecutorSuite method getChildren.

@Override
protected List<Runner> getChildren() {
    List<Runner> runners = new ArrayList<Runner>();
    // Filter the test classes to run.
    for (Runner runner : super.getChildren()) {
        Description description = runner.getDescription();
        String runnerName = description.getClassName();
        if (runnerName.matches(PATTERN)) {
            // If the entire test class matches, add it.
            runners.add(runner);
        } else {
            // Otherwise, filter the test methods to run.
            try {
                METHOD_FILTER.apply(runner);
                // If the runner still has tests remaining after the filtering, add it.
                runners.add(runner);
            } catch (NoTestsRemainException e) {
                LOGGER.info("Skipping test class: {}", description.getClassName());
            }
        }
    }
    return runners;
}
Also used : Runner(org.junit.runner.Runner) Description(org.junit.runner.Description) ArrayList(java.util.ArrayList) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException)

Example 82 with Runner

use of org.junit.runner.Runner in project xwiki-platform by xwiki.

the class ArchiveSuite method createRunners.

/**
 * Read the archive and build a list of runners for its content.
 *
 * @param archivePath path to the archive to use
 * @return a list of test runners
 * @throws InitializationError on errors
 */
private List<Runner> createRunners(String archivePath) throws InitializationError {
    File archiveFile = new File(archivePath);
    if (!archiveFile.exists()) {
        throw new InitializationError("Not a file or directory ({" + archivePath + "])");
    }
    List<Runner> list = new ArrayList<Runner>();
    if (archiveFile.isDirectory()) {
        createRunners(archiveFile, list);
    } else {
        try {
            final ZipFile archive = new ZipFile(archivePath);
            Enumeration<? extends ZipEntry> entries = archive.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.isDirectory()) {
                    continue;
                }
                Reader reader = new InputStreamReader(archive.getInputStream(entry));
                addTest(list, entry.getName(), reader);
            }
            archive.close();
        } catch (IOException exception) {
            throw new InitializationError(exception);
        }
    }
    return list;
}
Also used : Runner(org.junit.runner.Runner) ParentRunner(org.junit.runners.ParentRunner) ZipFile(java.util.zip.ZipFile) InputStreamReader(java.io.InputStreamReader) InitializationError(org.junit.runners.model.InitializationError) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileReader(java.io.FileReader) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 83 with Runner

use of org.junit.runner.Runner in project dspot by STAMP-project.

the class DefaultTestRunner method run.

@Override
public TestListener run(Class<?> testClass, Collection<String> testMethodNames, RunListener... additionalListeners) {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    TestListener listener = new TestListener();
    final Future<?> submit = executor.submit(() -> {
        Request request = Request.aClass(testClass);
        if (!testMethodNames.isEmpty()) {
            request = request.filterWith(new MethodFilter(testMethodNames));
        }
        Runner runner = request.getRunner();
        RunNotifier runNotifier = new RunNotifier();
        Arrays.stream(additionalListeners).forEach(runNotifier::addListener);
        runNotifier.addFirstListener(listener);
        // Since we want to use our custom ClassLoader to run the tests of the project being executed by DSpot,
        // and since we create a new thread for starting the JUnit Runner, we need to set the context ClassLoader
        // to be our custom ClassLoader. This is so that any code in the tests or triggered by the test that uses
        // the context ClassLoader will work.
        // As an example if the tests call some code that uses Java's ServiceLoader then it would fail to find and
        // load any provider located in our custom ClassLoader.
        Thread.currentThread().setContextClassLoader(this.classLoader);
        runner.run(runNotifier);
    });
    try {
        long timeBeforeTimeOut = testMethodNames.isEmpty() ? AmplificationHelper.getTimeOutInMs() * (testClass.getMethods().length + 1) : AmplificationHelper.getTimeOutInMs() * (testMethodNames.size() + 1);
        submit.get(timeBeforeTimeOut, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        submit.cancel(true);
        executor.shutdownNow();
    }
    return listener;
}
Also used : Runner(org.junit.runner.Runner) RunNotifier(org.junit.runner.notification.RunNotifier) MethodFilter(fr.inria.stamp.test.filter.MethodFilter) Request(org.junit.runner.Request) TestListener(fr.inria.stamp.test.listener.TestListener)

Example 84 with Runner

use of org.junit.runner.Runner in project junit5 by junit-team.

the class TestClassRequestResolver method determineRunnerTestDescriptor.

private RunnerTestDescriptor determineRunnerTestDescriptor(Class<?> testClass, Runner runner, List<RunnerTestDescriptorAwareFilter> filters, UniqueId engineId) {
    RunnerTestDescriptor runnerTestDescriptor = createCompleteRunnerTestDescriptor(testClass, runner, engineId);
    if (!filters.isEmpty()) {
        if (runner instanceof Filterable) {
            Filter filter = createOrFilter(filters, runnerTestDescriptor);
            Runner filteredRunner = runnerTestDescriptor.toRequest().filterWith(filter).getRunner();
            runnerTestDescriptor = createCompleteRunnerTestDescriptor(testClass, filteredRunner, engineId);
        } else {
            Runner runnerToReport = (runner instanceof RunnerDecorator) ? ((RunnerDecorator) runner).getDecoratedRunner() : runner;
            logger.warn(() -> // 
            "Runner " + runnerToReport.getClass().getName() + " (used on " + testClass.getName() + // 
            ") does not support filtering" + " and will therefore be run completely.");
        }
    }
    return runnerTestDescriptor;
}
Also used : RunnerTestDescriptor(org.junit.vintage.engine.descriptor.RunnerTestDescriptor) Runner(org.junit.runner.Runner) Filter(org.junit.runner.manipulation.Filter) Filterable(org.junit.runner.manipulation.Filterable)

Example 85 with Runner

use of org.junit.runner.Runner in project kanonizo by kanonizo.

the class JUnit4TestRunner method getRequest.

private Request getRequest(TestCase tc) {
    Class<?> testClass = tc.getTestClass();
    Method testMethod = tc.getMethod();
    try {
        final RunWith runWith = testClass.getAnnotation(RunWith.class);
        if (runWith != null) {
            final Class<? extends Runner> runnerClass = runWith.value();
            if (runnerClass.isAssignableFrom(Parameterized.class)) {
                try {
                    if (tc instanceof ParameterisedTestCase) {
                        ParameterisedTestCase ptc = (ParameterisedTestCase) tc;
                        Class.forName(// ignore IgnoreIgnored for junit4.4 and <
                        "org.junit.runners.BlockJUnit4ClassRunner");
                        return Request.runner(new ParameterizedMethodRunner(testClass, testMethod.getName(), ptc.getParameters()));
                    }
                } catch (Throwable thrown) {
                    logger.error(thrown);
                }
            } else {
                Constructor<?> con = Util.getConstructor(runnerClass, new Class[] { Class.class });
                if (con != null) {
                    try {
                        Runner runner = (Runner) con.newInstance(new Object[] { testClass });
                        return Request.runner(runner).filterWith(Description.createTestDescription(testClass, testMethod.getName()));
                    } catch (InvocationTargetException e) {
                    }
                }
            }
        } else {
            if (testMethod != null && testMethod.getAnnotation(Ignore.class) != null) {
                // override ignored case only
                final Request classRequest = new ClassRequest(testClass) {

                    public Runner getRunner() {
                        try {
                            return new IgnoreIgnoredTestJUnit4ClassRunner(testClass);
                        } catch (Exception ignored) {
                        }
                        return super.getRunner();
                    }
                };
                return classRequest.filterWith(Description.createTestDescription(testClass, testMethod.getName()));
            }
        }
    } catch (Exception ignored) {
        logger.error(ignored);
    }
    return new Request() {

        @Override
        public Runner getRunner() {
            try {
                return new BlockJUnit4ClassRunner(testCase.getTestClass()) {

                    @Override
                    protected Statement withBeforeClasses(Statement statement) {
                        List<FrameworkMethod> beforeClass = getTestClass().getAnnotatedMethods(BeforeClass.class);
                        TestClass testClass = getTestClass();
                        if (beforeClass.size() > 0 && !initialisedClasses.stream().anyMatch(tc -> tc.getJavaClass().equals(testClass.getJavaClass()))) {
                            initialisedClasses.add(testClass);
                            return super.withBeforeClasses(statement);
                        } else {
                            return statement;
                        }
                    }
                };
            } catch (InitializationError e) {
                return new ErrorReportingRunner(tc.getTestClass(), e);
            }
        }
    }.filterWith(Description.createTestDescription(testClass, testMethod.getName()));
}
Also used : Statement(org.junit.runners.model.Statement) Arrays(java.util.Arrays) JUnitCore(org.junit.runner.JUnitCore) Result(org.junit.runner.Result) BeforeClass(org.junit.BeforeClass) Request(org.junit.runner.Request) RunWith(org.junit.runner.RunWith) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) TestCase(org.kanonizo.framework.objects.TestCase) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) TestClass(org.junit.runners.model.TestClass) Runner(org.junit.runner.Runner) ClassRequest(org.junit.internal.requests.ClassRequest) KanonizoTestFailure(org.kanonizo.junit.KanonizoTestFailure) InitializationError(org.junit.runners.model.InitializationError) Method(java.lang.reflect.Method) Parameterized(org.junit.runners.Parameterized) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) AssumptionViolatedException(org.junit.AssumptionViolatedException) ErrorReportingRunner(org.junit.internal.runners.ErrorReportingRunner) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Iterator(java.util.Iterator) EachTestNotifier(org.junit.internal.runners.model.EachTestNotifier) Filter(org.junit.runner.manipulation.Filter) Description(org.junit.runner.Description) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) Ignore(org.junit.Ignore) ParentRunner(org.junit.runners.ParentRunner) RunNotifier(org.junit.runner.notification.RunNotifier) LogManager(org.apache.logging.log4j.LogManager) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) Runner(org.junit.runner.Runner) ErrorReportingRunner(org.junit.internal.runners.ErrorReportingRunner) ParentRunner(org.junit.runners.ParentRunner) BeforeClass(org.junit.BeforeClass) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) Statement(org.junit.runners.model.Statement) InitializationError(org.junit.runners.model.InitializationError) Request(org.junit.runner.Request) ClassRequest(org.junit.internal.requests.ClassRequest) TestClass(org.junit.runners.model.TestClass) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) RunWith(org.junit.runner.RunWith) ClassRequest(org.junit.internal.requests.ClassRequest) InvocationTargetException(java.lang.reflect.InvocationTargetException) AssumptionViolatedException(org.junit.AssumptionViolatedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ErrorReportingRunner(org.junit.internal.runners.ErrorReportingRunner) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Runner (org.junit.runner.Runner)106 Test (org.junit.Test)39 RunNotifier (org.junit.runner.notification.RunNotifier)22 ArrayList (java.util.ArrayList)18 Description (org.junit.runner.Description)18 JUnitCore (org.junit.runner.JUnitCore)18 ParentRunner (org.junit.runners.ParentRunner)16 Request (org.junit.runner.Request)15 Result (org.junit.runner.Result)13 Filter (org.junit.runner.manipulation.Filter)12 NoTestsRemainException (org.junit.runner.manipulation.NoTestsRemainException)12 InitializationError (org.junit.runners.model.InitializationError)10 Method (java.lang.reflect.Method)9 JUnit4ParameterizedTest (androidx.test.testing.fixtures.JUnit4ParameterizedTest)7 SuiteConfiguration (org.eclipse.reddeer.junit.internal.configuration.SuiteConfiguration)7 Failure (org.junit.runner.notification.Failure)7 ErrorReportingRunner (org.junit.internal.runners.ErrorReportingRunner)6 Filterable (org.junit.runner.manipulation.Filterable)6 BlockJUnit4ClassRunner (org.junit.runners.BlockJUnit4ClassRunner)6 Field (java.lang.reflect.Field)5