Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/tools/jps/JpsHelper.java
41152 views
1
/*
2
* Copyright (c) 2014, 2020, 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.test.lib.Asserts.assertGreaterThan;
25
import static jdk.test.lib.Asserts.assertTrue;
26
27
import java.io.BufferedWriter;
28
import java.io.File;
29
import java.io.FileWriter;
30
import java.io.IOException;
31
import java.nio.file.Files;
32
import java.nio.file.Path;
33
import java.nio.file.Paths;
34
import java.util.ArrayList;
35
import java.util.Arrays;
36
import java.util.List;
37
38
import jdk.test.lib.JDKToolLauncher;
39
import jdk.test.lib.process.OutputAnalyzer;
40
import jdk.test.lib.process.ProcessTools;
41
import jdk.test.lib.Asserts;
42
import jdk.test.lib.Utils;
43
44
/**
45
* The helper class for running jps utility and verifying output from it
46
*/
47
public final class JpsHelper {
48
49
/**
50
* Helper class for handling jps arguments
51
*/
52
public enum JpsArg {
53
q,
54
l,
55
m,
56
v,
57
V;
58
59
/**
60
* Generate all possible combinations of {@link JpsArg}
61
* (31 argument combinations and no arguments case)
62
*/
63
public static List<List<JpsArg>> generateCombinations() {
64
final int argCount = JpsArg.values().length;
65
// If there are more than 30 args this algorithm will overflow.
66
Asserts.assertLessThan(argCount, 31, "Too many args");
67
68
List<List<JpsArg>> combinations = new ArrayList<>();
69
int combinationCount = (int) Math.pow(2, argCount);
70
for (int currCombo = 0; currCombo < combinationCount; ++currCombo) {
71
List<JpsArg> combination = new ArrayList<>();
72
for (int position = 0; position < argCount; ++position) {
73
int bit = 1 << position;
74
if ((bit & currCombo) != 0) {
75
combination.add(JpsArg.values()[position]);
76
}
77
}
78
combinations.add(combination);
79
}
80
return combinations;
81
}
82
83
/**
84
* Return combination of {@link JpsArg} as a String array
85
*/
86
public static String[] asCmdArray(List<JpsArg> jpsArgs) {
87
List<String> list = new ArrayList<>();
88
for (JpsArg jpsArg : jpsArgs) {
89
list.add("-" + jpsArg.toString());
90
}
91
return list.toArray(new String[list.size()]);
92
}
93
94
}
95
96
/**
97
* VM flag to start test application with
98
*/
99
public static final String VM_FLAG = "+DisableExplicitGC";
100
101
private static File vmFlagsFile = null;
102
/**
103
* VM arguments to start test application with.
104
* -XX:+UsePerfData is required for running the tests on embedded platforms.
105
*/
106
private static String[] testVmArgs = {
107
"-XX:+UsePerfData", "-Xmx512m", "-Xlog:gc",
108
"-Dmultiline.prop=value1\nvalue2\r\nvalue3",
109
null /* lazily initialized -XX:Flags */};
110
private static File manifestFile = null;
111
112
/**
113
* Create a file containing VM_FLAG in the working directory
114
*/
115
public static File getVmFlagsFile() throws IOException {
116
if (vmFlagsFile == null) {
117
vmFlagsFile = new File("vmflags");
118
try (BufferedWriter output = new BufferedWriter(new FileWriter(vmFlagsFile))) {
119
output.write(VM_FLAG);
120
}
121
vmFlagsFile.deleteOnExit();
122
}
123
return vmFlagsFile;
124
}
125
126
/**
127
* Return a list of VM arguments
128
*/
129
public static String[] getVmArgs() throws IOException {
130
if (testVmArgs[testVmArgs.length - 1] == null) {
131
testVmArgs[testVmArgs.length - 1] = "-XX:Flags=" + getVmFlagsFile().getAbsolutePath();
132
}
133
return testVmArgs;
134
}
135
136
/**
137
* Start jps utility without any arguments
138
*/
139
public static OutputAnalyzer jps() throws Exception {
140
return jps(null, null);
141
}
142
143
/**
144
* Start jps utility with tool arguments
145
*/
146
public static OutputAnalyzer jps(String... toolArgs) throws Exception {
147
return jps(null, Arrays.asList(toolArgs));
148
}
149
150
/**
151
* Start jps utility with VM args and tool arguments
152
*/
153
public static OutputAnalyzer jps(List<String> vmArgs, List<String> toolArgs) throws Exception {
154
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jps");
155
launcher.addVMArgs(Utils.getFilteredTestJavaOpts("-XX:+UsePerfData"));
156
launcher.addVMArg("-XX:+UsePerfData");
157
if (vmArgs != null) {
158
for (String vmArg : vmArgs) {
159
launcher.addVMArg(vmArg);
160
}
161
}
162
if (toolArgs != null) {
163
for (String toolArg : toolArgs) {
164
launcher.addToolArg(toolArg);
165
}
166
}
167
168
ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
169
System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
170
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
171
System.out.println(output.getOutput());
172
173
return output;
174
}
175
176
/**
177
* Verify jps stdout contains only pids and programs' name information.
178
* jps stderr may contain VM warning messages which will be ignored.
179
*
180
* The output can look like:
181
* 35536 Jps
182
* 35417 Main
183
* 31103 org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
184
*/
185
public static void verifyJpsOutput(OutputAnalyzer output, String regex) {
186
output.shouldHaveExitValue(0);
187
output.stdoutShouldMatchByLine(regex);
188
output.stderrShouldNotMatch("[E|e]xception");
189
output.stderrShouldNotMatch("[E|e]rror");
190
}
191
192
/**
193
* Compare jps output with a content in a file line by line
194
*/
195
public static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException {
196
String testSrc = System.getProperty("test.src", "?");
197
Path path = Paths.get(testSrc, "usage.out");
198
List<String> fileOutput = Files.readAllLines(path);
199
List<String> outputAsLines = output.asLines();
200
assertTrue(outputAsLines.containsAll(fileOutput),
201
"The ouput should contain all content of " + path.toAbsolutePath());
202
}
203
204
public static void runJpsVariants(Long pid, String processName, String fullProcessName, String argument) throws Exception {
205
System.out.printf("INFO: user.dir: '%s''\n", System.getProperty("user.dir"));
206
List<List<JpsHelper.JpsArg>> combinations = JpsHelper.JpsArg.generateCombinations();
207
for (List<JpsHelper.JpsArg> combination : combinations) {
208
OutputAnalyzer output = JpsHelper.jps(JpsHelper.JpsArg.asCmdArray(combination));
209
output.shouldHaveExitValue(0);
210
211
boolean isQuiet = false;
212
boolean isFull = false;
213
String pattern;
214
for (JpsHelper.JpsArg jpsArg : combination) {
215
switch (jpsArg) {
216
case q:
217
// If '-q' is specified output should contain only a list of local VM identifiers:
218
// 30673
219
isQuiet = true;
220
JpsHelper.verifyJpsOutput(output, "^\\d+$");
221
output.shouldContain(Long.toString(pid));
222
break;
223
case l:
224
// If '-l' is specified output should contain the full package name for the application's main class
225
// or the full path name to the application's JAR file:
226
// 30673 /tmp/jtreg/jtreg-workdir/scratch/LingeredAppForJps.jar ...
227
isFull = true;
228
pattern = "^" + pid + "\\s+" + replaceSpecialChars(fullProcessName) + ".*";
229
output.shouldMatch(pattern);
230
break;
231
case m:
232
// If '-m' is specified output should contain the arguments passed to the main method:
233
// 30673 LingeredAppForJps lockfilename ...
234
pattern = "^" + pid + ".*" + replaceSpecialChars(argument) + ".*";
235
output.shouldMatch(pattern);
236
break;
237
case v:
238
// If '-v' is specified output should contain VM arguments:
239
// 30673 LingeredAppForJps -Xmx512m -XX:+UseParallelGC -XX:Flags=/tmp/jtreg/jtreg-workdir/scratch/vmflags ...
240
for (String vmArg : JpsHelper.getVmArgs()) {
241
pattern = "^" + pid + ".*" + replaceSpecialChars(vmArg) + ".*";
242
output.shouldMatch(pattern);
243
}
244
break;
245
case V:
246
// If '-V' is specified output should contain VM flags:
247
// 30673 LingeredAppForJps +DisableExplicitGC ...
248
pattern = "^" + pid + ".*" + replaceSpecialChars(JpsHelper.VM_FLAG) + ".*";
249
output.shouldMatch(pattern);
250
break;
251
}
252
253
if (isQuiet) {
254
break;
255
}
256
}
257
258
if (!isQuiet) {
259
// Verify output line by line.
260
// Output should only contain lines with pids after the first line with pid.
261
JpsHelper.verifyJpsOutput(output, "^\\d+\\s+.*");
262
if (!isFull) {
263
pattern = "^" + pid + "\\s+" + replaceSpecialChars(processName);
264
if (combination.isEmpty()) {
265
// If no arguments are specified output should only contain
266
// pid and process name
267
pattern += "$";
268
} else {
269
pattern += ".*";
270
}
271
output.shouldMatch(pattern);
272
}
273
}
274
}
275
}
276
277
private static String replaceSpecialChars(String str) {
278
String tmp = str.replace("\\", "\\\\");
279
tmp = tmp.replace("+", "\\+");
280
tmp = tmp.replace(".", "\\.");
281
tmp = tmp.replace("\n", "\\\\n");
282
tmp = tmp.replace("\r", "\\\\r");
283
return tmp;
284
}
285
}
286
287