Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/jps/JpsBase.java
38840 views
/*1* Copyright (c) 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 java.io.File;24import java.net.URL;25import java.util.List;2627import jdk.testlibrary.OutputAnalyzer;28import jdk.testlibrary.ProcessTools;2930/**31* The base class for testing the jps utility.32* The test sequence is to start jps with different combinations of arguments33* and verify the output contains proper values.34*/35public final class JpsBase {3637private static final String shortProcessName;38private static final String fullProcessName;3940/**41* The jps output should contain processes' names42* (except when jps is started in quite mode).43* The expected name of the test process is prepared here.44*/45static {46URL url = JpsBase.class.getResource("JpsBase.class");47boolean isJar = url.getProtocol().equals("jar");4849if (isJar) {50shortProcessName = JpsBase.class.getSimpleName() + ".jar";51String urlPath = url.getPath();52File jar = new File(urlPath.substring(urlPath.indexOf("file:") + 5, urlPath.indexOf("jar!") + 3));53fullProcessName = jar.getAbsolutePath();54} else {55shortProcessName = JpsBase.class.getSimpleName();56fullProcessName = JpsBase.class.getName();57}58}5960public static void main(String[] args) throws Exception {61int pid = ProcessTools.getProcessId();6263List<List<JpsHelper.JpsArg>> combinations = JpsHelper.JpsArg.generateCombinations();64for (List<JpsHelper.JpsArg> combination : combinations) {65OutputAnalyzer output = JpsHelper.jps(JpsHelper.JpsArg.asCmdArray(combination));66output.shouldHaveExitValue(0);6768boolean isQuiet = false;69boolean isFull = false;70String pattern;71for (JpsHelper.JpsArg jpsArg : combination) {72switch (jpsArg) {73case q:74// If '-q' is specified output should contain only a list of local VM identifiers:75// 3067376isQuiet = true;77JpsHelper.verifyJpsOutput(output, "^\\d+$");78output.shouldContain(Integer.toString(pid));79break;80case l:81// If '-l' is specified output should contain the full package name for the application's main class82// or the full path name to the application's JAR file:83// 30673 /tmp/jtreg/jtreg-workdir/scratch/JpsBase.jar ...84isFull = true;85pattern = "^" + pid + "\\s+" + replaceSpecialChars(fullProcessName) + ".*";86output.shouldMatch(pattern);87break;88case m:89// If '-m' is specified output should contain the arguments passed to the main method:90// 30673 JpsBase monkey ...91for (String arg : args) {92pattern = "^" + pid + ".*" + replaceSpecialChars(arg) + ".*";93output.shouldMatch(pattern);94}95break;96case v:97// If '-v' is specified output should contain VM arguments:98// 30673 JpsBase -Xmx512m -XX:+UseParallelGC -XX:Flags=/tmp/jtreg/jtreg-workdir/scratch/vmflags ...99for (String vmArg : JpsHelper.getVmArgs()) {100pattern = "^" + pid + ".*" + replaceSpecialChars(vmArg) + ".*";101output.shouldMatch(pattern);102}103break;104case V:105// If '-V' is specified output should contain VM flags:106// 30673 JpsBase +DisableExplicitGC ...107pattern = "^" + pid + ".*" + replaceSpecialChars(JpsHelper.VM_FLAG) + ".*";108output.shouldMatch(pattern);109break;110}111112if (isQuiet) {113break;114}115}116117if (!isQuiet) {118// Verify output line by line.119// Output should only contain lines with pids after the first line with pid.120JpsHelper.verifyJpsOutput(output, "^\\d+\\s+.*");121if (!isFull) {122pattern = "^" + pid + "\\s+" + replaceSpecialChars(shortProcessName);123if (combination.isEmpty()) {124// If no arguments are specified output should only contain125// pid and process name126pattern += "$";127} else {128pattern += ".*";129}130output.shouldMatch(pattern);131}132}133}134}135136private static String replaceSpecialChars(String str) {137String tmp = str.replace("\\", "\\\\");138tmp = tmp.replace("+", "\\+");139tmp = tmp.replace(".", "\\.");140return tmp;141}142143}144145146