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 JPackageCreateAppImageArgumentsBase {
  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 = "argument";
  36     private static final String ARGUMENT2 = "Some Arguments";
  37     private static final String ARGUMENT3 = "Value \"with\" quotes";
  38 
  39     private static final String ARGUMENT_CMD1 = "test";
  40 
  41     private static final List<String> arguments = new ArrayList<>();
  42     private static final List<String> argumentsCmd = new ArrayList<>();
  43 
  44     public static void initArguments(boolean toolProvider, String[] cmd) {
  45         if (arguments.isEmpty()) {
  46             arguments.add(ARGUMENT1);
  47             arguments.add(ARGUMENT2);
  48             arguments.add(ARGUMENT3);
  49         }
  50 
  51         if (argumentsCmd.isEmpty()) {
  52             argumentsCmd.add(ARGUMENT_CMD1);
  53         }
  54 
  55         String argumentsMap
  56                 = JPackageHelper.listToArgumentsMap(arguments, toolProvider);
  57         cmd[cmd.length - 1] = argumentsMap;
  58     }
  59 
  60     private static void validateResult(String[] result, List<String> args)
  61             throws Exception {
  62         if (result.length != (args.size() + 2)) {
  63             throw new AssertionError(
  64                     "Unexpected number of lines: " + result.length);
  65         }
  66 
  67         if (!result[0].trim().equals("jpackage test application")) {
  68             throw new AssertionError("Unexpected result[0]: " + result[0]);
  69         }
  70 
  71         if (!result[1].trim().equals("args.length: " + args.size())) {
  72             throw new AssertionError("Unexpected result[1]: " + result[1]);
  73         }
  74 
  75         int index = 2;
  76         for (String arg : args) {
  77             if (!result[index].trim().equals(arg)) {
  78                 throw new AssertionError(
  79                         "Unexpected result[" + index + "]: " + result[index]);
  80             }
  81             index++;
  82         }
  83     }
  84 
  85     private static void validate(String arg, List<String> expectedArgs)
  86             throws Exception {
  87         int retVal;
  88 
  89         if (arg == null) {
  90             retVal = JPackageHelper.execute(null, app);
  91         } else {
  92             retVal = JPackageHelper.execute(null, app, arg);
  93         }
  94         if (retVal != 0) {
  95             throw new AssertionError("Test application exited with error: "
  96                     + retVal);
  97         }
  98 
  99         File outfile = new File(appWorkingDir + File.separator + appOutput);
 100         if (!outfile.exists()) {
 101             throw new AssertionError(appOutput + " was not created");
 102         }
 103 
 104         String output = Files.readString(outfile.toPath());
 105         String[] result = output.split("\n");
 106         validateResult(result, expectedArgs);
 107     }
 108 
 109     public static void testCreateAppImage(String[] cmd) throws Exception {
 110         initArguments(false, cmd);
 111         JPackageHelper.executeCLI(true, cmd);
 112         validate(null, arguments);
 113         validate(ARGUMENT_CMD1, argumentsCmd);
 114     }
 115 
 116     public static void testCreateAppImageToolProvider(String[] cmd) throws Exception {
 117         initArguments(true, cmd);
 118         JPackageHelper.executeToolProvider(true, cmd);
 119         validate(null, arguments);
 120         validate(ARGUMENT_CMD1, argumentsCmd);
 121     }
 122 }