1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.nio.file.Path;
  25 import java.util.HashMap;
  26 import java.util.Map;
  27 import java.util.List;
  28 import java.util.Optional;
  29 import java.lang.invoke.MethodHandles;
  30 import jdk.jpackage.test.HelloApp;
  31 import jdk.jpackage.test.PackageTest;
  32 import jdk.jpackage.test.PackageType;
  33 import jdk.jpackage.test.FileAssociations;
  34 import jdk.jpackage.test.Test;
  35 
  36 /**
  37  * Test --add-launcher parameter. Output of the test should be
  38  * additionallauncherstest*.* installer. The output installer should provide the
  39  * same functionality as the default installer (see description of the default
  40  * installer in SimplePackageTest.java) plus install three extra application
  41  * launchers.
  42  */
  43 
  44 /*
  45  * @test
  46  * @summary jpackage with --add-launcher
  47  * @library ../helpers
  48  * @modules jdk.jpackage/jdk.jpackage.internal
  49  * @run main/othervm/timeout=360 -Xmx512m AdditionalLaunchersTest
  50  */
  51 public class AdditionalLaunchersTest {
  52 
  53     public static void main(String[] args) {
  54         Test.run(args, () -> {
  55             FileAssociations fa = new FileAssociations(
  56                     MethodHandles.lookup().lookupClass().getSimpleName());
  57 
  58             // Configure a bunch of additional launchers and also setup
  59             // file association to make sure it will be linked only to the main
  60             // launcher.
  61 
  62             PackageTest packageTest = new PackageTest().configureHelloApp()
  63             .addInitializer(cmd -> {
  64                 fa.createFile();
  65                 cmd.addArguments("--file-associations", fa.getPropertiesFile());
  66                 cmd.addArguments("--arguments", "Duke", "--arguments", "is",
  67                         "--arguments", "the", "--arguments", "King");
  68             });
  69 
  70             packageTest.addHelloAppFileAssociationsVerifier(fa);
  71 
  72             new AdditionalLauncher("Baz2").setArguments().applyTo(packageTest);
  73             new AdditionalLauncher("foo").setArguments("yep!").applyTo(packageTest);
  74 
  75             AdditionalLauncher barLauncher = new AdditionalLauncher("Bar").setArguments(
  76                     "one", "two", "three");
  77             packageTest.forTypes(PackageType.LINUX).addInitializer(cmd -> {
  78                 barLauncher.setIcon(Test.TEST_SRC_ROOT.resolve("apps/dukeplug.png"));
  79             });
  80             barLauncher.applyTo(packageTest);
  81 
  82             packageTest.run();
  83         });
  84     }
  85 
  86     private static Path replaceFileName(Path path, String newFileName) {
  87         String fname = path.getFileName().toString();
  88         int lastDotIndex = fname.lastIndexOf(".");
  89         if (lastDotIndex != -1) {
  90             fname = newFileName + fname.substring(lastDotIndex);
  91         }
  92         return path.getParent().resolve(fname);
  93     }
  94 
  95     static class AdditionalLauncher {
  96 
  97         AdditionalLauncher(String name) {
  98             this.name = name;
  99         }
 100 
 101         AdditionalLauncher setArguments(String... args) {
 102             arguments = List.of(args);
 103             return this;
 104         }
 105 
 106         AdditionalLauncher setIcon(Path iconPath) {
 107             icon = iconPath;
 108             return this;
 109         }
 110 
 111         void applyTo(PackageTest test) {
 112             final Path propsFile = Test.workDir().resolve(name + ".properties");
 113 
 114             test.addInitializer(cmd -> {
 115                 cmd.addArguments("--add-launcher", String.format("%s=%s", name,
 116                         propsFile));
 117 
 118                 Map<String, String> properties = new HashMap<>();
 119                 if (arguments != null) {
 120                     properties.put("arguments", String.join(" ",
 121                             arguments.toArray(String[]::new)));
 122                 }
 123 
 124                 if (icon != null) {
 125                     properties.put("icon", icon.toAbsolutePath().toString());
 126                 }
 127 
 128                 Test.createPropertiesFile(propsFile, properties);
 129             });
 130             test.addInstallVerifier(cmd -> {
 131                 Path launcherPath = replaceFileName(
 132                         cmd.launcherInstallationPath(), name);
 133 
 134                 Test.assertExecutableFileExists(launcherPath, true);
 135 
 136                 if (cmd.isFakeRuntimeInstalled(String.format(
 137                         "Not running %s launcher", launcherPath))) {
 138                     return;
 139                 }
 140                 HelloApp.executeAndVerifyOutput(launcherPath,
 141                         Optional.ofNullable(arguments).orElse(List.of()).toArray(
 142                                 String[]::new));
 143             });
 144             test.addUninstallVerifier(cmd -> {
 145                 Path launcherPath = replaceFileName(
 146                         cmd.launcherInstallationPath(), name);
 147 
 148                 Test.assertExecutableFileExists(launcherPath, false);
 149             });
 150         }
 151 
 152         private List<String> arguments;
 153         private Path icon;
 154         private final String name;
 155     }
 156 }