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.Annotations.Test;
  35 import jdk.jpackage.test.TKit;
  36 
  37 /**
  38  * Test --add-launcher parameter. Output of the test should be
  39  * additionallauncherstest*.* installer. The output installer should provide the
  40  * same functionality as the default installer (see description of the default
  41  * installer in SimplePackageTest.java) plus install three extra application
  42  * launchers.
  43  */
  44 
  45 /*
  46  * @test
  47  * @summary jpackage with --add-launcher
  48  * @key jpackagePlatformPackage
  49  * @library ../helpers
  50  * @build jdk.jpackage.test.*
  51  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  52  * @compile AdditionalLaunchersTest.java
  53  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  54  *  --jpt-run=AdditionalLaunchersTest
  55  */
  56 
  57 public class AdditionalLaunchersTest {
  58 
  59     @Test
  60     public void test() {
  61         // Configure a bunch of additional launchers and also setup
  62         // file association to make sure it will be linked only to the main
  63         // launcher.
  64 
  65         PackageTest packageTest = new PackageTest().configureHelloApp();
  66         packageTest.addInitializer(cmd -> {
  67             cmd.addArguments("--arguments", "Duke", "--arguments", "is",
  68                     "--arguments", "the", "--arguments", "King");
  69         });
  70 
  71         new FileAssociations(
  72                 MethodHandles.lookup().lookupClass().getSimpleName()).applyTo(
  73                 packageTest);
  74 
  75         new AdditionalLauncher("Baz2").setArguments().applyTo(packageTest);
  76         new AdditionalLauncher("foo").setArguments("yep!").applyTo(packageTest);
  77 
  78         AdditionalLauncher barLauncher = new AdditionalLauncher("Bar").setArguments(
  79                 "one", "two", "three");
  80         if (TKit.isLinux()) {
  81             barLauncher.setIcon(TKit.TEST_SRC_ROOT.resolve("apps/dukeplug.png"));
  82         }
  83         barLauncher.applyTo(packageTest);
  84 
  85         packageTest.run();
  86     }
  87 
  88     private static Path replaceFileName(Path path, String newFileName) {
  89         String fname = path.getFileName().toString();
  90         int lastDotIndex = fname.lastIndexOf(".");
  91         if (lastDotIndex != -1) {
  92             fname = newFileName + fname.substring(lastDotIndex);
  93         } else {
  94             fname = newFileName;
  95         }
  96         return path.getParent().resolve(fname);
  97     }
  98 
  99     static class AdditionalLauncher {
 100 
 101         AdditionalLauncher(String name) {
 102             this.name = name;
 103         }
 104 
 105         AdditionalLauncher setArguments(String... args) {
 106             arguments = List.of(args);
 107             return this;
 108         }
 109 
 110         AdditionalLauncher setIcon(Path iconPath) {
 111             icon = iconPath;
 112             return this;
 113         }
 114 
 115         void applyTo(PackageTest test) {
 116             final Path propsFile = TKit.workDir().resolve(name + ".properties");
 117 
 118             test.addInitializer(cmd -> {
 119                 cmd.addArguments("--add-launcher", String.format("%s=%s", name,
 120                         propsFile));
 121 
 122                 Map<String, String> properties = new HashMap<>();
 123                 if (arguments != null) {
 124                     properties.put("arguments", String.join(" ",
 125                             arguments.toArray(String[]::new)));
 126                 }
 127 
 128                 if (icon != null) {
 129                     properties.put("icon", icon.toAbsolutePath().toString());
 130                 }
 131 
 132                 TKit.createPropertiesFile(propsFile, properties);
 133             });
 134             test.addInstallVerifier(cmd -> {
 135                 Path launcherPath = replaceFileName(cmd.appLauncherPath(), name);
 136 
 137                 TKit.assertExecutableFileExists(launcherPath);
 138 
 139                 if (cmd.isFakeRuntime(String.format(
 140                         "Not running %s launcher", launcherPath))) {
 141                     return;
 142                 }
 143                 HelloApp.executeAndVerifyOutput(launcherPath,
 144                         Optional.ofNullable(arguments).orElse(List.of()).toArray(
 145                                 String[]::new));
 146             });
 147             test.addUninstallVerifier(cmd -> {
 148                 Path launcherPath = replaceFileName(cmd.appLauncherPath(), name);
 149 
 150                 TKit.assertPathExists(launcherPath, false);
 151             });
 152         }
 153 
 154         private List<String> arguments;
 155         private Path icon;
 156         private final String name;
 157     }
 158 }