Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/tools/launcher/SourceMode.java
66643 views
1
/*
2
* Copyright (c) 2017, 2021, 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
/**
25
* @test
26
* @bug 8192920 8204588 8210275
27
* @summary Test source mode
28
* @modules jdk.compiler jdk.jlink
29
* @run main SourceMode
30
*/
31
32
33
import java.io.IOException;
34
import java.io.PrintStream;
35
import java.nio.file.Files;
36
import java.nio.file.Path;
37
import java.nio.file.Paths;
38
import java.nio.file.attribute.PosixFilePermission;
39
import java.util.ArrayList;
40
import java.util.Arrays;
41
import java.util.HashMap;
42
import java.util.List;
43
import java.util.Map;
44
import java.util.Set;
45
import java.util.spi.ToolProvider;
46
47
public class SourceMode extends TestHelper {
48
49
public static void main(String... args) throws Exception {
50
new SourceMode().run(args);
51
}
52
53
// To reduce the chance of creating shebang lines that are too long,
54
// use a shorter path for a java command if the standard path is too long.
55
private final Path shebangJavaCmd;
56
57
// Whether or not to automatically skip the shebang tests
58
private final boolean skipShebangTest;
59
60
private final PrintStream log;
61
62
private static final String thisVersion = System.getProperty("java.specification.version");
63
64
SourceMode() throws Exception {
65
log = System.err;
66
67
if (isWindows) {
68
// Skip shebang tests on Windows, because that requires Cygwin.
69
skipShebangTest = true;
70
shebangJavaCmd = null;
71
} else {
72
// Try to ensure the path to the Java launcher is reasonably short,
73
// to work around the mostly undocumented limit of 120 characters
74
// for a shebang line.
75
// The value of 120 is the typical kernel compile-time buffer limit.
76
// The following limit of 80 allows room for arguments to be placed
77
// after the path to the launcher on the shebang line.
78
Path cmd = Paths.get(javaCmd);
79
if (cmd.toString().length() < 80) {
80
shebangJavaCmd = cmd;
81
} else {
82
// Create a small image in the current directory, such that
83
// the path for the launcher is just "tmpJDK/bin/java".
84
Path tmpJDK = Paths.get("tmpJDK");
85
ToolProvider jlink = ToolProvider.findFirst("jlink")
86
.orElseThrow(() -> new Exception("cannot find jlink"));
87
jlink.run(System.out, System.err,
88
"--add-modules", "jdk.compiler,jdk.zipfs", "--output", tmpJDK.toString());
89
shebangJavaCmd = tmpJDK.resolve("bin").resolve("java");
90
}
91
log.println("Using java command: " + shebangJavaCmd);
92
skipShebangTest = false;
93
}
94
}
95
96
// java Simple.java 1 2 3
97
@Test
98
void testSimpleJava() throws IOException {
99
starting("testSimpleJava");
100
Path file = getSimpleFile("Simple.java", false);
101
TestResult tr = doExec(javaCmd, file.toString(), "1", "2", "3");
102
if (!tr.isOK())
103
error(tr, "Bad exit code: " + tr.exitValue);
104
if (!tr.contains("[1, 2, 3]"))
105
error(tr, "Expected output not found");
106
show(tr);
107
}
108
109
// java --source N simple 1 2 3
110
@Test
111
void testSimple() throws IOException {
112
starting("testSimple");
113
Path file = getSimpleFile("simple", false);
114
TestResult tr = doExec(javaCmd, "--source", thisVersion, file.toString(), "1", "2", "3");
115
if (!tr.isOK())
116
error(tr, "Bad exit code: " + tr.exitValue);
117
if (!tr.contains("[1, 2, 3]"))
118
error(tr, "Expected output not found");
119
show(tr);
120
}
121
122
// execSimple 1 2 3
123
@Test
124
void testExecSimple() throws IOException {
125
starting("testExecSimple");
126
if (skipShebangTest) {
127
log.println("SKIPPED");
128
return;
129
}
130
Path file = setExecutable(getSimpleFile("execSimple", true));
131
TestResult tr = doExec(file.toAbsolutePath().toString(), "1", "2", "3");
132
if (!tr.isOK())
133
error(tr, "Bad exit code: " + tr.exitValue);
134
if (!tr.contains("[1, 2, 3]"))
135
error(tr, "Expected output not found");
136
show(tr);
137
}
138
139
// java @simpleJava.at (contains Simple.java 1 2 3)
140
@Test
141
void testSimpleJavaAtFile() throws IOException {
142
starting("testSimpleJavaAtFile");
143
Path file = getSimpleFile("Simple.java", false);
144
Path atFile = Paths.get("simpleJava.at");
145
createFile(atFile, List.of(file + " 1 2 3"));
146
TestResult tr = doExec(javaCmd, "@" + atFile);
147
if (!tr.isOK())
148
error(tr, "Bad exit code: " + tr.exitValue);
149
if (!tr.contains("[1, 2, 3]"))
150
error(tr, "Expected output not found");
151
show(tr);
152
}
153
154
// java @simple.at (contains --source N simple 1 2 3)
155
@Test
156
void testSimpleAtFile() throws IOException {
157
starting("testSimpleAtFile");
158
Path file = getSimpleFile("simple", false);
159
Path atFile = Paths.get("simple.at");
160
createFile(atFile, List.of("--source " + thisVersion + " " + file + " 1 2 3"));
161
TestResult tr = doExec(javaCmd, "@" + atFile);
162
if (!tr.isOK())
163
error(tr, "Bad exit code: " + tr.exitValue);
164
if (!tr.contains("[1, 2, 3]"))
165
error(tr, "Expected output not found");
166
show(tr);
167
}
168
169
// java -cp classes Main.java 1 2 3
170
@Test
171
void testClasspath() throws IOException {
172
starting("testClasspath");
173
Path base = Files.createDirectories(Paths.get("testClasspath"));
174
Path otherJava = base.resolve("Other.java");
175
createFile(otherJava, List.of(
176
"public class Other {",
177
" public static String join(String[] args) {",
178
" return String.join(\"-\", args);",
179
" }",
180
"}"
181
));
182
Path classes = Files.createDirectories(base.resolve("classes"));
183
Path mainJava = base.resolve("Main.java");
184
createFile(mainJava, List.of(
185
"class Main {",
186
" public static void main(String[] args) {",
187
" System.out.println(Other.join(args));",
188
" }}"
189
));
190
compile("-d", classes.toString(), otherJava.toString());
191
TestResult tr = doExec(javaCmd, "-cp", classes.toString(),
192
mainJava.toString(), "1", "2", "3");
193
if (!tr.isOK())
194
error(tr, "Bad exit code: " + tr.exitValue);
195
if (!tr.contains("1-2-3"))
196
error(tr, "Expected output not found");
197
show(tr);
198
}
199
200
// java --add-exports=... Export.java --help
201
@Test
202
void testAddExports() throws IOException {
203
if (!isEnglishLocale()) {
204
return;
205
}
206
207
starting("testAddExports");
208
Path exportJava = Paths.get("Export.java");
209
createFile(exportJava, List.of(
210
"public class Export {",
211
" public static void main(String[] args) {",
212
" new com.sun.tools.javac.main.Main(\"demo\").compile(args);",
213
" }",
214
"}"
215
));
216
// verify access fails without --add-exports
217
TestResult tr1 = doExec(javaCmd, exportJava.toString(), "--help");
218
if (tr1.isOK())
219
error(tr1, "Compilation succeeded unexpectedly");
220
show(tr1);
221
// verify access succeeds with --add-exports
222
TestResult tr2 = doExec(javaCmd,
223
"--add-exports", "jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
224
exportJava.toString(), "--help");
225
if (!tr2.isOK())
226
error(tr2, "Bad exit code: " + tr2.exitValue);
227
if (!(tr2.contains("demo") && tr2.contains("Usage")))
228
error(tr2, "Expected output not found");
229
show(tr2);
230
}
231
232
// java -cp ... HelloWorld.java (for a class "java" in package "HelloWorld")
233
@Test
234
void testClassNamedJava() throws IOException {
235
starting("testClassNamedJava");
236
Path base = Files.createDirectories(Paths.get("testClassNamedJava"));
237
Path src = Files.createDirectories(base.resolve("src"));
238
Path srcfile = src.resolve("java.java");
239
createFile(srcfile, List.of(
240
"package HelloWorld;",
241
"class java {",
242
" public static void main(String... args) {",
243
" System.out.println(HelloWorld.java.class.getName());",
244
" }",
245
"}"
246
));
247
Path classes = base.resolve("classes");
248
compile("-d", classes.toString(), srcfile.toString());
249
TestResult tr =
250
doExec(javaCmd, "-cp", classes.toString(), "HelloWorld.java");
251
if (!tr.isOK())
252
error(tr, "Command failed");
253
if (!tr.contains("HelloWorld.java"))
254
error(tr, "Expected output not found");
255
show(tr);
256
}
257
258
// java --source N -cp ... HelloWorld
259
@Test
260
void testSourceClasspath() throws IOException {
261
if (!isEnglishLocale()) {
262
return;
263
}
264
265
starting("testSourceClasspath");
266
Path base = Files.createDirectories(Paths.get("testSourceClasspath"));
267
Path src = Files.createDirectories(base.resolve("src"));
268
Path srcfile = src.resolve("java.java");
269
createFile(srcfile, List.of(
270
"class HelloWorld {",
271
" public static void main(String... args) {",
272
" System.out.println(\"Hello World\");",
273
" }",
274
"}"
275
));
276
Path classes = base.resolve("classes");
277
compile("-d", classes.toString(), srcfile.toString());
278
TestResult tr =
279
doExec(javaCmd, "--source", thisVersion, "-cp", classes.toString(), "HelloWorld");
280
if (tr.isOK())
281
error(tr, "Command succeeded unexpectedly");
282
if (!tr.contains("file not found: HelloWorld"))
283
error(tr, "Expected output not found");
284
show(tr);
285
}
286
287
// java --source
288
@Test
289
void testSourceNoArg() throws IOException {
290
starting("testSourceNoArg");
291
TestResult tr = doExec(javaCmd, "--source");
292
if (tr.isOK())
293
error(tr, "Command succeeded unexpectedly");
294
if (!tr.contains("--source requires source version"))
295
error(tr, "Expected output not found");
296
show(tr);
297
}
298
299
// java --source N -jar simple.jar
300
@Test
301
void testSourceJarConflict() throws IOException {
302
starting("testSourceJarConflict");
303
Path base = Files.createDirectories(Paths.get("testSourceJarConflict"));
304
Path file = getSimpleFile("Simple.java", false);
305
Path classes = Files.createDirectories(base.resolve("classes"));
306
compile("-d", classes.toString(), file.toString());
307
Path simpleJar = base.resolve("simple.jar");
308
createJar("cf", simpleJar.toString(), "-C", classes.toString(), ".");
309
TestResult tr =
310
doExec(javaCmd, "--source", thisVersion, "-jar", simpleJar.toString());
311
if (tr.isOK())
312
error(tr, "Command succeeded unexpectedly");
313
if (!tr.contains("Option -jar is not allowed with --source"))
314
error(tr, "Expected output not found");
315
show(tr);
316
}
317
318
// java --source N -m jdk.compiler
319
@Test
320
void testSourceModuleConflict() throws IOException {
321
starting("testSourceModuleConflict");
322
TestResult tr = doExec(javaCmd, "--source", thisVersion, "-m", "jdk.compiler");
323
if (tr.isOK())
324
error(tr, "Command succeeded unexpectedly");
325
if (!tr.contains("Option -m is not allowed with --source"))
326
error(tr, "Expected output not found");
327
show(tr);
328
}
329
330
// #!.../java --source N -version
331
@Test
332
void testTerminalOptionInShebang() throws IOException {
333
starting("testTerminalOptionInShebang");
334
if (skipShebangTest || isAIX || isMacOSX) {
335
// On MacOSX, we cannot distinguish between terminal options on the
336
// shebang line and those on the command line.
337
// On Solaris, all options after the first on the shebang line are
338
// ignored. Similar on AIX.
339
log.println("SKIPPED");
340
return;
341
}
342
Path base = Files.createDirectories(
343
Paths.get("testTerminalOptionInShebang"));
344
Path bad = base.resolve("bad");
345
createFile(bad, List.of(
346
"#!" + shebangJavaCmd + " --source " + thisVersion + " -version"));
347
setExecutable(bad);
348
TestResult tr = doExec(bad.toString());
349
if (!tr.contains("Option -version is not allowed in this context"))
350
error(tr, "Expected output not found");
351
show(tr);
352
}
353
354
// #!.../java --source N @bad.at (contains -version)
355
@Test
356
void testTerminalOptionInShebangAtFile() throws IOException {
357
starting("testTerminalOptionInShebangAtFile");
358
if (skipShebangTest || isAIX || isMacOSX) {
359
// On MacOSX, we cannot distinguish between terminal options in a
360
// shebang @-file and those on the command line.
361
// On Solaris, all options after the first on the shebang line are
362
// ignored. Similar on AIX.
363
log.println("SKIPPED");
364
return;
365
}
366
// Use a short directory name, to avoid line length limitations
367
Path base = Files.createDirectories(Paths.get("testBadAtFile"));
368
Path bad_at = base.resolve("bad.at");
369
createFile(bad_at, List.of("-version"));
370
Path bad = base.resolve("bad");
371
createFile(bad, List.of(
372
"#!" + shebangJavaCmd + " --source " + thisVersion + " @" + bad_at));
373
setExecutable(bad);
374
TestResult tr = doExec(bad.toString());
375
if (!tr.contains("Option -version in @testBadAtFile/bad.at is "
376
+ "not allowed in this context"))
377
error(tr, "Expected output not found");
378
show(tr);
379
}
380
381
// #!.../java --source N HelloWorld
382
@Test
383
void testMainClassInShebang() throws IOException {
384
starting("testMainClassInShebang");
385
if (skipShebangTest || isAIX || isMacOSX) {
386
// On MacOSX, we cannot distinguish between a main class on the
387
// shebang line and one on the command line.
388
// On Solaris, all options after the first on the shebang line are
389
// ignored. Similar on AIX.
390
log.println("SKIPPED");
391
return;
392
}
393
Path base = Files.createDirectories(Paths.get("testMainClassInShebang"));
394
Path bad = base.resolve("bad");
395
createFile(bad, List.of(
396
"#!" + shebangJavaCmd + " --source " + thisVersion + " HelloWorld"));
397
setExecutable(bad);
398
TestResult tr = doExec(bad.toString());
399
if (!tr.contains("Cannot specify main class in this context"))
400
error(tr, "Expected output not found");
401
show(tr);
402
}
403
404
//--------------------------------------------------------------------------
405
406
private void starting(String label) {
407
System.out.println();
408
System.out.println("*** Starting: " + label + " (stdout)");
409
410
System.err.println();
411
System.err.println("*** Starting: " + label + " (stderr)");
412
}
413
414
private void show(TestResult tr) {
415
log.println("*** Test Output:");
416
for (String line: tr.testOutput) {
417
log.println(line);
418
}
419
log.println("*** End Of Test Output:");
420
}
421
422
private Map<String,String> getLauncherDebugEnv() {
423
return Map.of("_JAVA_LAUNCHER_DEBUG", "1");
424
}
425
426
private Path getSimpleFile(String name, boolean shebang) throws IOException {
427
Path file = Paths.get(name);
428
if (!Files.exists(file)) {
429
createFile(file, List.of(
430
(shebang ? "#!" + shebangJavaCmd + " --source=" + thisVersion: ""),
431
"public class Simple {",
432
" public static void main(String[] args) {",
433
" System.out.println(java.util.Arrays.toString(args));",
434
" }}"));
435
}
436
return file;
437
}
438
439
private void createFile(Path file, List<String> lines) throws IOException {
440
lines.stream()
441
.filter(line -> line.length() > 128)
442
.forEach(line -> {
443
log.println("*** Warning: long line ("
444
+ line.length()
445
+ " chars) in file " + file);
446
log.println("*** " + line);
447
});
448
log.println("*** File: " + file);
449
lines.stream().forEach(log::println);
450
log.println("*** End Of File");
451
createFile(file.toFile(), lines);
452
}
453
454
private Path setExecutable(Path file) throws IOException {
455
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);
456
perms.add(PosixFilePermission.OWNER_EXECUTE);
457
Files.setPosixFilePermissions(file, perms);
458
return file;
459
}
460
461
private void error(TestResult tr, String message) {
462
show(tr);
463
throw new RuntimeException(message);
464
}
465
}
466
467