8059047: Extract parser/validator from jhat for use in tests
authorykantser
Tue, 28 Apr 2015 14:33:32 +0200
changeset 30659 f7d610c0c95d
parent 30361 76bb3472a9dd
child 30660 f7067154c7a4
8059047: Extract parser/validator from jhat for use in tests Reviewed-by: sla
jdk/test/com/sun/management/HotSpotDiagnosticMXBean/DumpHeap.java
jdk/test/com/sun/management/HotSpotDiagnosticMXBean/DumpHeap.sh
jdk/test/sun/tools/jmap/BasicJMapTest.java
--- a/jdk/test/com/sun/management/HotSpotDiagnosticMXBean/DumpHeap.java	Mon Apr 27 09:02:48 2015 -0700
+++ b/jdk/test/com/sun/management/HotSpotDiagnosticMXBean/DumpHeap.java	Tue Apr 28 14:33:32 2015 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2015, 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
@@ -21,22 +21,55 @@
  * questions.
  */
 
-/*
- *
- * @bug 6455258
- * @summary Sanity test for com.sun.management.HotSpotDiagnosticMXBean.dumpHeap
- *          method
- */
+import static jdk.testlibrary.Asserts.assertTrue;
+import static jdk.testlibrary.Asserts.fail;
 
+import java.io.File;
 import java.lang.management.*;
 import java.util.List;
-import javax.management.MBeanServer;
+
+import jdk.test.lib.hprof.HprofParser;
+import jdk.testlibrary.ProcessTools;
+
 import com.sun.management.HotSpotDiagnosticMXBean;
 
+/*
+ * @test
+ * @bug 6455258
+ * @summary Sanity test for com.sun.management.HotSpotDiagnosticMXBean.dumpHeap method
+ * @library /lib/testlibrary
+ * @library /../../test/lib/share/classes
+ * @build jdk.testlibrary.*
+ * @build jdk.test.lib.hprof.*
+ * @build jdk.test.lib.hprof.module.*
+ * @build jdk.test.lib.hprof.parser.*
+ * @build jdk.test.lib.hprof.utils.*
+ * @run main DumpHeap
+ */
 public class DumpHeap {
-    public static void main(String[] argv) throws Exception {
-         List<HotSpotDiagnosticMXBean> list = ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
-         System.out.println("Dumping to file: " + argv[0] + " ....");
-         list.get(0).dumpHeap(argv[0], true);
+
+    public static void main(String[] args) throws Exception {
+        List<HotSpotDiagnosticMXBean> list = ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
+        File dump = new File(ProcessTools.getProcessId() + ".hprof");
+        if (dump.exists()) {
+            dump.delete();
+        }
+        System.out.println("Dumping to file: " + dump.getAbsolutePath());
+        list.get(0).dumpHeap(dump.getAbsolutePath(), true);
+
+        verifyDumpFile(dump);
+
+        dump.delete();
     }
+
+    private static void verifyDumpFile(File dump) {
+        assertTrue(dump.exists() && dump.isFile(), "Could not create dump file");
+        try {
+            HprofParser.parse(dump);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail("Could not parse dump file");
+        }
+    }
+
 }
--- a/jdk/test/com/sun/management/HotSpotDiagnosticMXBean/DumpHeap.sh	Mon Apr 27 09:02:48 2015 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-#!/bin/sh
-
-#
-# Copyright (c) 2006, 2007, 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.
-#
-
-
-# @test
-# @bug 6455258
-# @summary Sanity test for com.sun.management.HotSpotDiagnosticMXBean.dumpHeap 
-#          method 
-#
-# @build DumpHeap
-# @run shell DumpHeap.sh
-
-if [ "${TESTJAVA}" = "" ] ; then
-     echo "--Error: TESTJAVA must be defined as the pathname of a jdk to test."
-     exit 1
-fi
-
-if [ "${COMPILEJAVA}" = "" ] ; then
-    COMPILEJAVA="${TESTJAVA}"
-fi
-
-failed=0
-
-# we use the pid of this shell process to name the heap dump output file.
-DUMPFILE="java_pid$$.hprof"
-
-${TESTJAVA}/bin/java ${TESTVMOPTS} -classpath $TESTCLASSES \
-    DumpHeap ${DUMPFILE} || exit 2
-
-# check that heap dump is parsable
-${COMPILEJAVA}/bin/jhat ${TESTTOOLVMOPTS} -parseonly true ${DUMPFILE}
-if [ $? != 0 ]; then failed=1; fi
-
-# dump file is large so remove it
-rm ${DUMPFILE}
-
-exit $failed
--- a/jdk/test/sun/tools/jmap/BasicJMapTest.java	Mon Apr 27 09:02:48 2015 -0700
+++ b/jdk/test/sun/tools/jmap/BasicJMapTest.java	Tue Apr 28 14:33:32 2015 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2015, 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
@@ -21,10 +21,13 @@
  * questions.
  */
 
+import static jdk.testlibrary.Asserts.assertTrue;
+import static jdk.testlibrary.Asserts.fail;
+
 import java.io.File;
 import java.util.Arrays;
 
-import static jdk.testlibrary.Asserts.*;
+import jdk.test.lib.hprof.HprofParser;
 import jdk.testlibrary.JDKToolLauncher;
 import jdk.testlibrary.OutputAnalyzer;
 import jdk.testlibrary.ProcessTools;
@@ -34,7 +37,12 @@
  * @bug 6321286
  * @summary Unit test for jmap utility
  * @library /lib/testlibrary
+ * @library /../../test/lib/share/classes
  * @build jdk.testlibrary.*
+ * @build jdk.test.lib.hprof.*
+ * @build jdk.test.lib.hprof.module.*
+ * @build jdk.test.lib.hprof.parser.*
+ * @build jdk.test.lib.hprof.utils.*
  * @run main BasicJMapTest
  */
 public class BasicJMapTest {
@@ -59,24 +67,38 @@
     }
 
     private static void testDump() throws Exception {
-        File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
-        OutputAnalyzer output = jmap("-dump:format=b,file=" + dump.getName());
-        output.shouldHaveExitValue(0);
-        output.shouldContain("Heap dump file created");
-        verifyDumpFile(dump);
+        dump(false);
     }
 
     private static void testDumpLive() throws Exception {
-        File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
-        OutputAnalyzer output = jmap("-dump:live,format=b,file=" + dump.getName());
+        dump(true);
+    }
+
+    private static void dump(boolean live) throws Exception {
+        File dump = new File("jmap.dump." + System.currentTimeMillis() + ".hprof");
+        if (dump.exists()) {
+            dump.delete();
+        }
+        OutputAnalyzer output;
+        if (live) {
+            output = jmap("-dump:live,format=b,file=" + dump.getName());
+        } else {
+            output = jmap("-dump:format=b,file=" + dump.getName());
+        }
         output.shouldHaveExitValue(0);
         output.shouldContain("Heap dump file created");
         verifyDumpFile(dump);
+        dump.delete();
     }
 
     private static void verifyDumpFile(File dump) {
-        assertTrue(dump.exists() && dump.isFile(), "Could not create dump file");
-        dump.delete();
+        assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
+        try {
+            HprofParser.parse(dump);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail("Could not parse dump file " + dump.getAbsolutePath());
+        }
     }
 
     private static OutputAnalyzer jmap(String... toolArgs) throws Exception {