8059047: Extract parser/validator from jhat for use in tests
authorykantser
Tue, 28 Apr 2015 14:36:31 +0200
changeset 30597 2f4c912c6a22
parent 30294 e16e14bd116a
child 30598 d50cc6db5e4c
8059047: Extract parser/validator from jhat for use in tests Reviewed-by: sla
hotspot/test/serviceability/dcmd/gc/HeapDumpTest.java
--- a/hotspot/test/serviceability/dcmd/gc/HeapDumpTest.java	Wed Apr 29 02:35:29 2015 +0200
+++ b/hotspot/test/serviceability/dcmd/gc/HeapDumpTest.java	Tue Apr 28 14:36:31 2015 +0200
@@ -24,7 +24,13 @@
 import org.testng.annotations.Test;
 import org.testng.Assert;
 
+import java.io.File;
+import java.nio.file.Files;
 import java.io.IOException;
+import java.util.List;
+
+import jdk.test.lib.hprof.HprofParser;
+import jdk.test.lib.hprof.model.Snapshot;
 
 import com.oracle.java.testlibrary.JDKToolFinder;
 import com.oracle.java.testlibrary.OutputAnalyzer;
@@ -35,59 +41,56 @@
  * @test
  * @summary Test of diagnostic command GC.heap_dump
  * @library /testlibrary
+ * @library /../../test/lib/share/classes
  * @modules java.base/sun.misc
  *          java.compiler
  *          java.management
  *          jdk.jvmstat/sun.jvmstat.monitor
  * @build com.oracle.java.testlibrary.*
  * @build com.oracle.java.testlibrary.dcmd.*
+ * @build jdk.test.lib.hprof.*
+ * @build jdk.test.lib.hprof.module.*
+ * @build jdk.test.lib.hprof.parser.*
+ * @build jdk.test.lib.hprof.utils.*
  * @run testng HeapDumpTest
  */
 public class HeapDumpTest {
     protected String heapDumpArgs = "";
 
-    public void run(CommandExecutor executor) {
-        String fileName = "jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof";
-        String cmd = "GC.heap_dump " + heapDumpArgs + " " + fileName;
+    public void run(CommandExecutor executor) throws IOException {
+        File dump = new File("jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof");
+        if (dump.exists()) {
+            dump.delete();
+        }
+
+        String cmd = "GC.heap_dump " + heapDumpArgs + " " + dump.getAbsolutePath();
         executor.execute(cmd);
 
-        verifyHeapDump(fileName);
+        dump.delete();
     }
 
-    private void verifyHeapDump(String fileName) {
-        String jhat = JDKToolFinder.getJDKTool("jhat");
-        String[] cmd = { jhat, "-parseonly", "true", fileName };
-
-        ProcessBuilder pb = new ProcessBuilder(cmd);
-        pb.redirectErrorStream(true);
-        Process p = null;
-        OutputAnalyzer output = null;
-
+    private void verifyHeapDump(File dump) {
+        Assert.assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
         try {
-            p = pb.start();
-            output = new OutputAnalyzer(p);
+            File out = HprofParser.parse(dump);
 
-            /*
-             * Some hprof dumps of all objects contain constantPoolOop references that cannot be resolved, so we ignore
-             * failures about resolving constantPoolOop fields using a negative lookahead
-             */
-            output.shouldNotMatch(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*");
-        } catch (IOException e) {
-            Assert.fail("Test error: Caught exception while reading stdout/err of jhat", e);
-        } finally {
-            if (p != null) {
-                p.destroy();
+            Assert.assertTrue(out != null && out.exists() && out.isFile(), "Could not find hprof parser output file");
+            List<String> lines = Files.readAllLines(out.toPath());
+            Assert.assertTrue(lines.size() > 0, "hprof parser output file is empty");
+            for (String line : lines) {
+                Assert.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*"));
             }
-        }
 
-        if (output.getExitValue() != 0) {
-            Assert.fail("Test error: jhat exit code was nonzero");
+            out.delete();
+        } catch (Exception e) {
+            e.printStackTrace();
+            Assert.fail("Could not parse dump file " + dump.getAbsolutePath());
         }
     }
 
     /* GC.heap_dump is not available over JMX, running jcmd pid executor instead */
     @Test
-    public void pid() {
+    public void pid() throws IOException {
         run(new PidJcmdExecutor());
     }
 }