Search in sources :

Example 1 with Filterable

use of org.junit.runner.manipulation.Filterable in project gradle by gradle.

the class JUnitTestClassExecuter method runTestClass.

private void runTestClass(String testClassName) throws ClassNotFoundException {
    final Class<?> testClass = Class.forName(testClassName, false, applicationClassLoader);
    List<Filter> filters = new ArrayList<Filter>();
    if (options.hasCategoryConfiguration()) {
        verifyJUnitCategorySupport();
        Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {

            public Class<?> transform(final String original) {
                try {
                    return applicationClassLoader.loadClass(original);
                } catch (ClassNotFoundException e) {
                    throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
                }
            }
        };
        filters.add(new CategoryFilter(CollectionUtils.collect(options.getIncludeCategories(), transformer), CollectionUtils.collect(options.getExcludeCategories(), transformer)));
    }
    Request request = Request.aClass(testClass);
    Runner runner = request.getRunner();
    if (!options.getIncludedTests().isEmpty()) {
        TestSelectionMatcher matcher = new TestSelectionMatcher(options.getIncludedTests());
        // matches the filter, run the entire suite instead of filtering away its contents.
        if (!runner.getDescription().isSuite() || !matcher.matchesTest(testClassName, null)) {
            filters.add(new MethodNameFilter(matcher));
        }
    }
    if (runner instanceof Filterable) {
        Filterable filterable = (Filterable) runner;
        for (Filter filter : filters) {
            try {
                filterable.filter(filter);
            } catch (NoTestsRemainException e) {
                // Ignore
                return;
            }
        }
    } else if (allTestsFiltered(runner, filters)) {
        return;
    }
    RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);
    runner.run(notifier);
}
Also used : Runner(org.junit.runner.Runner) Transformer(org.gradle.api.Transformer) RunNotifier(org.junit.runner.notification.RunNotifier) ArrayList(java.util.ArrayList) Request(org.junit.runner.Request) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) TestSelectionMatcher(org.gradle.api.internal.tasks.testing.filter.TestSelectionMatcher) Filter(org.junit.runner.manipulation.Filter) InvalidUserDataException(org.gradle.api.InvalidUserDataException) Filterable(org.junit.runner.manipulation.Filterable)

Example 2 with Filterable

use of org.junit.runner.manipulation.Filterable in project junit4 by junit-team.

the class JUnit38ClassRunner method filter.

public void filter(Filter filter) throws NoTestsRemainException {
    if (getTest() instanceof Filterable) {
        Filterable adapter = (Filterable) getTest();
        adapter.filter(filter);
    } else if (getTest() instanceof TestSuite) {
        TestSuite suite = (TestSuite) getTest();
        TestSuite filtered = new TestSuite(suite.getName());
        int n = suite.testCount();
        for (int i = 0; i < n; i++) {
            Test test = suite.testAt(i);
            if (filter.shouldRun(makeDescription(test))) {
                filtered.addTest(test);
            }
        }
        setTest(filtered);
        if (filtered.testCount() == 0) {
            throw new NoTestsRemainException();
        }
    }
}
Also used : TestSuite(junit.framework.TestSuite) Test(junit.framework.Test) Filterable(org.junit.runner.manipulation.Filterable) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException)

Example 3 with Filterable

use of org.junit.runner.manipulation.Filterable in project junit4 by junit-team.

the class SingleMethodTest method filteringAwayEverythingThrowsException.

@Test(expected = NoTestsRemainException.class)
public void filteringAwayEverythingThrowsException() throws NoTestsRemainException {
    Filterable runner = (Filterable) Request.aClass(OneTimeSetup.class).getRunner();
    runner.filter(new Filter() {

        @Override
        public boolean shouldRun(Description description) {
            return false;
        }

        @Override
        public String describe() {
            return null;
        }
    });
}
Also used : Description(org.junit.runner.Description) Filter(org.junit.runner.manipulation.Filter) Filterable(org.junit.runner.manipulation.Filterable) Test(org.junit.Test)

Example 4 with Filterable

use of org.junit.runner.manipulation.Filterable 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 5 with Filterable

use of org.junit.runner.manipulation.Filterable in project gradle by gradle.

the class JUnitTestClassExecutor method runTestClass.

private void runTestClass(String testClassName) throws ClassNotFoundException {
    final Class<?> testClass = Class.forName(testClassName, false, applicationClassLoader);
    if (isNestedClassInsideEnclosedRunner(testClass)) {
        return;
    }
    List<Filter> filters = new ArrayList<Filter>();
    if (options.hasCategoryConfiguration()) {
        verifyJUnitCategorySupport();
        filters.add(new CategoryFilter(options.getIncludeCategories(), options.getExcludeCategories(), applicationClassLoader));
    }
    Request request = Request.aClass(testClass);
    Runner runner = request.getRunner();
    if (!options.getIncludedTests().isEmpty() || !options.getIncludedTestsCommandLine().isEmpty() || !options.getExcludedTests().isEmpty()) {
        TestSelectionMatcher matcher = new TestSelectionMatcher(options.getIncludedTests(), options.getExcludedTests(), options.getIncludedTestsCommandLine());
        // matches the filter, run the entire suite instead of filtering away its contents.
        if (!runner.getDescription().isSuite() || !matcher.matchesTest(testClassName, null)) {
            filters.add(new MethodNameFilter(matcher));
        }
    }
    if (runner instanceof Filterable) {
        Filterable filterable = (Filterable) runner;
        for (Filter filter : filters) {
            try {
                filterable.filter(filter);
            } catch (NoTestsRemainException e) {
                // Ignore
                return;
            }
        }
    } else if (allTestsFiltered(runner, filters)) {
        return;
    }
    RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);
    runner.run(notifier);
}
Also used : Runner(org.junit.runner.Runner) TestSelectionMatcher(org.gradle.api.internal.tasks.testing.filter.TestSelectionMatcher) RunNotifier(org.junit.runner.notification.RunNotifier) Filter(org.junit.runner.manipulation.Filter) Filterable(org.junit.runner.manipulation.Filterable) ArrayList(java.util.ArrayList) Request(org.junit.runner.Request) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException)

Aggregations

Filterable (org.junit.runner.manipulation.Filterable)5 Filter (org.junit.runner.manipulation.Filter)4 Runner (org.junit.runner.Runner)3 NoTestsRemainException (org.junit.runner.manipulation.NoTestsRemainException)3 ArrayList (java.util.ArrayList)2 TestSelectionMatcher (org.gradle.api.internal.tasks.testing.filter.TestSelectionMatcher)2 Request (org.junit.runner.Request)2 RunNotifier (org.junit.runner.notification.RunNotifier)2 Test (junit.framework.Test)1 TestSuite (junit.framework.TestSuite)1 InvalidUserDataException (org.gradle.api.InvalidUserDataException)1 Transformer (org.gradle.api.Transformer)1 Test (org.junit.Test)1 Description (org.junit.runner.Description)1 RunnerTestDescriptor (org.junit.vintage.engine.descriptor.RunnerTestDescriptor)1