Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/jcmd/TestJcmdSanity.java
38840 views
/*1* Copyright (c) 2011, 2014, 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.testlibrary.Asserts.*;2425import java.io.File;26import java.io.IOException;27import java.util.List;2829import jdk.testlibrary.JcmdBase;30import jdk.testlibrary.OutputAnalyzer;31import jdk.testlibrary.ProcessTools;32import jdk.testlibrary.Utils;3334/**35* Unit test for jcmd utility. The test will send different diagnostic command36* requests to the current java process.37*/38/*39* @test40* @bug 7104647 715482241* @library /lib/testlibrary42* @run main TestJcmdSanity43*/44public class TestJcmdSanity {4546private static final String TEST_SRC = System.getProperty("test.src").trim();47private static final String[] VM_ARGS = new String[] { "-XX:+UsePerfData" };48private static final String JCMD_COMMAND_REGEX = "(\\w|\\.)*";49private static final String PERF_COUNTER_REGEX = "(\\w|\\.)*\\=.*";5051public static void main(String[] args) throws Exception {52testJcmdPidHelp();53testJcmdPidHelpHelp();54testJcmdPid_f();55testJcmdPidPerfCounterPrint();56testJcmdPidBigScript();57}5859/**60* jcmd -J-XX:+UsePerfData pid help61*/62private static void testJcmdPidHelp() throws Exception {63OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,64new String[] {"help"});65output.shouldHaveExitValue(0);66output.shouldNotContain("Exception");67output.shouldContain(Integer.toString(ProcessTools.getProcessId()) + ":");68matchJcmdCommands(output);69output.shouldContain("For more information about a specific command use 'help <command>'.");70}7172/**73* jcmd -J-XX:+UsePerfData pid help help74*/75private static void testJcmdPidHelpHelp() throws Exception {76OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,77new String[] {"help", "help"});7879output.shouldHaveExitValue(0);80verifyOutputAgainstFile(output);81}8283/**84* jcmd -J-XX:+UsePerfData pid PerfCounter.print85*/86private static void testJcmdPidPerfCounterPrint() throws Exception {87OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,88new String[] {"PerfCounter.print"});8990output.shouldHaveExitValue(0);91matchPerfCounters(output);92}9394/**95* jcmd -J-XX:+UsePerfData pid -f dcmd-script.txt96*/97private static void testJcmdPid_f() throws Exception {98File scrpitFile = new File(TEST_SRC, "dcmd-script.txt");99OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,100new String[] {"-f", scrpitFile.getAbsolutePath()});101102output.shouldHaveExitValue(0);103verifyOutputAgainstFile(output);104}105106/**107* Tests that it possible send a file over 1024 bytes large via jcmd -f.108*109* jcmd -J-XX:+UsePerfData pid -f dcmd-big-script.txt110*/111private static void testJcmdPidBigScript() throws Exception {112File scrpitFile = new File(TEST_SRC, "dcmd-big-script.txt");113OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,114new String[] {"-f", scrpitFile.getAbsolutePath()});115116output.shouldHaveExitValue(0);117output.shouldNotContain("Exception");118output.shouldContain(System.getProperty("java.vm.name").trim());119}120121/**122* Verifies the listed jcmd commands match a certain pattern.123*124* The output of the jcmd commands should look like:125* VM.uptime126* VM.flags127* VM.system_properties128*129* @param output The generated output from the jcmd.130* @throws Exception131*/132private static void matchJcmdCommands(OutputAnalyzer output) throws Exception {133int matchedCount = output.shouldMatchByLine(JCMD_COMMAND_REGEX,134"help",135JCMD_COMMAND_REGEX);136assertGreaterThan(matchedCount , 0,137"Found no lines matching pattern: " + JCMD_COMMAND_REGEX);138}139140/**141* Verifies the generated output from the PerfCounter.print command142* matches a certain pattern.143*144* The output of perf counters should look like:145* java.property.java.vm.name="Java HotSpot(TM) 64-Bit Server VM"146* java.threads.daemon=7147* sun.rt.javaCommand="com.sun.javatest.regtest.MainWrapper /tmp/jtreg/jtreg-workdir/classes/sun/tools/jcmd/TestJcmdSanity.jta"148*149* @param output The generated output from the PerfCounter.print command.150* @throws Exception151*/152private static void matchPerfCounters(OutputAnalyzer output) throws Exception {153int matchedCount = output.shouldMatchByLineFrom(PERF_COUNTER_REGEX,154PERF_COUNTER_REGEX);155assertGreaterThan(matchedCount , 0,156"Found no lines matching pattern: " + PERF_COUNTER_REGEX);157}158159private static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException {160File file = new File(TEST_SRC, "help_help.out");161List<String> fileOutput = Utils.fileAsList(file);162List<String> outputAsLines = output.asLines();163assertTrue(outputAsLines.containsAll(fileOutput),164"The ouput should contain all content of " + file.getAbsolutePath());165}166167}168169170