Search in sources :

Example 21 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class JUnit4Runner method run.

/**
   * Runs the JUnit4 test.
   *
   * @return Result of running the test
   */
public Result run() {
    testRunnerOut.println("JUnit4 Test Runner");
    checkJUnitRunnerApiVersion();
    for (Initializer init : initializers) {
        init.initialize();
    }
    // Sharding
    TestSuiteModel model = modelSupplier.get();
    Filter shardingFilter = model.getShardingFilter();
    Request filteredRequest = applyFilters(request, shardingFilter, config.getTestIncludeFilterRegexp(), config.getTestExcludeFilterRegexp());
    JUnitCore core = new JUnitCore();
    for (RunListener runListener : runListeners) {
        core.addListener(runListener);
    }
    File exitFile = getExitFile();
    exitFileActive(exitFile);
    try {
        try {
            if (config.shouldInstallSecurityManager()) {
                installSecurityManager();
            }
            Request cancellableRequest = requestFactory.createRequest(filteredRequest);
            return core.run(cancellableRequest);
        } finally {
            disableSecurityManager();
        }
    } finally {
        exitFileInactive(exitFile);
    }
}
Also used : JUnitCore(org.junit.runner.JUnitCore) Filter(org.junit.runner.manipulation.Filter) RegExTestCaseFilter(com.google.testing.junit.junit4.runner.RegExTestCaseFilter) SuiteTrimmingFilter(com.google.testing.junit.junit4.runner.SuiteTrimmingFilter) TestSuiteModel(com.google.testing.junit.runner.model.TestSuiteModel) Request(org.junit.runner.Request) File(java.io.File) RunListener(org.junit.runner.notification.RunListener)

Example 22 with Request

use of org.junit.runner.Request in project buck by facebook.

the class TimeoutTest method doTestUsingThreadGuardedTestClass.

private void doTestUsingThreadGuardedTestClass() throws InitializationError {
    Class<?> testClass = ThreadGuardedTest.class;
    RunnerBuilder builder = new RunnerBuilder() {

        @Override
        public Runner runnerForClass(Class<?> clazz) throws Throwable {
            return new BuckBlockJUnit4ClassRunner(clazz, /* defaultTestTimeoutMillis */
            500);
        }
    };
    Runner suite = new Computer().getSuite(builder, new Class<?>[] { testClass });
    Request request = Request.runner(suite);
    final Set<Result> results = Sets.newHashSet();
    JUnitCore core = new JUnitCore();
    core.addListener(new RunListener() {

        @Override
        public void testRunFinished(Result result) throws Exception {
            results.add(result);
        }
    });
    core.run(request);
    Result result = Iterables.getOnlyElement(results);
    assertEquals(3, result.getRunCount());
    assertEquals(2, result.getFailureCount());
    // The order in which the tests were run doesn't matter. What matters is that we see our
    // expected messages.
    Set<String> messages = result.getFailures().stream().map(Failure::getMessage).collect(MoreCollectors.toImmutableSet());
    assertEquals("Should contain explicit call to fail() from failingTestsAreReported() and " + "the timeout message from testsMayTimeOut().", ImmutableSet.of("This is expected", "test testsMayTimeOut timed out after 500 milliseconds"), messages);
}
Also used : Runner(org.junit.runner.Runner) JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) Result(org.junit.runner.Result) RunListener(org.junit.runner.notification.RunListener) RunnerBuilder(org.junit.runners.model.RunnerBuilder) Computer(org.junit.runner.Computer)

Example 23 with Request

use of org.junit.runner.Request 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 24 with Request

use of org.junit.runner.Request in project intellij-community by JetBrains.

the class JUnit4TestRunnerUtil method buildRequest.

