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  * @run main BasicJMapTest
  38  */
  39 public class BasicJMapTest {
  40 
  41     private static ProcessBuilder processBuilder = new ProcessBuilder();
  42 
  43     public static void main(String[] args) throws Exception {
  44         testHisto();
  45         testHistoLive();
  46         testDump();
  47         testDumpLive();
  48     }
  49 
  50     private static void testHisto() throws Exception {
  51         OutputAnalyzer output = jmap("-histo");
  52         output.shouldHaveExitValue(0);
  53     }
  54 
  55     private static void testHistoLive() throws Exception {
  56         OutputAnalyzer output = jmap("-histo:live");
  57         output.shouldHaveExitValue(0);
  58     }
  59 
  60     private static void testDump() throws Exception {
  61         File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
  62         OutputAnalyzer output = jmap("-dump:format=b,file=" + dump.getName());
  63         output.shouldHaveExitValue(0);
  64         output.shouldContain("Heap dump file created");
  65         verifyDumpFile(dump);
  66     }
  67 
  68     private static void testDumpLive() throws Exception {
  69         File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
  70         OutputAnalyzer output = jmap("-dump:live,format=b,file=" + dump.getName());
  71         output.shouldHaveExitValue(0);
  72         output.shouldContain("Heap dump file created");
  73         verifyDumpFile(dump);
  74     }
  75 
  76     private static void verifyDumpFile(File dump) {
  77         assertTrue(dump.exists() && dump.isFile(), "Could not create dump file");
  78         dump.delete();
  79     }
  80 
  81     private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
  82         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
  83         launcher.addVMArg("-XX:+UsePerfData");
  84         if (toolArgs != null) {
  85             for (String toolArg : toolArgs) {
  86                 launcher.addToolArg(toolArg);
  87             }
  88         }
  89         launcher.addToolArg(Integer.toString(ProcessTools.getProcessId()));
  90 
  91         processBuilder.command(launcher.getCommand());
  92         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
  93         OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());
  94         System.out.println(output.getOutput());
  95 
  96         return output;
  97     }
  98 
  99 }