/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.jfr.jcmd; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import jdk.jfr.Recording; import jdk.test.lib.Asserts; /** * @test * @bug 8220657 * @key jfr * @requires vm.hasJFR * @library /test/lib /test/jdk * @run main/othervm jdk.jfr.jcmd.TestJcmdDumpWithFileName */ public class TestJcmdDumpWithFileName { private static File destination; private static final String RECORD_NAME = "RecordingDumpTest"; private static final File RECORD_DESTINATION = new File("test.jfr"); public static void main(String[] args) throws Exception { testJfrDump(null, null); testJfrDump(RECORD_NAME, null); testJfrDump(RECORD_NAME, new File("dump.jfr")); } private static File checkDefaultBehavior() throws Exception { try (var files = Files.find(Path.of("."), 1, (p, a) -> p.toString().matches("^.*hotspot-pid-.*\\.jfr$") && (a.size() > 0L))) { var result = files.findAny(); Asserts.assertTrue(result.isPresent(), "Flight record does not exist"); return result.get().toFile(); } } private static void testJfrDump(String name, File file) throws Exception { File dumpFile = (file == null) ? RECORD_DESTINATION : file; try (Recording r = new Recording()) { r.setName(RECORD_NAME); r.setDestination(RECORD_DESTINATION.toPath()); r.start(); String[] params = buildParameters(name, file); JcmdHelper.jcmd(params); if ((name == null) && (file == null)) { dumpFile = checkDefaultBehavior(); } else { Asserts.assertTrue(dumpFile.exists(), dumpFile.toString() + " does not exist"); Asserts.assertGT(dumpFile.length(), 0L, "Flight record should be dumped to " + dumpFile.toString()); } } dumpFile.delete(); if (!RECORD_DESTINATION.equals(dumpFile)) { RECORD_DESTINATION.delete(); } } private static String[] buildParameters(String name, File file) { List params = new ArrayList<>(); params.add("JFR.dump"); if (name != null) { params.add("name=" + name); } if (file != null) { params.add("filename=" + file.toString()); } return params.toArray(new String[0]); } }