|
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; |
@@ -265,49 +264,64 @@ public void testAddModules(String... addModulesArg) { |
265 | 264 | cmd.executeAndAssertHelloAppImageCreated(); |
266 | 265 | } |
267 | 266 |
|
| 267 | + public static enum TestTempType { |
| 268 | + TEMPDIR_EMPTY, |
| 269 | + TEMPDIR_NOT_EMPTY, |
| 270 | + TEMPDIR_NOT_EXIST, |
| 271 | + } |
| 272 | + |
268 | 273 | /** |
269 | 274 | * Test --temp option. Doesn't make much sense for app image as temporary |
270 | 275 | * directory is used only on Windows. Test it in packaging mode. |
271 | | - * @throws IOException |
272 | 276 | */ |
273 | 277 | @Test |
274 | | - @Parameter("true") |
275 | | - @Parameter("false") |
276 | | - public void testTemp(boolean withExistingTempDir) throws IOException { |
| 278 | + @Parameter("TEMPDIR_EMPTY") |
| 279 | + @Parameter("TEMPDIR_NOT_EMPTY") |
| 280 | + @Parameter("TEMPDIR_NOT_EXIST") |
| 281 | + public void testTemp(TestTempType type) throws IOException { |
277 | 282 | final Path tempRoot = TKit.createTempDirectory("tmp"); |
278 | 283 |
|
279 | | - Supplier<PackageTest> createTest = () -> { |
280 | | - return new PackageTest() |
281 | | - .configureHelloApp() |
282 | | - // Force save of package bundle in test work directory. |
283 | | - .addInitializer(JPackageCommand::setDefaultInputOutput) |
284 | | - .addInitializer(cmd -> { |
285 | | - Path tempDir = getTempDirectory(cmd, tempRoot); |
286 | | - if (withExistingTempDir) { |
287 | | - Files.createDirectories(tempDir); |
288 | | - } else { |
289 | | - Files.createDirectories(tempDir.getParent()); |
| 284 | + var pkgTest = new PackageTest() |
| 285 | + .configureHelloApp() |
| 286 | + // Force save of package bundle in test work directory. |
| 287 | + .addInitializer(JPackageCommand::setDefaultInputOutput) |
| 288 | + .addInitializer(cmd -> { |
| 289 | + Path tempDir = getTempDirectory(cmd, tempRoot); |
| 290 | + switch (type) { |
| 291 | + case TEMPDIR_EMPTY -> Files.createDirectories(tempDir); |
| 292 | + case TEMPDIR_NOT_EXIST -> Files.createDirectories(tempDir.getParent()); |
| 293 | + case TEMPDIR_NOT_EMPTY -> { |
| 294 | + Files.createDirectories(tempDir); |
| 295 | + TKit.createTextFile(tempDir.resolve("foo.txt"), List.of( |
| 296 | + "Hello Duke!")); |
| 297 | + } |
290 | 298 | } |
291 | 299 | cmd.addArguments("--temp", tempDir); |
| 300 | + } |
| 301 | + ); |
| 302 | + |
| 303 | + if (TestTempType.TEMPDIR_NOT_EMPTY.equals(type)) { |
| 304 | + pkgTest.setExpectedExitCode(1).addBundleVerifier(cmd -> { |
| 305 | + // Check jpackage didn't use the supplied directory. |
| 306 | + Path tempDir = getTempDirectory(cmd, tempRoot); |
| 307 | + String[] tempDirContents = tempDir.toFile().list(); |
| 308 | + TKit.assertStringListEquals(List.of("foo.txt"), List.of( |
| 309 | + tempDirContents), String.format( |
| 310 | + "Check the contents of the supplied temporary directory [%s]", |
| 311 | + tempDir)); |
| 312 | + TKit.assertStringListEquals(List.of("Hello Duke!"), |
| 313 | + Files.readAllLines(tempDir.resolve(tempDirContents[0])), |
| 314 | + "Check the contents of the file in the supplied temporary directory"); |
292 | 315 | }); |
293 | | - }; |
| 316 | + } else { |
| 317 | + pkgTest.addBundleVerifier(cmd -> { |
| 318 | + // Check jpackage used the supplied directory. |
| 319 | + Path tempDir = getTempDirectory(cmd, tempRoot); |
| 320 | + TKit.assertPathNotEmptyDirectory(tempDir); |
| 321 | + }); |
| 322 | + } |
294 | 323 |
|
295 | | - createTest.get() |
296 | | - .addBundleVerifier(cmd -> { |
297 | | - // Check jpackage actually used the supplied directory. |
298 | | - Path tempDir = getTempDirectory(cmd, tempRoot); |
299 | | - TKit.assertNotEquals(0, tempDir.toFile().list().length, |
300 | | - String.format( |
301 | | - "Check jpackage wrote some data in the supplied temporary directory [%s]", |
302 | | - tempDir)); |
303 | | - }) |
304 | | - .run(PackageTest.Action.CREATE); |
305 | | - |
306 | | - createTest.get() |
307 | | - // Temporary directory should not be empty, |
308 | | - // jpackage should exit with error. |
309 | | - .setExpectedExitCode(1) |
310 | | - .run(PackageTest.Action.CREATE); |
| 324 | + pkgTest.run(PackageTest.Action.CREATE); |
311 | 325 | } |
312 | 326 |
|
313 | 327 | @Test |
|
0 commit comments