test/jdk/tools/jpackage/helpers/jdk/jpackage/test/PackageTest.java
branchJDK-8200758-branch
changeset 58304 7a61351edad2
parent 58301 e0efb29609bd
child 58416 f09bf58c1f17
equal deleted inserted replaced
58303:88453b906981 58304:7a61351edad2
    22  */
    22  */
    23 package jdk.jpackage.test;
    23 package jdk.jpackage.test;
    24 
    24 
    25 import java.awt.Desktop;
    25 import java.awt.Desktop;
    26 import java.io.File;
    26 import java.io.File;
    27 import java.io.IOException;
       
    28 import java.nio.file.Files;
    27 import java.nio.file.Files;
    29 import java.nio.file.Path;
    28 import java.nio.file.Path;
    30 import java.util.ArrayList;
    29 import java.util.ArrayList;
    31 import java.util.Collection;
    30 import java.util.Collection;
    32 import java.util.HashMap;
    31 import java.util.HashMap;
    40 import java.util.function.Supplier;
    39 import java.util.function.Supplier;
    41 import java.util.stream.Collectors;
    40 import java.util.stream.Collectors;
    42 import java.util.stream.Stream;
    41 import java.util.stream.Stream;
    43 import static jdk.jpackage.test.PackageType.LINUX_DEB;
    42 import static jdk.jpackage.test.PackageType.LINUX_DEB;
    44 import static jdk.jpackage.test.PackageType.LINUX_RPM;
    43 import static jdk.jpackage.test.PackageType.LINUX_RPM;
       
    44 import jdk.jpackage.test.Functional.ThrowingConsumer;
    45 
    45 
    46 /**
    46 /**
    47  * Instance of PackageTest is for configuring and running a single jpackage
    47  * Instance of PackageTest is for configuring and running a single jpackage
    48  * command to produce platform specific package bundle.
    48  * command to produce platform specific package bundle.
    49  *
    49  *
    50  * Provides methods hook up custom configuration of jpackage command and
    50  * Provides methods to hook up custom configuration of jpackage command and
    51  * verification of the output bundle.
    51  * verification of the output bundle.
    52  */
    52  */
    53 public final class PackageTest {
    53 public final class PackageTest {
    54 
    54 
    55     /**
    55     /**
    89     public PackageTest setJPackageExitCode(int v) {
    89     public PackageTest setJPackageExitCode(int v) {
    90         expectedJPackageExitCode = v;
    90         expectedJPackageExitCode = v;
    91         return this;
    91         return this;
    92     }
    92     }
    93 
    93 
    94     private PackageTest addInitializer(Consumer<JPackageCommand> v, String id) {
    94     private PackageTest addInitializer(ThrowingConsumer<JPackageCommand> v, String id) {
    95         if (id != null) {
    95         if (id != null) {
    96             if (namedInitializers.contains(id)) {
    96             if (namedInitializers.contains(id)) {
    97                 return this;
    97                 return this;
    98             }
    98             }
    99 
    99 
   100             namedInitializers.add(id);
   100             namedInitializers.add(id);
   101         }
   101         }
   102         currentTypes.stream().forEach(type -> handlers.get(type).addInitializer(
   102         currentTypes.stream().forEach(type -> handlers.get(type).addInitializer(
   103                 v));
   103                 ThrowingConsumer.toConsumer(v)));
   104         return this;
   104         return this;
   105     }
   105     }
   106 
   106 
   107     public PackageTest addInitializer(Consumer<JPackageCommand> v) {
   107     public PackageTest addInitializer(ThrowingConsumer<JPackageCommand> v) {
   108         return addInitializer(v, null);
   108         return addInitializer(v, null);
   109     }
   109     }
   110 
   110 
   111     public PackageTest addBundleVerifier(
   111     public PackageTest addBundleVerifier(
   112             BiConsumer<JPackageCommand, Executor.Result> v) {
   112             BiConsumer<JPackageCommand, Executor.Result> v) {
   113         currentTypes.stream().forEach(
   113         currentTypes.stream().forEach(
   114                 type -> handlers.get(type).addBundleVerifier(v));
   114                 type -> handlers.get(type).addBundleVerifier(v));
   115         return this;
   115         return this;
   116     }
   116     }
   117 
   117 
   118     public PackageTest addBundleVerifier(Consumer<JPackageCommand> v) {
   118     public PackageTest addBundleVerifier(ThrowingConsumer<JPackageCommand> v) {
   119         return addBundleVerifier((cmd, unused) -> v.accept(cmd));
   119         return addBundleVerifier(
       
   120                 (cmd, unused) -> ThrowingConsumer.toConsumer(v).accept(cmd));
   120     }
   121     }
   121 
   122 
   122     public PackageTest addBundlePropertyVerifier(String propertyName,
   123     public PackageTest addBundlePropertyVerifier(String propertyName,
   123             BiConsumer<String, String> pred) {
   124             BiConsumer<String, String> pred) {
   124         return addBundleVerifier(cmd -> {
   125         return addBundleVerifier(cmd -> {
   155             LinuxHelper.addDebBundleDesktopIntegrationVerifier(this, integrated);
   156             LinuxHelper.addDebBundleDesktopIntegrationVerifier(this, integrated);
   156         });
   157         });
   157         return this;
   158         return this;
   158     }
   159     }
   159 
   160 
   160     public PackageTest addInstallVerifier(Consumer<JPackageCommand> v) {
   161     public PackageTest addInstallVerifier(ThrowingConsumer<JPackageCommand> v) {
   161         currentTypes.stream().forEach(
   162         currentTypes.stream().forEach(
   162                 type -> handlers.get(type).addInstallVerifier(v));
   163                 type -> handlers.get(type).addInstallVerifier(
   163         return this;
   164                         ThrowingConsumer.toConsumer(v)));
   164     }
   165         return this;
   165 
   166     }
   166     public PackageTest addUninstallVerifier(Consumer<JPackageCommand> v) {
   167 
       
   168     public PackageTest addUninstallVerifier(ThrowingConsumer<JPackageCommand> v) {
   167         currentTypes.stream().forEach(
   169         currentTypes.stream().forEach(
   168                 type -> handlers.get(type).addUninstallVerifier(v));
   170                 type -> handlers.get(type).addUninstallVerifier(
       
   171                         ThrowingConsumer.toConsumer(v)));
   169         return this;
   172         return this;
   170     }
   173     }
   171 
   174 
   172     public PackageTest addHelloAppFileAssociationsVerifier(FileAssociations fa,
   175     public PackageTest addHelloAppFileAssociationsVerifier(FileAssociations fa,
   173             String... faLauncherDefaultArgs) {
   176             String... faLauncherDefaultArgs) {
   178                     "Not running file associations test")) {
   181                     "Not running file associations test")) {
   179                 return;
   182                 return;
   180             }
   183             }
   181 
   184 
   182             Test.withTempFile(fa.getSuffix(), testFile -> {
   185             Test.withTempFile(fa.getSuffix(), testFile -> {
       
   186                 testFile = testFile.toAbsolutePath().normalize();
   183                 if (PackageType.LINUX.contains(cmd.packageType())) {
   187                 if (PackageType.LINUX.contains(cmd.packageType())) {
   184                     LinuxHelper.initFileAssociationsTestFile(testFile);
   188                     LinuxHelper.initFileAssociationsTestFile(testFile);
   185                 }
   189                 }
   186 
   190 
   187                 try {
   191                 final Path appOutput = Path.of(HelloApp.OUTPUT_FILENAME);
   188                     final Path appOutput = Path.of(HelloApp.OUTPUT_FILENAME);
   192                 Files.deleteIfExists(appOutput);
   189                     Files.deleteIfExists(appOutput);
   193 
   190 
   194                 Test.trace(String.format("Use desktop to open [%s] file",
   191                     Test.trace(String.format("Use desktop to open [%s] file",
   195                         testFile));
   192                             testFile));
   196                 Desktop.getDesktop().open(testFile.toFile());
   193                     Desktop.getDesktop().open(testFile.toFile());
   197                 Test.waitForFileCreated(appOutput, 7);
   194                     Test.waitForFileCreated(appOutput, 7);
   198 
   195 
   199                 List<String> expectedArgs = new ArrayList<>(List.of(
   196                     List<String> expectedArgs = new ArrayList<>(List.of(
   200                         faLauncherDefaultArgs));
   197                             faLauncherDefaultArgs));
   201                 expectedArgs.add(testFile.toString());
   198                     expectedArgs.add(testFile.toString());
   202 
   199 
   203                 // Wait a little bit after file has been created to
   200                     // Wait a little bit after file has been created to
   204                 // make sure there are no pending writes into it.
   201                     // make sure there are no pending writes into it.
   205                 Thread.sleep(3000);
   202                     Thread.sleep(3000);
   206                 HelloApp.verifyOutputFile(appOutput, expectedArgs.toArray(String[]::new));
   203                     HelloApp.verifyOutputFile(appOutput, expectedArgs.toArray(
       
   204                             String[]::new));
       
   205                 } catch (IOException | InterruptedException ex) {
       
   206                     throw new RuntimeException(ex);
       
   207                 }
       
   208             });
   207             });
   209         });
   208         });
   210 
   209 
   211         forTypes(PackageType.LINUX, () -> {
   210         forTypes(PackageType.LINUX, () -> {
   212             LinuxHelper.addFileAssociationsVerifier(this, fa);
   211             LinuxHelper.addFileAssociationsVerifier(this, fa);
   343             Test.trace(String.format("Verify installed: %s",
   342             Test.trace(String.format("Verify installed: %s",
   344                     cmd.getPrintableCommandLine()));
   343                     cmd.getPrintableCommandLine()));
   345             if (cmd.isRuntime()) {
   344             if (cmd.isRuntime()) {
   346                 Test.assertDirectoryExists(
   345                 Test.assertDirectoryExists(
   347                         cmd.appRuntimeInstallationDirectory(), false);
   346                         cmd.appRuntimeInstallationDirectory(), false);
   348                 Test.assertDirectoryExists(
       
   349                         cmd.appInstallationDirectory().resolve("app"), false);
       
   350             } else {
   347             } else {
   351                 Test.assertExecutableFileExists(cmd.launcherInstallationPath(),
   348                 Test.assertExecutableFileExists(cmd.launcherInstallationPath(), true);
   352                         true);
       
   353             }
   349             }
   354 
   350 
   355             if (PackageType.WINDOWS.contains(cmd.packageType())) {
   351             if (PackageType.WINDOWS.contains(cmd.packageType())) {
   356                 new WindowsHelper.AppVerifier(cmd);
   352                 new WindowsHelper.AppVerifier(cmd);
   357             }
   353             }
   361 
   357 
   362         private void verifyPackageUninstalled(JPackageCommand cmd) {
   358         private void verifyPackageUninstalled(JPackageCommand cmd) {
   363             Test.trace(String.format("Verify uninstalled: %s",
   359             Test.trace(String.format("Verify uninstalled: %s",
   364                     cmd.getPrintableCommandLine()));
   360                     cmd.getPrintableCommandLine()));
   365             if (!cmd.isRuntime()) {
   361             if (!cmd.isRuntime()) {
   366                 Test.assertFileExists(cmd.launcherInstallationPath(), false);
   362                 Test.assertPathExists(cmd.launcherInstallationPath(), false);
   367                 Test.assertDirectoryExists(cmd.appInstallationDirectory(), false);
   363                 Test.assertPathExists(cmd.appInstallationDirectory(), false);
   368             }
   364             }
   369 
   365 
   370             if (PackageType.WINDOWS.contains(cmd.packageType())) {
   366             if (PackageType.WINDOWS.contains(cmd.packageType())) {
   371                 new WindowsHelper.AppVerifier(cmd);
   367                 new WindowsHelper.AppVerifier(cmd);
   372             }
   368             }