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.File;
  25 import java.nio.file.Files;
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 
  29 /*
  30  * @test
  31  * @summary jpackager create image with --arguments test
  32  * @build JPackagerHelper
  33  * @modules jdk.jpackager
  34  * @run main/othervm -Xmx512m JPackagerCreateImageArgumentsTest
  35  */
  36 public class JPackagerCreateImageArgumentsTest {
  37 
  38     private static final String app;
  39     private static final String appOutput = "appOutput.txt";
  40     private static final String appOutputPath;
  41 
  42     private static final String[] CMD = {
  43         "create-image",
  44         "--input", "input",
  45         "--output", "output",
  46         "--name", "test",
  47         "--main-jar", "hello.jar",
  48         "--class", "Hello",
  49         "--files", "hello.jar",
  50         "--force",
  51         "--arguments", "TBD"};
  52 
  53     private static final String ARGUMENT1 = "argument";
  54     private static final String ARGUMENT2 = "Some Arguments";
  55     private static final String ARGUMENT3 = "Value \"with\" quotes";
  56 
  57     private static final String ARGUMENT_CMD1 = "test";
  58 
  59     private static final List<String> arguments = new ArrayList<>();
  60     private static final List<String> argumentsCmd = new ArrayList<>();
  61 
  62     static {
  63         if (JPackagerHelper.isWindows()) {
  64             app = "output" + File.separator + "test"
  65                     + File.separator + "test.exe";
  66             appOutputPath = "output" + File.separator + "test"
  67                     + File.separator + "app";
  68         } else if (JPackagerHelper.isOSX()) {
  69             app = "output" + File.separator + "test.app"
  70                     + File.separator + "Contents"
  71                     + File.separator + "MacOS"
  72                     + File.separator + "test";
  73             appOutputPath = "output" + File.separator + "test.app"
  74                     + File.separator + "Contents" + File.separator + "Java";
  75         } else {
  76             app = "output" + File.separator + "test" + File.separator + "test";
  77             appOutputPath = "output"
  78                     + File.separator + "test" + File.separator + "app";
  79         }
  80     }
  81 
  82     private static void initArguments(boolean toolProvider) {
  83         if (arguments.isEmpty()) {
  84             arguments.add(ARGUMENT1);
  85             arguments.add(ARGUMENT2);
  86             arguments.add(ARGUMENT3);
  87         }
  88 
  89         if (argumentsCmd.isEmpty()) {
  90             argumentsCmd.add(ARGUMENT_CMD1);
  91         }
  92 
  93         String argumentsMap =
  94                 JPackagerHelper.listToArgumentsMap(arguments, toolProvider);
  95         CMD[CMD.length - 1] = argumentsMap;
  96     }
  97 
  98     private static void validateResult(String[] result, List<String> args)
  99             throws Exception {
 100         if (result.length != (args.size() + 2)) {
 101             throw new AssertionError(
 102                     "Unexpected number of lines: " + result.length);
 103         }
 104 
 105         if (!result[0].trim().equals("jpackager test application")) {
 106             throw new AssertionError("Unexpected result[0]: " + result[0]);
 107         }
 108 
 109         if (!result[1].trim().equals("args.length: " + args.size())) {
 110             throw new AssertionError("Unexpected result[1]: " + result[1]);
 111         }
 112 
 113         int index = 2;
 114         for (String arg : args) {
 115             if (!result[index].trim().equals(arg)) {
 116                 throw new AssertionError(
 117                         "Unexpected result[" + index + "]: " + result[index]);
 118             }
 119             index++;
 120         }
 121     }
 122 
 123     private static void validate(String arg, List<String> expectedArgs)
 124             throws Exception {
 125         int retVal;
 126 
 127         if (arg == null) {
 128             retVal = JPackagerHelper.execute(null, app);
 129         } else {
 130             retVal = JPackagerHelper.execute(null, app, arg);
 131         }
 132         if (retVal != 0) {
 133             throw new AssertionError("Test application exited with error: "
 134                     + retVal);
 135         }
 136 
 137         File outfile = new File(appOutputPath + File.separator + appOutput);
 138         if (!outfile.exists()) {
 139             throw new AssertionError(appOutput + " was not created");
 140         }
 141 
 142         String output = Files.readString(outfile.toPath());
 143         String[] result = output.split("\n");
 144         validateResult(result, expectedArgs);
 145     }
 146 
 147     private static void testCreateImage() throws Exception {
 148         initArguments(false);
 149         JPackagerHelper.executeCLI(true, CMD);
 150         validate(null, arguments);
 151         validate(ARGUMENT_CMD1, argumentsCmd);
 152     }
 153 
 154     private static void testCreateImageToolProvider() throws Exception {
 155         initArguments(true);
 156         JPackagerHelper.executeToolProvider(true, CMD);
 157         validate(null, arguments);
 158         validate(ARGUMENT_CMD1, argumentsCmd);
 159     }
 160 
 161     public static void main(String[] args) throws Exception {
 162         JPackagerHelper.createHelloJar();
 163         testCreateImage();
 164         testCreateImageToolProvider();
 165     }
 166 
 167 }