187 Map<String, String> params) { 188 if (!outputFile.isAbsolute()) { 189 verifyOutputFile(outputFile.toAbsolutePath().normalize(), args, 190 params); 191 return; 192 } 193 194 TKit.assertFileExists(outputFile); 195 196 List<String> contents = ThrowingSupplier.toSupplier( 197 () -> Files.readAllLines(outputFile)).get(); 198 199 List<String> expected = new ArrayList<>(List.of( 200 "jpackage test application", 201 String.format("args.length: %d", args.size()) 202 )); 203 expected.addAll(args); 204 expected.addAll(params.entrySet().stream() 205 .sorted(Comparator.comparing(Map.Entry::getKey)) 206 .map(entry -> String.format("-D%s=%s", entry.getKey(), 207 entry.getValue())) 208 .collect(Collectors.toList())); 209 210 TKit.assertStringListEquals(expected, contents, String.format( 211 "Check contents of [%s] file", outputFile)); 212 } 213 214 public static Path createBundle(JavaAppDesc appDesc, Path outputDir) { 215 String jmodFileName = appDesc.jmodFileName(); 216 if (jmodFileName != null) { 217 final Path jmodFilePath = outputDir.resolve(jmodFileName); 218 TKit.withTempDirectory("jmod-workdir", jmodWorkDir -> { 219 var jarAppDesc = JavaAppDesc.parse(appDesc.toString()) 220 .setBundleFileName("tmp.jar"); 221 Path jarPath = createBundle(jarAppDesc, jmodWorkDir); 222 Executor exec = new Executor() 223 .setToolProvider(JavaTool.JMOD) 224 .addArguments("create", "--class-path") 225 .addArgument(jarPath) 226 .addArgument(jmodFilePath); 259 } 260 261 public static void executeLauncherAndVerifyOutput(JPackageCommand cmd, 262 String... args) { 263 final Path launcherPath = cmd.appLauncherPath(); 264 if (cmd.isFakeRuntime(String.format("Not running [%s] launcher", 265 launcherPath))) { 266 return; 267 } 268 269 assertApp(launcherPath) 270 .addDefaultArguments(Optional 271 .ofNullable(cmd.getAllArgumentValues("--arguments")) 272 .orElseGet(() -> new String[0])) 273 .addJavaOptions(Optional 274 .ofNullable(cmd.getAllArgumentValues("--java-options")) 275 .orElseGet(() -> new String[0])) 276 .executeAndVerifyOutput(args); 277 } 278 279 public final static class AppOutputVerifier { 280 AppOutputVerifier(Path helloAppLauncher) { 281 this.launcherPath = helloAppLauncher; 282 this.params = new HashMap<>(); 283 this.defaultLauncherArgs = new ArrayList<>(); 284 } 285 286 public AppOutputVerifier addDefaultArguments(String... v) { 287 return addDefaultArguments(List.of(v)); 288 } 289 290 public AppOutputVerifier addDefaultArguments(Collection<String> v) { 291 defaultLauncherArgs.addAll(v); 292 return this; 293 } 294 295 public AppOutputVerifier addParam(String name, String value) { 296 if (name.startsWith("param")) { 297 params.put(name, value); 298 } 300 } 301 302 public AppOutputVerifier addParams(Collection<Map.Entry<String, String>> v) { 303 v.forEach(entry -> addParam(entry.getKey(), entry.getValue())); 304 return this; 305 } 306 public AppOutputVerifier addParams(Map<String, String> v) { 307 return addParams(v.entrySet()); 308 } 309 310 public AppOutputVerifier addParams(Map.Entry<String, String>... v) { 311 return addParams(List.of(v)); 312 } 313 314 public AppOutputVerifier addJavaOptions(String... v) { 315 return addJavaOptions(List.of(v)); 316 } 317 318 public AppOutputVerifier addJavaOptions(Collection<String> v) { 319 return addParams(v.stream() 320 .filter(javaOpt -> javaOpt.startsWith("-D")) 321 .map(javaOpt -> { 322 var components = javaOpt.split("=", 2); 323 return Map.entry(components[0].substring(2), components[1]); 324 }) 325 .collect(Collectors.toList())); 326 } 327 328 public void executeAndVerifyOutput(String... args) { 329 // Output file will be created in the current directory. 330 Path outputFile = TKit.workDir().resolve(OUTPUT_FILENAME); 331 ThrowingFunction.toFunction(Files::deleteIfExists).apply(outputFile); 332 333 final Path executablePath; 334 if (launcherPath.isAbsolute()) { 335 executablePath = launcherPath; 336 } else { 337 // Make sure path to executable is relative to the current directory. 338 executablePath = Path.of(".").resolve(launcherPath.normalize()); 339 } 340 341 final List<String> launcherArgs = List.of(args); 342 new Executor() 343 .setDirectory(outputFile.getParent()) 344 .setExecutable(executablePath) 345 .addArguments(launcherArgs) 346 .dumpOutput() 347 .execute(); 348 349 final List<String> appArgs; 350 if (launcherArgs.isEmpty()) { 351 appArgs = defaultLauncherArgs; 352 } else { 353 appArgs = launcherArgs; 354 } 355 356 verifyOutputFile(outputFile, appArgs, params); 357 } 358 359 private final Path launcherPath; 360 private final List<String> defaultLauncherArgs; 361 private final Map<String, String> params; 362 } 363 364 public static AppOutputVerifier assertApp(Path helloAppLauncher) { 365 return new AppOutputVerifier(helloAppLauncher); 366 } 367 368 final static String OUTPUT_FILENAME = "appOutput.txt"; 369 370 private final JavaAppDesc appDesc; 371 372 private static final Path HELLO_JAVA = TKit.TEST_SRC_ROOT.resolve( 373 "apps/image/Hello.java"); 374 375 private final static String CLASS_NAME = HELLO_JAVA.getFileName().toString().split( 376 "\\.", 2)[0]; | 187 Map<String, String> params) { 188 if (!outputFile.isAbsolute()) { 189 verifyOutputFile(outputFile.toAbsolutePath().normalize(), args, 190 params); 191 return; 192 } 193 194 TKit.assertFileExists(outputFile); 195 196 List<String> contents = ThrowingSupplier.toSupplier( 197 () -> Files.readAllLines(outputFile)).get(); 198 199 List<String> expected = new ArrayList<>(List.of( 200 "jpackage test application", 201 String.format("args.length: %d", args.size()) 202 )); 203 expected.addAll(args); 204 expected.addAll(params.entrySet().stream() 205 .sorted(Comparator.comparing(Map.Entry::getKey)) 206 .map(entry -> String.format("-D%s=%s", entry.getKey(), 207 /* 208 unQuote(entry.getValue()))) 209 */ 210 entry.getValue())) 211 .collect(Collectors.toList())); 212 213 TKit.assertStringListEquals(expected, contents, String.format( 214 "Check contents of [%s] file", outputFile)); 215 } 216 217 public static Path createBundle(JavaAppDesc appDesc, Path outputDir) { 218 String jmodFileName = appDesc.jmodFileName(); 219 if (jmodFileName != null) { 220 final Path jmodFilePath = outputDir.resolve(jmodFileName); 221 TKit.withTempDirectory("jmod-workdir", jmodWorkDir -> { 222 var jarAppDesc = JavaAppDesc.parse(appDesc.toString()) 223 .setBundleFileName("tmp.jar"); 224 Path jarPath = createBundle(jarAppDesc, jmodWorkDir); 225 Executor exec = new Executor() 226 .setToolProvider(JavaTool.JMOD) 227 .addArguments("create", "--class-path") 228 .addArgument(jarPath) 229 .addArgument(jmodFilePath); 262 } 263 264 public static void executeLauncherAndVerifyOutput(JPackageCommand cmd, 265 String... args) { 266 final Path launcherPath = cmd.appLauncherPath(); 267 if (cmd.isFakeRuntime(String.format("Not running [%s] launcher", 268 launcherPath))) { 269 return; 270 } 271 272 assertApp(launcherPath) 273 .addDefaultArguments(Optional 274 .ofNullable(cmd.getAllArgumentValues("--arguments")) 275 .orElseGet(() -> new String[0])) 276 .addJavaOptions(Optional 277 .ofNullable(cmd.getAllArgumentValues("--java-options")) 278 .orElseGet(() -> new String[0])) 279 .executeAndVerifyOutput(args); 280 } 281 282 public static Executor.Result executeLauncher(JPackageCommand cmd, 283 String... args) { 284 final Path launcherPath = cmd.appLauncherPath(); 285 if (cmd.isFakeRuntime(String.format("Not running [%s] launcher", 286 launcherPath))) { 287 return null; 288 } 289 290 return assertApp(launcherPath) 291 .addDefaultArguments(Optional 292 .ofNullable(cmd.getAllArgumentValues("--arguments")) 293 .orElseGet(() -> new String[0])) 294 .addJavaOptions(Optional 295 .ofNullable(cmd.getAllArgumentValues("--java-options")) 296 .orElseGet(() -> new String[0])) 297 .executeOnly(args); 298 } 299 300 private static String unQuote(String s) { 301 return ((s.startsWith("\"") && s.endsWith("\"")) || 302 (s.startsWith("'") && s.endsWith("'"))) ? 303 s.substring(1, s.length() - 1) : s; 304 } 305 306 public final static class AppOutputVerifier { 307 AppOutputVerifier(Path helloAppLauncher) { 308 this.launcherPath = helloAppLauncher; 309 this.params = new HashMap<>(); 310 this.defaultLauncherArgs = new ArrayList<>(); 311 } 312 313 public AppOutputVerifier addDefaultArguments(String... v) { 314 return addDefaultArguments(List.of(v)); 315 } 316 317 public AppOutputVerifier addDefaultArguments(Collection<String> v) { 318 defaultLauncherArgs.addAll(v); 319 return this; 320 } 321 322 public AppOutputVerifier addParam(String name, String value) { 323 if (name.startsWith("param")) { 324 params.put(name, value); 325 } 327 } 328 329 public AppOutputVerifier addParams(Collection<Map.Entry<String, String>> v) { 330 v.forEach(entry -> addParam(entry.getKey(), entry.getValue())); 331 return this; 332 } 333 public AppOutputVerifier addParams(Map<String, String> v) { 334 return addParams(v.entrySet()); 335 } 336 337 public AppOutputVerifier addParams(Map.Entry<String, String>... v) { 338 return addParams(List.of(v)); 339 } 340 341 public AppOutputVerifier addJavaOptions(String... v) { 342 return addJavaOptions(List.of(v)); 343 } 344 345 public AppOutputVerifier addJavaOptions(Collection<String> v) { 346 return addParams(v.stream() 347 /* 348 .filter(javaOpt -> unQuote(javaOpt).startsWith("-D")) 349 */ 350 .filter(javaOpt -> javaOpt.startsWith("-D")) 351 .map(javaOpt -> { 352 /* 353 var components = unQuote(javaOpt).split("=", 2); 354 */ 355 var components = javaOpt.split("=", 2); 356 return Map.entry(components[0].substring(2), components[1]); 357 }) 358 .collect(Collectors.toList())); 359 } 360 361 public void executeAndVerifyOutput(String... args) { 362 // Output file will be created in the current directory. 363 Path outputFile = TKit.workDir().resolve(OUTPUT_FILENAME); 364 ThrowingFunction.toFunction(Files::deleteIfExists).apply(outputFile); 365 366 final Path executablePath; 367 if (launcherPath.isAbsolute()) { 368 executablePath = launcherPath; 369 } else { 370 // Make sure path to executable is relative to the current directory. 371 executablePath = Path.of(".").resolve(launcherPath.normalize()); 372 } 373 374 final List<String> launcherArgs = List.of(args); 375 new Executor() 376 .setDirectory(outputFile.getParent()) 377 .setExecutable(executablePath) 378 .addArguments(launcherArgs) 379 .dumpOutput() 380 .execute(); 381 382 final List<String> appArgs; 383 if (launcherArgs.isEmpty()) { 384 appArgs = defaultLauncherArgs; 385 } else { 386 appArgs = launcherArgs; 387 } 388 389 verifyOutputFile(outputFile, appArgs, params); 390 } 391 392 public Executor.Result executeOnly(String...args) { 393 // Output file might be created in the current directory. 394 Path outputFile = TKit.workDir().resolve(OUTPUT_FILENAME); 395 ThrowingFunction.toFunction(Files::deleteIfExists).apply(outputFile); 396 397 final Path executablePath; 398 if (launcherPath.isAbsolute()) { 399 executablePath = launcherPath; 400 } else { 401 // Make sure path to executable is relative to the current directory. 402 executablePath = Path.of(".").resolve(launcherPath.normalize()); 403 } 404 405 final List<String> launcherArgs = List.of(args); 406 return new Executor() 407 .setDirectory(outputFile.getParent()) 408 .setExecutable(executablePath) 409 .addArguments(launcherArgs) 410 .saveOutput() 411 .executeWithoutExitCodeCheck(); 412 } 413 414 private final Path launcherPath; 415 private final List<String> defaultLauncherArgs; 416 private final Map<String, String> params; 417 } 418 419 public static AppOutputVerifier assertApp(Path helloAppLauncher) { 420 return new AppOutputVerifier(helloAppLauncher); 421 } 422 423 final static String OUTPUT_FILENAME = "appOutput.txt"; 424 425 private final JavaAppDesc appDesc; 426 427 private static final Path HELLO_JAVA = TKit.TEST_SRC_ROOT.resolve( 428 "apps/image/Hello.java"); 429 430 private final static String CLASS_NAME = HELLO_JAVA.getFileName().toString().split( 431 "\\.", 2)[0]; |