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