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