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