Search in sources :

Example 26 with InitializationError

use of org.junit.runners.model.InitializationError 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 27 with InitializationError

use of org.junit.runners.model.InitializationError in project pact-jvm by DiUS.

the class PactRunner method getPactSource.

protected PactLoader getPactSource(final TestClass clazz) throws InitializationError {
    final PactSource pactSource = clazz.getAnnotation(PactSource.class);
    final List<Annotation> pactLoaders = Arrays.stream(clazz.getAnnotations()).filter(annotation -> annotation.annotationType().getAnnotation(PactSource.class) != null).collect(toList());
    if ((pactSource == null ? 0 : 1) + pactLoaders.size() != 1) {
        throw new InitializationError("Exactly one pact source should be set");
    }
    try {
        if (pactSource != null) {
            final Class<? extends PactLoader> pactLoaderClass = pactSource.value();
            try {
                // Checks if there is a constructor with one argument of type Class.
                final Constructor<? extends PactLoader> contructorWithClass = pactLoaderClass.getDeclaredConstructor(Class.class);
                contructorWithClass.setAccessible(true);
                return contructorWithClass.newInstance(clazz.getJavaClass());
            } catch (NoSuchMethodException e) {
                LOGGER.error(e.getMessage(), e);
                return pactLoaderClass.newInstance();
            }
        } else {
            final Annotation annotation = pactLoaders.iterator().next();
            return annotation.annotationType().getAnnotation(PactSource.class).value().getConstructor(annotation.annotationType()).newInstance(annotation);
        }
    } catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        LOGGER.error("Error while creating pact source", e);
        throw new InitializationError(e);
    }
}
Also used : PactSource(au.com.dius.pact.provider.junit.loader.PactSource) Arrays(java.util.Arrays) PactBroker(au.com.dius.pact.provider.junit.loader.PactBroker) Logger(org.slf4j.Logger) Pact(au.com.dius.pact.model.Pact) LoggerFactory(org.slf4j.LoggerFactory) Description(org.junit.runner.Description) IOException(java.io.IOException) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) HttpTarget(au.com.dius.pact.provider.junit.target.HttpTarget) TestTarget(au.com.dius.pact.provider.junit.target.TestTarget) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TestClass(org.junit.runners.model.TestClass) Annotation(java.lang.annotation.Annotation) PactLoader(au.com.dius.pact.provider.junit.loader.PactLoader) ParentRunner(org.junit.runners.ParentRunner) RunNotifier(org.junit.runner.notification.RunNotifier) InitializationError(org.junit.runners.model.InitializationError) PactFolder(au.com.dius.pact.provider.junit.loader.PactFolder) PactSource(au.com.dius.pact.provider.junit.loader.PactSource) Target(au.com.dius.pact.provider.junit.target.Target) InitializationError(org.junit.runners.model.InitializationError) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 28 with InitializationError

use of org.junit.runners.model.InitializationError in project graal by oracle.

the class SLTestRunner method createTests.

protected static List<TestCase> createTests(final Class<?> c) throws IOException, InitializationError {
    SLTestSuite suite = c.getAnnotation(SLTestSuite.class);
    if (suite == null) {
        throw new InitializationError(String.format("@%s annotation required on class '%s' to run with '%s'.", SLTestSuite.class.getSimpleName(), c.getName(), SLTestRunner.class.getSimpleName()));
    }
    String[] paths = suite.value();
    Class<?> testCaseDirectory = c;
    if (suite.testCaseDirectory() != SLTestSuite.class) {
        testCaseDirectory = suite.testCaseDirectory();
    }
    Path root = getRootViaResourceURL(testCaseDirectory, paths);
    if (root == null) {
        for (String path : paths) {
            Path candidate = FileSystems.getDefault().getPath(path);
            if (Files.exists(candidate)) {
                root = candidate;
                break;
            }
        }
    }
    if (root == null && paths.length > 0) {
        throw new FileNotFoundException(paths[0]);
    }
    final Path rootPath = root;
    final List<TestCase> foundCases = new ArrayList<>();
    Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path sourceFile, BasicFileAttributes attrs) throws IOException {
            String sourceName = sourceFile.getFileName().toString();
            if (sourceName.endsWith(SOURCE_SUFFIX)) {
                String baseName = sourceName.substring(0, sourceName.length() - SOURCE_SUFFIX.length());
                Path inputFile = sourceFile.resolveSibling(baseName + INPUT_SUFFIX);
                String testInput = "";
                if (Files.exists(inputFile)) {
                    testInput = readAllLines(inputFile);
                }
                Path outputFile = sourceFile.resolveSibling(baseName + OUTPUT_SUFFIX);
                String expectedOutput = "";
                if (Files.exists(outputFile)) {
                    expectedOutput = readAllLines(outputFile);
                }
                foundCases.add(new TestCase(c, baseName, sourceName, sourceFile, testInput, expectedOutput));
            }
            return FileVisitResult.CONTINUE;
        }
    });
    return foundCases;
}
Also used : Path(java.nio.file.Path) InitializationError(org.junit.runners.model.InitializationError) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) TestCase(com.oracle.truffle.sl.test.SLTestRunner.TestCase) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 29 with InitializationError

use of org.junit.runners.model.InitializationError 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 30 with InitializationError

use of org.junit.runners.model.InitializationError in project st-js by st-js.

the class JUnitSession method startInParallel.

private void startInParallel(Collection<? extends AsyncProcess> processes) throws InitializationError {
    List<Thread> threads = new ArrayList<Thread>();
    final List<Throwable> errors = new CopyOnWriteArrayList<Throwable>();
    // start all the dependencies in parallel
    for (final AsyncProcess proc : processes) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    proc.start();
                } catch (Exception e) {
                    errors.add(e);
                }
            }
        });
        t.start();
        threads.add(t);
    }
    // dependencies have started (or failed to start)
    for (Thread t : threads) {
        try {
            t.join();
        } catch (InterruptedException e) {
            throw new InitializationError(e);
        }
    }
    // check if any of the dependencies has failed to start. If some did, throw an exception
    if (!errors.isEmpty()) {
        throw new InitializationError(errors);
    }
}
Also used : InitializationError(org.junit.runners.model.InitializationError) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

InitializationError (org.junit.runners.model.InitializationError)42 ArrayList (java.util.ArrayList)12 IOException (java.io.IOException)9 Runner (org.junit.runner.Runner)7 BlockJUnit4ClassRunner (org.junit.runners.BlockJUnit4ClassRunner)5 Field (java.lang.reflect.Field)4 Method (java.lang.reflect.Method)4 JUnitCore (org.junit.runner.JUnitCore)4 RunNotifier (org.junit.runner.notification.RunNotifier)4 File (java.io.File)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 CucumberScenario (cucumber.runtime.model.CucumberScenario)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 List (java.util.List)2 Test (org.junit.Test)2 AllDefaultPossibilitiesBuilder (org.junit.internal.builders.AllDefaultPossibilitiesBuilder)2 ErrorReportingRunner (org.junit.internal.runners.ErrorReportingRunner)2 ParentRunner (org.junit.runners.ParentRunner)2 FrameworkMethod (org.junit.runners.model.FrameworkMethod)2