Path: blob/master/test/jdk/sun/tools/jcmd/TestJcmdSanity.java
41149 views
/*1* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import static jdk.test.lib.Asserts.*;2425import java.io.File;26import java.io.IOException;27import java.nio.file.Files;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.util.List;3132import jdk.test.lib.process.OutputAnalyzer;33import jdk.test.lib.process.ProcessTools;34import jdk.test.lib.Utils;3536/*37* @test38* @bug 7104647 715482239* @summary Unit test for jcmd utility. The test will send different diagnostic40* command requests to the current java process.41*42* @library /test/lib43*44* @run main/othervm -XX:+UsePerfData TestJcmdSanity45*/46public class TestJcmdSanity {4748private static final String TEST_SRC = System.getProperty("test.src").trim();49private static final String[] VM_ARGS = new String[] { "-XX:+UsePerfData" };50private static final String JCMD_COMMAND_REGEX = "(\\w|\\.)*";51private static final String PERF_COUNTER_REGEX = "(\\w|\\.)*\\=.*";5253public static void main(String[] args) throws Exception {54testJcmdPidHelp();55testJcmdPidHelpHelp();56testJcmdPid_f();57testJcmdPidPerfCounterPrint();58testJcmdPidBigScript();59}6061/**62* jcmd -J-XX:+UsePerfData pid help63*/64private static void testJcmdPidHelp() throws Exception {65OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,66new String[] {"help"});6768output.shouldHaveExitValue(0);69output.shouldNotContain("Exception");70output.shouldContain(Long.toString(ProcessTools.getProcessId()) + ":");71matchJcmdCommands(output);72output.shouldContain("For more information about a specific command use 'help <command>'.");73}7475/**76* jcmd -J-XX:+UsePerfData pid help help77*/78private static void testJcmdPidHelpHelp() throws Exception {79OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,80new String[] {"help", "help"});8182output.shouldHaveExitValue(0);83verifyOutputAgainstFile(output);84}8586/**87* jcmd -J-XX:+UsePerfData pid PerfCounter.print88*/89private static void testJcmdPidPerfCounterPrint() throws Exception {90OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,91new String[] {"PerfCounter.print"});9293output.shouldHaveExitValue(0);94matchPerfCounters(output);95}9697/**98* jcmd -J-XX:+UsePerfData pid -f dcmd-script.txt99*/100private static void testJcmdPid_f() throws Exception {101File scrpitFile = new File(TEST_SRC, "dcmd-script.txt");102OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,103new String[] {"-f", scrpitFile.getAbsolutePath()});104105output.shouldHaveExitValue(0);106verifyOutputAgainstFile(output);107}108109/**110* Tests that it possible send a file over 1024 bytes large via jcmd -f.111*112* jcmd -J-XX:+UsePerfData pid -f dcmd-big-script.txt113*/114private static void testJcmdPidBigScript() throws Exception {115File scrpitFile = new File(TEST_SRC, "dcmd-big-script.txt");116OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,117new String[] {"-f", scrpitFile.getAbsolutePath()});118119output.shouldHaveExitValue(0);120output.shouldNotContain("Exception");121output.shouldContain(System.getProperty("java.vm.name").trim());122}123124/**125* Verifies the listed jcmd commands match a certain pattern.126*127* The output of the jcmd commands should look like:128* VM.uptime129* VM.flags130* VM.system_properties131*132* @param output The generated output from the jcmd.133* @throws Exception134*/135private static void matchJcmdCommands(OutputAnalyzer output) {136output.shouldMatchByLine(JCMD_COMMAND_REGEX,137"help",138JCMD_COMMAND_REGEX);139}140141/**142* Verifies the generated output from the PerfCounter.print command143* matches a certain pattern.144*145* The output of perf counters should look like:146* java.property.java.vm.name="Java HotSpot(TM) 64-Bit Server VM"147* java.threads.daemon=7148* sun.rt.javaCommand="com.sun.javatest.regtest.MainWrapper /tmp/jtreg/jtreg-workdir/classes/sun/tools/jcmd/TestJcmdSanity.jta"149*150* @param output The generated output from the PerfCounter.print command.151* @throws Exception152*/153private static void matchPerfCounters(OutputAnalyzer output) {154output.stdoutShouldMatchByLine(PERF_COUNTER_REGEX, null, PERF_COUNTER_REGEX);155output.stderrShouldBeEmptyIgnoreDeprecatedWarnings();156}157158private static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException {159Path path = Paths.get(TEST_SRC, "help_help.out");160List<String> fileOutput = Files.readAllLines(path);161List<String> outputAsLines = output.asLines();162assertTrue(outputAsLines.containsAll(fileOutput),163"The ouput should contain all content of " + path.toAbsolutePath());164}165166}167168169