All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.oracle.svm.junit.SVMJUnitRunner Maven / Gradle / Ivy

There is a newer version: 19.2.1
Show newest version
/*
 * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.svm.junit;

import com.oracle.mxtool.junit.MxJUnitRequest;
import com.oracle.mxtool.junit.MxJUnitWrapper;
import com.oracle.mxtool.junit.MxJUnitWrapper.MxJUnitConfig;
import com.oracle.svm.core.option.HostedOptionKey;
import com.oracle.svm.core.util.VMError;
import java.io.BufferedReader;
import java.io.FileReader;
import junit.runner.Version;
import org.graalvm.compiler.options.Option;
import org.graalvm.nativeimage.Feature.FeatureAccess;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.junit.internal.JUnitSystem;
import org.junit.internal.RealSystem;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

public class SVMJUnitRunner {

    public static class Options {
        @Option(help = "") //
        public static final HostedOptionKey TestFile = new HostedOptionKey<>("");
    }

    private final MxJUnitRequest request;

    @Platforms(Platform.HOSTED_ONLY.class)
    SVMJUnitRunner(FeatureAccess access) {
        MxJUnitRequest.Builder builder = new MxJUnitRequest.Builder() {

            @Override
            protected Class resolveClass(String name) throws ClassNotFoundException {
                Class ret = access.findClassByName(name);
                if (ret == null) {
                    throw new ClassNotFoundException(name);
                }
                return ret;
            }
        };

        try (BufferedReader br = new BufferedReader(new FileReader(Options.TestFile.getValue()))) {
            String buf;
            while ((buf = br.readLine()) != null) {
                builder.addTestSpec(buf);
            }
        } catch (Exception ex) {
            throw VMError.shouldNotReachHere(ex);
        }

        request = builder.build();
    }

    private void run(String[] args) {
        JUnitSystem system = new RealSystem();
        JUnitCore junitCore = new JUnitCore();
        system.out().println("SVMJUnitCore");
        system.out().println("JUnit version " + Version.id());

        MxJUnitConfig config = new MxJUnitConfig();

        int i = 0;
        while (i < args.length) {
            String arg = args[i++];
            switch (arg) {
                case "--verbose":
                    config.verbose = true;
                    break;
                case "--very-verbose":
                    config.veryVerbose = true;
                    break;
                case "--fail-fast":
                    config.failFast = true;
                    break;
                case "--enable-timing":
                    config.enableTiming = true;
                    break;
                case "--color":
                    config.color = true;
                    break;
                case "--gc-after-test":
                    config.gcAfterTest = true;
                    break;
                case "--repeat":
                    if (i < args.length) {
                        String count = args[i++];
                        try {
                            config.repeatCount = Integer.valueOf(count);
                        } catch (NumberFormatException ex) {
                            system.out().println("Invalid argument to --repeat, expected number, but got " + count);
                        }
                    } else {
                        system.out().println("Missing argument to --repeat");
                    }
                    break;
                case "--eager-stacktrace":
                    config.eagerStackTrace = true;
                    break;
                default:
                    system.out().println("Unknown command line argument: " + arg);
                    break;
            }
        }

        Result result = MxJUnitWrapper.runRequest(junitCore, system, config, request);
        System.exit(result.wasSuccessful() ? 0 : 1);
    }

    public static void main(String[] args) {
        ImageSingletons.lookup(SVMJUnitRunner.class).run(args);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy