Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -97,8 +104,83 @@ public void applyTo(PackageTest test) {
});
}

Iterable<TestRun> getTestRuns() {
return Optional.ofNullable(testRuns).orElseGet(() -> {
return createTestRuns().addTestRunForFilenames("test").testRuns;
});
}

public static TestRunsBuilder createTestRuns() {
return new TestRunsBuilder();
}

static class TestRun {
Iterable<String> getFileNames() {
return testFileNames;
}

void openFiles(List<Path> 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<String> 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<String> 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<TestRun> 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<TestRun> testRuns;
}
16 changes: 14 additions & 2 deletions test/jdk/tools/jpackage/helpers/jdk/jpackage/test/LinuxHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -471,13 +472,24 @@ private static Path getSystemDesktopFilesFolder() {
"Failed to locate system .desktop files folder"));
}

private static void withTestFileAssociationsFile(FileAssociations fa,
ThrowingConsumer<Path> 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(
Expand All @@ -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(
Expand Down
39 changes: 22 additions & 17 deletions test/jdk/tools/jpackage/helpers/jdk/jpackage/test/PackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -227,15 +228,21 @@ public PackageTest disablePackageUninstaller() {
return this;
}

static void withTestFileAssociationsFile(FileAssociations fa,
ThrowingConsumer<Path> 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<FileAssociations.TestRun, List<Path>> consumer) {
for (var testRun : fa.getTestRuns()) {
TKit.withTempDirectory("fa-test-files", tempDir -> {
List<Path> 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,
Expand All @@ -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<String> 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.
Expand Down