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