|
1 | 1 | /* |
2 | | - * Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
30 | 30 | import java.util.ArrayList; |
31 | 31 | import java.util.function.Function; |
32 | 32 | import java.util.function.Predicate; |
33 | | -import java.util.function.Supplier; |
34 | 33 | import java.util.regex.Pattern; |
35 | 34 | import java.util.stream.Stream; |
36 | 35 | import jdk.jpackage.test.TKit; |
@@ -264,49 +263,64 @@ public void testAddModules(String... addModulesArg) { |
264 | 263 | cmd.executeAndAssertHelloAppImageCreated(); |
265 | 264 | } |
266 | 265 |
|
| 266 | + public static enum TestTempType { |
| 267 | + TEMPDIR_EMPTY, |
| 268 | + TEMPDIR_NOT_EMPTY, |
| 269 | + TEMPDIR_NOT_EXIST, |
| 270 | + } |
| 271 | + |
267 | 272 | /** |
268 | 273 | * Test --temp option. Doesn't make much sense for app image as temporary |
269 | 274 | * directory is used only on Windows. Test it in packaging mode. |
270 | | - * @throws IOException |
271 | 275 | */ |
272 | 276 | @Test |
273 | | - @Parameter("true") |
274 | | - @Parameter("false") |
275 | | - public void testTemp(boolean withExistingTempDir) throws IOException { |
| 277 | + @Parameter("TEMPDIR_EMPTY") |
| 278 | + @Parameter("TEMPDIR_NOT_EMPTY") |
| 279 | + @Parameter("TEMPDIR_NOT_EXIST") |
| 280 | + public void testTemp(TestTempType type) throws IOException { |
276 | 281 | final Path tempRoot = TKit.createTempDirectory("tmp"); |
277 | 282 |
|
278 | | - Supplier<PackageTest> createTest = () -> { |
279 | | - return new PackageTest() |
280 | | - .configureHelloApp() |
281 | | - // Force save of package bundle in test work directory. |
282 | | - .addInitializer(JPackageCommand::setDefaultInputOutput) |
283 | | - .addInitializer(cmd -> { |
284 | | - Path tempDir = getTempDirectory(cmd, tempRoot); |
285 | | - if (withExistingTempDir) { |
286 | | - Files.createDirectories(tempDir); |
287 | | - } else { |
288 | | - Files.createDirectories(tempDir.getParent()); |
| 283 | + var pkgTest = new PackageTest() |
| 284 | + .configureHelloApp() |
| 285 | + // Force save of package bundle in test work directory. |
| 286 | + .addInitializer(JPackageCommand::setDefaultInputOutput) |
| 287 | + .addInitializer(cmd -> { |
| 288 | + Path tempDir = getTempDirectory(cmd, tempRoot); |
| 289 | + switch (type) { |
| 290 | + case TEMPDIR_EMPTY -> Files.createDirectories(tempDir); |
| 291 | + case TEMPDIR_NOT_EXIST -> Files.createDirectories(tempDir.getParent()); |
| 292 | + case TEMPDIR_NOT_EMPTY -> { |
| 293 | + Files.createDirectories(tempDir); |
| 294 | + TKit.createTextFile(tempDir.resolve("foo.txt"), List.of( |
| 295 | + "Hello Duke!")); |
| 296 | + } |
289 | 297 | } |
290 | 298 | cmd.addArguments("--temp", tempDir); |
| 299 | + } |
| 300 | + ); |
| 301 | + |
| 302 | + if (TestTempType.TEMPDIR_NOT_EMPTY.equals(type)) { |
| 303 | + pkgTest.setExpectedExitCode(1).addBundleVerifier(cmd -> { |
| 304 | + // Check jpackage didn't use the supplied directory. |
| 305 | + Path tempDir = getTempDirectory(cmd, tempRoot); |
| 306 | + String[] tempDirContents = tempDir.toFile().list(); |
| 307 | + TKit.assertStringListEquals(List.of("foo.txt"), List.of( |
| 308 | + tempDirContents), String.format( |
| 309 | + "Check the contents of the supplied temporary directory [%s]", |
| 310 | + tempDir)); |
| 311 | + TKit.assertStringListEquals(List.of("Hello Duke!"), |
| 312 | + Files.readAllLines(tempDir.resolve(tempDirContents[0])), |
| 313 | + "Check the contents of the file in the supplied temporary directory"); |
291 | 314 | }); |
292 | | - }; |
| 315 | + } else { |
| 316 | + pkgTest.addBundleVerifier(cmd -> { |
| 317 | + // Check jpackage used the supplied directory. |
| 318 | + Path tempDir = getTempDirectory(cmd, tempRoot); |
| 319 | + TKit.assertDirectoryNotEmpty(tempDir); |
| 320 | + }); |
| 321 | + } |
293 | 322 |
|
294 | | - createTest.get() |
295 | | - .addBundleVerifier(cmd -> { |
296 | | - // Check jpackage actually used the supplied directory. |
297 | | - Path tempDir = getTempDirectory(cmd, tempRoot); |
298 | | - TKit.assertNotEquals(0, tempDir.toFile().list().length, |
299 | | - String.format( |
300 | | - "Check jpackage wrote some data in the supplied temporary directory [%s]", |
301 | | - tempDir)); |
302 | | - }) |
303 | | - .run(PackageTest.Action.CREATE); |
304 | | - |
305 | | - createTest.get() |
306 | | - // Temporary directory should not be empty, |
307 | | - // jpackage should exit with error. |
308 | | - .setExpectedExitCode(1) |
309 | | - .run(PackageTest.Action.CREATE); |
| 323 | + pkgTest.run(PackageTest.Action.CREATE); |
310 | 324 | } |
311 | 325 |
|
312 | 326 | @Test |
|
0 commit comments