Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/launcher/HelpFlagsTest.java
41145 views
1
/*
2
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2018, 2020 SAP SE. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*/
24
25
/**
26
* @test
27
* @summary Validate and test -?, -h and --help flags. All tools in the jdk
28
* should take the same flags to display the help message. These
29
* flags should be documented in the printed help message. The
30
* tool should quit without error code after displaying the
31
* help message (if there is no other problem with the command
32
* line).
33
* Also check that tools that used to accept -help still do
34
* so. Test that tools that never accepted -help don't do so
35
* in future. I.e., check that the tool returns with the same
36
* return code as called with an invalid flag, and does not
37
* print anything containing '-help' in that case.
38
* @compile HelpFlagsTest.java
39
* @run main HelpFlagsTest
40
*/
41
42
import java.io.File;
43
44
public class HelpFlagsTest extends TestHelper {
45
46
// Tools that should not be tested because a usage message is pointless.
47
static final String[] TOOLS_NOT_TO_TEST = {
48
"appletviewer", // deprecated, don't test
49
"jaccessinspector", // gui, don't test, win only
50
"jaccesswalker", // gui, don't test, win only
51
"jconsole", // gui, don't test
52
"servertool", // none. Shell, don't test.
53
"javaw", // don't test, win only
54
// These shall have a help message that resembles that of
55
// MIT's tools. Thus -?, -h and --help are supported, but not
56
// mentioned in the help text.
57
"kinit",
58
"klist",
59
"ktab",
60
// Oracle proprietary tools without help message.
61
"javacpl",
62
"jmc",
63
"jweblauncher",
64
"jcontrol",
65
"ssvagent"
66
};
67
68
// Lists which tools support which flags.
69
private static class ToolHelpSpec {
70
String toolname;
71
72
// How the flags supposed to be supported are handled.
73
//
74
// These flags are supported, i.e.,
75
// * the tool accepts the flag
76
// * the tool prints a help message if the flag is specified
77
// * this help message lists the flag
78
// * the tool exits with exit code '0'.
79
boolean supportsQuestionMark;
80
boolean supportsH;
81
boolean supportsHelp;
82
83
// One tool returns with exit code != '0'.
84
int exitcodeOfHelp;
85
86
// How legacy -help is handled.
87
//
88
// Tools that so far support -help should still do so, but
89
// not print documentation about it. Tools that do not
90
// support -help should not do so in future.
91
//
92
// The tools accepts legacy -help. -help should not be
93
// documented in the usage message.
94
boolean supportsLegacyHelp;
95
96
// Java itself documents -help. -help prints to stderr,
97
// while --help prints to stdout. Leave as is.
98
boolean documentsLegacyHelp;
99
100
// The exit code of the tool if an invalid argument is passed to it.
101
// An exit code != 0 would be expected, but not all tools handle it
102
// that way.
103
int exitcodeOfWrongFlag;
104
105
ToolHelpSpec(String n, int q, int h, int hp, int ex1, int l, int dl, int ex2) {
106
toolname = n;
107
supportsQuestionMark = ( q == 1 ? true : false );
108
supportsH = ( h == 1 ? true : false );
109
supportsHelp = ( hp == 1 ? true : false );
110
exitcodeOfHelp = ex1;
111
112
supportsLegacyHelp = ( l == 1 ? true : false );
113
documentsLegacyHelp = ( dl == 1 ? true : false );
114
exitcodeOfWrongFlag = ex2;
115
}
116
}
117
118
static ToolHelpSpec[] jdkTools = {
119
// name -? -h --help exitcode -help -help exitcode
120
// of help docu of wrong
121
// mented flag
122
new ToolHelpSpec("jabswitch", 0, 0, 0, 0, 0, 0, 0), // /?, prints help message anyways, win only
123
new ToolHelpSpec("jar", 1, 1, 1, 0, 0, 0, 1), // -?, -h, --help
124
new ToolHelpSpec("jarsigner", 1, 1, 1, 0, 1, 0, 1), // -?, -h, --help, -help accepted but not documented.
125
new ToolHelpSpec("java", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
126
new ToolHelpSpec("javac", 1, 0, 1, 0, 1, 1, 2), // -?, --help -help, Documents -help, -h is already taken for "native header output directory".
127
new ToolHelpSpec("javadoc", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
128
new ToolHelpSpec("javap", 1, 1, 1, 0, 1, 1, 2), // -?, -h, --help -help, Documents -help
129
new ToolHelpSpec("javaw", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help, win only
130
new ToolHelpSpec("jcmd", 1, 1, 1, 0, 1, 0, 1), // -?, -h, --help, -help accepted but not documented.
131
new ToolHelpSpec("jdb", 1, 1, 1, 0, 1, 1, 0), // -?, -h, --help -help, Documents -help
132
new ToolHelpSpec("jdeprscan", 1, 1, 1, 0, 0, 0, 1), // -?, -h, --help
133
new ToolHelpSpec("jdeps", 1, 1, 1, 0, 1, 0, 2), // -?, -h, --help, -help accepted but not documented.
134
new ToolHelpSpec("jfr", 1, 1, 1, 0, 0, 0, 2), // -?, -h, --help
135
new ToolHelpSpec("jhsdb", 0, 0, 0, 0, 0, 0, 0), // none, prints help message anyways.
136
new ToolHelpSpec("jimage", 1, 1, 1, 0, 0, 0, 2), // -?, -h, --help
137
new ToolHelpSpec("jinfo", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
138
new ToolHelpSpec("jjs", 0, 1, 1, 100, 0, 0, 100), // -h, --help, return code 100
139
new ToolHelpSpec("jlink", 1, 1, 1, 0, 0, 0, 2), // -?, -h, --help
140
new ToolHelpSpec("jmap", 1, 1, 1, 0, 1, 0, 1), // -?, -h, --help, -help accepted but not documented.
141
new ToolHelpSpec("jmod", 1, 1, 1, 0, 1, 0, 2), // -?, -h, --help, -help accepted but not documented.
142
new ToolHelpSpec("jps", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
143
new ToolHelpSpec("jrunscript", 1, 1, 1, 0, 1, 1, 7), // -?, -h, --help -help, Documents -help
144
new ToolHelpSpec("jshell", 1, 1, 1, 0, 1, 0, 1), // -?, -h, --help, -help accepted but not documented.
145
new ToolHelpSpec("jstack", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
146
new ToolHelpSpec("jstat", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
147
new ToolHelpSpec("jstatd", 1, 1, 1, 0, 0, 0, 1), // -?, -h, --help
148
new ToolHelpSpec("keytool", 1, 1, 1, 0, 1, 0, 1), // none, prints help message anyways.
149
new ToolHelpSpec("rmiregistry", 0, 0, 0, 0, 0, 0, 1), // none, prints help message anyways.
150
new ToolHelpSpec("serialver", 0, 0, 0, 0, 0, 0, 1), // none, prints help message anyways.
151
new ToolHelpSpec("jpackage", 0, 1, 1, 0, 0, 1, 1), // -h, --help,
152
};
153
154
// Returns corresponding object from jdkTools array.
155
static ToolHelpSpec getToolHelpSpec(String tool) {
156
for (ToolHelpSpec x : jdkTools) {
157
if (tool.toLowerCase().equals(x.toolname) ||
158
tool.toLowerCase().equals(x.toolname + ".exe"))
159
return x;
160
}
161
return null;
162
}
163
164
// Check whether 'flag' appears in 'line' as a word of itself. It must not
165
// be a substring of a word, as then similar flags might be matched.
166
// E.g.: --help matches in the documentation of --help-extra.
167
// This works only with english locale, as some tools have translated
168
// usage messages.
169
static boolean findFlagInLine(String line, String flag) {
170
if (line.contains(flag) &&
171
!line.contains("nknown") && // Some tools say 'Unknown option "<flag>"',
172
!line.contains("invalid flag") && // 'invalid flag: <flag>'
173
!line.contains("invalid option") && // or 'invalid option: <flag>'. Skip that.
174
!line.contains("FileNotFoundException: -help") && // Special case for idlj.
175
!line.contains("-h requires an argument") && // Special case for javac.
176
!line.contains("port argument,")) { // Special case for rmiregistry.
177
// There might be several appearances of 'flag' in
178
// 'line'. (-h as substring of --help).
179
int flagLen = flag.length();
180
int lineLen = line.length();
181
for (int i = line.indexOf(flag); i >= 0; i = line.indexOf(flag, i+1)) {
182
// There should be a space before 'flag' in 'line', or it's right at the beginning.
183
if (i > 0 &&
184
line.charAt(i-1) != ' ' &&
185
line.charAt(i-1) != '[' && // jarsigner
186
line.charAt(i-1) != '|' && // jstatd
187
line.charAt(i-1) != '\t') { // jjs
188
continue;
189
}
190
// There should be a space or comma after 'flag' in 'line', or it's just at the end.
191
int posAfter = i + flagLen;
192
if (posAfter < lineLen &&
193
line.charAt(posAfter) != ' ' &&
194
line.charAt(posAfter) != ',' &&
195
line.charAt(posAfter) != '[' && // jar
196
line.charAt(posAfter) != ']' && // jarsigner
197
line.charAt(posAfter) != ')' && // jfr
198
line.charAt(posAfter) != '|' && // jstatd
199
line.charAt(posAfter) != ':' && // jps
200
line.charAt(posAfter) != '"') { // keytool
201
continue;
202
}
203
return true;
204
}
205
}
206
return false;
207
}
208
209
static TestResult runToolWithFlag(File f, String flag) {
210
String x = f.getAbsolutePath();
211
TestResult tr = doExec(x, flag);
212
System.out.println("Testing " + f.getName());
213
System.out.println("#> " + x + " " + flag);
214
tr.testOutput.forEach(System.out::println);
215
System.out.println("#> echo $?");
216
System.out.println(tr.exitValue);
217
218
return tr;
219
}
220
221
// Checks whether tool supports flag 'flag' and documents it
222
// in the help message.
223
static String testTool(File f, String flag, int exitcode) {
224
String result = "";
225
TestResult tr = runToolWithFlag(f, flag);
226
227
// Check that the tool accepted the flag.
228
if (exitcode == 0 && !tr.isOK()) {
229
System.out.println("failed");
230
result = "failed: " + f.getName() + " " + flag + " has exit code " + tr.exitValue + ".\n";
231
}
232
233
// Check there is a help message listing the flag.
234
boolean foundFlag = false;
235
for (String y : tr.testOutput) {
236
if (!foundFlag && findFlagInLine(y, flag)) { // javac
237
foundFlag = true;
238
System.out.println("Found documentation of '" + flag + "': '" + y.trim() +"'");
239
}
240
}
241
if (!foundFlag) {
242
result += "failed: " + f.getName() + " does not document " +
243
flag + " in help message.\n";
244
}
245
246
if (!result.isEmpty())
247
System.out.println(result);
248
249
return result;
250
}
251
252
// Test the tool supports legacy option -help, but does
253
// not document it.
254
static String testLegacyFlag(File f, int exitcode) {
255
String result = "";
256
TestResult tr = runToolWithFlag(f, "-help");
257
258
// Check that the tool accepted the flag.
259
if (exitcode == 0 && !tr.isOK()) {
260
System.out.println("failed");
261
result = "failed: " + f.getName() + " -help has exit code " + tr.exitValue + ".\n";
262
}
263
264
// Check there is _no_ documentation of -help.
265
boolean foundFlag = false;
266
for (String y : tr.testOutput) {
267
if (!foundFlag && findFlagInLine(y, "-help")) { // javac
268
foundFlag = true;
269
System.out.println("Found documentation of '-help': '" + y.trim() +"'");
270
}
271
}
272
if (foundFlag) {
273
result += "failed: " + f.getName() + " does document -help " +
274
"in help message. This legacy flag should not be documented.\n";
275
}
276
277
if (!result.isEmpty())
278
System.out.println(result);
279
280
return result;
281
}
282
283
// Test that the tool exits with the exit code expected for
284
// invalid flags. In general, one would expect this to be != 0,
285
// but currently a row of tools exit with 0 in this case.
286
// The output should not ask to get help with flag '-help'.
287
static String testInvalidFlag(File f, String flag, int exitcode, boolean documentsLegacyHelp) {
288
String result = "";
289
TestResult tr = runToolWithFlag(f, flag);
290
291
// Check that the tool did exit with the expected return code.
292
if (!((exitcode == tr.exitValue) ||
293
// Windows reports -1 where unix reports 255.
294
(tr.exitValue < 0 && exitcode == tr.exitValue + 256))) {
295
System.out.println("failed");
296
result = "failed: " + f.getName() + " " + flag + " should not be " +
297
"accepted. But it has exit code " + tr.exitValue + ".\n";
298
}
299
300
if (!documentsLegacyHelp) {
301
// Check there is _no_ documentation of -help.
302
boolean foundFlag = false;
303
for (String y : tr.testOutput) {
304
if (!foundFlag && findFlagInLine(y, "-help")) { // javac
305
foundFlag = true;
306
System.out.println("Found documentation of '-help': '" + y.trim() +"'");
307
}
308
}
309
if (foundFlag) {
310
result += "failed: " + f.getName() + " does document -help " +
311
"in error message. This legacy flag should not be documented.\n";
312
}
313
}
314
315
if (!result.isEmpty())
316
System.out.println(result);
317
318
return result;
319
}
320
321
public static void main(String[] args) {
322
String errorMessage = "";
323
324
// The test analyses the help messages printed. It assumes englisch
325
// help messages. Thus it only works with english locale.
326
if (!isEnglishLocale()) { return; }
327
328
for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(TOOLS_NOT_TO_TEST))) {
329
String toolName = f.getName();
330
331
ToolHelpSpec tool = getToolHelpSpec(toolName);
332
if (tool == null) {
333
errorMessage += "Tool " + toolName + " not covered by this test. " +
334
"Add specification to jdkTools array!\n";
335
continue;
336
}
337
338
// Test for help flags to be supported.
339
if (tool.supportsQuestionMark == true) {
340
errorMessage += testTool(f, "-?", tool.exitcodeOfHelp);
341
} else {
342
System.out.println("Skip " + tool.toolname + ". It does not support -?.");
343
}
344
if (tool.supportsH == true) {
345
errorMessage += testTool(f, "-h", tool.exitcodeOfHelp);
346
} else {
347
System.out.println("Skip " + tool.toolname + ". It does not support -h.");
348
}
349
if (tool.supportsHelp == true) {
350
errorMessage += testTool(f, "--help", tool.exitcodeOfHelp);
351
} else {
352
System.out.println("Skip " + tool.toolname + ". It does not support --help.");
353
}
354
355
// Check that the return code listing in jdkTools[] is
356
// correct for an invalid flag.
357
errorMessage += testInvalidFlag(f, "-asdfxgr", tool.exitcodeOfWrongFlag, tool.documentsLegacyHelp);
358
359
// Test for legacy -help flag.
360
if (!tool.documentsLegacyHelp) {
361
if (tool.supportsLegacyHelp == true) {
362
errorMessage += testLegacyFlag(f, tool.exitcodeOfHelp);
363
} else {
364
errorMessage += testInvalidFlag(f, "-help", tool.exitcodeOfWrongFlag, false);
365
}
366
}
367
}
368
369
if (errorMessage.isEmpty()) {
370
System.out.println("All help string tests: PASS");
371
} else {
372
throw new AssertionError("HelpFlagsTest failed:\n" + errorMessage);
373
}
374
}
375
}
376
377