--- old/test/jdk/tools/jpackage/helpers/JPackageHelper.java 2019-11-18 20:26:11.763123800 -0500 +++ new/test/jdk/tools/jpackage/helpers/JPackageHelper.java 2019-11-18 20:26:10.344132700 -0500 @@ -25,6 +25,7 @@ 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; @@ -33,8 +34,11 @@ 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 { @@ -49,6 +53,24 @@ 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"); @@ -63,34 +85,36 @@ } // 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 + ".."; + 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 { - 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"); - } + 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 = System.getProperty("test.src"); + TEST_SRC_ROOT = root.toString(); } static final ToolProvider JPACKAGE_TOOL = - ToolProvider.findFirst("jpackage").orElseThrow( + ToolProviderFactory.findFirst("jpackage").orElseThrow( () -> new RuntimeException("jpackage tool not found")); public static int execute(File out, String... command) throws Exception { @@ -163,8 +187,15 @@ @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException { + file.toFile().setWritable(true); if (OS.startsWith("win")) { - Files.setAttribute(file, "dos:readonly", false); + 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; @@ -190,12 +221,12 @@ public static void deleteOutputFolder(String output) throws IOException { File outputFolder = new File(output); - System.out.println("AMDEBUG output: " + outputFolder.getAbsolutePath()); + System.out.println("deleteOutputFolder: " + outputFolder.getAbsolutePath()); try { deleteRecursive(outputFolder); } catch (IOException ioe) { - System.out.println("IOException: " + ioe); - ioe.printStackTrace(); + System.err.println("IOException: " + ioe); + ioe.printStackTrace(System.err); deleteRecursive(outputFolder); } } @@ -203,8 +234,8 @@ public static String executeCLI(boolean retValZero, String... args) throws Exception { int retVal; File outfile = new File("output.log"); + String[] command = getCommand(args); try { - String[] command = getCommand(args); retVal = execute(outfile, command); } catch (Exception ex) { if (outfile.exists()) { @@ -216,6 +247,9 @@ 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); } @@ -272,33 +306,44 @@ 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"); + createJar(false, "Hello", "image", "input"); } public static void createHelloImageJarWithMainClass() throws Exception { - createJar(true, "Hello", "image"); + createJar(true, "Hello", "image", "input"); } public static void createHelloInstallerJar() throws Exception { - createJar(false, "Hello", "installer"); + createJar(false, "Hello", "installer", "input"); } public static void createHelloInstallerJarWithMainClass() throws Exception { - createJar(true, "Hello", "installer"); + createJar(true, "Hello", "installer", "input"); } private static void createJar(boolean mainClassAttribute, String name, - String testType) throws Exception { + String testType, String inputDir) throws Exception { int retVal; - File input = new File("input"); + File input = new File(inputDir); if (!input.exists()) { - input.mkdir(); + input.mkdirs(); } - Files.copy(Path.of(TEST_SRC_ROOT + File.separator + "apps" + File.separator - + testType + File.separator + name + ".java"), Path.of(name + ".java")); + 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 { @@ -324,7 +369,7 @@ args.add("-c"); args.add("-v"); args.add("-f"); - args.add("input" + File.separator + name.toLowerCase() + ".jar"); + args.add(inputDir + File.separator + name.toLowerCase() + ".jar"); if (mainClassAttribute) { args.add("-e"); args.add(name); @@ -347,15 +392,19 @@ } public static void createHelloModule() throws Exception { - createModule("Hello.java", "input", "hello"); + 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"); + createModule("Other.java", "input-other", "other", null, false); } - private static void createModule(String javaFile, String inputDir, - String aName) throws Exception { + private static void createModule(String javaFile, String inputDir, String aName, + ModuleArgs moduleArgs, boolean createModularJar) throws Exception { int retVal; File input = new File(inputDir); @@ -394,34 +443,52 @@ throw new AssertionError("javac exited with error: " + retVal); } - File jarLog = new File("jar.log"); - try { - List args = new ArrayList<>(); - args.add(JAR.toString()); - args.add("--create"); - args.add("--file"); - args.add(inputDir + File.separator + "com." + aName + ".jar"); - 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())); + if (createModularJar) { + File jarLog = new File("jar.log"); + try { + List 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; } - throw ex; - } - if (retVal != 0) { - if (jarLog.exists()) { - System.err.println(Files.readString(jarLog.toPath())); + if (retVal != 0) { + if (jarLog.exists()) { + System.err.println(Files.readString(jarLog.toPath())); + } + throw new AssertionError("jar exited with error: " + retVal); } - throw new AssertionError("jar exited with error: " + retVal); } } public static void createRuntime() throws Exception { + List moreArgs = new ArrayList<>(); + createRuntime(moreArgs); + } + + public static void createRuntime(List moreArgs) throws Exception { int retVal; File jlinkLog = new File("jlink.log"); @@ -432,6 +499,8 @@ 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()) { @@ -473,6 +542,42 @@ return argsStr; } + public static String[] cmdWithAtFilename(String [] cmd, int ndx, int len) + throws IOException { + ArrayList newAList = new ArrayList<>(); + String fileString = null; + for (int i=0; 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; @@ -576,5 +681,4 @@ return in; } - }