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.*;
  31 import jdk.jpackage.test.Annotations.*;
  32 
  33 /**
  34  * Test --add-launcher parameter. Output of the test should be
  35  * additionallauncherstest*.* installer. The output installer should provide the
  36  * same functionality as the default installer (see description of the default
  37  * installer in SimplePackageTest.java) plus install three extra application
  38  * launchers.
  39  */
  40 
  41 /*
  42  * @test
  43  * @summary jpackage with --add-launcher
  44  * @key jpackagePlatformPackage
  45  * @requires (jpackage.test.SQETest != null)
  46  * @library ../helpers
  47  * @build jdk.jpackage.test.*
  48  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  49  * @compile AdditionalLaunchersTest.java
  50  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  51  *  --jpt-run=AdditionalLaunchersTest.test
  52  */
  53 
  54 /*
  55  * @test
  56  * @summary jpackage with --add-launcher
  57  * @key jpackagePlatformPackage
  58  * @requires (jpackage.test.SQETest == null)
  59  * @library ../helpers
  60  * @build jdk.jpackage.test.*
  61  * @modules jdk.jpackage/jdk.jpackage.internal
  62  * @compile AdditionalLaunchersTest.java
  63  * @run main/othervm/timeout=540 -Xmx512m jdk.jpackage.test.Main
  64  *  --jpt-run=AdditionalLaunchersTest
  65  */
  66 
  67 public class AdditionalLaunchersTest {
  68 
  69     @Test
  70     public void test() {
  71         // Configure a bunch of additional launchers and also setup
  72         // file association to make sure it will be linked only to the main
  73         // launcher.
  74 
  75         PackageTest packageTest = new PackageTest().configureHelloApp();
  76         packageTest.addInitializer(cmd -> {
  77             cmd.addArguments("--arguments", "Duke", "--arguments", "is",
  78                     "--arguments", "the", "--arguments", "King");
  79         });
  80 
  81         new FileAssociations(
  82                 MethodHandles.lookup().lookupClass().getSimpleName()).applyTo(
  83                 packageTest);
  84 
  85         new AdditionalLauncher("Baz2")
  86                 .setDefaultArguments()
  87                 .applyTo(packageTest);
  88 
  89         new AdditionalLauncher("foo")
  90                 .setDefaultArguments("yep!")
  91                 .applyTo(packageTest);
  92 
  93         new AdditionalLauncher("Bar")
  94                 .setDefaultArguments("one", "two", "three")
  95                 .setIcon(GOLDEN_ICON)
  96                 .applyTo(packageTest);
  97 
  98         packageTest.run();
  99     }
 100 
 101     @Test
 102     public void bug8230933() {
 103         PackageTest packageTest = new PackageTest().configureHelloApp();
 104 
 105         new AdditionalLauncher("default_icon")
 106                 .applyTo(packageTest);
 107 
 108         new AdditionalLauncher("no_icon")
 109                 .setNoIcon().applyTo(packageTest);
 110 
 111         new AdditionalLauncher("custom_icon")
 112                 .setIcon(GOLDEN_ICON)
 113                 .applyTo(packageTest);
 114 
 115         packageTest.run();
 116     }
 117 
 118     @Test
 119     // Regular app
 120     @Parameter("Hello")
 121     // Modular app
 122     @Parameter("com.other/com.other.CiaoBella")
 123     public void testJavaOptions(String javaAppDesc) {
 124         JPackageCommand cmd = JPackageCommand.helloAppImage(javaAppDesc)
 125         .addArguments("--arguments", "courageous")
 126         .addArguments("--java-options", "-Dparam1=xxx")
 127         .addArguments("--java-options", "-Dparam2=yyy")
 128         .addArguments("--java-options", "-Dparam3=zzz");
 129 
 130         new AdditionalLauncher("Jack")
 131                 .addDefaultArguments("Jack of All Trades", "Master of None")
 132                 .setJavaOptions("-Dparam1=Contractor")
 133                 .applyTo(cmd);
 134 
 135         new AdditionalLauncher("Monday")
 136                 .addDefaultArguments("Knock Your", "Socks Off")
 137                 .setJavaOptions("-Dparam2=Surprise workers!")
 138                 .applyTo(cmd);
 139 
 140         // Should inherit default arguments and java options from the main launcher
 141         new AdditionalLauncher("void").applyTo(cmd);
 142 
 143         cmd.executeAndAssertHelloAppImageCreated();
 144     }
 145 
 146     /**
 147      * Test usage of modular and non modular apps in additional launchers.
 148      */
 149     @Test
 150     @Parameter("true")
 151     @Parameter("fase")
 152     public void testMainLauncherIsModular(boolean mainLauncherIsModular) {
 153         final var nonModularAppDesc = JavaAppDesc.parse("a.b.c.Hello");
 154         final var modularAppDesc = JavaAppDesc.parse(
 155                 "module.jar:com.that/com.that.main.Florence");
 156 
 157         final var nonModularJarCmd = JPackageCommand.helloAppImage(nonModularAppDesc);
 158         final var modularJarCmd = JPackageCommand.helloAppImage(modularAppDesc);
 159 
 160         final JPackageCommand cmd;
 161         if (mainLauncherIsModular) {
 162             // Create non modular jar.
 163             nonModularJarCmd.executePrerequisiteActions();
 164 
 165             cmd = modularJarCmd;
 166             cmd.addArguments("--description",
 167                     "Test modular app with multiple add-launchers where one is modular app and other is non modular app");
 168             cmd.addArguments("--input", nonModularJarCmd.getArgumentValue(
 169                     "--input"));
 170         } else {
 171             // Create modular jar.
 172             modularJarCmd.executePrerequisiteActions();
 173 
 174             cmd = nonModularJarCmd;
 175             cmd.addArguments("--description",
 176                     "Test non modular app with multiple add-launchers where one is modular app and other is non modular app");
 177             cmd.addArguments("--module-path", modularJarCmd.getArgumentValue(
 178                     "--module-path"));
 179             cmd.addArguments("--add-modules", modularAppDesc.moduleName());
 180         }
 181 
 182         new AdditionalLauncher("ModularAppLauncher")
 183         .addRawProperties(Map.entry("module", JavaAppDesc.parse(
 184                 modularAppDesc.toString()).setJarFileName(null).toString()))
 185         .addRawProperties(Map.entry("main-jar", ""))
 186         .applyTo(cmd);
 187 
 188         new AdditionalLauncher("NonModularAppLauncher")
 189         // Use space ( ) character instead of equality sign (=) as
 190         // a key/value separator
 191         .setPersistenceHandler((path, properties) -> TKit.createTextFile(path,
 192                 properties.stream().map(entry -> String.join(" ", entry.getKey(),
 193                         entry.getValue()))))
 194         .addRawProperties(Map.entry("main-class", nonModularAppDesc.className()))
 195         .addRawProperties(Map.entry("main-jar", nonModularAppDesc.jarFileName()))
 196         .applyTo(cmd);
 197 
 198         cmd.executeAndAssertHelloAppImageCreated();
 199     }
 200 
 201     private final static Path GOLDEN_ICON = TKit.TEST_SRC_ROOT.resolve(Path.of(
 202             "resources", "icon" + TKit.ICON_SUFFIX));
 203 }