8245202: Convert existing jpackage tests to newer form

Reviewed-by: asemenyuk, almatvee
This commit is contained in:
Andy Herrick 2020-05-27 08:59:43 -04:00
parent afbdb4932e
commit 4af3a1e078
15 changed files with 387 additions and 1897 deletions

View File

@ -1,679 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.BufferedWriter;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.spi.ToolProvider;
public class JPackageHelper {
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");
public static final String TEST_SRC_ROOT;
public static final String TEST_SRC;
private static final Path BIN_DIR = Path.of(JAVA_HOME, "bin");
private static final Path JPACKAGE;
private static final Path JAVAC;
private static final Path JAR;
private static final Path JLINK;
public static class ModuleArgs {
private final String version;
private final String mainClass;
ModuleArgs(String version, String mainClass) {
this.version = version;
this.mainClass = mainClass;
}
public String getVersion() {
return version;
}
public String getMainClass() {
return mainClass;
}
}
static {
if (OS.startsWith("win")) {
JPACKAGE = BIN_DIR.resolve("jpackage.exe");
JAVAC = BIN_DIR.resolve("javac.exe");
JAR = BIN_DIR.resolve("jar.exe");
JLINK = BIN_DIR.resolve("jlink.exe");
} else {
JPACKAGE = BIN_DIR.resolve("jpackage");
JAVAC = BIN_DIR.resolve("javac");
JAR = BIN_DIR.resolve("jar");
JLINK = BIN_DIR.resolve("jlink");
}
// Figure out test src based on where we called
TEST_SRC = System.getProperty("test.src");
Path root = Path.of(TEST_SRC);
Path apps = Path.of(TEST_SRC, "apps");
if (apps.toFile().exists()) {
// fine - test is at root
} else {
apps = Path.of(TEST_SRC, "..", "apps");
if (apps.toFile().exists()) {
root = apps.getParent().normalize(); // test is 1 level down
} else {
apps = Path.of(TEST_SRC, "..", "..", "apps");
if (apps.toFile().exists()) {
root = apps.getParent().normalize(); // 2 levels down
} else {
apps = Path.of(TEST_SRC, "..", "..", "..", "apps");
if (apps.toFile().exists()) {
root = apps.getParent().normalize(); // 3 levels down
} else {
// if we ever have tests more than three levels
// down we need to add code here
throw new RuntimeException("we should never get here");
}
}
}
}
TEST_SRC_ROOT = root.toString();
}
static final ToolProvider JPACKAGE_TOOL =
ToolProvider.findFirst("jpackage").orElseThrow(
() -> new RuntimeException("jpackage 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();
}
public static Process executeNoWait(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);
}
return builder.start();
}
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] = JPACKAGE.toString();
if (args != null) {
for (String arg : args) {
index++;
command[index] = arg;
}
}
return command;
}
public static void deleteRecursive(File path) throws IOException {
if (!path.exists()) {
return;
}
Path directory = path.toPath();
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attr) throws IOException {
file.toFile().setWritable(true);
if (OS.startsWith("win")) {
try {
Files.setAttribute(file, "dos:readonly", false);
} catch (Exception ioe) {
// just report and try to contune
System.err.println("IOException: " + ioe);
ioe.printStackTrace(System.err);
}
}
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attr) throws IOException {
if (OS.startsWith("win")) {
Files.setAttribute(dir, "dos:readonly", false);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e)
throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
public static void deleteOutputFolder(String output) throws IOException {
File outputFolder = new File(output);
System.out.println("deleteOutputFolder: " + outputFolder.getAbsolutePath());
try {
deleteRecursive(outputFolder);
} catch (IOException ioe) {
System.err.println("IOException: " + ioe);
ioe.printStackTrace(System.err);
deleteRecursive(outputFolder);
}
}
public static String executeCLI(boolean retValZero, String... args) throws Exception {
int retVal;
File outfile = new File("output.log");
String[] command = getCommand(args);
try {
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("command run:");
for (String s : command) { System.err.println(s); }
System.err.println("command output:");
System.err.println(output);
throw new AssertionError("jpackage exited with error: " + retVal);
}
} else {
if (retVal == 0) {
System.err.println(output);
throw new AssertionError("jpackage 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 = JPACKAGE_TOOL.run(pw, pw, args);
String output = writer.toString();
if (retValZero) {
if (retVal != 0) {
System.err.println(output);
throw new AssertionError("jpackage exited with error: " + retVal);
}
} else {
if (retVal == 0) {
System.err.println(output);
throw new AssertionError("jpackage 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 createHelloImageJar(String inputDir) throws Exception {
createJar(false, "Hello", "image", inputDir);
}
public static void createHelloImageJar() throws Exception {
createJar(false, "Hello", "image", "input");
}
public static void createHelloImageJarWithMainClass() throws Exception {
createJar(true, "Hello", "image", "input");
}
public static void createHelloInstallerJar() throws Exception {
createJar(false, "Hello", "installer", "input");
}
public static void createHelloInstallerJarWithMainClass() throws Exception {
createJar(true, "Hello", "installer", "input");
}
private static void createJar(boolean mainClassAttribute, String name,
String testType, String inputDir) throws Exception {
int retVal;
File input = new File(inputDir);
if (!input.exists()) {
input.mkdirs();
}
Path src = Path.of(TEST_SRC_ROOT + File.separator + "apps"
+ File.separator + testType + File.separator + name + ".java");
Path dst = Path.of(name + ".java");
if (dst.toFile().exists()) {
Files.delete(dst);
}
Files.copy(src, dst);
File javacLog = new File("javac.log");
try {
retVal = execute(javacLog, JAVAC.toString(), name + ".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 {
List<String> args = new ArrayList<>();
args.add(JAR.toString());
args.add("-c");
args.add("-v");
args.add("-f");
args.add(inputDir + File.separator + name.toLowerCase() + ".jar");
if (mainClassAttribute) {
args.add("-e");
args.add(name);
}
args.add(name + ".class");
retVal = execute(jarLog, args.stream().toArray(String[]::new));
} 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 void createHelloModule() throws Exception {
createModule("Hello.java", "input", "hello", null, true);
}
public static void createHelloModule(ModuleArgs moduleArgs) throws Exception {
createModule("Hello.java", "input", "hello", moduleArgs, true);
}
private static void createModule(String javaFile, String inputDir, String aName,
ModuleArgs moduleArgs, boolean createModularJar) throws Exception {
int retVal;
File input = new File(inputDir);
if (!input.exists()) {
input.mkdir();
}
File module = new File("module" + File.separator + "com." + aName);
if (!module.exists()) {
module.mkdirs();
}
File javacLog = new File("javac.log");
try {
List<String> args = new ArrayList<>();
args.add(JAVAC.toString());
args.add("-d");
args.add("module" + File.separator + "com." + aName);
args.add(TEST_SRC_ROOT + File.separator + "apps" + File.separator
+ "com." + aName + File.separator + "module-info.java");
args.add(TEST_SRC_ROOT + File.separator + "apps"
+ File.separator + "com." + aName + File.separator + "com"
+ File.separator + aName + File.separator + javaFile);
retVal = execute(javacLog, args.stream().toArray(String[]::new));
} 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);
}
if (createModularJar) {
File jarLog = new File("jar.log");
try {
List<String> args = new ArrayList<>();
args.add(JAR.toString());
args.add("--create");
args.add("--file");
args.add(inputDir + File.separator + "com." + aName + ".jar");
if (moduleArgs != null) {
if (moduleArgs.getVersion() != null) {
args.add("--module-version");
args.add(moduleArgs.getVersion());
}
if (moduleArgs.getMainClass()!= null) {
args.add("--main-class");
args.add(moduleArgs.getMainClass());
}
}
args.add("-C");
args.add("module" + File.separator + "com." + aName);
args.add(".");
retVal = execute(jarLog, args.stream().toArray(String[]::new));
} 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 void createRuntime() throws Exception {
List<String> moreArgs = new ArrayList<>();
createRuntime(moreArgs);
}
public static void createRuntime(List<String> moreArgs) throws Exception {
int retVal;
File jlinkLog = new File("jlink.log");
try {
List<String> args = new ArrayList<>();
args.add(JLINK.toString());
args.add("--output");
args.add("runtime");
args.add("--add-modules");
args.add("java.base");
args.addAll(moreArgs);
retVal = execute(jlinkLog, args.stream().toArray(String[]::new));
} catch (Exception ex) {
if (jlinkLog.exists()) {
System.err.println(Files.readString(jlinkLog.toPath()));
}
throw ex;
}
if (retVal != 0) {
if (jlinkLog.exists()) {
System.err.println(Files.readString(jlinkLog.toPath()));
}
throw new AssertionError("jlink 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;
}
public static String[] cmdWithAtFilename(String [] cmd, int ndx, int len)
throws IOException {
ArrayList<String> newAList = new ArrayList<>();
String fileString = null;
for (int i=0; i<cmd.length; i++) {
if (i == ndx) {
newAList.add("@argfile.cmds");
fileString = cmd[i];
} else if (i > ndx && i < ndx + len) {
fileString += " " + cmd[i];
} else {
newAList.add(cmd[i]);
}
}
if (fileString != null) {
Path path = new File("argfile.cmds").toPath();
try (BufferedWriter bw = Files.newBufferedWriter(path);
PrintWriter out = new PrintWriter(bw)) {
out.println(fileString);
}
}
return newAList.toArray(new String[0]);
}
public static String [] splitAndFilter(String output) {
if (output == null) {
return null;
}
return Stream.of(output.split("\\R"))
.filter(str -> !str.startsWith("Picked up"))
.filter(str -> !str.startsWith("WARNING: Using incubator"))
.filter(str -> !str.startsWith("hello: "))
.collect(Collectors.toList()).toArray(String[]::new);
}
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
// jpackage 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
// jpackage will need:
// "-Djnlp.Prop1=\"Some \\"Value\\" 1\" -Djnlp.Prop2=\"Some Value 2\""
// but since we using ProcessBuilder to run jpackage 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) != '\\') {
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 {
sb.appendCodePoint(code);
}
break;
default:
sb.appendCodePoint(code);
break;
}
}
return sb.toString();
}
return in;
}
}

View File

@ -1,147 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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.StandardCopyOption;
import java.util.List;
public class JPackageInstallerHelper {
private static final String JPACKAGE_TEST_OUTPUT = "jpackage.test.output";
private static final String JPACKAGE_VERIFY_INSTALL = "jpackage.verify.install";
private static final String JPACKAGE_VERIFY_UNINSTALL = "jpackage.verify.uninstall";
private static String testOutput;
private static final boolean isTestOutputSet;
private static final boolean isVerifyInstall;
private static final boolean isVerifyUnInstall;
static {
String out = System.getProperty(JPACKAGE_TEST_OUTPUT);
isTestOutputSet = (out != null);
if (isTestOutputSet) {
File file = new File(out);
if (!file.exists()) {
throw new AssertionError(file.getAbsolutePath() + " does not exist");
}
if (!file.isDirectory()) {
throw new AssertionError(file.getAbsolutePath() + " is not a directory");
}
if (!file.canWrite()) {
throw new AssertionError(file.getAbsolutePath() + " is not writable");
}
if (out.endsWith(File.separator)) {
out = out.substring(0, out.length() - 2);
}
testOutput = out;
}
isVerifyInstall = (System.getProperty(JPACKAGE_VERIFY_INSTALL) != null);
isVerifyUnInstall = (System.getProperty(JPACKAGE_VERIFY_UNINSTALL) != null);
}
public static boolean isTestOutputSet() {
return isTestOutputSet;
}
public static boolean isVerifyInstall() {
return isVerifyInstall;
}
public static boolean isVerifyUnInstall() {
return isVerifyUnInstall;
}
public static void copyTestResults(List<String> files) throws Exception {
if (!isTestOutputSet()) {
return;
}
File dest = new File(testOutput);
if (!dest.exists()) {
dest.mkdirs();
}
if (JPackageHelper.isWindows()) {
files.add(JPackagePath.getTestSrc() + File.separator + "install.bat");
files.add(JPackagePath.getTestSrc() + File.separator + "uninstall.bat");
} else {
files.add(JPackagePath.getTestSrc() + File.separator + "install.sh");
files.add(JPackagePath.getTestSrc() + File.separator + "uninstall.sh");
}
for (String file : files) {
Path source = Path.of(file);
Path target = Path.of(dest.toPath() + File.separator + source.getFileName());
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
}
}
public static void validateApp(String app) throws Exception {
File outFile = new File("appOutput.txt");
if (outFile.exists()) {
outFile.delete();
}
int retVal = JPackageHelper.execute(outFile, app);
if (retVal != 0) {
throw new AssertionError(
"Test application exited with error: " + retVal);
}
if (!outFile.exists()) {
throw new AssertionError(outFile.getAbsolutePath() + " was not created");
}
String output = Files.readString(outFile.toPath());
String[] result = output.split("\n");
if (result.length != 2) {
System.err.println(output);
throw new AssertionError(
"Unexpected number of lines: " + result.length);
}
if (!result[0].trim().equals("jpackage 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]);
}
}
public static void validateOutput(String output) throws Exception {
File file = new File(output);
if (!file.exists()) {
// Try lower case in case of OS is case sensitive
file = new File(output.toLowerCase());
if (!file.exists()) {
throw new AssertionError("Cannot find " + file.getAbsolutePath());
}
}
}
}

View File

@ -1,167 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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.Path;
/**
* Helper class which contains functions to get different system
* dependent paths used by tests
*/
public class JPackagePath {
// Return path to test src adjusted to location of caller
public static String getTestSrcRoot() {
return JPackageHelper.TEST_SRC_ROOT;
}
// Return path to calling test
public static String getTestSrc() {
return JPackageHelper.TEST_SRC;
}
// Returns path to generate test application
public static String getApp() {
return getApp("test");
}
public static String getApp(String name) {
return getAppSL(name, name);
}
// Returns path to generate test application icon
public static String getAppIcon() {
return getAppIcon("test");
}
public static String getAppIcon(String name) {
if (JPackageHelper.isWindows()) {
return Path.of("output", name, name + ".ico").toString();
} else if (JPackageHelper.isOSX()) {
return Path.of("output", name + ".app",
"Contents", "Resources", name + ".icns").toString();
} else if (JPackageHelper.isLinux()) {
return Path.of("output", name, "lib", name + ".png").toString();
} else {
throw new AssertionError("Cannot detect platform");
}
}
// Returns path to generate secondary launcher of given application
public static String getAppSL(String sl) {
return getAppSL("test", sl);
}
public static String getAppSL(String app, String sl) {
if (JPackageHelper.isWindows()) {
return Path.of("output", app, sl + ".exe").toString();
} else if (JPackageHelper.isOSX()) {
return Path.of("output", app + ".app",
"Contents", "MacOS", sl).toString();
} else if (JPackageHelper.isLinux()) {
return Path.of("output", app, "bin", sl).toString();
} else {
throw new AssertionError("Cannot detect platform");
}
}
// Returns path to test application cfg file
public static String getAppCfg() {
return getAppCfg("test");
}
public static String getAppCfg(String name) {
if (JPackageHelper.isWindows()) {
return Path.of("output", name, "app", name + ".cfg").toString();
} else if (JPackageHelper.isOSX()) {
return Path.of("output", name + ".app",
"Contents", "app", name + ".cfg").toString();
} else if (JPackageHelper.isLinux()) {
return Path.of("output", name, "lib", "app", name + ".cfg").toString();
} else {
throw new AssertionError("Cannot detect platform");
}
}
// Returns path including executable to java in image runtime folder
public static String getRuntimeJava() {
return getRuntimeJava("test");
}
public static String getRuntimeJava(String name) {
if (JPackageHelper.isWindows()) {
return Path.of(getRuntimeBin(name), "java.exe").toString();
}
return Path.of(getRuntimeBin(name), "java").toString();
}
// Returns output file name generate by test application
public static String getAppOutputFile() {
return "appOutput.txt";
}
// Returns path to bin folder in image runtime
public static String getRuntimeBin() {
return getRuntimeBin("test");
}
public static String getRuntimeBin(String name) {
if (JPackageHelper.isWindows()) {
return Path.of("output", name, "runtime", "bin").toString();
} else if (JPackageHelper.isOSX()) {
return Path.of("output", name + ".app",
"Contents", "runtime",
"Contents", "Home", "bin").toString();
} else if (JPackageHelper.isLinux()) {
return Path.of("output", name, "lib", "runtime", "bin").toString();
} else {
throw new AssertionError("Cannot detect platform");
}
}
public static String getOSXInstalledApp(String testName) {
return File.separator + "Applications"
+ File.separator + testName + ".app"
+ File.separator + "Contents"
+ File.separator + "MacOS"
+ File.separator + testName;
}
public static String getOSXInstalledApp(String subDir, String testName) {
return File.separator + "Applications"
+ File.separator + subDir
+ File.separator + testName + ".app"
+ File.separator + "Contents"
+ File.separator + "MacOS"
+ File.separator + testName;
}
// Returs path to test license file
public static String getLicenseFilePath() {
String path = JPackagePath.getTestSrcRoot()
+ File.separator + "resources"
+ File.separator + "license.txt";
return path;
}
}

View File

@ -137,9 +137,11 @@ public final class HelloApp {
if (moduleName == null && CLASS_NAME.equals(qualifiedClassName)) {
// Use Hello.java as is.
cmd.addPrerequisiteAction((self) -> {
Path jarFile = self.inputDir().resolve(appDesc.jarFileName());
createJarBuilder().setOutputJar(jarFile).addSourceFile(
HELLO_JAVA).create();
if (self.inputDir() != null) {
Path jarFile = self.inputDir().resolve(appDesc.jarFileName());
createJarBuilder().setOutputJar(jarFile).addSourceFile(
HELLO_JAVA).create();
}
});
} else if (appDesc.jmodFileName() != null) {
// Modular app in .jmod file
@ -152,12 +154,15 @@ public final class HelloApp {
final Path jarFile;
if (moduleName == null) {
jarFile = cmd.inputDir().resolve(appDesc.jarFileName());
} else {
} else if (getModulePath.get() != null) {
jarFile = getModulePath.get().resolve(appDesc.jarFileName());
} else {
jarFile = null;
}
if (jarFile != null) {
TKit.withTempDirectory("src",
workDir -> prepareSources(workDir).setOutputJar(jarFile).create());
}
TKit.withTempDirectory("src",
workDir -> prepareSources(workDir).setOutputJar(jarFile).create());
});
}
@ -260,22 +265,36 @@ public final class HelloApp {
public static void executeLauncherAndVerifyOutput(JPackageCommand cmd,
String... args) {
AppOutputVerifier av = getVerifier(cmd, args);
if (av != null) {
av.executeAndVerifyOutput(args);
}
}
public static Executor.Result executeLauncher(JPackageCommand cmd,
String... args) {
AppOutputVerifier av = getVerifier(cmd, args);
return av.executeOnly(args);
}
private static AppOutputVerifier getVerifier(JPackageCommand cmd,
String... args) {
final Path launcherPath = cmd.appLauncherPath();
if (cmd.isFakeRuntime(String.format("Not running [%s] launcher",
launcherPath))) {
return;
return null;
}
assertApp(launcherPath)
return assertApp(launcherPath)
.addDefaultArguments(Optional
.ofNullable(cmd.getAllArgumentValues("--arguments"))
.orElseGet(() -> new String[0]))
.addJavaOptions(Optional
.ofNullable(cmd.getAllArgumentValues("--java-options"))
.orElseGet(() -> new String[0]))
.executeAndVerifyOutput(args);
.orElseGet(() -> new String[0]));
}
public final static class AppOutputVerifier {
AppOutputVerifier(Path helloAppLauncher) {
this.launcherPath = helloAppLauncher;
@ -326,7 +345,27 @@ public final class HelloApp {
}
public void executeAndVerifyOutput(String... args) {
// Output file will be created in the current directory.
getExecutor(args).dumpOutput().execute();
final List<String> launcherArgs = List.of(args);
final List<String> appArgs;
if (launcherArgs.isEmpty()) {
appArgs = defaultLauncherArgs;
} else {
appArgs = launcherArgs;
}
Path outputFile = TKit.workDir().resolve(OUTPUT_FILENAME);
verifyOutputFile(outputFile, appArgs, params);
}
public Executor.Result executeOnly(String...args) {
return getExecutor(args).saveOutput().executeWithoutExitCodeCheck();
}
private Executor getExecutor(String...args) {
// Output file might be created in the current directory.
Path outputFile = TKit.workDir().resolve(OUTPUT_FILENAME);
ThrowingFunction.toFunction(Files::deleteIfExists).apply(outputFile);
@ -339,21 +378,10 @@ public final class HelloApp {
}
final List<String> launcherArgs = List.of(args);
new Executor()
return new Executor()
.setDirectory(outputFile.getParent())
.setExecutable(executablePath)
.addArguments(launcherArgs)
.dumpOutput()
.execute();
final List<String> appArgs;
if (launcherArgs.isEmpty()) {
appArgs = defaultLauncherArgs;
} else {
appArgs = launcherArgs;
}
verifyOutputFile(outputFile, appArgs, params);
.addArguments(launcherArgs);
}
private final Path launcherPath;

View File

@ -1,80 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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.nio.file.Files;
import java.nio.file.Path;
public abstract class Base {
private static final String appOutput = JPackagePath.getAppOutputFile();
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().endsWith("jpackage 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]);
}
}
public static void validate(String app) throws Exception {
Path outPath = Path.of(appOutput);
int retVal = JPackageHelper.execute(null, app);
if (outPath.toFile().exists()) {
System.out.println("output contents: ");
System.out.println(Files.readString(outPath) + "\n");
} else {
System.out.println("no output file: " + outPath
+ " from command: " + app);
}
if (retVal != 0) {
throw new AssertionError(
"Test application (" + app + ") exited with error: " + retVal);
}
if (!outPath.toFile().exists()) {
throw new AssertionError(appOutput + " was not created");
}
String output = Files.readString(outPath);
String[] result = JPackageHelper.splitAndFilter(output);
validateResult(result);
}
public static void testCreateAppImage(String [] cmd) throws Exception {
JPackageHelper.executeCLI(true, cmd);
validate(JPackagePath.getApp());
}
public static void testCreateAppImageToolProvider(String [] cmd) throws Exception {
JPackageHelper.executeToolProvider(true, cmd);
validate(JPackagePath.getApp());
}
}

View File

@ -1,100 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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 jpackage create app image error test
* @library ../helpers
* @build JPackageHelper
* @build JPackagePath
* @build Base
* @modules jdk.incubator.jpackage
* @run main/othervm -Xmx512m ErrorTest
*/
import java.util.*;
import java.io.*;
import java.nio.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
public class ErrorTest {
private static final String OUTPUT = "output";
private static final String ARG1 = "--no-such-argument";
private static final String EXPECTED1 =
"Invalid Option: [--no-such-argument]";
private static final String ARG2 = "--dest";
private static final String EXPECTED2 = "--main-jar or --module";
private static final String [] CMD1 = {
"--type", "app-image",
"--input", "input",
"--dest", OUTPUT,
"--name", "test",
"--main-jar", "non-existant.jar",
};
private static final String EXP1 = "main jar does not exist";
private static final String [] CMD2 = {
"--type", "app-image",
"--input", "input",
"--dest", OUTPUT,
"--name", "test",
"--main-jar", "hello.jar",
};
private static final String EXP2 = "class was not specified nor was";
private static void validate(String output, String expected, boolean single)
throws Exception {
String[] result = JPackageHelper.splitAndFilter(output);
if (single && result.length != 1) {
System.err.println(output);
throw new AssertionError("Unexpected multiple lines of output: "
+ output);
}
if (!result[0].trim().contains(expected)) {
throw new AssertionError("Unexpected output: " + result[0]
+ " - expected output to contain: " + expected);
}
}
public static void main(String[] args) throws Exception {
JPackageHelper.createHelloImageJar();
validate(JPackageHelper.executeToolProvider(false,
"--type", "app-image", ARG1), EXPECTED1, true);
validate(JPackageHelper.executeToolProvider(false,
"--type", "app-image", ARG2), EXPECTED2, true);
JPackageHelper.deleteOutputFolder(OUTPUT);
validate(JPackageHelper.executeToolProvider(false, CMD1), EXP1, false);
JPackageHelper.deleteOutputFolder(OUTPUT);
validate(JPackageHelper.executeToolProvider(false, CMD2), EXP2, false);
}
}

View File

@ -1,137 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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 jdk.incubator.jpackage.internal.Bundlers;
import jdk.incubator.jpackage.internal.Bundler;
/*
* @test
* @summary jpackage invalid argument test
* @library ../helpers
* @build JPackageHelper
* @build JPackagePath
* @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
* @run main/othervm -Xmx512m InvalidArgTest
*/
public class InvalidArgTest {
private static final String ARG1 = "--no-such-argument";
private static final String ARG2 = "--dest";
private static final String ARG3 = "--runtime-image";
private static final String ARG4 = "--resource-dir";
private static final String RESULT1 =
"Invalid Option: [--no-such-argument]";
private static final String RESULT2 = "--main-jar or --module";
private static final String RESULT3 = "does not exist";
private static final String RESULT4 = "does not exist";
private static void validate(String arg, String output) throws Exception {
String[] result = JPackageHelper.splitAndFilter(output);
if (result.length != 1) {
System.err.println(output);
throw new AssertionError("Invalid number of lines in output: "
+ result.length);
}
if (arg.equals(ARG1)) {
if (!result[0].trim().contains(RESULT1)) {
System.err.println("Expected: " + RESULT1);
System.err.println("Actual: " + result[0]);
throw new AssertionError("Unexpected output: " + result[0]);
}
} else if (arg.equals(ARG2)) {
if (!result[0].trim().contains(RESULT2)) {
System.err.println("Expected: " + RESULT2);
System.err.println("Actual: " + result[0]);
throw new AssertionError("Unexpected output: " + result[0]);
}
} else if (arg.equals(ARG3)) {
if (!result[0].trim().contains(RESULT3)) {
System.err.println("Expected error msg to contain: " + RESULT3);
System.err.println("Actual: " + result[0]);
throw new AssertionError("Unexpected output: " + result[0]);
}
} else if (arg.equals(ARG4)) {
if (!result[0].trim().contains(RESULT4)) {
System.err.println("Expected error msg to contain: " + RESULT4);
System.err.println("Actual: " + result[0]);
throw new AssertionError("Unexpected output: " + result[0]);
}
}
}
private static boolean defaultSupported() {
for (Bundler bundler :
Bundlers.createBundlersInstance().getBundlers("INSTALLER")) {
if (bundler.isDefault() && bundler.supported(true)) {
return true;
}
}
return false;
}
private static void testInvalidArg() throws Exception {
String output = JPackageHelper.executeCLI(false,
"--type", "app-image", ARG1);
validate(ARG1, output);
output = JPackageHelper.executeCLI(false,
"--type", "app-image", ARG2);
validate(ARG2, output);
output = JPackageHelper.executeCLI(false,
ARG3, "JDK-non-existant");
validate(ARG3, output);
output = JPackageHelper.executeCLI(false,
ARG3, System.getProperty("java.home"),
ARG4, "non-existant-resource-dir");
validate(ARG4, output);
}
private static void testInvalidArgToolProvider() throws Exception {
String output = JPackageHelper.executeToolProvider(false,
"--type", "app-image", ARG1);
validate(ARG1, output);
output = JPackageHelper.executeToolProvider(false,
"--type", "app-image", ARG2);
validate(ARG2, output);
output = JPackageHelper.executeToolProvider(false,
ARG3, "JDK-non-existant");
validate(ARG3, output);
output = JPackageHelper.executeCLI(false,
ARG3, System.getProperty("java.home"),
ARG4, "non-existant-resource-dir");
validate(ARG4, output);
}
public static void main(String[] args) throws Exception {
testInvalidArg();
testInvalidArgToolProvider();
}
}

View File

@ -1,148 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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;
public class JavaOptionsBase {
private static final String app = JPackagePath.getApp();
private static final String appOutput = JPackagePath.getAppOutputFile();
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<>();
private static void initArguments(boolean toolProvider, String [] cmd) {
if (arguments.isEmpty()) {
arguments.add(ARGUMENT1);
arguments.add(ARGUMENT2);
arguments.add(ARGUMENT3);
}
String argumentsMap = JPackageHelper.listToArgumentsMap(arguments,
toolProvider);
cmd[cmd.length - 1] = argumentsMap;
}
private static void initArguments2(boolean toolProvider, String [] cmd) {
int index = cmd.length - 6;
cmd[index++] = "--java-options";
arguments.clear();
arguments.add(ARGUMENT1);
cmd[index++] = JPackageHelper.listToArgumentsMap(arguments,
toolProvider);
cmd[index++] = "--java-options";
arguments.clear();
arguments.add(ARGUMENT2);
cmd[index++] = JPackageHelper.listToArgumentsMap(arguments,
toolProvider);
cmd[index++] = "--java-options";
arguments.clear();
arguments.add(ARGUMENT3);
cmd[index++] = JPackageHelper.listToArgumentsMap(arguments,
toolProvider);
arguments.clear();
arguments.add(ARGUMENT1);
arguments.add(ARGUMENT2);
arguments.add(ARGUMENT3);
}
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("jpackage 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 = JPackageHelper.execute(null, app);
if (retVal != 0) {
throw new AssertionError("Test application exited with error: "
+ retVal);
}
File outfile = new File(appOutput);
if (!outfile.exists()) {
throw new AssertionError(appOutput + " was not created");
}
String output = Files.readString(outfile.toPath());
String[] result = JPackageHelper.splitAndFilter(output);
validateResult(result, expectedArgs);
}
public static void testCreateAppImageJavaOptions(String [] cmd) throws Exception {
initArguments(false, cmd);
JPackageHelper.executeCLI(true, cmd);
validate(arguments);
}
public static void testCreateAppImageJavaOptionsToolProvider(String [] cmd) throws Exception {
initArguments(true, cmd);
JPackageHelper.executeToolProvider(true, cmd);
validate(arguments);
}
public static void testCreateAppImageJavaOptions2(String [] cmd) throws Exception {
initArguments2(false, cmd);
JPackageHelper.executeCLI(true, cmd);
validate(arguments);
}
public static void testCreateAppImageJavaOptions2ToolProvider(String [] cmd) throws Exception {
initArguments2(true, cmd);
JPackageHelper.executeToolProvider(true, cmd);
validate(arguments);
}
}

View File

@ -1,123 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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 jpackage create image with --java-options test
* @library ../helpers
* @build JPackageHelper
* @build JPackagePath
* @build JavaOptionsBase
* @modules jdk.incubator.jpackage
* @run main/othervm -Xmx512m JavaOptionsEqualsTest
*/
public class JavaOptionsEqualsTest {
private static final String app = JPackagePath.getApp();
private static final String OUTPUT = "output";
private static final String WARNING_1
= "WARNING: Unknown module: me.mymodule.foo";
private static final String WARNING_2
= "WARNING: Unknown module: other.mod.bar";
private static final String[] CMD = {
"--type", "app-image",
"--input", "input",
"--description", "the two options below should cause two app execution "
+ "Warnings with two lines output saying: "
+ "WARNING: Unknown module: <module-name>",
"--dest", OUTPUT,
"--name", "test",
"--main-jar", "hello.jar",
"--main-class", "Hello",
"--java-options",
"--add-exports=java.base/sun.util=me.mymodule.foo,ALL-UNNAMED",
"--java-options",
"--add-exports=java.base/sun.security.util=other.mod.bar,ALL-UNNAMED",
};
private static void validate() throws Exception {
File outfile = new File("app.out");
int retVal = JPackageHelper.execute(outfile, app);
if (retVal != 0) {
throw new AssertionError(
"Test application exited with error: " + retVal);
}
if (!outfile.exists()) {
throw new AssertionError(
"outfile: " + outfile + " was not created");
}
String output = Files.readString(outfile.toPath());
System.out.println("App output:");
System.out.print(output);
String[] result = JPackageHelper.splitAndFilter(output);
if (result.length < 4) {
throw new AssertionError(
"Unexpected number of lines: " + result.length
+ " - output: " + output);
}
String nextWarning = WARNING_1;
if (!result[0].startsWith(nextWarning)){
nextWarning = WARNING_2;
if (!result[0].startsWith(WARNING_2)){
throw new AssertionError("Unexpected result[0]: " + result[0]);
} else {
nextWarning = WARNING_1;
}
}
if (!result[1].startsWith(nextWarning)) {
throw new AssertionError("Unexpected result[1]: " + result[1]);
}
if (!result[result.length - 2].trim().endsWith("jpackage test application")) {
throw new AssertionError("Unexpected result[2]: " + result[2]);
}
if (!result[result.length - 1].trim().equals("args.length: 0")) {
throw new AssertionError("Unexpected result[3]: " + result[3]);
}
}
public static void main(String[] args) throws Exception {
JPackageHelper.createHelloImageJar();
String output = JPackageHelper.executeCLI(true, CMD);
validate();
JPackageHelper.deleteOutputFolder(OUTPUT);
output = JPackageHelper.executeToolProvider(true, CMD);
validate();
}
}

View File

@ -1,68 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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 jpackage create image with --java-options test
* @library ../helpers
* @build JPackageHelper
* @build JPackagePath
* @build JavaOptionsBase
* @modules jdk.incubator.jpackage
* @run main/othervm -Xmx512m JavaOptionsModuleTest
*/
public class JavaOptionsModuleTest {
private static final String OUTPUT = "output";
private static final String[] CMD = {
"--type", "app-image",
"--dest", OUTPUT,
"--name", "test",
"--module", "com.hello/com.hello.Hello",
"--module-path", "input",
"--java-options", "TBD"};
private static final String[] CMD2 = {
"--type", "app-image",
"--dest", OUTPUT,
"--name", "test",
"--module", "com.hello/com.hello.Hello",
"--module-path", "input",
"--java-options", "TBD",
"--java-options", "TBD",
"--java-options", "TBD"};
public static void main(String[] args) throws Exception {
JPackageHelper.createHelloModule();
JavaOptionsBase.testCreateAppImageJavaOptions(CMD);
JPackageHelper.deleteOutputFolder(OUTPUT);
JavaOptionsBase.testCreateAppImageJavaOptionsToolProvider(CMD);
JPackageHelper.deleteOutputFolder(OUTPUT);
JavaOptionsBase.testCreateAppImageJavaOptions2(CMD2);
JPackageHelper.deleteOutputFolder(OUTPUT);
JavaOptionsBase.testCreateAppImageJavaOptions2ToolProvider(CMD2);
}
}

View File

@ -1,69 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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 jpackage create image with --java-options test
* @library ../helpers
* @build JPackageHelper
* @build JPackagePath
* @build JavaOptionsBase
* @modules jdk.incubator.jpackage
* @run main/othervm -Xmx512m JavaOptionsTest
*/
public class JavaOptionsTest {
private static final String OUTPUT = "output";
private static final String[] CMD = {
"--type", "app-image",
"--input", "input",
"--dest", OUTPUT,
"--name", "test",
"--main-jar", "hello.jar",
"--main-class", "Hello",
"--java-options", "TBD"};
private static final String[] CMD2 = {
"--type", "app-image",
"--input", "input",
"--dest", OUTPUT,
"--name", "test",
"--main-jar", "hello.jar",
"--main-class", "Hello",
"--java-options", "TBD",
"--java-options", "TBD",
"--java-options", "TBD"};
public static void main(String[] args) throws Exception {
JPackageHelper.createHelloImageJar();
JavaOptionsBase.testCreateAppImageJavaOptions(CMD);
JPackageHelper.deleteOutputFolder(OUTPUT);
JavaOptionsBase.testCreateAppImageJavaOptionsToolProvider(CMD);
JPackageHelper.deleteOutputFolder(OUTPUT);
JavaOptionsBase.testCreateAppImageJavaOptions2(CMD2);
JPackageHelper.deleteOutputFolder(OUTPUT);
JavaOptionsBase.testCreateAppImageJavaOptions2ToolProvider(CMD2);
}
}

View File

@ -1,154 +0,0 @@
/*
* Copyright (c) 2018, 2019, 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 jpackage create image missing arguments test
* @library ../helpers
* @build JPackageHelper
* @build JPackagePath
* @modules jdk.incubator.jpackage
* @run main/othervm -Xmx512m MissingArgumentsTest
*/
public class MissingArgumentsTest {
private static final String [] RESULT_1 = {"--input"};
private static final String [] CMD_1 = {
"--type", "app-image",
"--dest", "output",
"--name", "test",
"--main-jar", "hello.jar",
"--main-class", "Hello",
};
private static final String [] RESULT_2 = {"--input", "--app-image"};
private static final String [] CMD_2 = {
"--type", "app-image",
"--type", "invalid-type",
"--dest", "output",
"--name", "test",
"--main-jar", "hello.jar",
"--main-class", "Hello",
};
private static final String [] RESULT_3 = {"main class was not specified"};
private static final String [] CMD_3 = {
"--type", "app-image",
"--input", "input",
"--dest", "output",
"--name", "test",
"--main-jar", "hello.jar",
};
private static final String [] RESULT_4 = {"--main-jar"};
private static final String [] CMD_4 = {
"--type", "app-image",
"--input", "input",
"--dest", "output",
"--name", "test",
"--main-class", "Hello",
};
private static final String [] RESULT_5 = {"--module-path", "--runtime-image"};
private static final String [] CMD_5 = {
"--type", "app-image",
"--dest", "output",
"--name", "test",
"--module", "com.hello/com.hello.Hello",
};
private static final String [] RESULT_6 = {"--module-path", "--runtime-image",
"--app-image"};
private static final String [] CMD_6 = {
"--type", "invalid-type",
"--dest", "output",
"--name", "test",
"--module", "com.hello/com.hello.Hello",
};
private static void validate(String output, String [] expected,
boolean single) throws Exception {
String[] result = JPackageHelper.splitAndFilter(output);
if (single && 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 = JPackageHelper.executeCLI(false, CMD_1);
validate(output, RESULT_1, true);
output = JPackageHelper.executeCLI(false, CMD_2);
validate(output, RESULT_2, true);
output = JPackageHelper.executeCLI(false, CMD_3);
validate(output, RESULT_3, false);
output = JPackageHelper.executeCLI(false, CMD_4);
validate(output, RESULT_4, true);
output = JPackageHelper.executeCLI(false, CMD_5);
validate(output, RESULT_5, true);
output = JPackageHelper.executeCLI(false, CMD_6);
validate(output, RESULT_6, true);
}
private static void testMissingArgToolProvider() throws Exception {
String output = JPackageHelper.executeToolProvider(false, CMD_1);
validate(output, RESULT_1, true);
output = JPackageHelper.executeToolProvider(false, CMD_2);
validate(output, RESULT_2, true);
output = JPackageHelper.executeToolProvider(false, CMD_3);
validate(output, RESULT_3, false);
output = JPackageHelper.executeToolProvider(false, CMD_4);
validate(output, RESULT_4, true);
output = JPackageHelper.executeToolProvider(false, CMD_5);
validate(output, RESULT_5, true);
output = JPackageHelper.executeToolProvider(false, CMD_6);
validate(output, RESULT_6, true);
}
public static void main(String[] args) throws Exception {
JPackageHelper.createHelloImageJar();
testMissingArg();
testMissingArgToolProvider();
}
}

View File

@ -0,0 +1,136 @@
/*
* Copyright (c) 2020, 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.
*/
package jdk.jpackage.tests;
import java.util.Collection;
import java.util.List;
import jdk.jpackage.test.Annotations.Parameters;
import jdk.jpackage.test.Annotations.Test;
import jdk.jpackage.test.JPackageCommand;
import jdk.jpackage.test.TKit;
/*
* @test
* @summary jpackage application version testing
* @library ../../../../helpers
* @build jdk.jpackage.test.*
* @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
* @compile ErrorTest.java
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
* --jpt-run=jdk.jpackage.tests.ErrorTest
* --jpt-before-run=jdk.jpackage.test.JPackageCommand.useExecutableByDefault
*/
/*
* @test
* @summary jpackage application version testing
* @library ../../../../helpers
* @build jdk.jpackage.test.*
* @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
* @compile ErrorTest.java
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
* --jpt-run=jdk.jpackage.tests.ErrorTest
* --jpt-before-run=jdk.jpackage.test.JPackageCommand.useToolProviderByDefault
*/
public final class ErrorTest {
private final String expectedError;
private final JPackageCommand cmd;
@Parameters
public static Collection input() {
return List.of(new Object[][]{
// non-existent arg
{"Hello",
new String[]{"--no-such-argument"},
null,
"Invalid Option: [--no-such-argument]"},
// no main jar
{"Hello",
null,
new String[]{"--main-jar"},
"--main-jar or --module"},
// no main-class
{"Hello",
null,
new String[]{"--main-class"},
"main class was not specified"},
// non-existent main jar
{"Hello",
new String[]{"--main-jar", "non-existent.jar"},
null,
"main jar does not exist"},
// non-existent runtime
{"Hello",
new String[]{"--runtime-image", "non-existent.runtime"},
null,
"does not exist"},
// non-existent resource-dir
{"Hello",
new String[]{"--resource-dir", "non-existent.dir"},
null,
"does not exist"},
// invalid type
{"Hello",
new String[]{"--type", "invalid-type"},
null,
"Invalid or unsupported type:"},
// no --input
{"Hello",
null,
new String[]{"--input"},
"Missing argument: --input"},
// no --module-path
{"com.other/com.other.Hello",
null,
new String[]{"--module-path"},
"Missing argument: --runtime-image or --module-path"},
});
}
public ErrorTest(String javaAppDesc, String[] jpackageArgs,
String[] removeArgs,
String expectedError) {
this.expectedError = expectedError;
cmd = JPackageCommand.helloAppImage(javaAppDesc)
.saveConsoleOutput(true).dumpOutput(true);
if (jpackageArgs != null) {
cmd.addArguments(jpackageArgs);
} if (removeArgs != null) {
for (String arg : removeArgs) {
cmd.removeArgumentWithValue(arg);
}
}
}
@Test
public void test() {
List<String> output = cmd.execute(1).getOutput();
TKit.assertNotNull(output, "output is null");
TKit.assertTextStream(expectedError).apply(output.stream());
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright (c) 2018, 2020, 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.
*/
package jdk.jpackage.tests;
import java.util.Collection;
import java.util.List;
import jdk.jpackage.test.Annotations.Parameters;
import jdk.jpackage.test.Annotations.Test;
import jdk.jpackage.test.JPackageCommand;
import jdk.jpackage.test.HelloApp;
import jdk.jpackage.test.Executor;
import jdk.jpackage.test.TKit;
/*
* @test
* @summary jpackage create image with --java-options test
* @library ../../../../helpers
* @build jdk.jpackage.test.*
* @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
* @compile JavaOptionsEqualsTest.java
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
* --jpt-run=jdk.jpackage.tests.JavaOptionsEqualsTest
* --jpt-before-run=jdk.jpackage.test.JPackageCommand.useExecutableByDefault
*/
/*
* @test
* @summary jpackage create image with --java-options test
* @library ../../../../helpers
* @build jdk.jpackage.test.*
* @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
* @compile JavaOptionsEqualsTest.java
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
* --jpt-run=jdk.jpackage.tests.JavaOptionsEqualsTest
* --jpt-before-run=jdk.jpackage.test.JPackageCommand.useToolProviderByDefault
*/
public class JavaOptionsEqualsTest {
private final static String OPTION1 =
"--add-exports=java.base/sun.util=me.mymodule.foo,ALL-UNNAMED";
private final static String OPTION2 =
"--add-exports=java.base/sun.security.util=other.mod.bar,ALL-UNNAMED";
private final static String WARNING1 =
"WARNING: Unknown module: me.mymodule.foo";
private final static String WARNING2 =
"WARNING: Unknown module: other.mod.bar";
private final JPackageCommand cmd;
@Parameters
public static Collection input() {
return List.of(new Object[][]{
{"Hello", new String[]{"--java-options", OPTION1,
"--java-options", OPTION2 },
},
});
}
public JavaOptionsEqualsTest(String javaAppDesc, String[] jpackageArgs) {
cmd = JPackageCommand.helloAppImage(javaAppDesc);
if (jpackageArgs != null) {
cmd.addArguments(jpackageArgs);
}
}
@Test
public void test() {
cmd.executeAndAssertHelloAppImageCreated();
List<String> output = HelloApp.executeLauncher(cmd).getOutput();
TKit.assertNotNull(output, "output is null");
TKit.assertTextStream(WARNING1).apply(output.stream());
TKit.assertTextStream(WARNING2).apply(output.stream());
}
}

View File

@ -0,0 +1,102 @@
/*
* Copyright (c) 2018, 2020, 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.
*/
package jdk.jpackage.tests;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import jdk.jpackage.test.Annotations.Parameters;
import jdk.jpackage.test.Annotations.Test;
import jdk.jpackage.test.JPackageCommand;
import jdk.jpackage.test.HelloApp;
import jdk.jpackage.test.Executor;
import jdk.jpackage.test.TKit;
/*
* @test
* @summary jpackage create image with --java-options test
* @library ../../../../helpers
* @build jdk.jpackage.test.*
* @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
* @compile JavaOptionsTest.java
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
* --jpt-run=jdk.jpackage.tests.JavaOptionsTest
* --jpt-before-run=jdk.jpackage.test.JPackageCommand.useToolProviderByDefault
*/
public class JavaOptionsTest {
private static final String PARAM1 = "Some Param 1";
private static final String PARAM2 = "Some \"Param\" 2";
private static final String PARAM3 = "Some \"Param\" with \" 3";
private static final String ARG1 = "-Dparam1=" + "\'" + PARAM1 + "\'";
private static final String ARG2 = "-Dparam2=" + "\'" + PARAM2 + "\'";
private static final String ARG3 = "-Dparam3=" + "\'" + PARAM3 + "\'";
private static final String EXPECT1 = "-Dparam1=" + PARAM1;
private static final String EXPECT2 = "-Dparam2=" + PARAM2;
private static final String EXPECT3 = "-Dparam3=" + PARAM3;
private final JPackageCommand cmd;
private final String[] expected;
@Parameters
public static Collection input() {
List<Object[]> result = new ArrayList<>();
for (var app : List.of("Hello", "com.other/com.other.Hello")) {
result.add(new Object[]{app, new String[]{"--java-options", ARG1},
new String[]{EXPECT1},});
result.add(new Object[]{app, new String[]{"--java-options", ARG2},
new String[]{EXPECT2},});
result.add(new Object[]{app, new String[]{"--java-options", ARG3},
new String[]{EXPECT3},});
result.add(new Object[]{app, new String[]{"--java-options", ARG1,
"--java-options", ARG2, "--java-options", ARG3}, new String[]{
EXPECT1, EXPECT2, EXPECT3},});
}
return List.of(result.toArray(Object[][]::new));
}
public JavaOptionsTest(String javaAppDesc, String[] jpackageArgs,
String[] expectedParams) {
cmd = JPackageCommand.helloAppImage(javaAppDesc);
if (jpackageArgs != null) {
cmd.addArguments(jpackageArgs);
}
expected = expectedParams;
}
@Test
public void test() {
// 1.) run the jpackage command
cmd.executeAndAssertImageCreated();
// 2.) run the launcher it generated
List<String> output = HelloApp.executeLauncher(cmd).getOutput();
TKit.assertNotNull(output, "output is null");
for (String expect : expected) {
TKit.assertTextStream(expect).apply(output.stream());
}
}
}