1 /* 2 * Copyright (c) 2011, 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. 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.*; 25 26 import java.io.File; 27 import java.io.IOException; 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import java.util.List; 32 33 import jdk.test.lib.process.OutputAnalyzer; 34 import jdk.test.lib.process.ProcessTools; 35 import jdk.testlibrary.Utils; 36 37 /* 38 * @test 39 * @bug 7104647 7154822 40 * @summary Unit test for jcmd utility. The test will send different diagnostic 41 * command requests to the current java process. 42 * 43 * @library /lib/testlibrary 44 * @library /test/lib 45 * 46 * @build jdk.testlibrary.* 47 * @run main/othervm -XX:+UsePerfData TestJcmdSanity 48 */ 49 public class TestJcmdSanity { 50 51 private static final String TEST_SRC = System.getProperty("test.src").trim(); 52 private static final String[] VM_ARGS = new String[] { "-XX:+UsePerfData" }; 53 private static final String JCMD_COMMAND_REGEX = "(\\w|\\.)*"; 54 private static final String PERF_COUNTER_REGEX = "(\\w|\\.)*\\=.*"; 55 56 public static void main(String[] args) throws Exception { 57 testJcmdPidHelp(); 58 testJcmdPidHelpHelp(); 59 testJcmdPid_f(); 60 testJcmdPidPerfCounterPrint(); 61 testJcmdPidBigScript(); 62 } 63 64 /** 65 * jcmd -J-XX:+UsePerfData pid help 66 */ 67 private static void testJcmdPidHelp() throws Exception { 68 OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS, 69 new String[] {"help"}); 70 71 output.shouldHaveExitValue(0); 72 output.shouldNotContain("Exception"); 73 output.shouldContain(Long.toString(ProcessTools.getProcessId()) + ":"); 74 matchJcmdCommands(output); 75 output.shouldContain("For more information about a specific command use 'help <command>'."); 76 } 77 78 /** 79 * jcmd -J-XX:+UsePerfData pid help help 80 */ 81 private static void testJcmdPidHelpHelp() throws Exception { 82 OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS, 83 new String[] {"help", "help"}); 84 85 output.shouldHaveExitValue(0); 86 verifyOutputAgainstFile(output); 87 } 88 89 /** 90 * jcmd -J-XX:+UsePerfData pid PerfCounter.print 91 */ 92 private static void testJcmdPidPerfCounterPrint() throws Exception { 93 OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS, 94 new String[] {"PerfCounter.print"}); 95 96 output.shouldHaveExitValue(0); 97 matchPerfCounters(output); 98 } 99 100 /** 101 * jcmd -J-XX:+UsePerfData pid -f dcmd-script.txt 102 */ 103 private static void testJcmdPid_f() throws Exception { 104 File scrpitFile = new File(TEST_SRC, "dcmd-script.txt"); 105 OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS, 106 new String[] {"-f", scrpitFile.getAbsolutePath()}); 107 108 output.shouldHaveExitValue(0); 109 verifyOutputAgainstFile(output); 110 } 111 112 /** 113 * Tests that it possible send a file over 1024 bytes large via jcmd -f. 114 * 115 * jcmd -J-XX:+UsePerfData pid -f dcmd-big-script.txt 116 */ 117 private static void testJcmdPidBigScript() throws Exception { 118 File scrpitFile = new File(TEST_SRC, "dcmd-big-script.txt"); 119 OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS, 120 new String[] {"-f", scrpitFile.getAbsolutePath()}); 121 122 output.shouldHaveExitValue(0); 123 output.shouldNotContain("Exception"); 124 output.shouldContain(System.getProperty("java.vm.name").trim()); 125 } 126 127 /** 128 * Verifies the listed jcmd commands match a certain pattern. 129 * 130 * The output of the jcmd commands should look like: 131 * VM.uptime 132 * VM.flags 133 * VM.system_properties 134 * 135 * @param output The generated output from the jcmd. 136 * @throws Exception 137 */ 138 private static void matchJcmdCommands(OutputAnalyzer output) throws Exception { 139 int matchedCount = output.shouldMatchByLine(JCMD_COMMAND_REGEX, 140 "help", 141 JCMD_COMMAND_REGEX); 142 assertGreaterThan(matchedCount , 0, 143 "Found no lines matching pattern: " + JCMD_COMMAND_REGEX); 144 } 145 146 /** 147 * Verifies the generated output from the PerfCounter.print command 148 * matches a certain pattern. 149 * 150 * The output of perf counters should look like: 151 * java.property.java.vm.name="Java HotSpot(TM) 64-Bit Server VM" 152 * java.threads.daemon=7 153 * sun.rt.javaCommand="com.sun.javatest.regtest.MainWrapper /tmp/jtreg/jtreg-workdir/classes/sun/tools/jcmd/TestJcmdSanity.jta" 154 * 155 * @param output The generated output from the PerfCounter.print command. 156 * @throws Exception 157 */ 158 private static void matchPerfCounters(OutputAnalyzer output) throws Exception { 159 int matchedCount = output.shouldMatchByLineFrom(PERF_COUNTER_REGEX, 160 PERF_COUNTER_REGEX); 161 assertGreaterThan(matchedCount , 0, 162 "Found no lines matching pattern: " + PERF_COUNTER_REGEX); 163 } 164 165 private static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException { 166 Path path = Paths.get(TEST_SRC, "help_help.out"); 167 List<String> fileOutput = Files.readAllLines(path); 168 List<String> outputAsLines = output.asLines(); 169 assertTrue(outputAsLines.containsAll(fileOutput), 170 "The ouput should contain all content of " + path.toAbsolutePath()); 171 } 172 173 }