Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/tools/launcher/HelpFlagsTest.java
66643 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
"jaccessinspector-32", // gui, don't test, win-32 only
51
"jaccesswalker", // gui, don't test, win only
52
"jaccesswalker-32", // gui, don't test, win-32 only
53
"jconsole", // gui, don't test
54
"servertool", // none. Shell, don't test.
55
"javaw", // don't test, win only
56
// These shall have a help message that resembles that of
57
// MIT's tools. Thus -?, -h and --help are supported, but not
58
// mentioned in the help text.
59
"kinit",
60
"klist",
61
"ktab",
62
// Oracle proprietary tools without help message.
63
"javacpl",
64
"jmc",
65
"jweblauncher",
66
"jcontrol",
67
"ssvagent"
68
};
69
70
// Lists which tools support which flags.
71
private static class ToolHelpSpec {
72
String toolname;
73
74
// How the flags supposed to be supported are handled.
75
//
76
// These flags are supported, i.e.,
77
// * the tool accepts the flag
78
// * the tool prints a help message if the flag is specified
79
// * this help message lists the flag
80
// * the tool exits with exit code '0'.
81
boolean supportsQuestionMark;
82
boolean supportsH;
83
boolean supportsHelp;
84
85
// One tool returns with exit code != '0'.
86
int exitcodeOfHelp;
87
88
// How legacy -help is handled.
89
//
90
// Tools that so far support -help should still do so, but
91
// not print documentation about it. Tools that do not
92
// support -help should not do so in future.
93
//
94
// The tools accepts legacy -help. -help should not be
95
// documented in the usage message.
96
boolean supportsLegacyHelp;
97
98
// Java itself documents -help. -help prints to stderr,
99
// while --help prints to stdout. Leave as is.
100
boolean documentsLegacyHelp;
101
102
// The exit code of the tool if an invalid argument is passed to it.
103
// An exit code != 0 would be expected, but not all tools handle it
104
// that way.
105
int exitcodeOfWrongFlag;
106
107
ToolHelpSpec(String n, int q, int h, int hp, int ex1, int l, int dl, int ex2) {
108
toolname = n;
109
supportsQuestionMark = ( q == 1 ? true : false );
110
supportsH = ( h == 1 ? true : false );
111
supportsHelp = ( hp == 1 ? true : false );
112
exitcodeOfHelp = ex1;
113
114
supportsLegacyHelp = ( l == 1 ? true : false );
115
documentsLegacyHelp = ( dl == 1 ? true : false );
116
exitcodeOfWrongFlag = ex2;
117
}
118
}
119
120
static ToolHelpSpec[] jdkTools = {
121
// name -? -h --help exitcode -help -help exitcode
122
// of help docu of wrong
123
// mented flag
124
new ToolHelpSpec("jabswitch", 0, 0, 0, 0, 0, 0, 0), // /?, prints help message anyways, win only
125
new ToolHelpSpec("jar", 1, 1, 1, 0, 0, 0, 1), // -?, -h, --help
126
new ToolHelpSpec("jarsigner", 1, 1, 1, 0, 1, 0, 1), // -?, -h, --help, -help accepted but not documented.
127
new ToolHelpSpec("java", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
128
new ToolHelpSpec("javac", 1, 0, 1, 0, 1, 1, 2), // -?, --help -help, Documents -help, -h is already taken for "native header output directory".
129
new ToolHelpSpec("javadoc", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
130
new ToolHelpSpec("javap", 1, 1, 1, 0, 1, 1, 2), // -?, -h, --help -help, Documents -help
131
new ToolHelpSpec("javaw", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help, win only
132
new ToolHelpSpec("jcmd", 1, 1, 1, 0, 1, 0, 1), // -?, -h, --help, -help accepted but not documented.
133
new ToolHelpSpec("jdb", 1, 1, 1, 0, 1, 1, 0), // -?, -h, --help -help, Documents -help
134
new ToolHelpSpec("jdeprscan", 1, 1, 1, 0, 0, 0, 1), // -?, -h, --help
135
new ToolHelpSpec("jdeps", 1, 1, 1, 0, 1, 0, 2), // -?, -h, --help, -help accepted but not documented.
136
new ToolHelpSpec("jfr", 1, 1, 1, 0, 0, 0, 2), // -?, -h, --help
137
new ToolHelpSpec("jhsdb", 0, 0, 0, 0, 0, 0, 0), // none, prints help message anyways.
138
new ToolHelpSpec("jimage", 1, 1, 1, 0, 0, 0, 2), // -?, -h, --help
139
new ToolHelpSpec("jinfo", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
140
new ToolHelpSpec("jjs", 0, 1, 1, 100, 0, 0, 100), // -h, --help, return code 100
141
new ToolHelpSpec("jlink", 1, 1, 1, 0, 0, 0, 2), // -?, -h, --help
142
new ToolHelpSpec("jmap", 1, 1, 1, 0, 1, 0, 1), // -?, -h, --help, -help accepted but not documented.
143
new ToolHelpSpec("jmod", 1, 1, 1, 0, 1, 0, 2), // -?, -h, --help, -help accepted but not documented.
144
new ToolHelpSpec("jps", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
145
new ToolHelpSpec("jrunscript", 1, 1, 1, 0, 1, 1, 7), // -?, -h, --help -help, Documents -help
146
new ToolHelpSpec("jshell", 1, 1, 1, 0, 1, 0, 1), // -?, -h, --help, -help accepted but not documented.
147
new ToolHelpSpec("jstack", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
148
new ToolHelpSpec("jstat", 1, 1, 1, 0, 1, 1, 1), // -?, -h, --help -help, Documents -help
149
new ToolHelpSpec("jstatd", 1, 1, 1, 0, 0, 0, 1), // -?, -h, --help
150
new ToolHelpSpec("keytool", 1, 1, 1, 0, 1, 0, 1), // none, prints help message anyways.
151
new ToolHelpSpec("rmiregistry", 0, 0, 0, 0, 0, 0, 1), // none, prints help message anyways.
152
new ToolHelpSpec("serialver", 0, 0, 0, 0, 0, 0, 1), // none, prints help message anyways.
153
new ToolHelpSpec("jpackage", 0, 1, 1, 0, 0, 1, 1), // -h, --help,
154
};
155
156
// Returns corresponding object from jdkTools array.
157
static ToolHelpSpec getToolHelpSpec(String tool) {
158
for (ToolHelpSpec x : jdkTools) {
159
if (tool.toLowerCase().equals(x.toolname) ||
160
tool.toLowerCase().equals(x.toolname + ".exe"))
161
return x;
162
}
163
return null;
164
}
165
166
// Check whether 'flag' appears in 'line' as a word of itself. It must not
167
// be a substring of a word, as then similar flags might be matched.
168
// E.g.: --help matches in the documentation of --help-extra.
169
// This works only with english locale, as some tools have translated
170
// usage messages.
171
static boolean findFlagInLine(String line, String flag) {
172
if (line.contains(flag) &&
173
!line.contains("nknown") && // Some tools say 'Unknown option "<flag>"',
174
!line.contains("invalid flag") && // 'invalid flag: <flag>'
175
!line.contains("invalid option") && // or 'invalid option: <flag>'. Skip that.
176
!line.contains("FileNotFoundException: -help") && // Special case for idlj.
177
!line.contains("-h requires an argument") && // Special case for javac.
178
!line.contains("port argument,")) { // Special case for rmiregistry.
179
// There might be several appearances of 'flag' in
180
// 'line'. (-h as substring of --help).
181
int flagLen = flag.length();
182
int lineLen = line.length();
183
for (int i = line.indexOf(flag); i >= 0; i = line.indexOf(flag, i+1)) {
184
// There should be a space before 'flag' in 'line', or it's right at the beginning.
185
if (i > 0 &&
186
line.charAt(i-1) != ' ' &&
187
line.charAt(i-1) != '[' && // jarsigner
188
line.charAt(i-1) != '|' && // jstatd
189
line.charAt(i-1) != '\t') { // jjs
190
continue;
191
}
192
// There should be a space or comma after 'flag' in 'line', or it's just at the end.
193
int posAfter = i + flagLen;
194
if (posAfter < lineLen &&
195
line.charAt(posAfter) != ' ' &&
196
line.charAt(posAfter) != ',' &&
197
line.charAt(posAfter) != '[' && // jar
198
line.charAt(posAfter) != ']' && // jarsigner
199
line.charAt(posAfter) != ')' && // jfr
200
line.charAt(posAfter) != '|' && // jstatd
201
line.charAt(posAfter) != ':' && // jps
202
line.charAt(posAfter) != '"') { // keytool
203
continue;
204
}
205
return true;
206
}
207
}
208
return false;
209
}
210
211
static TestResult runToolWithFlag(File f, String flag) {
212
String x = f.getAbsolutePath();
213
TestResult tr = doExec(x, flag);
214
System.out.println("Testing " + f.getName());
215
System.out.println("#> " + x + " " + flag);
216
tr.testOutput.forEach(System.out::println);
217
System.out.println("#> echo $?");
218
System.out.println(tr.exitValue);
219
220
return tr;
221
}
222
223
// Checks whether tool supports flag 'flag' and documents it
224
// in the help message.
225
static String testTool(File f, String flag, int exitcode) {
226
String result = "";
227
TestResult tr = runToolWithFlag(f, flag);
228
229
// Check that the tool accepted the flag.
230
if (exitcode == 0 && !tr.isOK()) {
231
System.out.println("failed");
232
result = "failed: " + f.getName() + " " + flag + " has exit code " + tr.exitValue + ".\n";
233
}
234
235
// Check there is a help message listing the flag.
236
boolean foundFlag = false;
237
for (String y : tr.testOutput) {
238
if (!foundFlag && findFlagInLine(y, flag)) { // javac
239
foundFlag = true;
240
System.out.println("Found documentation of '" + flag + "': '" + y.trim() +"'");
241
}
242
}
243
if (!foundFlag) {
244
result += "failed: " + f.getName() + " does not document " +
245
flag + " in help message.\n";
246
}
247
248
if (!result.isEmpty())
249
System.out.println(result);
250
251
return result;
252
}
253
254
// Test the tool supports legacy option -help, but does
255
// not document it.
256
static String testLegacyFlag(File f, int exitcode) {
257
String result = "";
258
TestResult tr = runToolWithFlag(f, "-help");
259
260
// Check that the tool accepted the flag.
261
if (exitcode == 0 && !tr.isOK()) {
262
System.out.println("failed");
263
result = "failed: " + f.getName() + " -help has exit code " + tr.exitValue + ".\n";
264
}
265
266
// Check there is _no_ documentation of -help.
267
boolean foundFlag = false;
268
for (String y : tr.testOutput) {
269
if (!foundFlag && findFlagInLine(y, "-help")) { // javac
270
foundFlag = true;
271
System.out.println("Found documentation of '-help': '" + y.trim() +"'");
272
}
273
}
274
if (foundFlag) {
275
result += "failed: " + f.getName() + " does document -help " +
276
"in help message. This legacy flag should not be documented.\n";
277
}
278
279
if (!result.isEmpty())
280
System.out.println(result);
281
282
return result;
283
}
284
285
// Test that the tool exits with the exit code expected for
286
// invalid flags. In general, one would expect this to be != 0,
287
// but currently a row of tools exit with 0 in this case.
288
// The output should not ask to get help with flag '-help'.
289
static String testInvalidFlag(File f, String flag, int exitcode, boolean documentsLegacyHelp) {
290
String result = "";
291
TestResult tr = runToolWithFlag(f, flag);
292
293
// Check that the tool did exit with the expected return code.
294
if (!((exitcode == tr.exitValue) ||
295
// Windows reports -1 where unix reports 255.
296
(tr.exitValue < 0 && exitcode == tr.exitValue + 256))) {
297
System.out.println("failed");
298
result = "failed: " + f.getName() + " " + flag + " should not be " +
299
"accepted. But it has exit code " + tr.exitValue + ".\n";
300
}
301
302
if (!documentsLegacyHelp) {
303
// Check there is _no_ documentation of -help.
304
boolean foundFlag = false;
305
for (String y : tr.testOutput) {
306
if (!foundFlag && findFlagInLine(y, "-help")) { // javac
307
foundFlag = true;
308
System.out.println("Found documentation of '-help': '" + y.trim() +"'");
309
}
310
}
311
if (foundFlag) {
312
result += "failed: " + f.getName() + " does document -help " +
313
"in error message. This legacy flag should not be documented.\n";
314
}
315
}
316
317
if (!result.isEmpty())
318
System.out.println(result);
319
320
return result;
321
}
322
323
public static void main(String[] args) {
324
String errorMessage = "";
325
326
// The test analyses the help messages printed. It assumes englisch
327
// help messages. Thus it only works with english locale.
328
if (!isEnglishLocale()) { return; }
329
330
for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(TOOLS_NOT_TO_TEST))) {
331
String toolName = f.getName();
332
333
ToolHelpSpec tool = getToolHelpSpec(toolName);
334
if (tool == null) {
335
errorMessage += "Tool " + toolName + " not covered by this test. " +
336
"Add specification to jdkTools array!\n";
337
continue;
338
}
339
340
// Test for help flags to be supported.
341
if (tool.supportsQuestionMark == true) {
342
errorMessage += testTool(f, "-?", tool.exitcodeOfHelp);
343
} else {
344
System.out.println("Skip " + tool.toolname + ". It does not support -?.");
345
}
346
if (tool.supportsH == true) {
347
errorMessage += testTool(f, "-h", tool.exitcodeOfHelp);
348
} else {
349
System.out.println("Skip " + tool.toolname + ". It does not support -h.");
350
}
351
if (tool.supportsHelp == true) {
352
errorMessage += testTool(f, "--help", tool.exitcodeOfHelp);
353
} else {
354
System.out.println("Skip " + tool.toolname + ". It does not support --help.");
355
}
356
357
// Check that the return code listing in jdkTools[] is
358
// correct for an invalid flag.
359
errorMessage += testInvalidFlag(f, "-asdfxgr", tool.exitcodeOfWrongFlag, tool.documentsLegacyHelp);
360
361
// Test for legacy -help flag.
362
if (!tool.documentsLegacyHelp) {
363
if (tool.supportsLegacyHelp == true) {
364
errorMessage += testLegacyFlag(f, tool.exitcodeOfHelp);
365
} else {
366
errorMessage += testInvalidFlag(f, "-help", tool.exitcodeOfWrongFlag, false);
367
}
368
}
369
}
370
371
if (errorMessage.isEmpty()) {
372
System.out.println("All help string tests: PASS");
373
} else {
374
throw new AssertionError("HelpFlagsTest failed:\n" + errorMessage);
375
}
376
}
377
}
378
379