Search in sources :

Example 1 with TestSelectionMatcher

use of org.gradle.api.internal.tasks.testing.filter.TestSelectionMatcher 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)

Aggregations

ArrayList (java.util.ArrayList)1 InvalidUserDataException (org.gradle.api.InvalidUserDataException)1 Transformer (org.gradle.api.Transformer)1 TestSelectionMatcher (org.gradle.api.internal.tasks.testing.filter.TestSelectionMatcher)1 Request (org.junit.runner.Request)1 Runner (org.junit.runner.Runner)1 Filter (org.junit.runner.manipulation.Filter)1 Filterable (org.junit.runner.manipulation.Filterable)1 NoTestsRemainException (org.junit.runner.manipulation.NoTestsRemainException)1 RunNotifier (org.junit.runner.notification.RunNotifier)1