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