1 /*
   2  * Copyright (c) 2018, 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.io.BufferedWriter;
  25 import java.io.File;
  26 import java.io.FileWriter;
  27 import java.io.PrintWriter;
  28 import java.nio.file.Files;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 
  32  /*
  33  * @test
  34  * @summary jpackager create image with secondary launcher test
  35  * @build JPackagerHelper
  36  * @modules jdk.jpackager
  37  * @run main/othervm -Xmx512m JPackagerCreateImageSecondaryLauncherTest
  38  */
  39 public class JPackagerCreateImageSecondaryLauncherTest {
  40     private static final String app;
  41     private static final String app2;
  42     private static final String appOutput = "appOutput.txt";
  43     private static final String appOutputPath;
  44     private static final String MSG = "jpackager test application";
  45     private static final String [] CMD = {
  46         "create-image",
  47         "--input", "input",
  48         "--output", "output",
  49         "--name", "test",
  50         "--main-jar", "hello.jar",
  51         "--class", "Hello",
  52         "--files", "hello.jar",
  53         "--force",
  54         "--secondary-launcher", "sl.properties"};
  55 
  56     // Note: quotes in argument for secondary launcher is not support by test
  57     private static final String ARGUMENT1 = "argument 1";
  58     private static final String ARGUMENT2 = "argument 2";
  59     private static final String ARGUMENT3 = "argument 3";
  60 
  61     private static final List<String> arguments = new ArrayList<>();
  62 
  63     private static final String PARAM1 = "-Dparam1=Some Param 1";
  64     private static final String PARAM2 = "-Dparam2=Some Param 2";
  65     private static final String PARAM3 = "-Dparam3=Some Param 3";
  66 
  67     private static final List<String> vmArguments = new ArrayList<>();
  68 
  69     static {
  70         if (JPackagerHelper.isWindows()) {
  71             app = "output" + File.separator + "test"
  72                     + File.separator + "test.exe";
  73             app2 = "output" + File.separator + "test"
  74                     + File.separator + "test2.exe";
  75             appOutputPath = "output" + File.separator + "test"
  76                     + File.separator + "app";
  77         } else if (JPackagerHelper.isOSX()) {
  78             app = "output" + File.separator + "test.app"
  79                     + File.separator + "Contents"
  80                     + File.separator + "MacOS" + File.separator + "test";
  81             app2 = "output" + File.separator + "test.app"
  82                     + File.separator + "Contents"
  83                     + File.separator + "MacOS" + File.separator + "test2";
  84             appOutputPath = "output" + File.separator + "test.app"
  85                     + File.separator + "Contents" + File.separator + "Java";
  86         } else {
  87             app = "output" + File.separator + "test" + File.separator + "test";
  88             app2 = "output" + File.separator + "test"
  89                     + File.separator + "test2";
  90             appOutputPath = "output" + File.separator + "test"
  91                     + File.separator + "app";
  92         }
  93     }
  94 
  95     private static void validateResult(List<String> args, List<String> vmArgs)
  96             throws Exception {
  97         File outfile = new File(appOutputPath + File.separator + appOutput);
  98         if (!outfile.exists()) {
  99             throw new AssertionError(appOutput + " was not created");
 100         }
 101 
 102         String output = Files.readString(outfile.toPath());
 103         String[] result = output.split("\n");
 104 
 105         if (result.length != (args.size() + vmArgs.size() + 2)) {
 106             throw new AssertionError("Unexpected number of lines: "
 107                     + result.length);
 108         }
 109 
 110         if (!result[0].trim().equals("jpackager test application")) {
 111             throw new AssertionError("Unexpected result[0]: " + result[0]);
 112         }
 113 
 114         if (!result[1].trim().equals("args.length: " + args.size())) {
 115             throw new AssertionError("Unexpected result[1]: " + result[1]);
 116         }
 117 
 118         int index = 2;
 119         for (String arg : args) {
 120             if (!result[index].trim().equals(arg)) {
 121                 throw new AssertionError("Unexpected result[" + index + "]: "
 122                     + result[index]);
 123             }
 124             index++;
 125         }
 126 
 127         for (String vmArg : vmArgs) {
 128             if (!result[index].trim().equals(vmArg)) {
 129                 throw new AssertionError("Unexpected result[" + index + "]: "
 130                     + result[index]);
 131             }
 132             index++;
 133         }
 134     }
 135 
 136     private static void validate() throws Exception {
 137         int retVal = JPackagerHelper.execute(null, app);
 138         if (retVal != 0) {
 139             throw new AssertionError("Test application exited with error: "
 140                     + retVal);
 141         }
 142         validateResult(new ArrayList<>(), new ArrayList<>());
 143 
 144         retVal = JPackagerHelper.execute(null, app2);
 145         if (retVal != 0) {
 146             throw new AssertionError("Test application exited with error: "
 147                     + retVal);
 148         }
 149         validateResult(arguments, vmArguments);
 150     }
 151 
 152     private static void testCreateImage() throws Exception {
 153         JPackagerHelper.executeCLI(true, CMD);
 154         validate();
 155     }
 156 
 157     private static void testCreateImageToolProvider() throws Exception {
 158         JPackagerHelper.executeToolProvider(true, CMD);
 159         validate();
 160     }
 161 
 162     private static void createSLProperties() throws Exception {
 163         arguments.add(ARGUMENT1);
 164         arguments.add(ARGUMENT2);
 165         arguments.add(ARGUMENT3);
 166 
 167         String argumentsMap =
 168                 JPackagerHelper.listToArgumentsMap(arguments, true);
 169 
 170         vmArguments.add(PARAM1);
 171         vmArguments.add(PARAM2);
 172         vmArguments.add(PARAM3);
 173 
 174         String vmArgumentsMap =
 175                 JPackagerHelper.listToArgumentsMap(vmArguments, true);
 176 
 177         try (PrintWriter out = new PrintWriter(new BufferedWriter(
 178                 new FileWriter("sl.properties")))) {
 179             out.println("name=test2");
 180             out.println("arguments=" + argumentsMap);
 181             out.println("jvm-args=" + vmArgumentsMap);
 182         }
 183     }
 184 
 185     public static void main(String[] args) throws Exception {
 186         JPackagerHelper.createHelloJar();
 187         createSLProperties();
 188         testCreateImage();
 189         testCreateImageToolProvider();
 190     }
 191 
 192 }