Path: blob/master/test/hotspot/jtreg/gtest/GTestWrapper.java
41144 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary a jtreg wrapper for gtest tests25* @library /test/lib26* @modules java.base/jdk.internal.misc27* java.xml28* @requires vm.flagless29* @run main/native GTestWrapper30*/3132import jdk.test.lib.Platform;33import jdk.test.lib.Utils;34import jdk.test.lib.process.ProcessTools;3536import java.io.File;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.util.ArrayList;41import java.util.Collections;42import java.util.List;43import java.util.Map;4445public class GTestWrapper {46public static void main(String[] args) throws Throwable {47// gtestLauncher is located in <test_image>/hotspot/gtest/<vm_variant>/48// nativePath points either to <test_image>/hotspot/jtreg/native or to <test_image>/hotspot/gtest49Path nativePath = Paths.get(Utils.TEST_NATIVE_PATH);50String jvmVariantDir = getJVMVariantSubDir();51// let's assume it's <test_image>/hotspot/gtest52Path path = nativePath.resolve(jvmVariantDir);53if (!path.toFile().exists()) {54// maybe it is <test_image>/hotspot/jtreg/native55path = nativePath.getParent()56.getParent()57.resolve("gtest")58.resolve(jvmVariantDir);59}60if (!path.toFile().exists()) {61throw new Error("TESTBUG: the library has not been found in " + nativePath + ". Did you forget to use --with-gtest to configure?");62}6364Path execPath = path.resolve("gtestLauncher" + (Platform.isWindows() ? ".exe" : ""));65ProcessBuilder pb = new ProcessBuilder();66Map<String, String> env = pb.environment();6768// The GTestWrapper was started using the normal java launcher, which69// may have set LD_LIBRARY_PATH or LIBPATH to point to the jdk libjvm. In70// that case, prepend the path with the location of the gtest library."7172String pathVar = Platform.sharedLibraryPathVariableName();73String ldLibraryPath = System.getenv(pathVar);74if (ldLibraryPath != null) {75env.put(pathVar, path + File.pathSeparator + ldLibraryPath);76}7778Path resultFile = Paths.get("test_result.xml");7980ArrayList<String> command = new ArrayList<>();81command.add(execPath.toAbsolutePath().toString());82command.add("-jdk");83command.add(Utils.TEST_JDK);84command.add("--gtest_output=xml:" + resultFile);85command.add("--gtest_catch_exceptions=0" + resultFile);86for (String a : args) {87command.add(a);88}89pb.command(command);90int exitCode = ProcessTools.executeCommand(pb).getExitValue();91if (exitCode != 0) {92List<String> failedTests = failedTests(resultFile);93String message = "gtest execution failed; exit code = " + exitCode + ".";94if (!failedTests.isEmpty()) {95message += " the failed tests: " + failedTests;96}97throw new AssertionError(message);98}99}100101private static List<String> failedTests(Path xml) {102if (!Files.exists(xml)) {103System.err.println("WARNING: test result file (" + xml + ") hasn't been found");104}105106try {107return new GTestResultParser(xml).failedTests();108} catch (Throwable t) {109System.err.println("WARNING: failed to parse result file (" + xml + ") " + t);110t.printStackTrace();111}112return Collections.emptyList();113}114115private static String getJVMVariantSubDir() {116if (Platform.isServer()) {117return "server";118} else if (Platform.isClient()) {119return "client";120} else if (Platform.isMinimal()) {121return "minimal";122} else if (Platform.isZero()) {123return "zero";124} else {125throw new Error("TESTBUG: unsupported vm variant");126}127}128}129130131