test/jdk/sun/tools/jmap/BasicJMapTest.java
changeset 47216 71c04702a3d5
parent 40686 1b855fa5c9c5
child 51675 b487c1e914d0
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2005, 2016, 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  * @summary Unit test for jmap utility
       
    38  * @key intermittent
       
    39  * @library /lib/testlibrary
       
    40  * @library /test/lib
       
    41  * @build jdk.testlibrary.*
       
    42  * @build jdk.test.lib.hprof.*
       
    43  * @build jdk.test.lib.hprof.model.*
       
    44  * @build jdk.test.lib.hprof.parser.*
       
    45  * @build jdk.test.lib.hprof.util.*
       
    46  * @run main/timeout=240 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         testFinalizerInfo();
       
    56         testClstats();
       
    57         testDump();
       
    58         testDumpLive();
       
    59     }
       
    60 
       
    61     private static void testHisto() throws Exception {
       
    62         OutputAnalyzer output = jmap("-histo");
       
    63         output.shouldHaveExitValue(0);
       
    64     }
       
    65 
       
    66     private static void testHistoLive() throws Exception {
       
    67         OutputAnalyzer output = jmap("-histo:live");
       
    68         output.shouldHaveExitValue(0);
       
    69     }
       
    70 
       
    71     private static void testFinalizerInfo() throws Exception {
       
    72         OutputAnalyzer output = jmap("-finalizerinfo");
       
    73         output.shouldHaveExitValue(0);
       
    74     }
       
    75 
       
    76     private static void testClstats() throws Exception {
       
    77         OutputAnalyzer output = jmap("-clstats");
       
    78         output.shouldHaveExitValue(0);
       
    79     }
       
    80 
       
    81     private static void testDump() throws Exception {
       
    82         dump(false);
       
    83     }
       
    84 
       
    85     private static void testDumpLive() throws Exception {
       
    86         dump(true);
       
    87     }
       
    88 
       
    89     private static void dump(boolean live) throws Exception {
       
    90         File dump = new File("jmap.dump." + System.currentTimeMillis() + ".hprof");
       
    91         if (dump.exists()) {
       
    92             dump.delete();
       
    93         }
       
    94         OutputAnalyzer output;
       
    95         if (live) {
       
    96             output = jmap("-dump:live,format=b,file=" + dump.getName());
       
    97         } else {
       
    98             output = jmap("-dump:format=b,file=" + dump.getName());
       
    99         }
       
   100         output.shouldHaveExitValue(0);
       
   101         output.shouldContain("Heap dump file created");
       
   102         verifyDumpFile(dump);
       
   103         dump.delete();
       
   104     }
       
   105 
       
   106     private static void verifyDumpFile(File dump) {
       
   107         assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
       
   108         try {
       
   109             HprofParser.parse(dump);
       
   110         } catch (Exception e) {
       
   111             e.printStackTrace();
       
   112             fail("Could not parse dump file " + dump.getAbsolutePath());
       
   113         }
       
   114     }
       
   115 
       
   116     private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
       
   117         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
       
   118         if (toolArgs != null) {
       
   119             for (String toolArg : toolArgs) {
       
   120                 launcher.addToolArg(toolArg);
       
   121             }
       
   122         }
       
   123         launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
       
   124 
       
   125         processBuilder.command(launcher.getCommand());
       
   126         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
       
   127         OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
       
   128         System.out.println(output.getOutput());
       
   129 
       
   130         return output;
       
   131     }
       
   132 
       
   133 }