diff --git a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/FileAssociations.java b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/FileAssociations.java index ceb0dedcb6fa8..b3527e0b4ff33 100644 --- a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/FileAssociations.java +++ b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/FileAssociations.java @@ -22,9 +22,16 @@ */ package jdk.jpackage.test; +import java.awt.Desktop; +import java.io.IOException; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Optional; import jdk.jpackage.internal.IOUtils; @@ -97,8 +104,83 @@ public void applyTo(PackageTest test) { }); } + Iterable getTestRuns() { + return Optional.ofNullable(testRuns).orElseGet(() -> { + return createTestRuns().addTestRunForFilenames("test").testRuns; + }); + } + + public static TestRunsBuilder createTestRuns() { + return new TestRunsBuilder(); + } + + static class TestRun { + Iterable getFileNames() { + return testFileNames; + } + + void openFiles(List testFiles) throws IOException { + switch (invocationType) { + case DesktopOpenAssociatedFile: + TKit.trace(String.format("Use desktop to open [%s] file", testFiles.get(0))); + Desktop.getDesktop().open(testFiles.get(0).toFile()); + break; + + case WinCommandLine: + case WinDesktopOpenContextMenu: + // TBD: implement + } + } + + private TestRun(Collection testFileNames, + InvocationType invocationType) { + + Objects.requireNonNull(invocationType); + + if (testFileNames.size() == 0) { + throw new IllegalArgumentException("Empty test file names list"); + } + + if (invocationType == InvocationType.DesktopOpenAssociatedFile && testFileNames.size() != 1) { + throw new IllegalArgumentException("Only one file can be configured for opening with the desktop"); + } + + this.testFileNames = testFileNames; + this.invocationType = invocationType; + } + + private final Collection testFileNames; + private final InvocationType invocationType; + } + + public static class TestRunsBuilder { + public TestRunsBuilder setCurrentInvocationType(InvocationType v) { + curInvocationType = v; + return this; + } + + public TestRunsBuilder addTestRunForFilenames(String ... filenames) { + testRuns.add(new TestRun(List.of(filenames), curInvocationType)); + return this; + } + + public void applyTo(FileAssociations fa) { + fa.testRuns = testRuns; + } + + private InvocationType curInvocationType = InvocationType.DesktopOpenAssociatedFile; + private List testRuns = new ArrayList<>(); + } + + public static enum InvocationType { + DesktopOpenAssociatedFile, + WinCommandLine, + WinDesktopOpenContextMenu + } + private Path file; final private String suffixName; private String description; private Path icon; + private Collection testRuns; } diff --git a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/LinuxHelper.java b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/LinuxHelper.java index 3b45319553dd4..44212587f7dd9 100644 --- a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/LinuxHelper.java +++ b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/LinuxHelper.java @@ -41,6 +41,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import jdk.jpackage.internal.IOUtils; +import jdk.jpackage.test.Functional.ThrowingConsumer; import jdk.jpackage.test.PackageTest.PackageHandlers; @@ -471,13 +472,24 @@ private static Path getSystemDesktopFilesFolder() { "Failed to locate system .desktop files folder")); } + private static void withTestFileAssociationsFile(FileAssociations fa, + ThrowingConsumer consumer) { + boolean iterated[] = new boolean[] { false }; + PackageTest.withFileAssociationsTestRuns(fa, (testRun, testFiles) -> { + if (!iterated[0]) { + iterated[0] = true; + consumer.accept(testFiles.get(0)); + } + }); + } + static void addFileAssociationsVerifier(PackageTest test, FileAssociations fa) { test.addInstallVerifier(cmd -> { if (cmd.isPackageUnpacked("Not running file associations checks")) { return; } - PackageTest.withTestFileAssociationsFile(fa, testFile -> { + withTestFileAssociationsFile(fa, testFile -> { String mimeType = queryFileMimeType(testFile); TKit.assertEquals(fa.getMime(), mimeType, String.format( @@ -501,7 +513,7 @@ static void addFileAssociationsVerifier(PackageTest test, FileAssociations fa) { }); test.addUninstallVerifier(cmd -> { - PackageTest.withTestFileAssociationsFile(fa, testFile -> { + withTestFileAssociationsFile(fa, testFile -> { String mimeType = queryFileMimeType(testFile); TKit.assertNotEquals(fa.getMime(), mimeType, String.format( diff --git a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/PackageTest.java b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/PackageTest.java index bd5fd7ca65bd1..a36fc70ef59a2 100644 --- a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/PackageTest.java +++ b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/PackageTest.java @@ -46,6 +46,7 @@ import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.util.stream.StreamSupport; import jdk.jpackage.internal.ApplicationLayout; import jdk.jpackage.test.Functional.ThrowingBiConsumer; import static jdk.jpackage.test.Functional.ThrowingBiConsumer.toBiConsumer; @@ -227,15 +228,21 @@ public PackageTest disablePackageUninstaller() { return this; } - static void withTestFileAssociationsFile(FileAssociations fa, - ThrowingConsumer consumer) { - final Path testFileDefaultName = Path.of("test" + fa.getSuffix()); - TKit.withTempFile(testFileDefaultName, testFile -> { - if (TKit.isLinux()) { - LinuxHelper.initFileAssociationsTestFile(testFile); - } - consumer.accept(testFile); - }); + static void withFileAssociationsTestRuns(FileAssociations fa, + ThrowingBiConsumer> consumer) { + for (var testRun : fa.getTestRuns()) { + TKit.withTempDirectory("fa-test-files", tempDir -> { + List testFiles = StreamSupport.stream(testRun.getFileNames().spliterator(), false).map(fname -> { + return tempDir.resolve(fname + fa.getSuffix()).toAbsolutePath().normalize(); + }).toList(); + + if (TKit.isLinux()) { + testFiles.forEach(LinuxHelper::initFileAssociationsTestFile); + } + + consumer.accept(testRun, testFiles); + }); + } } PackageTest addHelloAppFileAssociationsVerifier(FileAssociations fa, @@ -261,21 +268,19 @@ PackageTest addHelloAppFileAssociationsVerifier(FileAssociations fa, return; } - withTestFileAssociationsFile(fa, testFile -> { - testFile = testFile.toAbsolutePath().normalize(); - - final Path appOutput = testFile.getParent() + withFileAssociationsTestRuns(fa, (testRun, testFiles) -> { + final Path appOutput = testFiles.get(0).getParent() .resolve(HelloApp.OUTPUT_FILENAME); Files.deleteIfExists(appOutput); - TKit.trace(String.format("Use desktop to open [%s] file", - testFile)); - Desktop.getDesktop().open(testFile.toFile()); + testRun.openFiles(testFiles); TKit.waitForFileCreated(appOutput, 7); List expectedArgs = new ArrayList<>(List.of( faLauncherDefaultArgs)); - expectedArgs.add(testFile.toString()); + testFiles.forEach(testFile -> { + expectedArgs.add(testFile.toString()); + }); // Wait a little bit after file has been created to // make sure there are no pending writes into it.