Search in sources :

Example 36 with Runner

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

the class JUnit4TestRunnerUtil method getParameterizedRequest.

private static Request getParameterizedRequest(final String parameterString, final String methodName, Class clazz, RunWith clazzAnnotation) {
    if (clazzAnnotation == null)
        return null;
    final Class runnerClass = clazzAnnotation.value();
    if (Parameterized.class.isAssignableFrom(runnerClass)) {
        try {
            if (methodName != null) {
                final Method method = clazz.getMethod(methodName, new Class[0]);
                if (method != null && !method.isAnnotationPresent(Test.class) && TestCase.class.isAssignableFrom(clazz)) {
                    return Request.runner(JUnit45ClassesRequestBuilder.createIgnoreAnnotationAndJUnit4ClassRunner(clazz));
                }
            }
            //ignore for junit4.4 and <
            Class.forName("org.junit.runners.BlockJUnit4ClassRunner");
            final Constructor runnerConstructor = runnerClass.getConstructor(new Class[] { Class.class });
            return Request.runner((Runner) runnerConstructor.newInstance(new Object[] { clazz })).filterWith(new Filter() {

                public boolean shouldRun(Description description) {
                    final String descriptionMethodName = description.getMethodName();
                    //filter by params
                    if (parameterString != null && descriptionMethodName != null && !descriptionMethodName.endsWith(parameterString)) {
                        return false;
                    }
                    //filter only selected method
                    if (methodName != null && descriptionMethodName != null && //valid for any parameter for current method
                    !descriptionMethodName.startsWith(methodName + "[") && !descriptionMethodName.equals(methodName)) {
                        //if fork mode used, parameter is included in the name itself
                        return false;
                    }
                    return true;
                }

                public String describe() {
                    if (parameterString == null) {
                        return methodName + " with any parameter";
                    }
                    if (methodName == null) {
                        return "Parameter " + parameterString + " for any method";
                    }
                    return methodName + " with parameter " + parameterString;
                }
            });
        } catch (Throwable throwable) {
        //return simple method runner
        }
    }
    return null;
}
Also used : Runner(org.junit.runner.Runner) Description(org.junit.runner.Description) Test(org.junit.Test) TestCase(junit.framework.TestCase) Filter(org.junit.runner.manipulation.Filter) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method)

Example 37 with Runner

use of org.junit.runner.Runner in project gerrit by GerritCodeReview.

the class ConfigSuite method runnersFor.

private static List<Runner> runnersFor(Class<?> clazz) {
    Method defaultConfig = getDefaultConfig(clazz);
    List<Method> configs = getConfigs(clazz);
    Map<String, org.eclipse.jgit.lib.Config> configMap = callConfigMapMethod(getConfigMap(clazz), configs);
    Field parameterField = getOnlyField(clazz, Parameter.class);
    checkArgument(parameterField != null, "No @ConfigSuite.Parameter found");
    Field nameField = getOnlyField(clazz, Name.class);
    List<Runner> result = Lists.newArrayListWithCapacity(configs.size() + 1);
    try {
        result.add(new ConfigRunner(clazz, parameterField, nameField, null, callConfigMethod(defaultConfig)));
        for (Method m : configs) {
            result.add(new ConfigRunner(clazz, parameterField, nameField, m.getName(), callConfigMethod(m)));
        }
        for (Map.Entry<String, org.eclipse.jgit.lib.Config> e : configMap.entrySet()) {
            result.add(new ConfigRunner(clazz, parameterField, nameField, e.getKey(), e.getValue()));
        }
        return result;
    } catch (InitializationError e) {
        System.err.println("Errors initializing runners:");
        for (Throwable t : e.getCauses()) {
            t.printStackTrace();
        }
        throw new RuntimeException(e);
    }
}
Also used : BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) Runner(org.junit.runner.Runner) InitializationError(org.junit.runners.model.InitializationError) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Field(java.lang.reflect.Field) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 38 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 39 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 40 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)

Aggregations

Runner (org.junit.runner.Runner)64 Test (org.junit.Test)21 Description (org.junit.runner.Description)14 ParentRunner (org.junit.runners.ParentRunner)13 ArrayList (java.util.ArrayList)12 JUnitCore (org.junit.runner.JUnitCore)11 Request (org.junit.runner.Request)11 RunNotifier (org.junit.runner.notification.RunNotifier)11 Filter (org.junit.runner.manipulation.Filter)10 NoTestsRemainException (org.junit.runner.manipulation.NoTestsRemainException)9 Result (org.junit.runner.Result)8 Method (java.lang.reflect.Method)7 Failure (org.junit.runner.notification.Failure)7 InitializationError (org.junit.runners.model.InitializationError)7 JUnit38ClassRunner (org.junit.internal.runners.JUnit38ClassRunner)5 RunnerSpy (org.junit.runner.RunnerSpy)5 LinkedList (java.util.LinkedList)4 BlockJUnit4ClassRunner (org.junit.runners.BlockJUnit4ClassRunner)4 File (java.io.File)3 ImmutableMap (com.google.common.collect.ImmutableMap)2