8158048: Fix failure message from jtreg gtest wrapper
authoriignatyev
Wed, 12 Jun 2019 11:48:14 -0700
changeset 55349 139f21bad9fd
parent 55348 d109188f6480
child 55350 1224b16c8ff4
8158048: Fix failure message from jtreg gtest wrapper Reviewed-by: kvn, mseledtsov
test/hotspot/jtreg/gtest/GTestResultParser.java
test/hotspot/jtreg/gtest/GTestWrapper.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/gtest/GTestResultParser.java	Wed Jun 12 11:48:14 2019 -0700
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2019, 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.
+ *
+ * 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.
+ */
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class GTestResultParser {
+    private final List<String> _failedTests;
+
+    public GTestResultParser(Path file) {
+        List<String> failedTests = new ArrayList<>();
+        try (Reader r = Files.newBufferedReader(file)) {
+            try {
+                XMLStreamReader xmlReader = XMLInputFactory.newInstance()
+                                                           .createXMLStreamReader(r);
+                String testSuite = null;
+                String testCase = null;
+                while (xmlReader.hasNext()) {
+                    switch (xmlReader.next()) {
+                        case XMLStreamConstants.START_ELEMENT:
+                            switch (xmlReader.getLocalName()) {
+                                case "testsuite":
+                                    testSuite = xmlReader.getAttributeValue("", "name");
+                                    break;
+                                case "testcase":
+                                    testCase = xmlReader.getAttributeValue("", "name");
+                                    break;
+                                case "failure":
+                                    failedTests.add(testSuite + "::" + testCase);
+                                default:
+                                    // ignore
+                            }
+                            break;
+                        default:
+                            // ignore
+                    }
+                }
+            } catch (XMLStreamException e) {
+                throw new IllegalArgumentException("can't open parse xml " + file, e);
+            }
+        } catch (IOException e) {
+            throw new IllegalArgumentException("can't open result file " + file, e);
+        }
+        _failedTests = Collections.unmodifiableList(failedTests);
+    }
+
+    public List<String> failedTests() {
+        return _failedTests;
+    }
+}
--- a/test/hotspot/jtreg/gtest/GTestWrapper.java	Wed Jun 12 10:45:46 2019 -0700
+++ b/test/hotspot/jtreg/gtest/GTestWrapper.java	Wed Jun 12 11:48:14 2019 -0700
@@ -25,23 +25,21 @@
  * @summary a jtreg wrapper for gtest tests
  * @library /test/lib
  * @modules java.base/jdk.internal.misc
+ *          java.xml
  * @run main/native GTestWrapper
  */
 
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Stream;
-import java.util.stream.Collectors;
-
-import java.io.File;
-import java.nio.file.Paths;
-import java.nio.file.Path;
-
 import jdk.test.lib.Platform;
 import jdk.test.lib.Utils;
 import jdk.test.lib.process.ProcessTools;
-import jdk.test.lib.process.OutputAnalyzer;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
 
 public class GTestWrapper {
     public static void main(String[] args) throws Throwable {
@@ -76,12 +74,33 @@
             env.put(pathVar, path + File.pathSeparator + ldLibraryPath);
         }
 
-        pb.command(new String[] {
-            execPath.toString(),
-            "-jdk",
-            System.getProperty("test.jdk")
-        });
-        ProcessTools.executeCommand(pb).shouldHaveExitValue(0);
+        Path resultFile = Paths.get("test_result.xml");
+        pb.command(execPath.toAbsolutePath().toString(),
+                "-jdk", Utils.TEST_JDK,
+                "--gtest_output=xml:" + resultFile);
+        int exitCode = ProcessTools.executeCommand(pb).getExitValue();
+        if (exitCode != 0) {
+            List<String> failedTests = failedTests(resultFile);
+            String message = "gtest execution failed; exit code = " + exitCode + ".";
+            if (!failedTests.isEmpty()) {
+                message += " the failed tests: " + failedTests;
+            }
+            throw new AssertionError(message);
+        }
+    }
+
+    private static List<String> failedTests(Path xml) {
+        if (!Files.exists(xml)) {
+            System.err.println("WARNING: test result file (" + xml + ") hasn't been found");
+        }
+
+        try {
+            return new GTestResultParser(xml).failedTests();
+        } catch (Throwable t) {
+            System.err.println("WARNING: failed to parse result file (" + xml + ") " + t);
+            t.printStackTrace();
+        }
+        return Collections.emptyList();
     }
 
     private static String getJVMVariantSubDir() {