< prev index next >

test/sun/tools/jmap/BasicJMapTest.java

Print this page
rev 11744 : 8059047: Extract parser/validator from jhat for use in tests
   1 /*
   2  * Copyright (c) 2005, 2014, 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 import java.io.File;
  25 import java.util.Arrays;
  26 
  27 import static jdk.testlibrary.Asserts.*;
  28 import jdk.testlibrary.JDKToolLauncher;
  29 import jdk.testlibrary.OutputAnalyzer;
  30 import jdk.testlibrary.ProcessTools;
  31 
  32 /*
  33  * @test
  34  * @bug 6321286
  35  * @summary Unit test for jmap utility
  36  * @library /lib/testlibrary

  37  * @build jdk.testlibrary.*




  38  * @run main BasicJMapTest
  39  */
  40 public class BasicJMapTest {
  41 
  42     private static ProcessBuilder processBuilder = new ProcessBuilder();
  43 
  44     public static void main(String[] args) throws Exception {
  45         testHisto();
  46         testHistoLive();
  47         testDump();
  48         testDumpLive();
  49     }
  50 
  51     private static void testHisto() throws Exception {
  52         OutputAnalyzer output = jmap("-histo");
  53         output.shouldHaveExitValue(0);
  54     }
  55 
  56     private static void testHistoLive() throws Exception {
  57         OutputAnalyzer output = jmap("-histo:live");
  58         output.shouldHaveExitValue(0);
  59     }
  60 
  61     private static void testDump() throws Exception {
  62         File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
  63         OutputAnalyzer output = jmap("-dump:format=b,file=" + dump.getName());
  64         output.shouldHaveExitValue(0);
  65         output.shouldContain("Heap dump file created");
  66         verifyDumpFile(dump);

  67     }
  68 
  69     private static void testDumpLive() throws Exception {
  70         File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
  71         OutputAnalyzer output = jmap("-dump:live,format=b,file=" + dump.getName());
  72         output.shouldHaveExitValue(0);
  73         output.shouldContain("Heap dump file created");
  74         verifyDumpFile(dump);

  75     }
  76 
  77     private static void verifyDumpFile(File dump) {
  78         assertTrue(dump.exists() && dump.isFile(), "Could not create dump file");
  79         dump.delete();





  80     }
  81 
  82     private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
  83         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
  84         launcher.addVMArg("-XX:+UsePerfData");
  85         if (toolArgs != null) {
  86             for (String toolArg : toolArgs) {
  87                 launcher.addToolArg(toolArg);
  88             }
  89         }
  90         launcher.addToolArg(Integer.toString(ProcessTools.getProcessId()));
  91 
  92         processBuilder.command(launcher.getCommand());
  93         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
  94         OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
  95         System.out.println(output.getOutput());
  96 
  97         return output;
  98     }
  99 
   1 /*
   2  * Copyright (c) 2005, 2015, 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 import static jdk.testlibrary.Asserts.assertTrue;
  25 import static jdk.testlibrary.Asserts.fail;
  26 
  27 import java.io.File;
  28 import java.util.Arrays;
  29 
  30 import jdk.test.lib.hprof.HprofParser;
  31 import jdk.testlibrary.JDKToolLauncher;
  32 import jdk.testlibrary.OutputAnalyzer;
  33 import jdk.testlibrary.ProcessTools;
  34 
  35 /*
  36  * @test
  37  * @bug 6321286
  38  * @summary Unit test for jmap utility
  39  * @library /lib/testlibrary
  40  * @library /../../test/lib/share/classes
  41  * @build jdk.testlibrary.*
  42  * @build jdk.test.lib.hprof.*
  43  * @build jdk.test.lib.hprof.module.*
  44  * @build jdk.test.lib.hprof.parser.*
  45  * @build jdk.test.lib.hprof.utils.*
  46  * @run main BasicJMapTest
  47  */
  48 public class BasicJMapTest {
  49 
  50     private static ProcessBuilder processBuilder = new ProcessBuilder();
  51 
  52     public static void main(String[] args) throws Exception {
  53         testHisto();
  54         testHistoLive();
  55         testDump();
  56         testDumpLive();
  57     }
  58 
  59     private static void testHisto() throws Exception {
  60         OutputAnalyzer output = jmap("-histo");
  61         output.shouldHaveExitValue(0);
  62     }
  63 
  64     private static void testHistoLive() throws Exception {
  65         OutputAnalyzer output = jmap("-histo:live");
  66         output.shouldHaveExitValue(0);
  67     }
  68 
  69     private static void testDump() throws Exception {
  70         File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
  71         OutputAnalyzer output = jmap("-dump:format=b,file=" + dump.getName());
  72         output.shouldHaveExitValue(0);
  73         output.shouldContain("Heap dump file created");
  74         verifyDumpFile(dump);
  75         dump.delete();
  76     }
  77 
  78     private static void testDumpLive() throws Exception {
  79         File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
  80         OutputAnalyzer output = jmap("-dump:live,format=b,file=" + dump.getName());
  81         output.shouldHaveExitValue(0);
  82         output.shouldContain("Heap dump file created");
  83         verifyDumpFile(dump);
  84         dump.delete();
  85     }
  86 
  87     private static void verifyDumpFile(File dump) {
  88         assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
  89         try {
  90             HprofParser.parse(dump);
  91         } catch (Exception e) {
  92             e.printStackTrace();
  93             fail("Could not parse dump file " + dump.getAbsolutePath());
  94         }
  95     }
  96 
  97     private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
  98         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
  99         launcher.addVMArg("-XX:+UsePerfData");
 100         if (toolArgs != null) {
 101             for (String toolArg : toolArgs) {
 102                 launcher.addToolArg(toolArg);
 103             }
 104         }
 105         launcher.addToolArg(Integer.toString(ProcessTools.getProcessId()));
 106 
 107         processBuilder.command(launcher.getCommand());
 108         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
 109         OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
 110         System.out.println(output.getOutput());
 111 
 112         return output;
 113     }
 114 
< prev index next >