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 --jvm-args test 32 * @build JPackagerHelper 33 * @modules jdk.jpackager 34 * @run main/othervm -Xmx512m JPackagerCreateImageJVMArgsTest 35 */ 36 public class JPackagerCreateImageJVMArgsTest { 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 "--jvm-args", "TBD"}; 52 53 private static final String ARGUMENT1 = "-Dparam1=Some Param 1"; 54 private static final String ARGUMENT2 = "-Dparam2=Some \"Param\" 2"; 55 private static final String ARGUMENT3 = 56 "-Dparam3=Some \"Param\" with \" 3"; 57 58 private static final List<String> arguments = new ArrayList<>(); 59 60 static { 61 if (JPackagerHelper.isWindows()) { 62 app = "output" + File.separator + "test" 63 + File.separator + "test.exe"; 64 appOutputPath = "output" + File.separator + "test" 65 + File.separator + "app"; 66 } else if (JPackagerHelper.isOSX()) { 67 app = "output" + File.separator + "test.app" 68 + File.separator + "Contents" + File.separator + "MacOS" 69 + File.separator + "test"; appOutputPath = "output" 70 + File.separator + "test.app" 71 + File.separator + "Contents" + File.separator + "Java"; 72 } else { 73 app = "output" + File.separator + "test" + File.separator + "test"; 74 appOutputPath = "output" 75 + File.separator + "test" + File.separator + "app"; 76 } 77 } 78 79 private static void initArguments(boolean toolProvider) { 80 if (arguments.isEmpty()) { 81 arguments.add(ARGUMENT1); 82 arguments.add(ARGUMENT2); 83 arguments.add(ARGUMENT3); 84 } 85 86 String argumentsMap = JPackagerHelper.listToArgumentsMap(arguments, 87 toolProvider); 88 CMD[CMD.length - 1] = argumentsMap; 89 } 90 91 private static void validateResult(String[] result, List<String> args) 92 throws Exception { 93 if (result.length != (args.size() + 2)) { 94 for (String r : result) { 95 System.err.println(r.trim()); 96 } 97 throw new AssertionError("Unexpected number of lines: " 98 + result.length); 99 } 100 101 if (!result[0].trim().equals("jpackager test application")) { 102 throw new AssertionError("Unexpected result[0]: " + result[0]); 103 } 104 105 if (!result[1].trim().equals("args.length: 0")) { 106 throw new AssertionError("Unexpected result[1]: " + result[1]); 107 } 108 109 int index = 2; 110 for (String arg : args) { 111 if (!result[index].trim().equals(arg)) { 112 throw new AssertionError("Unexpected result[" + index + "]: " 113 + result[index]); 114 } 115 index++; 116 } 117 } 118 119 private static void validate(List<String> expectedArgs) throws Exception { 120 int retVal = JPackagerHelper.execute(null, app); 121 if (retVal != 0) { 122 throw new AssertionError("Test application exited with error: " 123 + retVal); 124 } 125 126 File outfile = new File(appOutputPath + File.separator + appOutput); 127 if (!outfile.exists()) { 128 throw new AssertionError(appOutput + " was not created"); 129 } 130 131 String output = Files.readString(outfile.toPath()); 132 String[] result = output.split("\n"); 133 validateResult(result, expectedArgs); 134 } 135 136 private static void testCreateImage() throws Exception { 137 initArguments(false); 138 JPackagerHelper.executeCLI(true, CMD); 139 validate(arguments); 140 } 141 142 private static void testCreateImageToolProvider() throws Exception { 143 initArguments(true); 144 JPackagerHelper.executeToolProvider(true, CMD); 145 validate(arguments); 146 } 147 148 public static void main(String[] args) throws Exception { 149 JPackagerHelper.createHelloJar(); 150 testCreateImage(); 151 testCreateImageToolProvider(); 152 } 153 154 }