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.io.File;
  25 import java.nio.file.Files;
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 
  29 public class JPackageCreateImageJVMArgsBase {
  30 
  31     private static final String app = JPackagePath.getApp();
  32     private static final String appOutput = JPackagePath.getAppOutputFile();
  33     private static final String appWorkingDir = JPackagePath.getAppWorkingDir();
  34 
  35     private static final String ARGUMENT1 = "-Dparam1=Some Param 1";
  36     private static final String ARGUMENT2 = "-Dparam2=Some \"Param\" 2";
  37     private static final String ARGUMENT3 =
  38             "-Dparam3=Some \"Param\" with \" 3";
  39 
  40     private static final List<String> arguments = new ArrayList<>();
  41 
  42     private static void initArguments(boolean toolProvider, String [] cmd) {
  43         if (arguments.isEmpty()) {
  44             arguments.add(ARGUMENT1);
  45             arguments.add(ARGUMENT2);
  46             arguments.add(ARGUMENT3);
  47         }
  48 
  49         String argumentsMap = JPackageHelper.listToArgumentsMap(arguments,
  50                 toolProvider);
  51         cmd[cmd.length - 1] = argumentsMap;
  52     }
  53 
  54     private static void initArguments2(boolean toolProvider, String [] cmd) {
  55         int index = cmd.length - 6;
  56 
  57         cmd[index++] = "--jvm-args";
  58         arguments.clear();
  59         arguments.add(ARGUMENT1);
  60         cmd[index++] = JPackageHelper.listToArgumentsMap(arguments,
  61                 toolProvider);
  62 
  63         cmd[index++] = "--jvm-args";
  64         arguments.clear();
  65         arguments.add(ARGUMENT2);
  66         cmd[index++] = JPackageHelper.listToArgumentsMap(arguments,
  67                 toolProvider);
  68 
  69         cmd[index++] = "--jvm-args";
  70         arguments.clear();
  71         arguments.add(ARGUMENT3);
  72         cmd[index++] = JPackageHelper.listToArgumentsMap(arguments,
  73                 toolProvider);
  74 
  75         arguments.clear();
  76         arguments.add(ARGUMENT1);
  77         arguments.add(ARGUMENT2);
  78         arguments.add(ARGUMENT3);
  79     }
  80 
  81     private static void validateResult(String[] result, List<String> args)
  82             throws Exception {
  83         if (result.length != (args.size() + 2)) {
  84             for (String r : result) {
  85                 System.err.println(r.trim());
  86             }
  87             throw new AssertionError("Unexpected number of lines: "
  88                     + result.length);
  89         }
  90 
  91         if (!result[0].trim().equals("jpackage test application")) {
  92             throw new AssertionError("Unexpected result[0]: " + result[0]);
  93         }
  94 
  95         if (!result[1].trim().equals("args.length: 0")) {
  96             throw new AssertionError("Unexpected result[1]: " + result[1]);
  97         }
  98 
  99         int index = 2;
 100         for (String arg : args) {
 101             if (!result[index].trim().equals(arg)) {
 102                 throw new AssertionError("Unexpected result[" + index + "]: "
 103                     + result[index]);
 104             }
 105             index++;
 106         }
 107     }
 108 
 109     private static void validate(List<String> expectedArgs) throws Exception {
 110         int retVal = JPackageHelper.execute(null, app);
 111         if (retVal != 0) {
 112             throw new AssertionError("Test application exited with error: "
 113                     + retVal);
 114         }
 115 
 116         File outfile = new File(appWorkingDir + File.separator + appOutput);
 117         if (!outfile.exists()) {
 118             throw new AssertionError(appOutput + " was not created");
 119         }
 120 
 121         String output = Files.readString(outfile.toPath());
 122         String[] result = output.split("\n");
 123         validateResult(result, expectedArgs);
 124     }
 125 
 126     public static void testCreateImageJVMArgs(String [] cmd) throws Exception {
 127         initArguments(false, cmd);
 128         JPackageHelper.executeCLI(true, cmd);
 129         validate(arguments);
 130     }
 131 
 132     public static void testCreateImageJVMArgsToolProvider(String [] cmd) throws Exception {
 133         initArguments(true, cmd);
 134         JPackageHelper.executeToolProvider(true, cmd);
 135         validate(arguments);
 136     }
 137 
 138     public static void testCreateImageJVMArgs2(String [] cmd) throws Exception {
 139         initArguments2(false, cmd);
 140         JPackageHelper.executeCLI(true, cmd);
 141         validate(arguments);
 142     }
 143 
 144     public static void testCreateImageJVMArgs2ToolProvider(String [] cmd) throws Exception {
 145         initArguments2(true, cmd);
 146         JPackageHelper.executeToolProvider(true, cmd);
 147         validate(arguments);
 148     }
 149 }