60 Script() {
61 appDesc = JavaAppDesc.parse("test.Hello");
62 }
63
64 Script modular(boolean v) {
65 appDesc.setModuleName(v ? "com.other" : null);
66 return this;
67 }
68
69 Script withJLink(boolean v) {
70 withJLink = v;
71 return this;
72 }
73
74 Script withMainClass(MainClassType v) {
75 mainClass = v;
76 return this;
77 }
78
79 Script withJarMainClass(MainClassType v) {
80 appDesc.setJarWithMainClass(v != NotSet);
81 jarMainClass = v;
82 return this;
83 }
84
85 Script expectedErrorMessage(String v) {
86 expectedErrorMessage = v;
87 return this;
88 }
89
90 @Override
91 public String toString() {
92 return Stream.of(
93 format("modular", appDesc.moduleName() != null ? 'y' : 'n'),
94 format("main-class", mainClass),
95 format("jar-main-class", jarMainClass),
96 format("jlink", withJLink ? 'y' : 'n'),
97 format("error", expectedErrorMessage)
98 ).filter(Objects::nonNull).collect(Collectors.joining("; "));
99 }
100
248 // executePrerequisiteActions() is called by JPackageCommand instance
249 // only once.
250 cmd.executePrerequisiteActions();
251
252 final Path jarFile;
253 if (script.appDesc.moduleName() != null) {
254 jarFile = Path.of(cmd.getArgumentValue("--module-path"),
255 script.appDesc.jarFileName());
256 } else {
257 jarFile = cmd.inputDir().resolve(cmd.getArgumentValue("--main-jar"));
258 }
259
260 // Create new jar file filtering out main class from the old jar file.
261 TKit.withTempDirectory("repack-jar", workDir -> {
262 // Extract app's class from the old jar.
263 explodeJar(jarFile, workDir,
264 jarEntry -> Path.of(jarEntry.getName()).equals(
265 script.appDesc.classFilePath()));
266
267 // Create app's jar file with different main class.
268 var badAppDesc = JavaAppDesc.parse(script.appDesc.toString()).setClassName(
269 nonExistingMainClass);
270 JPackageCommand.helloAppImage(badAppDesc).executePrerequisiteActions();
271
272 // Extract new jar but skip app's class.
273 explodeJar(jarFile, workDir,
274 jarEntry -> !Path.of(jarEntry.getName()).equals(
275 badAppDesc.classFilePath()));
276
277 // At this point we should have:
278 // 1. Manifest from the new jar referencing non-existing class
279 // as the main class.
280 // 2. Module descriptor referencing non-existing class as the main
281 // class in case of modular app.
282 // 3. App's class from the old jar. We need it to let jlink find some
283 // classes in the package declared in module descriptor
284 // in case of modular app.
285
286 Files.delete(jarFile);
287 new Executor().setToolProvider(JavaTool.JAR)
288 .addArguments("-v", "-c", "-M", "-f", jarFile.toString())
289 .addArguments("-C", workDir.toString(), ".")
290 .dumpOutput()
|
60 Script() {
61 appDesc = JavaAppDesc.parse("test.Hello");
62 }
63
64 Script modular(boolean v) {
65 appDesc.setModuleName(v ? "com.other" : null);
66 return this;
67 }
68
69 Script withJLink(boolean v) {
70 withJLink = v;
71 return this;
72 }
73
74 Script withMainClass(MainClassType v) {
75 mainClass = v;
76 return this;
77 }
78
79 Script withJarMainClass(MainClassType v) {
80 appDesc.setWithMainClass(v != NotSet);
81 jarMainClass = v;
82 return this;
83 }
84
85 Script expectedErrorMessage(String v) {
86 expectedErrorMessage = v;
87 return this;
88 }
89
90 @Override
91 public String toString() {
92 return Stream.of(
93 format("modular", appDesc.moduleName() != null ? 'y' : 'n'),
94 format("main-class", mainClass),
95 format("jar-main-class", jarMainClass),
96 format("jlink", withJLink ? 'y' : 'n'),
97 format("error", expectedErrorMessage)
98 ).filter(Objects::nonNull).collect(Collectors.joining("; "));
99 }
100
248 // executePrerequisiteActions() is called by JPackageCommand instance
249 // only once.
250 cmd.executePrerequisiteActions();
251
252 final Path jarFile;
253 if (script.appDesc.moduleName() != null) {
254 jarFile = Path.of(cmd.getArgumentValue("--module-path"),
255 script.appDesc.jarFileName());
256 } else {
257 jarFile = cmd.inputDir().resolve(cmd.getArgumentValue("--main-jar"));
258 }
259
260 // Create new jar file filtering out main class from the old jar file.
261 TKit.withTempDirectory("repack-jar", workDir -> {
262 // Extract app's class from the old jar.
263 explodeJar(jarFile, workDir,
264 jarEntry -> Path.of(jarEntry.getName()).equals(
265 script.appDesc.classFilePath()));
266
267 // Create app's jar file with different main class.
268 var badAppDesc = JavaAppDesc
269 .parse(script.appDesc.toString())
270 .setClassName(nonExistingMainClass);
271 HelloApp.createBundle(badAppDesc, jarFile.getParent());
272
273 // Extract new jar but skip app's class.
274 explodeJar(jarFile, workDir,
275 jarEntry -> !Path.of(jarEntry.getName()).equals(
276 badAppDesc.classFilePath()));
277
278 // At this point we should have:
279 // 1. Manifest from the new jar referencing non-existing class
280 // as the main class.
281 // 2. Module descriptor referencing non-existing class as the main
282 // class in case of modular app.
283 // 3. App's class from the old jar. We need it to let jlink find some
284 // classes in the package declared in module descriptor
285 // in case of modular app.
286
287 Files.delete(jarFile);
288 new Executor().setToolProvider(JavaTool.JAR)
289 .addArguments("-v", "-c", "-M", "-f", jarFile.toString())
290 .addArguments("-C", workDir.toString(), ".")
291 .dumpOutput()
|