Path: blob/master/test/jdk/sun/tools/jcmd/TestJcmdDefaults.java
41149 views
/*1* Copyright (c) 2011, 2020, 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.test.lib.Asserts.*;2425import java.io.IOException;26import java.nio.file.Files;27import java.nio.file.Path;28import java.nio.file.Paths;29import java.util.List;3031import jdk.test.lib.process.OutputAnalyzer;32import jdk.test.lib.Utils;3334/*35* @test36* @bug 710464737* @summary Unit test for jcmd utility. Tests jcmd options which do not send38* requests to a specific JVM process.39*40* @library /test/lib41*42* @run main TestJcmdDefaults43*/44public class TestJcmdDefaults {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_LIST_REGEX = "(?s)^\\d+\\s*.*";4950public static void main(String[] args) throws Exception {51testJcmdUsage("-?");52testJcmdUsage("-h");53testJcmdUsage("--help");54testJcmdDefaults();55testJcmdDefaults("-l");56}5758/**59* jcmd -J-XX:+UsePerfData -?60* jcmd -J-XX:+UsePerfData -h61* jcmd -J-XX:+UsePerfData --help62*/63private static void testJcmdUsage(String... jcmdArgs) throws Exception {64OutputAnalyzer output = JcmdBase.jcmdNoPid(VM_ARGS, jcmdArgs);6566assertEquals(output.getExitValue(), 0);67verifyOutputAgainstFile(output);68}6970/**71* jcmd -J-XX:+UsePerfData72* jcmd -J-XX:+UsePerfData -l73*/74private static void testJcmdDefaults(String... jcmdArgs) throws Exception {75OutputAnalyzer output = JcmdBase.jcmdNoPid(VM_ARGS, jcmdArgs);7677output.shouldHaveExitValue(0);78output.shouldContain("sun.tools.jcmd.JCmd");79matchListedProcesses(output);80}8182/**83* Verifies the listed processes match a certain pattern.84*85* The output should look like:86* 12246 sun.tools.jcmd.JCmd87* 24428 com.sun.javatest.regtest.MainWrapper /tmp/jtreg/jtreg-workdir/classes/sun/tools/jcmd/TestJcmdDefaults.jta88*89* @param output The generated output from the jcmd.90*/91private static void matchListedProcesses(OutputAnalyzer output) {92output.stdoutShouldMatchByLine(JCMD_LIST_REGEX);93output.stderrShouldBeEmptyIgnoreDeprecatedWarnings();94}9596private static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException {97Path path = Paths.get(TEST_SRC, "usage.out");98List<String> fileOutput = Files.readAllLines(path);99List<String> outputAsLines = output.asLines();100assertTrue(outputAsLines.containsAll(fileOutput),101"The ouput should contain all content of " + path.toAbsolutePath());102}103104}105106107