Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/launcher/FXLauncherTest.java
41144 views
1
/*
2
* Copyright (c) 2012, 2018, 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
* @library /test/lib
27
* @build FXLauncherTest jdk.test.lib.compiler.CompilerUtils
28
* @bug 8001533 8004547 8035782 8202553
29
* @summary Test launching FX application with java -jar
30
* Test uses main method and blank main method, a jfx app class and an incorrect
31
* jfx app class, a main-class for the manifest, a bogus one and none.
32
* Now that FX is no longer bundled with the JDK, this test uses a
33
* "mock" javafx.graphics module to test the FX launcher. It also verifies
34
* that FX is, in fact, not included with the JDK.
35
* All should execute except the incorrect fx app class entries.
36
* @run main/othervm FXLauncherTest
37
*/
38
import java.io.File;
39
import java.nio.file.Path;
40
import java.nio.file.Paths;
41
import java.io.IOException;
42
import java.util.ArrayList;
43
import java.util.Arrays;
44
import java.util.List;
45
46
import jdk.test.lib.compiler.CompilerUtils;
47
48
public class FXLauncherTest extends TestHelper {
49
private static final String FX_MARKER_CLASS = "javafx.application.Application";
50
private static void line() {
51
System.out.println("_____________________________________________");
52
}
53
private static File MainJavaFile = null;
54
private static final File FXtestJar = new File("fxtest.jar");
55
private static final File ManifestFile = new File("manifest.txt");
56
private static final File ScratchDir = new File(".");
57
58
private static final Path SRC_DIR =
59
TEST_SOURCES_DIR.toPath().resolve("mockfx/src");
60
private static final Path MODS_DIR = Paths.get("mods");
61
private static final String MODULE_DIR = MODS_DIR.toString();
62
63
/* standard main class can be used as java main for fx app class */
64
static final String StdMainClass = "helloworld.HelloWorld";
65
static final String ExtMainClass = "helloworld.ExtHello";
66
static final String NonFXMainClass = "helloworld.HelloJava";
67
static int testcount = 0;
68
69
static final String LAUNCH_MODE_CLASS = "LM_CLASS";
70
static final String LAUNCH_MODE_JAR = "LM_JAR";
71
static final String LAUNCH_MODE_MODULE = "LM_MODULE";
72
73
/* a main method and a blank. */
74
static final String[] MAIN_METHODS = {
75
"public static void main(String[] args) { launch(args); }",
76
" "
77
};
78
79
// Array of parameters to pass to fx application.
80
static final String[] APP_PARMS = { "one", "two" };
81
82
// Create fx java file for test application
83
static void createJavaFile(String mainmethod) {
84
try {
85
String mainClass = "HelloWorld";
86
List<String> contents = new ArrayList<>();
87
contents.add("package helloworld;");
88
contents.add("import javafx.application.Application;");
89
contents.add("import javafx.stage.Stage;");
90
contents.add("public class HelloWorld extends Application {");
91
contents.add(mainmethod);
92
contents.add("@Override");
93
contents.add("public void start(Stage primaryStage) {");
94
contents.add(" throw new InternalError(\"should never get here\");");
95
contents.add("}");
96
contents.add("}");
97
98
// Create and compile java source.
99
MainJavaFile = new File(mainClass + JAVA_FILE_EXT);
100
createFile(MainJavaFile, contents);
101
doFxCompile("-d", ".", mainClass + JAVA_FILE_EXT);
102
} catch (java.io.IOException ioe) {
103
ioe.printStackTrace();
104
throw new RuntimeException("Failed creating HelloWorld.");
105
}
106
}
107
108
/*
109
* Create class that extends HelloWorld instead of Application
110
*/
111
static void createExtJavaFile(String mainmethod) {
112
try {
113
String mainClass = "ExtHello";
114
List<String> contents = new ArrayList<>();
115
contents.add("package helloworld;");
116
contents.add("public class ExtHello extends HelloWorld {");
117
contents.add(mainmethod);
118
contents.add("}");
119
// Create and compile java source.
120
MainJavaFile = new File(mainClass + JAVA_FILE_EXT);
121
createFile(MainJavaFile, contents);
122
doFxCompile("-cp", ".", "-d", ".", mainClass + JAVA_FILE_EXT);
123
} catch (java.io.IOException ioe) {
124
ioe.printStackTrace();
125
throw new RuntimeException("Failed creating ExtHello.");
126
}
127
}
128
129
/*
130
* Create non-JavaFX class for testing if jfxrt.jar is being loaded
131
* when it shouldn't be
132
*/
133
static void createNonFXJavaFile() {
134
try {
135
String mainClass = "HelloJava";
136
List<String> contents = new ArrayList<>();
137
contents.add("package helloworld;");
138
contents.add("public class HelloJava {");
139
contents.add(" public static void main(String[] args) {");
140
contents.add(" for(String aa : args)");
141
contents.add(" System.out.println(\"arg: \" + aa);" );
142
contents.add(" }");
143
contents.add("}");
144
// Create and compile java source.
145
MainJavaFile = new File(mainClass + JAVA_FILE_EXT);
146
createFile(MainJavaFile, contents);
147
doFxCompile("-cp", ".", "-d", ".", mainClass + JAVA_FILE_EXT);
148
} catch (java.io.IOException ioe) {
149
ioe.printStackTrace();
150
throw new RuntimeException("Failed creating HelloJava.");
151
}
152
}
153
154
// Create manifest for test fx application
155
static List<String> createManifestContents(String mainClassEntry, String fxMainEntry) {
156
List<String> mcontents = new ArrayList<>();
157
mcontents.add("Manifest-Version: 1.0");
158
mcontents.add("Created-By: FXLauncherTest");
159
if (mainClassEntry != null) {
160
mcontents.add("Main-Class: " + mainClassEntry);
161
System.out.println("Main-Class: " + mainClassEntry);
162
}
163
if (fxMainEntry != null) {
164
mcontents.add("JavaFX-Application-Class: " + fxMainEntry);
165
System.out.println("JavaFX-Application-Class: " + fxMainEntry);
166
}
167
return mcontents;
168
}
169
170
// Method to marshal createJar to TestHelper.createJar()
171
static void createJar(File theJar, File manifestFile) {
172
createJar("cvmf", manifestFile.getName(),
173
theJar.getAbsolutePath(), "helloworld");
174
}
175
176
static void saveFile(String tname, int testcount, File srcFile) {
177
File newFile = new File(tname + "-" + testcount + "-" + srcFile.getName());
178
System.out.println("renaming " + srcFile.getName() +
179
" to " + newFile.getName());
180
srcFile.renameTo(newFile);
181
}
182
183
static void cleanupFiles() throws IOException {
184
for(File f : ScratchDir.listFiles()) {
185
recursiveDelete(f);
186
}
187
}
188
189
static void checkStatus(TestResult tr, String testName, int testCount,
190
String mainclass) throws Exception {
191
if (tr.testStatus) {
192
System.out.println("PASS: " + testName + ":" + testCount +
193
" : test with " + mainclass);
194
cleanupFiles();
195
} else {
196
saveFile(testName, testcount, FXtestJar);
197
System.out.println("FAIL: " + testName + ":" + testCount +
198
" : test with " + mainclass);
199
cleanupFiles();
200
System.err.println(tr);
201
throw new Exception("Failed: " + testName + ":" + testCount);
202
}
203
}
204
205
public static void compileFXModule() {
206
final String JAVAFX_GRAPHICS_MODULE = "javafx.graphics";
207
208
try {
209
// Compile mockfx/src/javafx.graphics/** into mods/javafx.graphics
210
boolean compiled
211
= CompilerUtils.compile(SRC_DIR.resolve(JAVAFX_GRAPHICS_MODULE),
212
MODS_DIR.resolve(JAVAFX_GRAPHICS_MODULE));
213
214
if (!compiled) {
215
throw new RuntimeException("Error compiling mock javafx.graphics module");
216
}
217
} catch (IOException ioe) {
218
throw new RuntimeException(ioe);
219
}
220
}
221
222
static void doFxCompile(String...compilerArgs) {
223
compileFXModule();
224
225
List<String> fxCompilerArgs = new ArrayList<>();
226
fxCompilerArgs.add("--module-path=" + MODULE_DIR);
227
fxCompilerArgs.add("--add-modules=javafx.graphics");
228
fxCompilerArgs.addAll(Arrays.asList(compilerArgs));
229
compile(fxCompilerArgs.toArray(new String[fxCompilerArgs.size()]));
230
}
231
232
static TestResult doFxExec(String...cmds) {
233
List<String> fxCmds = new ArrayList<>();
234
fxCmds.addAll(Arrays.asList(cmds));
235
fxCmds.add(1, "--module-path=" + MODULE_DIR);
236
fxCmds.add(2, "--add-modules=javafx.graphics");
237
return doExec(fxCmds.toArray(new String[fxCmds.size()]));
238
}
239
240
/*
241
* Set Main-Class and iterate main_methods.
242
* Try launching with both -jar and -cp methods, with and without FX main
243
* class manifest entry.
244
* All cases should run.
245
*
246
* See sun.launcher.LauncherHelper$FXHelper for more details on how JavaFX
247
* applications are launched.
248
*/
249
@Test
250
static void testBasicFXApp() throws Exception {
251
testBasicFXApp(true, false); // -cp, no JAC
252
testBasicFXApp(false, true); // -jar, with JAC
253
testBasicFXApp(false, false); // -jar, no JAC
254
}
255
256
static void testBasicFXApp(boolean useCP, boolean setFXMainClass) throws Exception {
257
String testname = "testBasicFXApp";
258
if (useCP) {
259
testname = testname.concat("_useCP");
260
}
261
String fxMC = StdMainClass;
262
if (!setFXMainClass) {
263
testname = testname.concat("_noJAC");
264
fxMC = null;
265
}
266
for (String mm : MAIN_METHODS) {
267
testcount++;
268
line();
269
System.out.println("test# " + testcount + "- Main method: " + mm);
270
createJavaFile(mm);
271
createFile(ManifestFile, createManifestContents(StdMainClass, fxMC));
272
createJar(FXtestJar, ManifestFile);
273
String sTestJar = FXtestJar.getAbsolutePath();
274
String launchMode;
275
final TestResult tr;
276
if (useCP) {
277
tr = doFxExec(javaCmd, "-cp", sTestJar, StdMainClass, APP_PARMS[0], APP_PARMS[1]);
278
launchMode = LAUNCH_MODE_CLASS;
279
} else {
280
tr = doFxExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
281
launchMode = LAUNCH_MODE_JAR;
282
}
283
tr.checkPositive();
284
if (tr.testStatus) {
285
if (!tr.contains(launchMode)) {
286
System.err.println("ERROR: Did not find "
287
+ launchMode + " in output!");
288
}
289
for (String p : APP_PARMS) {
290
if (!tr.contains(p)) {
291
System.err.println("ERROR: Did not find "
292
+ p + " in output!");
293
}
294
}
295
}
296
checkStatus(tr, testname, testcount, StdMainClass);
297
}
298
}
299
300
/*
301
* Set Main-Class and iterate main methods.
302
* Main class extends another class that extends Application.
303
* Try launching with both -jar and -cp methods.
304
* All cases should run.
305
*/
306
@Test
307
static void testExtendFXApp() throws Exception {
308
testExtendFXApp(true, false); // -cp, no JAC
309
testExtendFXApp(false, true); // -jar, with JAC
310
testExtendFXApp(false, false); // -jar, no JAC
311
}
312
313
static void testExtendFXApp(boolean useCP, boolean setFXMainClass) throws Exception {
314
String testname = "testExtendFXApp";
315
if (useCP) {
316
testname = testname.concat("_useCP");
317
}
318
String fxMC = ExtMainClass;
319
if (!setFXMainClass) {
320
testname = testname.concat("_noJAC");
321
fxMC = null;
322
}
323
for (String mm : MAIN_METHODS) {
324
testcount++;
325
line();
326
System.out.println("test# " + testcount + "- Main method: " + mm);
327
createJavaFile(mm);
328
createExtJavaFile(mm);
329
createFile(ManifestFile, createManifestContents(ExtMainClass, fxMC));
330
createJar(FXtestJar, ManifestFile);
331
String sTestJar = FXtestJar.getAbsolutePath();
332
String launchMode;
333
final TestResult tr;
334
if (useCP) {
335
tr = doFxExec(javaCmd, "-cp", sTestJar, ExtMainClass, APP_PARMS[0], APP_PARMS[1]);
336
launchMode = LAUNCH_MODE_CLASS;
337
} else {
338
tr = doFxExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
339
launchMode = LAUNCH_MODE_JAR;
340
}
341
tr.checkPositive();
342
if (tr.testStatus) {
343
if (!tr.contains(launchMode)) {
344
System.err.println("ERROR: Did not find "
345
+ launchMode + " in output!");
346
}
347
for (String p : APP_PARMS) {
348
if (!tr.contains(p)) {
349
System.err.println("ERROR: Did not find "
350
+ p + " in output!");
351
}
352
}
353
}
354
checkStatus(tr, testname, testcount, ExtMainClass);
355
}
356
}
357
358
/*
359
* Ensure we can NOT launch a FX app jar with no Main-Class manifest entry
360
*/
361
@Test
362
static void testMissingMC() throws Exception {
363
String testname = "testMissingMC";
364
testcount++;
365
line();
366
System.out.println("test# " + testcount + ": abort on missing Main-Class");
367
createJavaFile(" "); // no main() needed
368
createFile(ManifestFile, createManifestContents(null, StdMainClass)); // No MC, but supply JAC
369
createJar(FXtestJar, ManifestFile);
370
String sTestJar = FXtestJar.getAbsolutePath();
371
TestResult tr = doFxExec(javaCmd, "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
372
tr.checkNegative(); // should abort if no Main-Class
373
if (tr.testStatus) {
374
if (!tr.contains("no main manifest attribute")) {
375
System.err.println("ERROR: launcher did not abort properly");
376
}
377
} else {
378
System.err.println("ERROR: jar executed with no Main-Class!");
379
}
380
checkStatus(tr, testname, testcount, StdMainClass);
381
}
382
383
/*
384
* test to ensure that we don't load any extraneous fx jars when
385
* launching a standard java application
386
* Test both -cp and -jar methods since they use different code paths.
387
* Neither case should cause jfxrt.jar to be loaded.
388
*/
389
@Test
390
static void testExtraneousJars() throws Exception {
391
testExtraneousJars(true);
392
testExtraneousJars(false);
393
}
394
395
static void testExtraneousJars(boolean useCP) throws Exception {
396
String testname = "testExtraneousJars";
397
if (useCP) {
398
testname = testname.concat("_useCP");
399
}
400
testcount++;
401
line();
402
System.out.println("test# " + testcount
403
+ ": test for erroneous jfxrt.jar loading");
404
createNonFXJavaFile();
405
createFile(ManifestFile, createManifestContents(NonFXMainClass, null));
406
createJar(FXtestJar, ManifestFile);
407
String sTestJar = FXtestJar.getAbsolutePath();
408
final TestResult tr;
409
410
if (useCP) {
411
tr = doFxExec(javaCmd, "-verbose:class", "-cp", sTestJar, NonFXMainClass, APP_PARMS[0], APP_PARMS[1]);
412
} else {
413
tr = doFxExec(javaCmd, "-verbose:class", "-jar", sTestJar, APP_PARMS[0], APP_PARMS[1]);
414
}
415
tr.checkPositive();
416
if (tr.testStatus) {
417
if (!tr.notContains("jfxrt.jar")) {
418
System.out.println("testing for extraneous jfxrt jar");
419
System.out.println(tr);
420
throw new Exception("jfxrt.jar is being loaded, it should not be!");
421
}
422
if (!tr.notContains("sun.launcher.LauncherHelper$FXHelper")) {
423
System.out.println("testing for extraneous 'sun.launcher.LauncherHelper$FXHelper'");
424
System.out.println(tr);
425
throw new Exception("FXHelper is being loaded, it should not be!");
426
}
427
for (String p : APP_PARMS) {
428
if (!tr.contains(p)) {
429
System.err.println("ERROR: Did not find "
430
+ p + " in output!");
431
}
432
}
433
}
434
checkStatus(tr, testname, testcount, NonFXMainClass);
435
}
436
437
public static void main(String... args) throws Exception {
438
439
// Ensure that FX is not part of jdk
440
Class<?> fxClass = null;
441
try {
442
fxClass = Class.forName(FX_MARKER_CLASS);
443
} catch (ClassNotFoundException ex) {
444
// do nothing
445
}
446
if (fxClass != null) {
447
throw new RuntimeException("JavaFX modules erroneously included in the JDK");
448
}
449
450
FXLauncherTest fxt = new FXLauncherTest();
451
fxt.run(args);
452
if (testExitValue > 0) {
453
System.out.println("Total of " + testExitValue
454
+ " failed. Test cases covered: "
455
+ FXLauncherTest.testcount);
456
System.exit(1);
457
} else {
458
System.out.println("All tests pass. Test cases covered: "
459
+ FXLauncherTest.testcount);
460
}
461
}
462
463
}
464
465