< prev index next >
test/jdk/tools/jpackage/helpers/JPackageHelper.java
Print this page
*** 23,42 ****
--- 23,46 ----
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;
+ import jdk.incubator.jpackage.ToolProviderFactory;
public class JPackageHelper {
private static final boolean VERBOSE = false;
private static final String OS = System.getProperty("os.name").toLowerCase();
*** 47,56 ****
--- 51,78 ----
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");
*** 61,98 ****
JAR = BIN_DIR.resolve("jar");
JLINK = BIN_DIR.resolve("jlink");
}
// Figure out test src based on where we called
! File testSrc = new File(System.getProperty("test.src") + File.separator + ".."
! + File.separator + "apps");
! if (testSrc.exists()) {
! TEST_SRC_ROOT = System.getProperty("test.src") + File.separator + "..";
! } else {
! testSrc = new File(System.getProperty("test.src") + File.separator
! + ".." + File.separator + ".." + File.separator + "apps");
! if (testSrc.exists()) {
! TEST_SRC_ROOT = System.getProperty("test.src") + File.separator + ".."
! + File.separator + "..";
! } else {
! testSrc = new File(System.getProperty("test.src") + File.separator
! + ".." + File.separator + ".." + File.separator + ".."
! + File.separator + "apps");
! if (testSrc.exists()) {
! TEST_SRC_ROOT = System.getProperty("test.src") + File.separator + ".."
! + File.separator + ".." + File.separator + "..";
} else {
! TEST_SRC_ROOT = System.getProperty("test.src");
}
}
}
!
! TEST_SRC = System.getProperty("test.src");
}
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: ");
--- 83,122 ----
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 =
! ToolProviderFactory.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: ");
*** 161,172 ****
--- 185,203 ----
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;
}
*** 188,212 ****
});
}
public static void deleteOutputFolder(String output) throws IOException {
File outputFolder = new File(output);
! System.out.println("AMDEBUG output: " + outputFolder.getAbsolutePath());
try {
deleteRecursive(outputFolder);
} catch (IOException ioe) {
! System.out.println("IOException: " + ioe);
! ioe.printStackTrace();
deleteRecursive(outputFolder);
}
}
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()));
}
--- 219,243 ----
});
}
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()));
}
*** 214,223 ****
--- 245,257 ----
}
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) {
*** 270,306 ****
public static boolean isLinux() {
return ((OS.contains("nix") || OS.contains("nux")));
}
public static void createHelloImageJar() throws Exception {
! createJar(false, "Hello", "image");
}
public static void createHelloImageJarWithMainClass() throws Exception {
! createJar(true, "Hello", "image");
}
public static void createHelloInstallerJar() throws Exception {
! createJar(false, "Hello", "installer");
}
public static void createHelloInstallerJarWithMainClass() throws Exception {
! createJar(true, "Hello", "installer");
}
private static void createJar(boolean mainClassAttribute, String name,
! String testType) throws Exception {
int retVal;
! File input = new File("input");
if (!input.exists()) {
! input.mkdir();
}
- Files.copy(Path.of(TEST_SRC_ROOT + File.separator + "apps" + File.separator
- + testType + File.separator + name + ".java"), Path.of(name + ".java"));
File javacLog = new File("javac.log");
try {
retVal = execute(javacLog, JAVAC.toString(), name + ".java");
} catch (Exception ex) {
--- 304,351 ----
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) {
*** 322,332 ****
List<String> args = new ArrayList<>();
args.add(JAR.toString());
args.add("-c");
args.add("-v");
args.add("-f");
! args.add("input" + File.separator + name.toLowerCase() + ".jar");
if (mainClassAttribute) {
args.add("-e");
args.add(name);
}
args.add(name + ".class");
--- 367,377 ----
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");
*** 345,363 ****
throw new AssertionError("jar exited with error: " + retVal);
}
}
public static void createHelloModule() throws Exception {
! createModule("Hello.java", "input", "hello");
}
public static void createOtherModule() throws Exception {
! createModule("Other.java", "input-other", "other");
}
! private static void createModule(String javaFile, String inputDir,
! String aName) throws Exception {
int retVal;
File input = new File(inputDir);
if (!input.exists()) {
input.mkdir();
--- 390,412 ----
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);
}
public static void createOtherModule() throws Exception {
! createModule("Other.java", "input-other", "other", null, false);
}
! 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();
*** 392,408 ****
--- 441,469 ----
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));
*** 418,439 ****
--- 479,508 ----
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()));
}
*** 471,480 ****
--- 540,585 ----
}
}
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;
}
*** 574,580 ****
return sb.toString();
}
return in;
}
-
}
--- 679,684 ----
< prev index next >