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.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 public class JPackageCreateAppImageAddLauncherBase {
  33     private static final String app = JPackagePath.getApp();
  34     private static final String appOutput = JPackagePath.getAppOutputFile();
  35     private static final String appWorkingDir = JPackagePath.getAppWorkingDir();
  36 
  37     // Note: quotes in argument for add launcher is not support by test
  38     private static final String ARGUMENT1 = "argument 1";
  39     private static final String ARGUMENT2 = "argument 2";
  40     private static final String ARGUMENT3 = "argument 3";
  41 
  42     private static final List<String> arguments = new ArrayList<>();
  43 
  44     private static final String PARAM1 = "-Dparam1=Some Param 1";
  45     private static final String PARAM2 = "-Dparam2=Some Param 2";
  46     private static final String PARAM3 = "-Dparam3=Some Param 3";
  47 
  48     private static final List<String> vmArguments = new ArrayList<>();
  49     private static final List<String> empty = new ArrayList<>();
  50 
  51     private static void validateResult(List<String> args, List<String> vmArgs)
  52             throws Exception {
  53         File outfile = new File(appWorkingDir + File.separator + appOutput);
  54         if (!outfile.exists()) {
  55             throw new AssertionError(appOutput + " was not created");
  56         }
  57 
  58         String output = Files.readString(outfile.toPath());
  59         String[] result = output.split("\n");
  60 
  61         int expected = 2 + args.size() + vmArgs.size();
  62 
  63         if (result.length != expected) {
  64             throw new AssertionError("Unexpected number of lines: "
  65                     + result.length + " expected: " + expected + " - results: " + output);
  66         }
  67 
  68         if (!result[0].trim().endsWith("jpackage test application")) {
  69             throw new AssertionError("Unexpected result[0]: " + result[0]);
  70         }
  71 
  72         if (!result[1].trim().equals("args.length: " + args.size())) {
  73             throw new AssertionError("Unexpected result[1]: " + result[1]);
  74         }
  75 
  76         int index = 2;
  77         for (String arg : args) {
  78             if (!result[index].trim().equals(arg)) {
  79                 throw new AssertionError("Unexpected result["
  80                         + index + "]: " + result[index]);
  81             }
  82             index++;
  83         }
  84 
  85         for (String vmArg : vmArgs) {
  86             if (!result[index].trim().equals(vmArg)) {
  87                 throw new AssertionError("Unexpected result["
  88                         + index + "]: " + result[index]);
  89             }
  90             index++;
  91         }
  92     }
  93 
  94     private static void validate(boolean includeArgs, String name)
  95             throws Exception {
  96         int retVal = JPackageHelper.execute(null, app);
  97         if (retVal != 0) {
  98             throw new AssertionError("Test application " + app
  99                     + " exited with error: " + retVal);
 100         }
 101         validateResult(new ArrayList<>(), new ArrayList<>());
 102 
 103         String app2 = JPackagePath.getAppSL(name);
 104         retVal = JPackageHelper.execute(null, app2);
 105         if (retVal != 0) {
 106             throw new AssertionError("Test application " + app2
 107                     +  " exited with error: " + retVal);
 108         }
 109         if (includeArgs) {
 110             validateResult(arguments, vmArguments);
 111         } else {
 112             validateResult(empty, empty);
 113         }
 114     }
 115 
 116     public static void testCreateAppImage(String [] cmd) throws Exception {
 117         testCreateAppImage(cmd, true, "test2");
 118     }
 119 
 120     public static void testCreateAppImage(String [] cmd,
 121             boolean includeArgs, String name) throws Exception {
 122         JPackageHelper.executeCLI(true, cmd);
 123         validate(includeArgs, name);
 124     }
 125 
 126     public static void testCreateAppImageToolProvider(String [] cmd)
 127             throws Exception {
 128         testCreateAppImageToolProvider(cmd, true, "test2");
 129     }
 130 
 131     public static void testCreateAppImageToolProvider(String [] cmd,
 132             boolean includeArgs, String name) throws Exception {
 133         JPackageHelper.executeToolProvider(true, cmd);
 134         validate(includeArgs, name);
 135     }
 136 
 137     public static void createSLProperties() throws Exception {
 138         arguments.add(ARGUMENT1);
 139         arguments.add(ARGUMENT2);
 140         arguments.add(ARGUMENT3);
 141 
 142         String argumentsMap =
 143                 JPackageHelper.listToArgumentsMap(arguments, true);
 144 
 145         vmArguments.add(PARAM1);
 146         vmArguments.add(PARAM2);
 147         vmArguments.add(PARAM3);
 148 
 149         String vmArgumentsMap =
 150                 JPackageHelper.listToArgumentsMap(vmArguments, true);
 151 
 152         try (PrintWriter out = new PrintWriter(new BufferedWriter(
 153                 new FileWriter("sl.properties")))) {
 154             out.println("arguments=" + argumentsMap);
 155             out.println("java-options=" + vmArgumentsMap);
 156         }
 157 
 158         try (PrintWriter out = new PrintWriter(new BufferedWriter(
 159                 new FileWriter("m1.properties")))) {
 160             out.println("module=com.hello/com.hello.Hello");
 161             out.println("main-jar=");
 162         }
 163 
 164         try (PrintWriter out = new PrintWriter(new BufferedWriter(
 165                 new FileWriter("j1.properties")))) {
 166             out.println("main-jar hello.jar");
 167             out.println("main-class Hello");
 168         }
 169 
 170         
 171     }
 172 
 173 }