public static Request buildRequest(String[] suiteClassNames, final String name, boolean notForked) {
    if (suiteClassNames.length == 0) {
        return null;
    }
    Vector result = new Vector();
    for (int i = 0; i < suiteClassNames.length; i++) {
        String suiteClassName = suiteClassNames[i];
        if (suiteClassName.charAt(0) == '@') {
            // all tests in the package specified
            try {
                final Map classMethods = new HashMap();
                BufferedReader reader = new BufferedReader(new FileReader(suiteClassName.substring(1)));
                try {
                    final String packageName = reader.readLine();
                    if (packageName == null)
                        return null;
                    final String categoryName = reader.readLine();
                    final Class category = categoryName != null && categoryName.length() > 0 ? loadTestClass(categoryName) : null;
                    String line;
                    while ((line = reader.readLine()) != null) {
                        String className = line;
                        final int idx = line.indexOf(',');
                        if (idx != -1) {
                            className = line.substring(0, idx);
                            Set methodNames = (Set) classMethods.get(className);
                            if (methodNames == null) {
                                methodNames = new HashSet();
                                classMethods.put(className, methodNames);
                            }
                            methodNames.add(line.substring(idx + 1));
                        }
                        appendTestClass(result, className);
                    }
                    String suiteName = packageName.length() == 0 ? "<default package>" : packageName;
                    Class[] classes = getArrayOfClasses(result);
                    if (classes.length == 0) {
                        System.out.println(TestRunnerUtil.testsFoundInPackageMesage(0, suiteName));
                        return null;
                    }
                    Request allClasses;
                    try {
                        Class.forName("org.junit.runner.Computer");
                        allClasses = JUnit46ClassesRequestBuilder.getClassesRequest(suiteName, classes, classMethods, category);
                    } catch (ClassNotFoundException e) {
                        allClasses = getClassRequestsUsing44API(suiteName, classes);
                    } catch (NoSuchMethodError e) {
                        allClasses = getClassRequestsUsing44API(suiteName, classes);
                    }
                    return classMethods.isEmpty() ? allClasses : allClasses.filterWith(new Filter() {

                        public boolean shouldRun(Description description) {
                            if (description.isTest()) {
                                final Set methods = (Set) classMethods.get(JUnit4ReflectionUtil.getClassName(description));
                                if (methods == null) {
                                    return true;
                                }
                                String methodName = JUnit4ReflectionUtil.getMethodName(description);
                                if (methods.contains(methodName)) {
                                    return true;
                                }
                                if (name != null) {
                                    return methodName.endsWith(name) && methods.contains(methodName.substring(0, methodName.length() - name.length()));
                                }
                                final Class testClass = description.getTestClass();
                                if (testClass != null) {
                                    final RunWith classAnnotation = (RunWith) testClass.getAnnotation(RunWith.class);
                                    if (classAnnotation != null && Parameterized.class.isAssignableFrom(classAnnotation.value())) {
                                        final int idx = methodName.indexOf("[");
                                        if (idx > -1) {
                                            return methods.contains(methodName.substring(0, idx));
                                        }
                                    }
                                }
                                return false;
                            }
                            return true;
                        }

                        public String describe() {
                            return "Tests";
                        }
                    });
                } finally {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(1);
            }
        } else {
            int index = suiteClassName.indexOf(',');
            if (index != -1) {
                final Class clazz = loadTestClass(suiteClassName.substring(0, index));
                final String methodName = suiteClassName.substring(index + 1);
                final RunWith clazzAnnotation = (RunWith) clazz.getAnnotation(RunWith.class);
                final Description testMethodDescription = Description.createTestDescription(clazz, methodName);
                if (clazzAnnotation == null) {
                    //do not override external runners
                    try {
                        final Method method = clazz.getMethod(methodName, null);
                        if (method != null && notForked && (method.getAnnotation(Ignore.class) != null || clazz.getAnnotation(Ignore.class) != null)) {
                            //override ignored case only
                            final Request classRequest = JUnit45ClassesRequestBuilder.createIgnoreIgnoredClassRequest(clazz, true);
                            final Filter ignoredTestFilter = Filter.matchMethodDescription(testMethodDescription);
                            return classRequest.filterWith(new Filter() {

                                public boolean shouldRun(Description description) {
                                    return ignoredTestFilter.shouldRun(description);
                                }

                                public String describe() {
                                    return "Ignored " + methodName;
                                }
                            });
                        }
                    } catch (Throwable ignored) {
                    //return simple method runner
                    }
                } else {
                    final Request request = getParameterizedRequest(name, methodName, clazz, clazzAnnotation);
                    if (request != null) {
                        return request;
                    }
                }
                try {
                    if (clazz.getMethod("suite", new Class[0]) != null && !methodName.equals("suite")) {
                        return Request.classWithoutSuiteMethod(clazz).filterWith(testMethodDescription);
                    }
                } catch (Throwable e) {
                //ignore
                }
                final Filter methodFilter;
                try {
                    methodFilter = Filter.matchMethodDescription(testMethodDescription);
                } catch (NoSuchMethodError e) {
                    return Request.method(clazz, methodName);
                }
                return Request.aClass(clazz).filterWith(new Filter() {

                    public boolean shouldRun(Description description) {
                        if (description.isTest() && description.getDisplayName().startsWith("warning(junit.framework.TestSuite$")) {
                            return true;
                        }
                        return methodFilter.shouldRun(description);
                    }

                    public String describe() {
                        return methodFilter.describe();
                    }
                });
            } else if (name != null && suiteClassNames.length == 1) {
                final Class clazz = loadTestClass(suiteClassName);
                if (clazz != null) {
                    final RunWith clazzAnnotation = (RunWith) clazz.getAnnotation(RunWith.class);
                    final Request request = getParameterizedRequest(name, null, clazz, clazzAnnotation);
                    if (request != null) {
                        return request;
                    }
                }
            }
            appendTestClass(result, suiteClassName);
        }
    }
    if (result.size() == 1) {
        final Class clazz = (Class) result.get(0);
        try {
            if (clazz.getAnnotation(Ignore.class) != null) {
                //override ignored case only
                return JUnit45ClassesRequestBuilder.createIgnoreIgnoredClassRequest(clazz, false);
            }
        } catch (ClassNotFoundException e) {
        //return simple class runner
        }
        return Request.aClass(clazz);
    }
    return Request.classes(getArrayOfClasses(result));
}
Also used : Ignore(org.junit.Ignore) Description(org.junit.runner.Description) Request(org.junit.runner.Request) IOException(java.io.IOException) Method(java.lang.reflect.Method) RunWith(org.junit.runner.RunWith) Parameterized(org.junit.runners.Parameterized) Filter(org.junit.runner.manipulation.Filter) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 25 with Request

use of org.junit.runner.Request in project intellij-community by JetBrains.

the class JUnit4TestRunnerUtil method getClassRequestsUsing44API.

private static Request getClassRequestsUsing44API(String suiteName, Class[] classes) {
    Request allClasses;
    try {
        Class.forName("org.junit.internal.requests.ClassesRequest");
        allClasses = JUnit4ClassesRequestBuilder.getClassesRequest(suiteName, classes);
    } catch (ClassNotFoundException e1) {
        allClasses = JUnit45ClassesRequestBuilder.getClassesRequest(suiteName, classes);
    }
    return allClasses;
}
Also used : Request(org.junit.runner.Request)

Aggregations

Request (org.junit.runner.Request)54 Test (org.junit.Test)37 JUnitCore (org.junit.runner.JUnitCore)32 Result (org.junit.runner.Result)22 Description (org.junit.runner.Description)20 TestSuiteModel (com.google.testing.junit.runner.model.TestSuiteModel)8 Runner (org.junit.runner.Runner)8 PrintableResult.testResult (org.junit.experimental.results.PrintableResult.testResult)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 RunListener (org.junit.runner.notification.RunListener)6 OutputStream (java.io.OutputStream)5 CategoryFilter (org.junit.experimental.categories.Categories.CategoryFilter)5 Filter (org.junit.runner.manipulation.Filter)5 Failure (org.junit.runner.notification.Failure)5 IOException (java.io.IOException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 InOrder (org.mockito.InOrder)4 ArrayList (java.util.ArrayList)3 Computer (org.junit.runner.Computer)3 TestNode (com.google.testing.junit.runner.model.TestNode)2