test/jdk/tools/jpackage/helpers/jdk/jpackage/test/PackageTest.java
branchJDK-8200758-branch
changeset 58416 f09bf58c1f17
parent 58304 7a61351edad2
child 58417 67ffaf3a2b75
equal deleted inserted replaced
58415:73f8e557549a 58416:f09bf58c1f17
    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.nio.file.Files;
    27 import java.nio.file.Files;
    28 import java.nio.file.Path;
    28 import java.nio.file.Path;
    29 import java.util.ArrayList;
    29 import java.util.*;
    30 import java.util.Collection;
       
    31 import java.util.HashMap;
       
    32 import java.util.HashSet;
       
    33 import java.util.List;
       
    34 import java.util.Map;
       
    35 import java.util.Optional;
       
    36 import java.util.Set;
       
    37 import java.util.function.BiConsumer;
    30 import java.util.function.BiConsumer;
    38 import java.util.function.Consumer;
    31 import java.util.function.Consumer;
    39 import java.util.function.Supplier;
    32 import java.util.function.Supplier;
    40 import java.util.stream.Collectors;
    33 import java.util.stream.Collectors;
    41 import java.util.stream.Stream;
    34 import java.util.stream.Stream;
    42 import static jdk.jpackage.test.PackageType.LINUX_DEB;
       
    43 import static jdk.jpackage.test.PackageType.LINUX_RPM;
       
    44 import jdk.jpackage.test.Functional.ThrowingConsumer;
    35 import jdk.jpackage.test.Functional.ThrowingConsumer;
       
    36 import static jdk.jpackage.test.PackageType.*;
    45 
    37 
    46 /**
    38 /**
    47  * Instance of PackageTest is for configuring and running a single jpackage
    39  * Instance of PackageTest is for configuring and running a single jpackage
    48  * command to produce platform specific package bundle.
    40  * command to produce platform specific package bundle.
    49  *
    41  *
   121     }
   113     }
   122 
   114 
   123     public PackageTest addBundlePropertyVerifier(String propertyName,
   115     public PackageTest addBundlePropertyVerifier(String propertyName,
   124             BiConsumer<String, String> pred) {
   116             BiConsumer<String, String> pred) {
   125         return addBundleVerifier(cmd -> {
   117         return addBundleVerifier(cmd -> {
   126             String propertyValue = null;
   118             pred.accept(propertyName,
   127             switch (cmd.packageType()) {
   119                     LinuxHelper.getBundleProperty(cmd, propertyName));
   128                 case LINUX_DEB:
       
   129                     propertyValue = LinuxHelper.getDebBundleProperty(
       
   130                             cmd.outputBundle(), propertyName);
       
   131                     break;
       
   132 
       
   133                 case LINUX_RPM:
       
   134                     propertyValue = LinuxHelper.getRpmBundleProperty(
       
   135                             cmd.outputBundle(), propertyName);
       
   136                     break;
       
   137 
       
   138                 default:
       
   139                     throw new UnsupportedOperationException();
       
   140             }
       
   141 
       
   142             pred.accept(propertyName, propertyValue);
       
   143         });
   120         });
   144     }
   121     }
   145 
   122 
   146     public PackageTest addBundlePropertyVerifier(String propertyName,
   123     public PackageTest addBundlePropertyVerifier(String propertyName,
   147             String expectedPropertyValue) {
   124             String expectedPropertyValue) {
   148         return addBundlePropertyVerifier(propertyName, (unused, v) -> {
   125         return addBundlePropertyVerifier(propertyName, (unused, v) -> {
   149             Test.assertEquals(expectedPropertyValue, v, String.format(
   126             TKit.assertEquals(expectedPropertyValue, v, String.format(
   150                     "Check value of %s property is [%s]", propertyName, v));
   127                     "Check value of %s property is [%s]", propertyName, v));
   151         });
   128         });
   152     }
   129     }
   153 
   130 
   154     public PackageTest addBundleDesktopIntegrationVerifier(boolean integrated) {
   131     public PackageTest addBundleDesktopIntegrationVerifier(boolean integrated) {
   170                 type -> handlers.get(type).addUninstallVerifier(
   147                 type -> handlers.get(type).addUninstallVerifier(
   171                         ThrowingConsumer.toConsumer(v)));
   148                         ThrowingConsumer.toConsumer(v)));
   172         return this;
   149         return this;
   173     }
   150     }
   174 
   151 
       
   152     static void withTestFileAssociationsFile(FileAssociations fa, ThrowingConsumer<Path> consumer) {
       
   153         final String testFileDefaultName = String.join(".", "test", fa.getSuffix());
       
   154         TKit.withTempFile(testFileDefaultName, fa.getSuffix(), testFile -> {
       
   155             if (TKit.isLinux()) {
       
   156                 LinuxHelper.initFileAssociationsTestFile(testFile);
       
   157             }
       
   158             consumer.accept(testFile);
       
   159         });
       
   160     }
       
   161 
   175     public PackageTest addHelloAppFileAssociationsVerifier(FileAssociations fa,
   162     public PackageTest addHelloAppFileAssociationsVerifier(FileAssociations fa,
   176             String... faLauncherDefaultArgs) {
   163             String... faLauncherDefaultArgs) {
   177 
   164 
   178         addInitializer(cmd -> HelloApp.addTo(cmd), "HelloApp");
   165         addInitializer(cmd -> HelloApp.addTo(cmd, null), "HelloApp");
   179         addInstallVerifier(cmd -> {
   166         addInstallVerifier(cmd -> {
   180             if (cmd.isFakeRuntimeInstalled(
   167             if (cmd.isFakeRuntimeInstalled(
   181                     "Not running file associations test")) {
   168                     "Not running file associations test")) {
   182                 return;
   169                 return;
   183             }
   170             }
   184 
   171 
   185             Test.withTempFile(fa.getSuffix(), testFile -> {
   172             withTestFileAssociationsFile(fa, testFile -> {
   186                 testFile = testFile.toAbsolutePath().normalize();
   173                 testFile = testFile.toAbsolutePath().normalize();
   187                 if (PackageType.LINUX.contains(cmd.packageType())) {
       
   188                     LinuxHelper.initFileAssociationsTestFile(testFile);
       
   189                 }
       
   190 
   174 
   191                 final Path appOutput = Path.of(HelloApp.OUTPUT_FILENAME);
   175                 final Path appOutput = Path.of(HelloApp.OUTPUT_FILENAME);
   192                 Files.deleteIfExists(appOutput);
   176                 Files.deleteIfExists(appOutput);
   193 
   177 
   194                 Test.trace(String.format("Use desktop to open [%s] file",
   178                 TKit.trace(String.format("Use desktop to open [%s] file",
   195                         testFile));
   179                         testFile));
   196                 Desktop.getDesktop().open(testFile.toFile());
   180                 Desktop.getDesktop().open(testFile.toFile());
   197                 Test.waitForFileCreated(appOutput, 7);
   181                 TKit.waitForFileCreated(appOutput, 7);
   198 
   182 
   199                 List<String> expectedArgs = new ArrayList<>(List.of(
   183                 List<String> expectedArgs = new ArrayList<>(List.of(
   200                         faLauncherDefaultArgs));
   184                         faLauncherDefaultArgs));
   201                 expectedArgs.add(testFile.toString());
   185                 expectedArgs.add(testFile.toString());
   202 
   186 
   203                 // Wait a little bit after file has been created to
   187                 // Wait a little bit after file has been created to
   204                 // make sure there are no pending writes into it.
   188                 // make sure there are no pending writes into it.
   205                 Thread.sleep(3000);
   189                 Thread.sleep(3000);
   206                 HelloApp.verifyOutputFile(appOutput, expectedArgs.toArray(String[]::new));
   190                 HelloApp.verifyOutputFile(appOutput, expectedArgs);
   207             });
   191             });
   208         });
   192         });
   209 
   193 
   210         forTypes(PackageType.LINUX, () -> {
   194         forTypes(PackageType.LINUX, () -> {
   211             LinuxHelper.addFileAssociationsVerifier(this, fa);
   195             LinuxHelper.addFileAssociationsVerifier(this, fa);
   212         });
   196         });
   213 
   197 
   214         return this;
   198         return this;
   215     }
   199     }
   216 
   200 
   217     private void forTypes(Collection<PackageType> types, Runnable action) {
   201     PackageTest forTypes(Collection<PackageType> types, Runnable action) {
   218         Set<PackageType> oldTypes = Set.of(currentTypes.toArray(
   202         Set<PackageType> oldTypes = Set.of(currentTypes.toArray(
   219                 PackageType[]::new));
   203                 PackageType[]::new));
   220         try {
   204         try {
   221             forTypes(types);
   205             forTypes(types);
   222             action.run();
   206             action.run();
   223         } finally {
   207         } finally {
   224             forTypes(oldTypes);
   208             forTypes(oldTypes);
   225         }
   209         }
   226     }
   210         return this;
   227 
   211     }
   228     private void forTypes(PackageType type, Runnable action) {
   212 
   229         forTypes(List.of(type), action);
   213     PackageTest forTypes(PackageType type, Runnable action) {
       
   214         return forTypes(List.of(type), action);
   230     }
   215     }
   231 
   216 
   232     public PackageTest configureHelloApp() {
   217     public PackageTest configureHelloApp() {
   233         addInitializer(cmd -> HelloApp.addTo(cmd), "HelloApp");
   218         return configureHelloApp(null);
       
   219     }
       
   220 
       
   221     public PackageTest configureHelloApp(String encodedName) {
       
   222         addInitializer(cmd -> HelloApp.addTo(cmd, encodedName));
   234         addInstallVerifier(HelloApp::executeLauncherAndVerifyOutput);
   223         addInstallVerifier(HelloApp::executeLauncherAndVerifyOutput);
   235         return this;
   224         return this;
   236     }
   225     }
   237 
   226 
   238     public void run() {
   227     public void run() {
   310             initializers.stream().forEach(v -> v.accept(cmd));
   299             initializers.stream().forEach(v -> v.accept(cmd));
   311             switch (action) {
   300             switch (action) {
   312                 case CREATE:
   301                 case CREATE:
   313                     Executor.Result result = cmd.execute();
   302                     Executor.Result result = cmd.execute();
   314                     result.assertExitCodeIs(expectedJPackageExitCode);
   303                     result.assertExitCodeIs(expectedJPackageExitCode);
   315                     Test.assertFileExists(cmd.outputBundle(),
   304                     if (expectedJPackageExitCode == 0) {
   316                             expectedJPackageExitCode == 0);
   305                         TKit.assertFileExists(cmd.outputBundle());
       
   306                     } else {
       
   307                         TKit.assertPathExists(cmd.outputBundle(), false);
       
   308                     }
   317                     verifyPackageBundle(cmd.createImmutableCopy(), result);
   309                     verifyPackageBundle(cmd.createImmutableCopy(), result);
   318                     break;
   310                     break;
   319 
   311 
   320                 case VERIFY_INSTALL:
   312                 case VERIFY_INSTALL:
   321                     verifyPackageInstalled(cmd.createImmutableCopy());
   313                     verifyPackageInstalled(cmd.createImmutableCopy());
   328         }
   320         }
   329 
   321 
   330         private void verifyPackageBundle(JPackageCommand cmd,
   322         private void verifyPackageBundle(JPackageCommand cmd,
   331                 Executor.Result result) {
   323                 Executor.Result result) {
   332             if (PackageType.LINUX.contains(cmd.packageType())) {
   324             if (PackageType.LINUX.contains(cmd.packageType())) {
   333                 Test.assertNotEquals(0L, LinuxHelper.getInstalledPackageSizeKB(
   325                 TKit.assertNotEquals(0L, LinuxHelper.getInstalledPackageSizeKB(
   334                         cmd), String.format(
   326                         cmd), String.format(
   335                                 "Check installed size of [%s] package in KB is not zero",
   327                                 "Check installed size of [%s] package in KB is not zero",
   336                                 LinuxHelper.getPackageName(cmd)));
   328                                 LinuxHelper.getPackageName(cmd)));
   337             }
   329             }
   338             bundleVerifiers.stream().forEach(v -> v.accept(cmd, result));
   330             bundleVerifiers.stream().forEach(v -> v.accept(cmd, result));
   339         }
   331         }
   340 
   332 
   341         private void verifyPackageInstalled(JPackageCommand cmd) {
   333         private void verifyPackageInstalled(JPackageCommand cmd) {
   342             Test.trace(String.format("Verify installed: %s",
   334             TKit.trace(String.format("Verify installed: %s",
   343                     cmd.getPrintableCommandLine()));
   335                     cmd.getPrintableCommandLine()));
   344             if (cmd.isRuntime()) {
   336             if (cmd.isRuntime()) {
   345                 Test.assertDirectoryExists(
   337                 TKit.assertPathExists(
   346                         cmd.appRuntimeInstallationDirectory(), false);
   338                         cmd.appRuntimeInstallationDirectory(), false);
   347             } else {
   339             } else {
   348                 Test.assertExecutableFileExists(cmd.launcherInstallationPath(), true);
   340                 TKit.assertExecutableFileExists(cmd.launcherInstallationPath());
   349             }
   341             }
   350 
   342 
   351             if (PackageType.WINDOWS.contains(cmd.packageType())) {
   343             if (PackageType.WINDOWS.contains(cmd.packageType())) {
   352                 new WindowsHelper.AppVerifier(cmd);
   344                 new WindowsHelper.AppVerifier(cmd);
   353             }
   345             }
   354 
   346 
   355             installVerifiers.stream().forEach(v -> v.accept(cmd));
   347             installVerifiers.stream().forEach(v -> v.accept(cmd));
   356         }
   348         }
   357 
   349 
   358         private void verifyPackageUninstalled(JPackageCommand cmd) {
   350         private void verifyPackageUninstalled(JPackageCommand cmd) {
   359             Test.trace(String.format("Verify uninstalled: %s",
   351             TKit.trace(String.format("Verify uninstalled: %s",
   360                     cmd.getPrintableCommandLine()));
   352                     cmd.getPrintableCommandLine()));
   361             if (!cmd.isRuntime()) {
   353             if (!cmd.isRuntime()) {
   362                 Test.assertPathExists(cmd.launcherInstallationPath(), false);
   354                 TKit.assertPathExists(cmd.launcherInstallationPath(), false);
   363                 Test.assertPathExists(cmd.appInstallationDirectory(), false);
   355                 TKit.assertPathExists(cmd.appInstallationDirectory(), false);
   364             }
   356             }
   365 
   357 
   366             if (PackageType.WINDOWS.contains(cmd.packageType())) {
   358             if (PackageType.WINDOWS.contains(cmd.packageType())) {
   367                 new WindowsHelper.AppVerifier(cmd);
   359                 new WindowsHelper.AppVerifier(cmd);
   368             }
   360             }
   408     private final static Action DEFAULT_ACTION;
   400     private final static Action DEFAULT_ACTION;
   409     private final static File bundleOutputDir;
   401     private final static File bundleOutputDir;
   410 
   402 
   411     static {
   403     static {
   412         final String propertyName = "output";
   404         final String propertyName = "output";
   413         String val = Test.getConfigProperty(propertyName);
   405         String val = TKit.getConfigProperty(propertyName);
   414         if (val == null) {
   406         if (val == null) {
   415             bundleOutputDir = null;
   407             bundleOutputDir = null;
   416         } else {
   408         } else {
   417             bundleOutputDir = new File(val).getAbsoluteFile();
   409             bundleOutputDir = new File(val).getAbsoluteFile();
   418 
   410 
   419             if (!bundleOutputDir.isDirectory()) {
   411             if (!bundleOutputDir.isDirectory()) {
   420                 throw new IllegalArgumentException(String.format(
   412                 throw new IllegalArgumentException(String.format("Invalid value of %s sytem property: [%s]. Should be existing directory",
   421                         "Invalid value of %s sytem property: [%s]. Should be existing directory",
   413                         TKit.getConfigPropertyName(propertyName),
   422                         Test.getConfigPropertyName(propertyName),
       
   423                         bundleOutputDir));
   414                         bundleOutputDir));
   424             }
   415             }
   425         }
   416         }
   426     }
   417     }
   427 
   418 
   428     static {
   419     static {
   429         final String propertyName = "action";
   420         final String propertyName = "action";
   430         String action = Optional.ofNullable(Test.getConfigProperty(propertyName)).orElse(
   421         String action = Optional.ofNullable(TKit.getConfigProperty(propertyName)).orElse(
   431                 Action.CREATE.toString()).toLowerCase();
   422                 Action.CREATE.toString()).toLowerCase();
   432         DEFAULT_ACTION = Stream.of(Action.values()).filter(
   423         DEFAULT_ACTION = Stream.of(Action.values()).filter(
   433                 a -> a.toString().equals(action)).findFirst().orElseThrow(
   424                 a -> a.toString().equals(action)).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("Unrecognized value of %s property: [%s]",
   434                         () -> new IllegalArgumentException(String.format(
   425                                 TKit.getConfigPropertyName(propertyName), action)));
   435                                 "Unrecognized value of %s property: [%s]",
       
   436                                 Test.getConfigPropertyName(propertyName), action)));
       
   437     }
   426     }
   438 }
   427 }