1 /* 2 * Copyright (c) 2019, 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.Files; 30 import java.nio.file.Path; 31 import java.util.ArrayList; 32 import java.util.List; 33 34 import jdk.jfr.Recording; 35 import jdk.test.lib.Asserts; 36 37 /** 38 * @test 39 * @bug 8220657 40 * @key jfr 41 * @requires vm.hasJFR 42 * @library /test/lib /test/jdk 43 * @run main/othervm jdk.jfr.jcmd.TestJcmdDumpWithFileName 44 */ 45 public class TestJcmdDumpWithFileName { 46 47 private static File destination; 48 49 private static final String RECORD_NAME = "RecordingDumpTest"; 50 51 private static final File RECORD_DESTINATION = new File("test.jfr"); 52 53 public static void main(String[] args) throws Exception { 54 testJfrDump(null, null); 55 testJfrDump(RECORD_NAME, null); 56 testJfrDump(RECORD_NAME, new File("dump.jfr")); 57 } 58 59 private static File checkDefaultBehavior() throws Exception { 60 try (var files = Files.find(Path.of("."), 1, (p, a) -> p.toString().matches("^.*hotspot-pid-.*\\.jfr$") && (a.size() > 0L))) { 61 var result = files.findAny(); 62 Asserts.assertTrue(result.isPresent(), "Flight record does not exist"); 63 return result.get().toFile(); 64 } 65 } 66 67 private static void testJfrDump(String name, File file) throws Exception { 68 File dumpFile = (file == null) ? RECORD_DESTINATION : file; 69 70 try (Recording r = new Recording()) { 71 r.setName(RECORD_NAME); 72 r.setDestination(RECORD_DESTINATION.toPath()); 73 r.start(); 74 75 String[] params = buildParameters(name, file); 76 JcmdHelper.jcmd(params); 77 78 if ((name == null) && (file == null)) { 79 dumpFile = checkDefaultBehavior(); 80 } else { 81 Asserts.assertTrue(dumpFile.exists(), dumpFile.toString() + " does not exist"); 82 Asserts.assertGT(dumpFile.length(), 0L, "Flight record should be dumped to " + dumpFile.toString()); 83 } 84 } 85 86 dumpFile.delete(); 87 if (!RECORD_DESTINATION.equals(dumpFile)) { 88 RECORD_DESTINATION.delete(); 89 } 90 } 91 92 private static String[] buildParameters(String name, File file) { 93 List<String> params = new ArrayList<>(); 94 params.add("JFR.dump"); 95 if (name != null) { 96 params.add("name=" + name); 97 } 98 if (file != null) { 99 params.add("filename=" + file.toString()); 100 } 101 return params.toArray(new String[0]); 102 } 103 }