8213332: Create minimal automated tests for jpackager
Submitten-by: almatvee
Reviewed-by: herrick. kcr
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/Hello.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.PrintWriter;
+
+public class Hello {
+
+ private static final String MSG = "jpackager test application";
+ private static final int EXPECTED_NUM_OF_PARAMS = 3; // Starts at 1
+
+ public static void main(String[] args) {
+ String outputFile = "appOutput.txt";
+ File file = new File(outputFile);
+
+ try (PrintWriter out =
+ new PrintWriter(new BufferedWriter(new FileWriter(file)))) {
+ System.out.println(MSG);
+ out.println(MSG);
+
+ System.out.println("args.length: " + args.length);
+ out.println("args.length: " + args.length);
+
+ for (String arg : args) {
+ System.out.println(arg);
+ out.println(arg);
+ }
+
+ for (int index = 1; index <= EXPECTED_NUM_OF_PARAMS; index++) {
+ String value = System.getProperty("param" + index);
+ if (value != null) {
+ System.out.println("-Dparam" + index + "=" + value);
+ out.println("-Dparam" + index + "=" + value);
+ }
+ }
+ } catch (Exception ex) {
+ System.err.println(ex.toString());
+ }
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerCreateImageArgumentsTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,167 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.List;
+
+/*
+ * @test
+ * @summary jpackager create image with --arguments test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerCreateImageArgumentsTest
+ */
+public class JPackagerCreateImageArgumentsTest {
+
+ private static final String app;
+ private static final String appOutput = "appOutput.txt";
+ private static final String appOutputPath;
+
+ private static final String[] CMD = {
+ "create-image",
+ "--input", "input",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--files", "hello.jar",
+ "--force",
+ "--arguments", "TBD"};
+
+ private static final String ARGUMENT1 = "argument";
+ private static final String ARGUMENT2 = "Some Arguments";
+ private static final String ARGUMENT3 = "Value \"with\" quotes";
+
+ private static final String ARGUMENT_CMD1 = "test";
+
+ private static final List<String> arguments = new ArrayList<>();
+ private static final List<String> argumentsCmd = new ArrayList<>();
+
+ static {
+ if (JPackagerHelper.isWindows()) {
+ app = "output" + File.separator + "test"
+ + File.separator + "test.exe";
+ appOutputPath = "output" + File.separator + "test"
+ + File.separator + "app";
+ } else if (JPackagerHelper.isOSX()) {
+ app = "output" + File.separator + "test.app"
+ + File.separator + "Contents"
+ + File.separator + "MacOS"
+ + File.separator + "test";
+ appOutputPath = "output" + File.separator + "test.app"
+ + File.separator + "Contents" + File.separator + "Java";
+ } else {
+ app = "output" + File.separator + "test" + File.separator + "test";
+ appOutputPath = "output"
+ + File.separator + "test" + File.separator + "app";
+ }
+ }
+
+ private static void initArguments(boolean toolProvider) {
+ if (arguments.isEmpty()) {
+ arguments.add(ARGUMENT1);
+ arguments.add(ARGUMENT2);
+ arguments.add(ARGUMENT3);
+ }
+
+ if (argumentsCmd.isEmpty()) {
+ argumentsCmd.add(ARGUMENT_CMD1);
+ }
+
+ String argumentsMap =
+ JPackagerHelper.listToArgumentsMap(arguments, toolProvider);
+ CMD[CMD.length - 1] = argumentsMap;
+ }
+
+ private static void validateResult(String[] result, List<String> args)
+ throws Exception {
+ if (result.length != (args.size() + 2)) {
+ throw new AssertionError(
+ "Unexpected number of lines: " + result.length);
+ }
+
+ if (!result[0].trim().equals("jpackager test application")) {
+ throw new AssertionError("Unexpected result[0]: " + result[0]);
+ }
+
+ if (!result[1].trim().equals("args.length: " + args.size())) {
+ throw new AssertionError("Unexpected result[1]: " + result[1]);
+ }
+
+ int index = 2;
+ for (String arg : args) {
+ if (!result[index].trim().equals(arg)) {
+ throw new AssertionError(
+ "Unexpected result[" + index + "]: " + result[index]);
+ }
+ index++;
+ }
+ }
+
+ private static void validate(String arg, List<String> expectedArgs)
+ throws Exception {
+ int retVal;
+
+ if (arg == null) {
+ retVal = JPackagerHelper.execute(null, app);
+ } else {
+ retVal = JPackagerHelper.execute(null, app, arg);
+ }
+ if (retVal != 0) {
+ throw new AssertionError("Test application exited with error: "
+ + retVal);
+ }
+
+ File outfile = new File(appOutputPath + File.separator + appOutput);
+ if (!outfile.exists()) {
+ throw new AssertionError(appOutput + " was not created");
+ }
+
+ String output = Files.readString(outfile.toPath());
+ String[] result = output.split("\n");
+ validateResult(result, expectedArgs);
+ }
+
+ private static void testCreateImage() throws Exception {
+ initArguments(false);
+ JPackagerHelper.executeCLI(true, CMD);
+ validate(null, arguments);
+ validate(ARGUMENT_CMD1, argumentsCmd);
+ }
+
+ private static void testCreateImageToolProvider() throws Exception {
+ initArguments(true);
+ JPackagerHelper.executeToolProvider(true, CMD);
+ validate(null, arguments);
+ validate(ARGUMENT_CMD1, argumentsCmd);
+ }
+
+ public static void main(String[] args) throws Exception {
+ JPackagerHelper.createHelloJar();
+ testCreateImage();
+ testCreateImageToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerCreateImageInputFilesTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.PrintWriter;
+
+ /*
+ * @test
+ * @summary jpackager create image input/files test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerCreateImageInputFilesTest
+ */
+public class JPackagerCreateImageInputFilesTest {
+ private static final String inputFile =
+ "input" + File.separator + "input.txt";
+ private static final String jarFile =
+ "input" + File.separator + "input.txt";
+ private static final String appOutputPath;
+ private static final String appInputFilePath;
+ private static final String appJarFilePath;
+
+ static {
+ if (JPackagerHelper.isOSX()) {
+ appOutputPath = "output" + File.separator + "test.app"
+ + File.separator + "Contents" + File.separator + "Java";
+ appInputFilePath = "output" + File.separator + "test.app"
+ + File.separator + "Contents" + File.separator + "Java"
+ + File.separator + "input.txt";
+ appJarFilePath = "output" + File.separator + "test.app"
+ + File.separator + "Contents" + File.separator + "Java"
+ + File.separator + "hello.jar";
+ } else {
+ appOutputPath = "output" + File.separator + "test"
+ + File.separator + "app";
+ appInputFilePath = "output" + File.separator + "test"
+ + File.separator + "app" + File.separator + "input.txt";
+ appJarFilePath = "output" + File.separator + "test"
+ + File.separator + "app" + File.separator + "hello.jar";
+ }
+ }
+
+ private static final String [] CMD_1 = {
+ "create-image",
+ "--input", "input",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--force",
+ "--class", "Hello"};
+
+ private static final String [] CMD_2 = {
+ "create-image",
+ "--input", "input",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--force",
+ "--files", "hello.jar"};
+
+ private static void validate1() throws Exception {
+ File input = new File(appInputFilePath);
+ if (!input.exists()) {
+ throw new AssertionError("Unexpected file does not exist: "
+ + input.getAbsolutePath());
+ }
+
+ File jar = new File(appJarFilePath);
+ if (!jar.exists()) {
+ throw new AssertionError("Unexpected file does not exist: "
+ + jar.getAbsolutePath());
+ }
+ }
+
+ private static void validate2() throws Exception {
+ File input = new File(appInputFilePath);
+ if (input.exists()) {
+ throw new AssertionError("Unexpected file exist: "
+ + input.getAbsolutePath());
+ }
+
+ File jar = new File(appJarFilePath);
+ if (!jar.exists()) {
+ throw new AssertionError("Unexpected file does not exist: "
+ + jar.getAbsolutePath());
+ }
+ }
+
+ private static void testCreateImage() throws Exception {
+ JPackagerHelper.executeCLI(true, CMD_1);
+ validate1();
+
+ JPackagerHelper.executeCLI(true, CMD_2);
+ validate2();
+ }
+
+ private static void testCreateImageToolProvider() throws Exception {
+ JPackagerHelper.executeToolProvider(true, CMD_1);
+ validate1();
+
+ JPackagerHelper.executeToolProvider(true, CMD_2);
+ validate2();
+ }
+
+ private static void createInputFile() throws Exception {
+ try (PrintWriter out = new PrintWriter(
+ new BufferedWriter(new FileWriter(inputFile)))) {
+ out.println("jpackgaer resource file");
+ }
+ }
+
+ private static void initCMD() {
+ File input = new File(inputFile);
+ File jar = new File(jarFile);
+
+ String inputPath = input.getAbsolutePath();
+ String jarPath = jar.getAbsolutePath();
+ }
+
+ public static void main(String[] args) throws Exception {
+ JPackagerHelper.createHelloJar();
+
+ createInputFile();
+ initCMD();
+
+ testCreateImage();
+ testCreateImageToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerCreateImageJVMArgsTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.List;
+
+/*
+ * @test
+ * @summary jpackager create image with --jvm-args test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerCreateImageJVMArgsTest
+ */
+public class JPackagerCreateImageJVMArgsTest {
+
+ private static final String app;
+ private static final String appOutput = "appOutput.txt";
+ private static final String appOutputPath;
+
+ private static final String[] CMD = {
+ "create-image",
+ "--input", "input",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--files", "hello.jar",
+ "--force",
+ "--jvm-args", "TBD"};
+
+ private static final String ARGUMENT1 = "-Dparam1=Some Param 1";
+ private static final String ARGUMENT2 = "-Dparam2=Some \"Param\" 2";
+ private static final String ARGUMENT3 =
+ "-Dparam3=Some \"Param\" with \" 3";
+
+ private static final List<String> arguments = new ArrayList<>();
+
+ static {
+ if (JPackagerHelper.isWindows()) {
+ app = "output" + File.separator + "test"
+ + File.separator + "test.exe";
+ appOutputPath = "output" + File.separator + "test"
+ + File.separator + "app";
+ } else if (JPackagerHelper.isOSX()) {
+ app = "output" + File.separator + "test.app"
+ + File.separator + "Contents" + File.separator + "MacOS"
+ + File.separator + "test"; appOutputPath = "output"
+ + File.separator + "test.app"
+ + File.separator + "Contents" + File.separator + "Java";
+ } else {
+ app = "output" + File.separator + "test" + File.separator + "test";
+ appOutputPath = "output"
+ + File.separator + "test" + File.separator + "app";
+ }
+ }
+
+ private static void initArguments(boolean toolProvider) {
+ if (arguments.isEmpty()) {
+ arguments.add(ARGUMENT1);
+ arguments.add(ARGUMENT2);
+ arguments.add(ARGUMENT3);
+ }
+
+ String argumentsMap = JPackagerHelper.listToArgumentsMap(arguments,
+ toolProvider);
+ CMD[CMD.length - 1] = argumentsMap;
+ }
+
+ private static void validateResult(String[] result, List<String> args)
+ throws Exception {
+ if (result.length != (args.size() + 2)) {
+ for (String r : result) {
+ System.err.println(r.trim());
+ }
+ throw new AssertionError("Unexpected number of lines: "
+ + result.length);
+ }
+
+ if (!result[0].trim().equals("jpackager test application")) {
+ throw new AssertionError("Unexpected result[0]: " + result[0]);
+ }
+
+ if (!result[1].trim().equals("args.length: 0")) {
+ throw new AssertionError("Unexpected result[1]: " + result[1]);
+ }
+
+ int index = 2;
+ for (String arg : args) {
+ if (!result[index].trim().equals(arg)) {
+ throw new AssertionError("Unexpected result[" + index + "]: "
+ + result[index]);
+ }
+ index++;
+ }
+ }
+
+ private static void validate(List<String> expectedArgs) throws Exception {
+ int retVal = JPackagerHelper.execute(null, app);
+ if (retVal != 0) {
+ throw new AssertionError("Test application exited with error: "
+ + retVal);
+ }
+
+ File outfile = new File(appOutputPath + File.separator + appOutput);
+ if (!outfile.exists()) {
+ throw new AssertionError(appOutput + " was not created");
+ }
+
+ String output = Files.readString(outfile.toPath());
+ String[] result = output.split("\n");
+ validateResult(result, expectedArgs);
+ }
+
+ private static void testCreateImage() throws Exception {
+ initArguments(false);
+ JPackagerHelper.executeCLI(true, CMD);
+ validate(arguments);
+ }
+
+ private static void testCreateImageToolProvider() throws Exception {
+ initArguments(true);
+ JPackagerHelper.executeToolProvider(true, CMD);
+ validate(arguments);
+ }
+
+ public static void main(String[] args) throws Exception {
+ JPackagerHelper.createHelloJar();
+ testCreateImage();
+ testCreateImageToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerCreateImageMissingArgumentsTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+ /*
+ * @test
+ * @summary jpackager create image missing arguments test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerCreateImageMissingArgumentsTest
+ */
+
+public class JPackagerCreateImageMissingArgumentsTest {
+ private static final String [] RESULT_1 = {"--output" };
+ private static final String [] CMD_1 = {
+ "create-image",
+ "--input", "input",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--force",
+ "--files", "hello.jar"};
+
+ private static final String [] RESULT_2 = {"--input"};
+ private static final String [] CMD_2 = {
+ "create-image",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--force",
+ "--files", "hello.jar"};
+
+ private static final String [] RESULT_3 = {"--input", "--app-image"};
+ private static final String [] CMD_3 = {
+ "create-installer",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--force",
+ "--files", "hello.jar"};
+
+ private static void validate(String output, String [] expected)
+ throws Exception {
+ String[] result = output.split("\n");
+ if (result.length != 1) {
+ System.err.println(output);
+ throw new AssertionError("Invalid number of lines in output: "
+ + result.length);
+ }
+
+ for (String s : expected) {
+ if (!result[0].contains(s)) {
+ System.err.println("Expected to contain: " + s);
+ System.err.println("Actual: " + result[0]);
+ throw new AssertionError("Unexpected error message");
+ }
+ }
+ }
+
+ private static void testMissingArg() throws Exception {
+ String output = JPackagerHelper.executeCLI(false, CMD_1);
+ validate(output, RESULT_1);
+
+ output = JPackagerHelper.executeCLI(false, CMD_2);
+ validate(output, RESULT_2);
+
+ output = JPackagerHelper.executeCLI(false, CMD_3);
+ validate(output, RESULT_3);
+ }
+
+ private static void testMissingArgToolProvider() throws Exception {
+ String output = JPackagerHelper.executeToolProvider(false, CMD_1);
+ validate(output, RESULT_1);
+
+ output = JPackagerHelper.executeToolProvider(false, CMD_2);
+ validate(output, RESULT_2);
+
+ output = JPackagerHelper.executeToolProvider(false, CMD_3);
+ validate(output, RESULT_3);
+ }
+
+ public static void main(String[] args) throws Exception {
+ JPackagerHelper.createHelloJar();
+ testMissingArg();
+ testMissingArgToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerCreateImageSecondaryLauncherTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.PrintWriter;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.List;
+
+ /*
+ * @test
+ * @summary jpackager create image with secondary launcher test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerCreateImageSecondaryLauncherTest
+ */
+public class JPackagerCreateImageSecondaryLauncherTest {
+ private static final String app;
+ private static final String app2;
+ private static final String appOutput = "appOutput.txt";
+ private static final String appOutputPath;
+ private static final String MSG = "jpackager test application";
+ private static final String [] CMD = {
+ "create-image",
+ "--input", "input",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--files", "hello.jar",
+ "--force",
+ "--secondary-launcher", "sl.properties"};
+
+ // Note: quotes in argument for secondary launcher is not support by test
+ private static final String ARGUMENT1 = "argument 1";
+ private static final String ARGUMENT2 = "argument 2";
+ private static final String ARGUMENT3 = "argument 3";
+
+ private static final List<String> arguments = new ArrayList<>();
+
+ private static final String PARAM1 = "-Dparam1=Some Param 1";
+ private static final String PARAM2 = "-Dparam2=Some Param 2";
+ private static final String PARAM3 = "-Dparam3=Some Param 3";
+
+ private static final List<String> vmArguments = new ArrayList<>();
+
+ static {
+ if (JPackagerHelper.isWindows()) {
+ app = "output" + File.separator + "test"
+ + File.separator + "test.exe";
+ app2 = "output" + File.separator + "test"
+ + File.separator + "test2.exe";
+ appOutputPath = "output" + File.separator + "test"
+ + File.separator + "app";
+ } else if (JPackagerHelper.isOSX()) {
+ app = "output" + File.separator + "test.app"
+ + File.separator + "Contents"
+ + File.separator + "MacOS" + File.separator + "test";
+ app2 = "output" + File.separator + "test.app"
+ + File.separator + "Contents"
+ + File.separator + "MacOS" + File.separator + "test2";
+ appOutputPath = "output" + File.separator + "test.app"
+ + File.separator + "Contents" + File.separator + "Java";
+ } else {
+ app = "output" + File.separator + "test" + File.separator + "test";
+ app2 = "output" + File.separator + "test"
+ + File.separator + "test2";
+ appOutputPath = "output" + File.separator + "test"
+ + File.separator + "app";
+ }
+ }
+
+ private static void validateResult(List<String> args, List<String> vmArgs)
+ throws Exception {
+ File outfile = new File(appOutputPath + File.separator + appOutput);
+ if (!outfile.exists()) {
+ throw new AssertionError(appOutput + " was not created");
+ }
+
+ String output = Files.readString(outfile.toPath());
+ String[] result = output.split("\n");
+
+ if (result.length != (args.size() + vmArgs.size() + 2)) {
+ throw new AssertionError("Unexpected number of lines: "
+ + result.length);
+ }
+
+ if (!result[0].trim().equals("jpackager test application")) {
+ throw new AssertionError("Unexpected result[0]: " + result[0]);
+ }
+
+ if (!result[1].trim().equals("args.length: " + args.size())) {
+ throw new AssertionError("Unexpected result[1]: " + result[1]);
+ }
+
+ int index = 2;
+ for (String arg : args) {
+ if (!result[index].trim().equals(arg)) {
+ throw new AssertionError("Unexpected result[" + index + "]: "
+ + result[index]);
+ }
+ index++;
+ }
+
+ for (String vmArg : vmArgs) {
+ if (!result[index].trim().equals(vmArg)) {
+ throw new AssertionError("Unexpected result[" + index + "]: "
+ + result[index]);
+ }
+ index++;
+ }
+ }
+
+ private static void validate() throws Exception {
+ int retVal = JPackagerHelper.execute(null, app);
+ if (retVal != 0) {
+ throw new AssertionError("Test application exited with error: "
+ + retVal);
+ }
+ validateResult(new ArrayList<>(), new ArrayList<>());
+
+ retVal = JPackagerHelper.execute(null, app2);
+ if (retVal != 0) {
+ throw new AssertionError("Test application exited with error: "
+ + retVal);
+ }
+ validateResult(arguments, vmArguments);
+ }
+
+ private static void testCreateImage() throws Exception {
+ JPackagerHelper.executeCLI(true, CMD);
+ validate();
+ }
+
+ private static void testCreateImageToolProvider() throws Exception {
+ JPackagerHelper.executeToolProvider(true, CMD);
+ validate();
+ }
+
+ private static void createSLProperties() throws Exception {
+ arguments.add(ARGUMENT1);
+ arguments.add(ARGUMENT2);
+ arguments.add(ARGUMENT3);
+
+ String argumentsMap =
+ JPackagerHelper.listToArgumentsMap(arguments, true);
+
+ vmArguments.add(PARAM1);
+ vmArguments.add(PARAM2);
+ vmArguments.add(PARAM3);
+
+ String vmArgumentsMap =
+ JPackagerHelper.listToArgumentsMap(vmArguments, true);
+
+ try (PrintWriter out = new PrintWriter(new BufferedWriter(
+ new FileWriter("sl.properties")))) {
+ out.println("name=test2");
+ out.println("arguments=" + argumentsMap);
+ out.println("jvm-args=" + vmArgumentsMap);
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ JPackagerHelper.createHelloJar();
+ createSLProperties();
+ testCreateImage();
+ testCreateImageToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerCreateImageStripNativeCommandsTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.stream.Collectors;
+
+ /*
+ * @test
+ * @summary jpackager create image with --strip-native-commands test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerCreateImageStripNativeCommandsTest
+ */
+public class JPackagerCreateImageStripNativeCommandsTest {
+ private static final String runtimeBinPath;
+
+ private static final String [] CMD = {
+ "create-image",
+ "--input", "input",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--files", "hello.jar",
+ "--force",
+ "--strip-native-commands"};
+
+ static {
+ if (JPackagerHelper.isWindows()) {
+ runtimeBinPath = "output" + File.separator + "test"
+ + File.separator + "runtime" + File.separator + "bin";
+ } else if (JPackagerHelper.isOSX()) {
+ runtimeBinPath = "output" + File.separator + "test.app"
+ + File.separator + "Contents"
+ + File.separator + "PlugIns" + File.separator
+ + "Java.runtime" + File.separator
+ + "Contents" + File.separator
+ + "Home" + File.separator + "bin";
+ } else {
+ runtimeBinPath = "output" + File.separator + "test"
+ + File.separator + "runtime" + File.separator + "bin";
+ }
+ }
+
+ private static void validate() throws Exception {
+ if (JPackagerHelper.isWindows()) {
+ Path binPath = Paths.get(runtimeBinPath).toAbsolutePath();
+ List<Path> files = Files.walk(binPath).collect(Collectors.toList());
+ files.forEach((f) -> {
+ if (f.toString().endsWith(".exe")) {
+ throw new AssertionError(
+ "Found executable file in runtime bin folder: "
+ + f.toString());
+ }
+ });
+ } else {
+ File binFolder = new File(runtimeBinPath);
+ if (binFolder.exists()) {
+ throw new AssertionError("Found bin folder in runtime: "
+ + binFolder.toString());
+ }
+ }
+ }
+
+ private static void testCreateImage() throws Exception {
+ JPackagerHelper.executeCLI(true, CMD);
+ validate();
+ }
+
+ private static void testCreateImageToolProvider() throws Exception {
+ JPackagerHelper.executeToolProvider(true, CMD);
+ validate();
+ }
+
+ public static void main(String[] args) throws Exception {
+ JPackagerHelper.createHelloJar();
+ testCreateImage();
+ testCreateImageToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerCreateImageTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.nio.file.Files;
+
+ /*
+ * @test
+ * @summary jpackager create image test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerCreateImageTest
+ */
+public class JPackagerCreateImageTest {
+ private static final String app;
+ private static final String appOutput = "appOutput.txt";
+ private static final String appOutputPath;
+ private static final String MSG = "jpackager test application";
+ private static final String [] CMD = {
+ "create-image",
+ "--input", "input",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--force",
+ "--files", "hello.jar"};
+
+ static {
+ if (JPackagerHelper.isWindows()) {
+ app = "output" + File.separator + "test"
+ + File.separator + "test.exe";
+ appOutputPath = "output" + File.separator + "test"
+ + File.separator + "app";
+ } else if (JPackagerHelper.isOSX()) {
+ app = "output" + File.separator + "test.app" + File.separator
+ + "Contents" + File.separator
+ + "MacOS" + File.separator + "test";
+ appOutputPath = "output" + File.separator + "test.app"
+ + File.separator + "Contents" + File.separator + "Java";
+ } else {
+ app = "output" + File.separator + "test" + File.separator + "test";
+ appOutputPath = "output" + File.separator + "test"
+ + File.separator + "app";
+ }
+ }
+
+ private static void validateResult(String[] result) throws Exception {
+ if (result.length != 2) {
+ throw new AssertionError(
+ "Unexpected number of lines: " + result.length);
+ }
+
+ if (!result[0].trim().equals("jpackager test application")) {
+ throw new AssertionError("Unexpected result[0]: " + result[0]);
+ }
+
+ if (!result[1].trim().equals("args.length: 0")) {
+ throw new AssertionError("Unexpected result[1]: " + result[1]);
+ }
+ }
+
+ private static void validate() throws Exception {
+ int retVal = JPackagerHelper.execute(null, app);
+ if (retVal != 0) {
+ throw new AssertionError(
+ "Test application exited with error: " + retVal);
+ }
+
+ File outfile = new File(appOutputPath + File.separator + appOutput);
+ if (!outfile.exists()) {
+ throw new AssertionError(appOutput + " was not created");
+ }
+
+ String output = Files.readString(outfile.toPath());
+ String[] result = output.split("\n");
+ validateResult(result);
+ }
+
+ private static void testCreateImage() throws Exception {
+ JPackagerHelper.executeCLI(true, CMD);
+ validate();
+ }
+
+ private static void testCreateImageToolProvider() throws Exception {
+ JPackagerHelper.executeToolProvider(true, CMD);
+ validate();
+ }
+
+ public static void main(String[] args) throws Exception {
+ JPackagerHelper.createHelloJar();
+ testCreateImage();
+ testCreateImageToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerCreateImageVerboseTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @summary jpackager create image verbose test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerCreateImageVerboseTest
+ */
+public class JPackagerCreateImageVerboseTest {
+
+ private static final String[] CMD = {
+ "create-image",
+ "--input", "input",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--force",
+ "--files", "hello.jar"};
+
+ private static final String[] CMD_VERBOSE = {
+ "create-image",
+ "--input", "input",
+ "--output", "output",
+ "--name", "test",
+ "--main-jar", "hello.jar",
+ "--class", "Hello",
+ "--files", "hello.jar",
+ "--force",
+ "--verbose"};
+
+ private static void validate(String result, String resultVerbose)
+ throws Exception {
+ String[] r = result.split("\n");
+ String[] rv = resultVerbose.split("\n");
+
+ if (r.length >= rv.length) {
+ System.err.println("r.length: " + r.length);
+ System.err.println(result);
+ System.err.println("rv.length: " + rv.length);
+ System.err.println(resultVerbose);
+ throw new AssertionError(
+ "non-verbose output is less or equal to verbose output");
+ }
+ }
+
+ private static void testCreateImage() throws Exception {
+ String result = JPackagerHelper.executeCLI(true, CMD);
+ String resultVerbose = JPackagerHelper.executeCLI(true, CMD_VERBOSE);
+ validate(result, resultVerbose);
+ }
+
+ private static void testCreateImageToolProvider() throws Exception {
+ String result = JPackagerHelper.executeToolProvider(true, CMD);
+ String resultVerbose =
+ JPackagerHelper.executeToolProvider(true, CMD_VERBOSE);
+ validate(result, resultVerbose);
+ }
+
+ public static void main(String[] args) throws Exception {
+ JPackagerHelper.createHelloJar();
+ testCreateImage();
+ testCreateImageToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerHelpTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+ /*
+ * @test
+ * @summary jpackager help test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerHelpTest
+ */
+public class JPackagerHelpTest {
+
+ // Platform specific help messages.
+ private static final String WINDOWS_HELP =
+ "The following options are valid for Windows platforms";
+ private static final String OSX_HELP =
+ "The following options are valid for Mac OS X platforms";
+ private static final String LINUX_HELP =
+ "The following options are valid for Linux platforms";
+
+ private static void validate(String output1, String output2)
+ throws Exception {
+ if (output1.split("\n").length < 25) {
+ throw new AssertionError("jpacakger --help failed");
+ }
+
+ if (output2.split("\n").length < 25) {
+ throw new AssertionError("jpacakger -h failed");
+ }
+
+ // Make sure output matches for --help and -h
+ if (!output1.equals(output2)) {
+ System.err.println("================= --help =================");
+ System.err.println(output1);
+
+ System.err.println("=================== -h ===================");
+ System.err.println(output2);
+
+ throw new AssertionError(
+ "jpacakger help text does not match between --help and -h");
+ }
+
+ if (JPackagerHelper.isWindows()) {
+ if (!output1.contains(WINDOWS_HELP)) {
+ throw new AssertionError(
+ "jpacakger help text missing Windows specific help");
+ }
+
+ if (output1.contains(OSX_HELP) || output1.contains(LINUX_HELP)) {
+ throw new AssertionError(
+ "jpacakger help text contains other platforms specific help");
+
+ }
+ } else if (JPackagerHelper.isOSX()) {
+ if (!output1.contains(OSX_HELP)) {
+ throw new AssertionError(
+ "jpacakger help text missing OS X specific help");
+ }
+
+ if (output1.contains(WINDOWS_HELP) ||
+ output1.contains(LINUX_HELP)) {
+ throw new AssertionError(
+ "jpacakger help text contains other platforms specific help");
+ }
+ } else if (JPackagerHelper.isLinux()) {
+ if (!output1.contains(LINUX_HELP)) {
+ throw new AssertionError(
+ "jpacakger help text missing Linux specific help");
+ }
+
+ if (output1.contains(OSX_HELP) || output1.contains(WINDOWS_HELP)) {
+ throw new AssertionError(
+ "jpacakger help text contains other platforms specific help");
+ }
+ }
+ }
+
+ private static void testHelp() throws Exception {
+ String output1 = JPackagerHelper.executeCLI(true, "--help");
+ String output2 = JPackagerHelper.executeCLI(true, "-h");
+ validate(output1, output2);
+ }
+
+ private static void testHelpToolProvider() throws Exception {
+ String output1 = JPackagerHelper.executeToolProvider(true, "--help");
+ String output2 = JPackagerHelper.executeToolProvider(true, "-h");
+ validate(output1, output2);
+ }
+
+ public static void main(String[] args) throws Exception {
+ testHelp();
+ testHelpToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerHelper.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,363 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+import java.util.spi.ToolProvider;
+
+public class JPackagerHelper {
+
+ private static final boolean VERBOSE = false;
+ private static final String OS =
+ System.getProperty("os.name").toLowerCase();
+ private static final String JAVA_HOME = System.getProperty("java.home");
+ private static final String TEST_SRC = System.getProperty("test.src");
+ private static final Path BIN_DIR = Path.of(JAVA_HOME, "bin");
+ private static final Path JPACKAGER;
+ private static final Path JAVAC;
+ private static final Path JAR;
+
+ static {
+ if (OS.startsWith("win")) {
+ JPACKAGER = BIN_DIR.resolve("jpackager.exe");
+ JAVAC = BIN_DIR.resolve("javac.exe");
+ JAR = BIN_DIR.resolve("jar.exe");
+ } else {
+ JPACKAGER = BIN_DIR.resolve("jpackager");
+ JAVAC = BIN_DIR.resolve("javac");
+ JAR = BIN_DIR.resolve("jar");
+ }
+ }
+
+ static final ToolProvider JPACKAGER_TOOL =
+ ToolProvider.findFirst("jpackager").orElseThrow(
+ () -> new RuntimeException("jpackager tool not found"));
+
+ public static int execute(File out, String... command) throws Exception {
+ if (VERBOSE) {
+ System.out.print("Execute command: ");
+ for (String c : command) {
+ System.out.print(c);
+ System.out.print(" ");
+ }
+ System.out.println();
+ }
+
+ ProcessBuilder builder = new ProcessBuilder(command);
+ if (out != null) {
+ builder.redirectErrorStream(true);
+ builder.redirectOutput(out);
+ }
+
+ Process process = builder.start();
+ return process.waitFor();
+ }
+
+ private static String[] getCommand(String... args) {
+ String[] command;
+ if (args == null) {
+ command = new String[1];
+ } else {
+ command = new String[args.length + 1];
+ }
+
+ int index = 0;
+ command[index] = JPACKAGER.toString();
+
+ if (args != null) {
+ for (String arg : args) {
+ index++;
+ command[index] = arg;
+ }
+ }
+
+ return command;
+ }
+
+ public static String executeCLI(boolean retValZero, String... args)
+ throws Exception {
+ int retVal;
+ File outfile = new File("output.log");
+ try {
+ String[] command = getCommand(args);
+ retVal = execute(outfile, command);
+ } catch (Exception ex) {
+ if (outfile.exists()) {
+ System.err.println(Files.readString(outfile.toPath()));
+ }
+ throw ex;
+ }
+
+ String output = Files.readString(outfile.toPath());
+ if (retValZero) {
+ if (retVal != 0) {
+ System.err.println(output);
+ throw new AssertionError("jpackager exited with error: "
+ + retVal);
+ }
+ } else {
+ if (retVal == 0) {
+ System.err.println(output);
+ throw new AssertionError("jpackager exited without error: "
+ + retVal);
+ }
+ }
+
+ if (VERBOSE) {
+ System.out.println("output =");
+ System.out.println(output);
+ }
+
+ return output;
+ }
+
+ public static String executeToolProvider(
+ boolean retValZero, String... args) throws Exception {
+ StringWriter writer = new StringWriter();
+ PrintWriter pw = new PrintWriter(writer);
+ int retVal = JPACKAGER_TOOL.run(pw, pw, args);
+ String output = writer.toString();
+
+ if (retValZero) {
+ if (retVal != 0) {
+ System.err.println(output);
+ throw new AssertionError("jpackager exited with error: "
+ + retVal);
+ }
+ } else {
+ if (retVal == 0) {
+ System.err.println(output);
+ throw new AssertionError("jpackager exited without error");
+ }
+ }
+
+ if (VERBOSE) {
+ System.out.println("output =");
+ System.out.println(output);
+ }
+
+ return output;
+ }
+
+ public static boolean isWindows() {
+ return (OS.contains("win"));
+ }
+
+ public static boolean isOSX() {
+ return (OS.contains("mac"));
+ }
+
+ public static boolean isLinux() {
+ return ((OS.contains("nix") || OS.contains("nux")));
+ }
+
+ public static void createHelloJar() throws Exception {
+ int retVal;
+
+ File input = new File("input");
+ if (!input.exists()) {
+ input.mkdir();
+ }
+
+ Files.copy(Path.of(TEST_SRC + File.separator + "Hello.java"),
+ Path.of("Hello.java"));
+
+ File javacLog = new File("javac.log");
+ try {
+ retVal = execute(javacLog, JAVAC.toString(), "Hello.java");
+ } catch (Exception ex) {
+ if (javacLog.exists()) {
+ System.err.println(Files.readString(javacLog.toPath()));
+ }
+ throw ex;
+ }
+
+ if (retVal != 0) {
+ if (javacLog.exists()) {
+ System.err.println(Files.readString(javacLog.toPath()));
+ }
+ throw new AssertionError("javac exited with error: " + retVal);
+ }
+
+ File jarLog = new File("jar.log");
+ try {
+ retVal = execute(null, JAR.toString(), "cvf",
+ "input" + File.separator + "hello.jar", "Hello.class");
+ } catch (Exception ex) {
+ if (jarLog.exists()) {
+ System.err.println(Files.readString(jarLog.toPath()));
+ }
+ throw ex;
+ }
+
+ if (retVal != 0) {
+ if (jarLog.exists()) {
+ System.err.println(Files.readString(jarLog.toPath()));
+ }
+ throw new AssertionError("jar exited with error: " + retVal);
+ }
+ }
+
+ public static String listToArgumentsMap(List<String> arguments,
+ boolean toolProvider) {
+ if (arguments.isEmpty()) {
+ return "";
+ }
+
+ String argsStr = "";
+ for (int i = 0; i < arguments.size(); i++) {
+ String arg = arguments.get(i);
+ argsStr += quote(arg, toolProvider);
+ if ((i + 1) != arguments.size()) {
+ argsStr += " ";
+ }
+ }
+
+ if (!toolProvider && isWindows()) {
+ if (argsStr.contains(" ")) {
+ if (argsStr.contains("\"")) {
+ argsStr = escapeQuote(argsStr, toolProvider);
+ }
+ argsStr = "\"" + argsStr + "\"";
+ }
+ }
+ return argsStr;
+ }
+
+ private static String quote(String in, boolean toolProvider) {
+ if (in == null) {
+ return null;
+ }
+
+ if (in.isEmpty()) {
+ return "";
+ }
+
+ if (!in.contains("=")) {
+ // Not a property
+ if (in.contains(" ")) {
+ in = escapeQuote(in, toolProvider);
+ return "\"" + in + "\"";
+ }
+ return in;
+ }
+
+ if (!in.contains(" ")) {
+ return in; // No need to quote
+ }
+
+ int paramIndex = in.indexOf("=");
+ if (paramIndex <= 0) {
+ return in; // Something wrong, just skip quoting
+ }
+
+ String param = in.substring(0, paramIndex);
+ String value = in.substring(paramIndex + 1);
+
+ if (value.length() == 0) {
+ return in; // No need to quote
+ }
+
+ value = escapeQuote(value, toolProvider);
+
+ return param + "=" + "\"" + value + "\"";
+ }
+
+ private static String escapeQuote(String in, boolean toolProvider) {
+ if (in == null) {
+ return null;
+ }
+
+ if (in.isEmpty()) {
+ return "";
+ }
+
+ if (in.contains("\"")) {
+ // Use code points to preserve non-ASCII chars
+ StringBuilder sb = new StringBuilder();
+ int codeLen = in.codePointCount(0, in.length());
+ for (int i = 0; i < codeLen; i++) {
+ int code = in.codePointAt(i);
+ // Note: No need to escape '\' on Linux or OS X
+ // jpackager expects us to pass arguments and properties with
+ // quotes and spaces as a map
+ // with quotes being escaped with additional \ for
+ // internal quotes.
+ // So if we want two properties below:
+ // -Djnlp.Prop1=Some "Value" 1
+ // -Djnlp.Prop2=Some Value 2
+ // jpackager will need:
+ // "-Djnlp.Prop1=\"Some \\"Value\\" 1\" -Djnlp.Prop2=\"Some Value 2\""
+ // but since we using ProcessBuilder to run jpackager we will need to escape
+ // our escape symbols as well, so we will need to pass string below to ProcessBuilder:
+ // "-Djnlp.Prop1=\\\"Some \\\\\\\"Value\\\\\\\" 1\\\" -Djnlp.Prop2=\\\"Some Value 2\\\""
+ switch (code) {
+ case '"':
+ // " -> \" -> \\\"
+ if (i == 0 || in.codePointAt(i - 1) != '\\') {
+ if (!toolProvider && isWindows()) {
+ sb.appendCodePoint('\\');
+ sb.appendCodePoint('\\');
+ }
+ sb.appendCodePoint('\\');
+ sb.appendCodePoint(code);
+ }
+ break;
+ case '\\':
+ // We need to escape already escaped symbols as well
+ if ((i + 1) < codeLen) {
+ int nextCode = in.codePointAt(i + 1);
+ if (nextCode == '"') {
+ // \" -> \\\"
+ sb.appendCodePoint('\\');
+ sb.appendCodePoint('\\');
+ sb.appendCodePoint('\\');
+ sb.appendCodePoint(nextCode);
+ } else {
+ sb.appendCodePoint('\\');
+ sb.appendCodePoint(code);
+ }
+ } else {
+ if (isWindows()) {
+ sb.appendCodePoint('\\');
+ }
+ sb.appendCodePoint(code);
+ }
+ break;
+ default:
+ sb.appendCodePoint(code);
+ break;
+ }
+ }
+ return sb.toString();
+ }
+
+ return in;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerInvalidArgTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+ /*
+ * @test
+ * @summary jpackager invalid argument test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerInvalidArgTest
+ */
+public class JPackagerInvalidArgTest {
+
+ private static final String ARG = "--no-such-argument";
+ private static final String RESULT1 =
+ "Illegal argument [--no-such-argument]";
+ private static final String RESULT2 = "ERROR: Mode is not specified";
+
+ private static void validate(String output) throws Exception {
+ String[] result = output.split("\n");
+ if (result.length != 2) {
+ System.err.println(output);
+ throw new AssertionError("Invalid number of lines in output: "
+ + result.length);
+ }
+
+ if (!result[0].trim().equals(RESULT1)) {
+ System.err.println("Expected: " + RESULT1);
+ System.err.println("Actual: " + result[0]);
+ throw new AssertionError("Unexpected line 1");
+ }
+
+ if (!result[1].trim().equals(RESULT2)) {
+ System.err.println("Expected: " + RESULT2);
+ System.err.println("Actual: " + result[1]);
+ throw new AssertionError("Unexpected line 2");
+ }
+ }
+
+ private static void testInvalidArg() throws Exception {
+ String output = JPackagerHelper.executeCLI(false, ARG);
+ validate(output);
+ }
+
+ private static void testInvalidArgToolProvider() throws Exception {
+ String output = JPackagerHelper.executeToolProvider(false, ARG);
+ validate(output);
+ }
+
+ public static void main(String[] args) throws Exception {
+ testInvalidArg();
+ testInvalidArgToolProvider();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackager/JPackagerNoArgTest.java Thu Nov 08 19:07:57 2018 -0500
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+ /*
+ * @test
+ * @summary jpackager no argument test
+ * @build JPackagerHelper
+ * @modules jdk.jpackager
+ * @run main/othervm -Xmx512m JPackagerNoArgTest
+ */
+public class JPackagerNoArgTest {
+
+ private static final String RESULT1 = "Usage: jpackager <mode> <options>";
+ private static final String RESULT2 =
+ "Use --help for a list of possible options";
+
+ private static void validate(String output) throws Exception {
+ String[] result = output.split("\n");
+ if (result.length != 2) {
+ System.err.println(output);
+ throw new AssertionError(
+ "Invalid number of lines in output: " + result.length);
+ }
+
+ if (!result[0].trim().equals(RESULT1)) {
+ System.err.println("Expected: " + RESULT1);
+ System.err.println("Actual: " + result[0]);
+ throw new AssertionError("Unexpected line 1");
+ }
+
+ if (!result[1].trim().equals(RESULT2)) {
+ System.err.println("Expected: " + RESULT2);
+ System.err.println("Actual: " + result[1]);
+ throw new AssertionError("Unexpected line 2");
+ }
+ }
+
+ private static void testNoArg() throws Exception {
+ String output = JPackagerHelper.executeCLI(true, new String[0]);
+ validate(output);
+ }
+
+ private static void testNoArgToolProvider() throws Exception {
+ String output =
+ JPackagerHelper.executeToolProvider(true, new String[0]);
+ validate(output);
+ }
+
+ public static void main(String[] args) throws Exception {
+ testNoArg();
+ testNoArgToolProvider();
+ }
+
+}