Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/jcmd/TestJcmdDefaults.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.Utils;3233/**34* Unit test for jcmd utility. Tests jcmd options which do not send35* requests to a specific JVM process.36*/37/*38* @test39* @bug 710464740* @library /lib/testlibrary41* @run main TestJcmdDefaults42*/43public class TestJcmdDefaults {4445private static final String TEST_SRC = System.getProperty("test.src").trim();46private static final String[] VM_ARGS = new String[] { "-XX:+UsePerfData" };47private static final String JCMD_LIST_REGEX = "^\\d+\\s*.*";4849public static void main(String[] args) throws Exception {50testJcmdUsage("-h");51testJcmdUsage("-help");52testJcmdDefaults();53testJcmdDefaults("-l");54}5556/**57* jcmd -J-XX:+UsePerfData -h58* jcmd -J-XX:+UsePerfData -help59*/60private static void testJcmdUsage(String... jcmdArgs) throws Exception {61OutputAnalyzer output = JcmdBase.jcmdNoPid(VM_ARGS, jcmdArgs);6263assertNotEquals(output.getExitValue(), 0);64verifyOutputAgainstFile(output);65}6667/**68* jcmd -J-XX:+UsePerfData69* jcmd -J-XX:+UsePerfData -l70*/71private static void testJcmdDefaults(String... jcmdArgs) throws Exception {72OutputAnalyzer output = JcmdBase.jcmdNoPid(VM_ARGS, jcmdArgs);7374output.shouldHaveExitValue(0);75output.shouldContain("sun.tools.jcmd.JCmd");76matchListedProcesses(output);77}7879/**80* Verifies the listed processes match a certain pattern.81*82* The output should look like:83* 12246 sun.tools.jcmd.JCmd84* 24428 com.sun.javatest.regtest.MainWrapper /tmp/jtreg/jtreg-workdir/classes/sun/tools/jcmd/TestJcmdDefaults.jta85*86* @param output The generated output from the jcmd.87* @throws Exception88*/89private static void matchListedProcesses(OutputAnalyzer output) throws Exception {90int matchedCount = output.shouldMatchByLine(JCMD_LIST_REGEX);91assertGreaterThan(matchedCount , 0,92"Found no lines matching pattern: " + JCMD_LIST_REGEX);93}9495private static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException {96File file = new File(TEST_SRC, "usage.out");97List<String> fileOutput = Utils.fileAsList(file);98List<String> outputAsLines = output.asLines();99assertTrue(outputAsLines.containsAll(fileOutput),100"The ouput should contain all content of " + file.getAbsolutePath());101}102103}104105106