1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @summary a jtreg wrapper for gtest tests
  26  * @library /test/lib
  27  * @modules java.base/jdk.internal.misc
  28  *          java.xml
  29  * @run main/native GTestWrapper
  30  */
  31 
  32 import jdk.test.lib.Platform;
  33 import jdk.test.lib.Utils;
  34 import jdk.test.lib.process.ProcessTools;
  35 
  36 import java.io.File;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.Collections;
  41 import java.util.List;
  42 import java.util.Map;
  43 
  44 public class GTestWrapper {
  45     public static void main(String[] args) throws Throwable {
  46         // gtestLauncher is located in <test_image>/hotspot/gtest/<vm_variant>/
  47         // nativePath points either to <test_image>/hotspot/jtreg/native or to <test_image>/hotspot/gtest
  48         Path nativePath = Paths.get(System.getProperty("test.nativepath"));
  49         String jvmVariantDir = getJVMVariantSubDir();
  50         // let's assume it's <test_image>/hotspot/gtest
  51         Path path = nativePath.resolve(jvmVariantDir);
  52         if (!path.toFile().exists()) {
  53             // maybe it is <test_image>/hotspot/jtreg/native
  54             path = nativePath.getParent()
  55                              .getParent()
  56                              .resolve("gtest")
  57                              .resolve(jvmVariantDir);
  58         }
  59         if (!path.toFile().exists()) {
  60             throw new Error("TESTBUG: the library has not been found in " + nativePath);
  61         }
  62 
  63         Path execPath = path.resolve("gtestLauncher" + (Platform.isWindows() ? ".exe" : ""));
  64         ProcessBuilder pb = new ProcessBuilder();
  65         Map<String, String> env = pb.environment();
  66 
  67         // The GTestWrapper was started using the normal java launcher, which
  68         // may have set LD_LIBRARY_PATH or LIBPATH to point to the jdk libjvm. In
  69         // that case, prepend the path with the location of the gtest library."
  70 
  71         String pathVar = Platform.sharedLibraryPathVariableName();
  72         String ldLibraryPath = System.getenv(pathVar);
  73         if (ldLibraryPath != null) {
  74             env.put(pathVar, path + File.pathSeparator + ldLibraryPath);
  75         }
  76 
  77         Path resultFile = Paths.get("test_result.xml");
  78         pb.command(execPath.toAbsolutePath().toString(),
  79                 "-jdk", Utils.TEST_JDK,
  80                 "--gtest_output=xml:" + resultFile);
  81         int exitCode = ProcessTools.executeCommand(pb).getExitValue();
  82         if (exitCode != 0) {
  83             List<String> failedTests = failedTests(resultFile);
  84             String message = "gtest execution failed; exit code = " + exitCode + ".";
  85             if (!failedTests.isEmpty()) {
  86                 message += " the failed tests: " + failedTests;
  87             }
  88             throw new AssertionError(message);
  89         }
  90     }
  91 
  92     private static List<String> failedTests(Path xml) {
  93         if (!Files.exists(xml)) {
  94             System.err.println("WARNING: test result file (" + xml + ") hasn't been found");
  95         }
  96 
  97         try {
  98             return new GTestResultParser(xml).failedTests();
  99         } catch (Throwable t) {
 100             System.err.println("WARNING: failed to parse result file (" + xml + ") " + t);
 101             t.printStackTrace();
 102         }
 103         return Collections.emptyList();
 104     }
 105 
 106     private static String getJVMVariantSubDir() {
 107         if (Platform.isServer()) {
 108             return "server";
 109         } else if (Platform.isClient()) {
 110             return "client";
 111         } else if (Platform.isMinimal()) {
 112             return "minimal";
 113         } else {
 114             throw new Error("TESTBUG: unsuppported vm variant");
 115         }
 116     }
 117 }