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