test/jdk/jdk/jfr/jcmd/TestJcmdDumpGeneratedFilename.java
changeset 50745 a390cbb82d47
child 51214 67736b4846a0
equal deleted inserted replaced
50744:6c306d54366d 50745:a390cbb82d47
       
     1 /*
       
     2  * Copyright (c) 2015, 2018, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.jfr.jcmd;
       
    27 
       
    28 import java.io.File;
       
    29 import java.nio.file.Path;
       
    30 import java.nio.file.Paths;
       
    31 import java.util.Iterator;
       
    32 
       
    33 import jdk.jfr.Configuration;
       
    34 import jdk.jfr.Recording;
       
    35 import jdk.test.lib.jfr.FileHelper;
       
    36 import jdk.test.lib.process.OutputAnalyzer;
       
    37 
       
    38 /*
       
    39  * @test
       
    40  * @summary The test verifies JFR.dump command
       
    41  * @key jfr
       
    42  * @library /test/lib /test/jdk
       
    43  * @run main/othervm jdk.jfr.jcmd.TestJcmdDumpGeneratedFilename
       
    44  */
       
    45 public class TestJcmdDumpGeneratedFilename {
       
    46 
       
    47     public static void main(String[] args) throws Exception {
       
    48         // Increase the id for a recording
       
    49         for (int i = 0; i < 300; i++) {
       
    50             new Recording();
       
    51         }
       
    52         try (Recording r = new Recording(Configuration.getConfiguration("default"))) {
       
    53             r.start();
       
    54             r.stop();
       
    55             testDumpFilename();
       
    56             testDumpFilename(r);
       
    57             testDumpDiectory();
       
    58             testDumpDiectory(r);
       
    59         }
       
    60     }
       
    61 
       
    62     private static void testDumpFilename() throws Exception {
       
    63         OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump");
       
    64         verifyFile(readFilename(output), null);
       
    65     }
       
    66 
       
    67     private static void testDumpFilename(Recording r) throws Exception {
       
    68         OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump", "name=" + r.getId());
       
    69         verifyFile(readFilename(output), r.getId());
       
    70     }
       
    71 
       
    72     private static void testDumpDiectory() throws Exception {
       
    73         Path directory = Paths.get(".").toAbsolutePath().normalize();
       
    74         OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump", "filename=" + directory);
       
    75         String filename = readFilename(output);
       
    76         verifyFile(filename, null);
       
    77         verifyDirectory(filename, directory);
       
    78     }
       
    79 
       
    80     private static void testDumpDiectory(Recording r) throws Exception {
       
    81         Path directory = Paths.get(".").toAbsolutePath().normalize();
       
    82         OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump", "name=" + r.getId(), "filename=" + directory);
       
    83         String filename = readFilename(output);
       
    84         verifyFile(filename, r.getId());
       
    85         verifyDirectory(filename, directory);
       
    86     }
       
    87 
       
    88     private static void verifyDirectory(String filename, Path directory) throws Exception {
       
    89         if (!filename.contains(directory.toAbsolutePath().normalize().toString())) {
       
    90             throw new Exception("Expected dump to be at " + directory);
       
    91         }
       
    92     }
       
    93 
       
    94     private static void verifyFile(String filename, Long id) throws Exception {
       
    95         String idText = id == null ? "" : "-id-" + Long.toString(id);
       
    96         String expectedName = "hotspot-pid-" + ProcessHandle.current().pid() + idText;
       
    97         if (!filename.contains(expectedName)) {
       
    98             throw new Exception("Expected filename to contain " + expectedName);
       
    99         }
       
   100         FileHelper.verifyRecording(new File(filename));
       
   101     }
       
   102 
       
   103     private static String readFilename(OutputAnalyzer output) throws Exception {
       
   104         Iterator<String> it = output.asLines().iterator();
       
   105         while (it.hasNext()) {
       
   106             String line = it.next();
       
   107             if (line.contains("written to")) {
       
   108                 line = it.next(); // blank line
       
   109                 return it.next();
       
   110             }
       
   111         }
       
   112         throw new Exception("Could not find filename of dumped recording.");
       
   113     }
       
   114 }