< prev index next >

test/jdk/tools/jpackage/helpers/JPackageHelper.java

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.PrintWriter;
  27 import java.io.StringWriter;

  28 import java.nio.file.FileVisitResult;
  29 
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.SimpleFileVisitor;
  33 import java.nio.file.attribute.BasicFileAttributes;
  34 import java.util.ArrayList;
  35 import java.util.List;


  36 
  37 import java.util.spi.ToolProvider;

  38 
  39 public class JPackageHelper {
  40 
  41     private static final boolean VERBOSE = false;
  42     private static final String OS = System.getProperty("os.name").toLowerCase();
  43     private static final String JAVA_HOME = System.getProperty("java.home");
  44     public static final String TEST_SRC_ROOT;
  45     public static final String TEST_SRC;
  46     private static final Path BIN_DIR = Path.of(JAVA_HOME, "bin");
  47     private static final Path JPACKAGE;
  48     private static final Path JAVAC;
  49     private static final Path JAR;
  50     private static final Path JLINK;
  51 


















  52     static {
  53         if (OS.startsWith("win")) {
  54             JPACKAGE = BIN_DIR.resolve("jpackage.exe");
  55             JAVAC = BIN_DIR.resolve("javac.exe");
  56             JAR = BIN_DIR.resolve("jar.exe");
  57             JLINK = BIN_DIR.resolve("jlink.exe");
  58         } else {
  59             JPACKAGE = BIN_DIR.resolve("jpackage");
  60             JAVAC = BIN_DIR.resolve("javac");
  61             JAR = BIN_DIR.resolve("jar");
  62             JLINK = BIN_DIR.resolve("jlink");
  63         }
  64 
  65         // Figure out test src based on where we called
  66         File testSrc = new File(System.getProperty("test.src") + File.separator + ".."
  67                 + File.separator + "apps");
  68         if (testSrc.exists()) {
  69             TEST_SRC_ROOT = System.getProperty("test.src") + File.separator + "..";
  70         } else {
  71             testSrc = new File(System.getProperty("test.src") + File.separator
  72                     + ".." + File.separator + ".." + File.separator + "apps");
  73             if (testSrc.exists()) {
  74                 TEST_SRC_ROOT = System.getProperty("test.src") + File.separator + ".."
  75                         + File.separator + "..";
  76             } else {
  77                 testSrc = new File(System.getProperty("test.src") + File.separator
  78                         + ".." + File.separator + ".."  + File.separator + ".."
  79                         + File.separator + "apps");
  80                 if (testSrc.exists()) {
  81                     TEST_SRC_ROOT = System.getProperty("test.src") + File.separator + ".."
  82                             + File.separator + ".." + File.separator + "..";
  83                 } else {
  84                     TEST_SRC_ROOT = System.getProperty("test.src");










  85                 }
  86             }
  87         }
  88 
  89         TEST_SRC = System.getProperty("test.src");
  90     }
  91 
  92     static final ToolProvider JPACKAGE_TOOL =
  93             ToolProvider.findFirst("jpackage").orElseThrow(
  94             () -> new RuntimeException("jpackage tool not found"));
  95 
  96     public static int execute(File out, String... command) throws Exception {
  97         if (VERBOSE) {
  98             System.out.print("Execute command: ");
  99             for (String c : command) {
 100                 System.out.print(c);
 101                 System.out.print(" ");
 102             }
 103             System.out.println();
 104         }
 105 
 106         ProcessBuilder builder = new ProcessBuilder(command);
 107         if (out != null) {
 108             builder.redirectErrorStream(true);
 109             builder.redirectOutput(out);
 110         }
 111 
 112         Process process = builder.start();
 113         return process.waitFor();


 146         if (args != null) {
 147             for (String arg : args) {
 148                 index++;
 149                 command[index] = arg;
 150             }
 151         }
 152 
 153         return command;
 154     }
 155 
 156     public static void deleteRecursive(File path) throws IOException {
 157         if (!path.exists()) {
 158             return;
 159         }
 160 
 161         Path directory = path.toPath();
 162         Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
 163             @Override
 164             public FileVisitResult visitFile(Path file,
 165                     BasicFileAttributes attr) throws IOException {

 166                 if (OS.startsWith("win")) {

 167                     Files.setAttribute(file, "dos:readonly", false);





 168                 }
 169                 Files.delete(file);
 170                 return FileVisitResult.CONTINUE;
 171             }
 172 
 173             @Override
 174             public FileVisitResult preVisitDirectory(Path dir,
 175                     BasicFileAttributes attr) throws IOException {
 176                 if (OS.startsWith("win")) {
 177                     Files.setAttribute(dir, "dos:readonly", false);
 178                 }
 179                 return FileVisitResult.CONTINUE;
 180             }
 181 
 182             @Override
 183             public FileVisitResult postVisitDirectory(Path dir, IOException e)
 184                     throws IOException {
 185                 Files.delete(dir);
 186                 return FileVisitResult.CONTINUE;
 187             }
 188         });
 189     }
 190 
 191     public static void deleteOutputFolder(String output) throws IOException {
 192         File outputFolder = new File(output);
 193         System.out.println("AMDEBUG output: " + outputFolder.getAbsolutePath());
 194         try {
 195             deleteRecursive(outputFolder);
 196         } catch (IOException ioe) {
 197             System.out.println("IOException: " + ioe);
 198             ioe.printStackTrace();
 199             deleteRecursive(outputFolder);
 200         }
 201     }
 202 
 203     public static String executeCLI(boolean retValZero, String... args) throws Exception {
 204         int retVal;
 205         File outfile = new File("output.log");
 206         try {
 207             String[] command = getCommand(args);

 208             retVal = execute(outfile, command);
 209         } catch (Exception ex) {
 210             if (outfile.exists()) {
 211                 System.err.println(Files.readString(outfile.toPath()));
 212             }
 213             throw ex;
 214         }
 215 
 216         String output = Files.readString(outfile.toPath());
 217         if (retValZero) {
 218             if (retVal != 0) {



 219                 System.err.println(output);
 220                 throw new AssertionError("jpackage exited with error: " + retVal);
 221             }
 222         } else {
 223             if (retVal == 0) {
 224                 System.err.println(output);
 225                 throw new AssertionError("jpackage exited without error: " + retVal);
 226             }
 227         }
 228 
 229         if (VERBOSE) {
 230             System.out.println("output =");
 231             System.out.println(output);
 232         }
 233 
 234         return output;
 235     }
 236 
 237     public static String executeToolProvider(boolean retValZero, String... args) throws Exception {
 238         StringWriter writer = new StringWriter();


 255         if (VERBOSE) {
 256             System.out.println("output =");
 257             System.out.println(output);
 258         }
 259 
 260         return output;
 261     }
 262 
 263     public static boolean isWindows() {
 264         return (OS.contains("win"));
 265     }
 266 
 267     public static boolean isOSX() {
 268         return (OS.contains("mac"));
 269     }
 270 
 271     public static boolean isLinux() {
 272         return ((OS.contains("nix") || OS.contains("nux")));
 273     }
 274 




 275     public static void createHelloImageJar() throws Exception {
 276         createJar(false, "Hello", "image");
 277     }
 278 
 279     public static void createHelloImageJarWithMainClass() throws Exception {
 280         createJar(true, "Hello", "image");
 281     }
 282 
 283     public static void createHelloInstallerJar() throws Exception {
 284         createJar(false, "Hello", "installer");
 285     }
 286 
 287     public static void createHelloInstallerJarWithMainClass() throws Exception {
 288         createJar(true, "Hello", "installer");
 289     }
 290 
 291     private static void createJar(boolean mainClassAttribute, String name,
 292                                   String testType) throws Exception {
 293         int retVal;
 294 
 295         File input = new File("input");
 296         if (!input.exists()) {
 297             input.mkdir();








 298         }

 299 
 300         Files.copy(Path.of(TEST_SRC_ROOT + File.separator + "apps" + File.separator
 301                 + testType + File.separator + name + ".java"), Path.of(name + ".java"));
 302 
 303         File javacLog = new File("javac.log");
 304         try {
 305             retVal = execute(javacLog, JAVAC.toString(), name + ".java");
 306         } catch (Exception ex) {
 307             if (javacLog.exists()) {
 308                 System.err.println(Files.readString(javacLog.toPath()));
 309             }
 310             throw ex;
 311         }
 312 
 313         if (retVal != 0) {
 314             if (javacLog.exists()) {
 315                 System.err.println(Files.readString(javacLog.toPath()));
 316             }
 317             throw new AssertionError("javac exited with error: " + retVal);
 318         }
 319 
 320         File jarLog = new File("jar.log");
 321         try {
 322             List<String> args = new ArrayList<>();
 323             args.add(JAR.toString());
 324             args.add("-c");
 325             args.add("-v");
 326             args.add("-f");
 327             args.add("input" + File.separator + name.toLowerCase() + ".jar");
 328             if (mainClassAttribute) {
 329                 args.add("-e");
 330                 args.add(name);
 331             }
 332             args.add(name + ".class");
 333             retVal = execute(jarLog, args.stream().toArray(String[]::new));
 334         } catch (Exception ex) {
 335             if (jarLog.exists()) {
 336                 System.err.println(Files.readString(jarLog.toPath()));
 337             }
 338             throw ex;
 339         }
 340 
 341         if (retVal != 0) {
 342             if (jarLog.exists()) {
 343                 System.err.println(Files.readString(jarLog.toPath()));
 344             }
 345             throw new AssertionError("jar exited with error: " + retVal);
 346         }
 347     }
 348 
 349     public static void createHelloModule() throws Exception {
 350         createModule("Hello.java", "input", "hello");




 351     }
 352 
 353     public static void createOtherModule() throws Exception {
 354         createModule("Other.java", "input-other", "other");
 355     }
 356 
 357     private static void createModule(String javaFile, String inputDir,
 358             String aName) throws Exception {
 359         int retVal;
 360 
 361         File input = new File(inputDir);
 362         if (!input.exists()) {
 363             input.mkdir();
 364         }
 365 
 366         File module = new File("module" + File.separator + "com." + aName);
 367         if (!module.exists()) {
 368             module.mkdirs();
 369         }
 370 
 371         File javacLog = new File("javac.log");
 372         try {
 373             List<String> args = new ArrayList<>();
 374             args.add(JAVAC.toString());
 375             args.add("-d");
 376             args.add("module" + File.separator + "com." + aName);
 377             args.add(TEST_SRC_ROOT + File.separator + "apps" + File.separator
 378                     + "com." + aName + File.separator + "module-info.java");
 379             args.add(TEST_SRC_ROOT + File.separator + "apps"
 380                     + File.separator + "com." + aName + File.separator + "com"
 381                     + File.separator + aName + File.separator + javaFile);
 382             retVal = execute(javacLog, args.stream().toArray(String[]::new));
 383         } catch (Exception ex) {
 384             if (javacLog.exists()) {
 385                 System.err.println(Files.readString(javacLog.toPath()));
 386             }
 387             throw ex;
 388         }
 389 
 390         if (retVal != 0) {
 391             if (javacLog.exists()) {
 392                 System.err.println(Files.readString(javacLog.toPath()));
 393             }
 394             throw new AssertionError("javac exited with error: " + retVal);
 395         }
 396 

 397         File jarLog = new File("jar.log");
 398         try {
 399             List<String> args = new ArrayList<>();
 400             args.add(JAR.toString());
 401             args.add("--create");
 402             args.add("--file");
 403             args.add(inputDir + File.separator + "com." + aName + ".jar");











 404             args.add("-C");
 405             args.add("module" + File.separator + "com." + aName);
 406             args.add(".");
 407 
 408             retVal = execute(jarLog, args.stream().toArray(String[]::new));
 409         } catch (Exception ex) {
 410             if (jarLog.exists()) {
 411                 System.err.println(Files.readString(jarLog.toPath()));
 412             }
 413             throw ex;
 414         }
 415 
 416         if (retVal != 0) {
 417             if (jarLog.exists()) {
 418                 System.err.println(Files.readString(jarLog.toPath()));
 419             }
 420             throw new AssertionError("jar exited with error: " + retVal);
 421         }
 422     }

 423 
 424     public static void createRuntime() throws Exception {





 425         int retVal;
 426 
 427         File jlinkLog = new File("jlink.log");
 428         try {
 429             List<String> args = new ArrayList<>();
 430             args.add(JLINK.toString());
 431             args.add("--output");
 432             args.add("runtime");
 433             args.add("--add-modules");
 434             args.add("java.base");


 435             retVal = execute(jlinkLog, args.stream().toArray(String[]::new));
 436         } catch (Exception ex) {
 437             if (jlinkLog.exists()) {
 438                 System.err.println(Files.readString(jlinkLog.toPath()));
 439             }
 440             throw ex;
 441         }
 442 
 443         if (retVal != 0) {
 444             if (jlinkLog.exists()) {
 445                 System.err.println(Files.readString(jlinkLog.toPath()));
 446             }
 447             throw new AssertionError("jlink exited with error: " + retVal);
 448         }
 449     }
 450 
 451     public static String listToArgumentsMap(List<String> arguments, boolean toolProvider) {
 452         if (arguments.isEmpty()) {
 453             return "";
 454         }


 456         String argsStr = "";
 457         for (int i = 0; i < arguments.size(); i++) {
 458             String arg = arguments.get(i);
 459             argsStr += quote(arg, toolProvider);
 460             if ((i + 1) != arguments.size()) {
 461                 argsStr += " ";
 462             }
 463         }
 464 
 465         if (!toolProvider && isWindows()) {
 466             if (argsStr.contains(" ")) {
 467                 if (argsStr.contains("\"")) {
 468                     argsStr = escapeQuote(argsStr, toolProvider);
 469                 }
 470                 argsStr = "\"" + argsStr + "\"";
 471             }
 472         }
 473         return argsStr;
 474     }
 475 




































 476     private static String quote(String in, boolean toolProvider) {
 477         if (in == null) {
 478             return null;
 479         }
 480 
 481         if (in.isEmpty()) {
 482             return "";
 483         }
 484 
 485         if (!in.contains("=")) {
 486             // Not a property
 487             if (in.contains(" ")) {
 488                 in = escapeQuote(in, toolProvider);
 489                 return "\"" + in + "\"";
 490             }
 491             return in;
 492         }
 493 
 494         if (!in.contains(" ")) {
 495             return in; // No need to quote


 559                                 sb.appendCodePoint('\\');
 560                                 sb.appendCodePoint(nextCode);
 561                             } else {
 562                                 sb.appendCodePoint('\\');
 563                                 sb.appendCodePoint(code);
 564                             }
 565                         } else {
 566                             sb.appendCodePoint(code);
 567                         }
 568                         break;
 569                     default:
 570                         sb.appendCodePoint(code);
 571                         break;
 572                 }
 573             }
 574             return sb.toString();
 575         }
 576 
 577         return in;
 578     }
 579 
 580 }


   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.PrintWriter;
  27 import java.io.StringWriter;
  28 import java.io.BufferedWriter;
  29 import java.nio.file.FileVisitResult;
  30 
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.nio.file.SimpleFileVisitor;
  34 import java.nio.file.attribute.BasicFileAttributes;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.Stream;
  39 
  40 import java.util.spi.ToolProvider;
  41 import jdk.incubator.jpackage.ToolProviderFactory;
  42 
  43 public class JPackageHelper {
  44 
  45     private static final boolean VERBOSE = false;
  46     private static final String OS = System.getProperty("os.name").toLowerCase();
  47     private static final String JAVA_HOME = System.getProperty("java.home");
  48     public static final String TEST_SRC_ROOT;
  49     public static final String TEST_SRC;
  50     private static final Path BIN_DIR = Path.of(JAVA_HOME, "bin");
  51     private static final Path JPACKAGE;
  52     private static final Path JAVAC;
  53     private static final Path JAR;
  54     private static final Path JLINK;
  55 
  56     public static class ModuleArgs {
  57         private final String version;
  58         private final String mainClass;
  59 
  60         ModuleArgs(String version, String mainClass) {
  61             this.version = version;
  62             this.mainClass = mainClass;
  63         }
  64 
  65         public String getVersion() {
  66             return version;
  67         }
  68 
  69         public String getMainClass() {
  70             return mainClass;
  71         }
  72     }
  73 
  74     static {
  75         if (OS.startsWith("win")) {
  76             JPACKAGE = BIN_DIR.resolve("jpackage.exe");
  77             JAVAC = BIN_DIR.resolve("javac.exe");
  78             JAR = BIN_DIR.resolve("jar.exe");
  79             JLINK = BIN_DIR.resolve("jlink.exe");
  80         } else {
  81             JPACKAGE = BIN_DIR.resolve("jpackage");
  82             JAVAC = BIN_DIR.resolve("javac");
  83             JAR = BIN_DIR.resolve("jar");
  84             JLINK = BIN_DIR.resolve("jlink");
  85         }
  86 
  87         // Figure out test src based on where we called
  88         TEST_SRC = System.getProperty("test.src");
  89         Path root = Path.of(TEST_SRC);
  90         Path apps = Path.of(TEST_SRC, "apps");
  91         if (apps.toFile().exists()) {
  92             // fine - test is at root
  93         } else {
  94              apps = Path.of(TEST_SRC, "..", "apps");
  95              if (apps.toFile().exists()) {
  96                  root = apps.getParent().normalize(); // test is 1 level down








  97              } else {
  98                  apps = Path.of(TEST_SRC, "..", "..", "apps");
  99                  if (apps.toFile().exists()) {
 100                      root = apps.getParent().normalize(); // 2 levels down
 101                  } else {
 102                      apps = Path.of(TEST_SRC, "..", "..", "..", "apps");
 103                      if (apps.toFile().exists()) {
 104                          root = apps.getParent().normalize(); // 3 levels down
 105                      } else {
 106                          // if we ever have tests more than three levels
 107                          // down we need to add code here
 108                          throw new RuntimeException("we should never get here");
 109                      }
 110                  }
 111             }
 112         }
 113         TEST_SRC_ROOT = root.toString();
 114     }
 115 
 116     static final ToolProvider JPACKAGE_TOOL =
 117             ToolProviderFactory.findFirst("jpackage").orElseThrow(
 118             () -> new RuntimeException("jpackage tool not found"));
 119 
 120     public static int execute(File out, String... command) throws Exception {
 121         if (VERBOSE) {
 122             System.out.print("Execute command: ");
 123             for (String c : command) {
 124                 System.out.print(c);
 125                 System.out.print(" ");
 126             }
 127             System.out.println();
 128         }
 129 
 130         ProcessBuilder builder = new ProcessBuilder(command);
 131         if (out != null) {
 132             builder.redirectErrorStream(true);
 133             builder.redirectOutput(out);
 134         }
 135 
 136         Process process = builder.start();
 137         return process.waitFor();


 170         if (args != null) {
 171             for (String arg : args) {
 172                 index++;
 173                 command[index] = arg;
 174             }
 175         }
 176 
 177         return command;
 178     }
 179 
 180     public static void deleteRecursive(File path) throws IOException {
 181         if (!path.exists()) {
 182             return;
 183         }
 184 
 185         Path directory = path.toPath();
 186         Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
 187             @Override
 188             public FileVisitResult visitFile(Path file,
 189                     BasicFileAttributes attr) throws IOException {
 190                 file.toFile().setWritable(true);
 191                 if (OS.startsWith("win")) {
 192                     try {
 193                         Files.setAttribute(file, "dos:readonly", false);
 194                     } catch (Exception ioe) {
 195                         // just report and try to contune
 196                         System.err.println("IOException: " + ioe);
 197                         ioe.printStackTrace(System.err);
 198                     }
 199                 }
 200                 Files.delete(file);
 201                 return FileVisitResult.CONTINUE;
 202             }
 203 
 204             @Override
 205             public FileVisitResult preVisitDirectory(Path dir,
 206                     BasicFileAttributes attr) throws IOException {
 207                 if (OS.startsWith("win")) {
 208                     Files.setAttribute(dir, "dos:readonly", false);
 209                 }
 210                 return FileVisitResult.CONTINUE;
 211             }
 212 
 213             @Override
 214             public FileVisitResult postVisitDirectory(Path dir, IOException e)
 215                     throws IOException {
 216                 Files.delete(dir);
 217                 return FileVisitResult.CONTINUE;
 218             }
 219         });
 220     }
 221 
 222     public static void deleteOutputFolder(String output) throws IOException {
 223         File outputFolder = new File(output);
 224         System.out.println("deleteOutputFolder: " + outputFolder.getAbsolutePath());
 225         try {
 226             deleteRecursive(outputFolder);
 227         } catch (IOException ioe) {
 228             System.err.println("IOException: " + ioe);
 229             ioe.printStackTrace(System.err);
 230             deleteRecursive(outputFolder);
 231         }
 232     }
 233 
 234     public static String executeCLI(boolean retValZero, String... args) throws Exception {
 235         int retVal;
 236         File outfile = new File("output.log");

 237         String[] command = getCommand(args);
 238         try {
 239             retVal = execute(outfile, command);
 240         } catch (Exception ex) {
 241             if (outfile.exists()) {
 242                 System.err.println(Files.readString(outfile.toPath()));
 243             }
 244             throw ex;
 245         }
 246 
 247         String output = Files.readString(outfile.toPath());
 248         if (retValZero) {
 249             if (retVal != 0) {
 250                 System.err.println("command run:");
 251                 for (String s : command) { System.err.println(s); }
 252                 System.err.println("command output:");
 253                 System.err.println(output);
 254                 throw new AssertionError("jpackage exited with error: " + retVal);
 255             }
 256         } else {
 257             if (retVal == 0) {
 258                 System.err.println(output);
 259                 throw new AssertionError("jpackage exited without error: " + retVal);
 260             }
 261         }
 262 
 263         if (VERBOSE) {
 264             System.out.println("output =");
 265             System.out.println(output);
 266         }
 267 
 268         return output;
 269     }
 270 
 271     public static String executeToolProvider(boolean retValZero, String... args) throws Exception {
 272         StringWriter writer = new StringWriter();


 289         if (VERBOSE) {
 290             System.out.println("output =");
 291             System.out.println(output);
 292         }
 293 
 294         return output;
 295     }
 296 
 297     public static boolean isWindows() {
 298         return (OS.contains("win"));
 299     }
 300 
 301     public static boolean isOSX() {
 302         return (OS.contains("mac"));
 303     }
 304 
 305     public static boolean isLinux() {
 306         return ((OS.contains("nix") || OS.contains("nux")));
 307     }
 308 
 309     public static void createHelloImageJar(String inputDir) throws Exception {
 310         createJar(false, "Hello", "image", inputDir);
 311     }
 312 
 313     public static void createHelloImageJar() throws Exception {
 314         createJar(false, "Hello", "image", "input");
 315     }
 316 
 317     public static void createHelloImageJarWithMainClass() throws Exception {
 318         createJar(true, "Hello", "image", "input");
 319     }
 320 
 321     public static void createHelloInstallerJar() throws Exception {
 322         createJar(false, "Hello", "installer", "input");
 323     }
 324 
 325     public static void createHelloInstallerJarWithMainClass() throws Exception {
 326         createJar(true, "Hello", "installer", "input");
 327     }
 328 
 329     private static void createJar(boolean mainClassAttribute, String name,
 330         String testType, String inputDir) throws Exception {
 331         int retVal;
 332 
 333         File input = new File(inputDir);
 334         if (!input.exists()) {
 335             input.mkdirs();
 336         }
 337 
 338         Path src = Path.of(TEST_SRC_ROOT + File.separator + "apps"
 339                 + File.separator + testType + File.separator + name + ".java");
 340         Path dst = Path.of(name + ".java");
 341 
 342         if (dst.toFile().exists()) {
 343             Files.delete(dst);
 344         }
 345         Files.copy(src, dst);
 346 


 347 
 348         File javacLog = new File("javac.log");
 349         try {
 350             retVal = execute(javacLog, JAVAC.toString(), name + ".java");
 351         } catch (Exception ex) {
 352             if (javacLog.exists()) {
 353                 System.err.println(Files.readString(javacLog.toPath()));
 354             }
 355             throw ex;
 356         }
 357 
 358         if (retVal != 0) {
 359             if (javacLog.exists()) {
 360                 System.err.println(Files.readString(javacLog.toPath()));
 361             }
 362             throw new AssertionError("javac exited with error: " + retVal);
 363         }
 364 
 365         File jarLog = new File("jar.log");
 366         try {
 367             List<String> args = new ArrayList<>();
 368             args.add(JAR.toString());
 369             args.add("-c");
 370             args.add("-v");
 371             args.add("-f");
 372             args.add(inputDir + File.separator + name.toLowerCase() + ".jar");
 373             if (mainClassAttribute) {
 374                 args.add("-e");
 375                 args.add(name);
 376             }
 377             args.add(name + ".class");
 378             retVal = execute(jarLog, args.stream().toArray(String[]::new));
 379         } catch (Exception ex) {
 380             if (jarLog.exists()) {
 381                 System.err.println(Files.readString(jarLog.toPath()));
 382             }
 383             throw ex;
 384         }
 385 
 386         if (retVal != 0) {
 387             if (jarLog.exists()) {
 388                 System.err.println(Files.readString(jarLog.toPath()));
 389             }
 390             throw new AssertionError("jar exited with error: " + retVal);
 391         }
 392     }
 393 
 394     public static void createHelloModule() throws Exception {
 395         createModule("Hello.java", "input", "hello", null, true);
 396     }
 397 
 398     public static void createHelloModule(ModuleArgs moduleArgs) throws Exception {
 399         createModule("Hello.java", "input", "hello", moduleArgs, true);
 400     }
 401 
 402     public static void createOtherModule() throws Exception {
 403         createModule("Other.java", "input-other", "other", null, false);
 404     }
 405 
 406     private static void createModule(String javaFile, String inputDir, String aName,
 407             ModuleArgs moduleArgs, boolean createModularJar) throws Exception {
 408         int retVal;
 409 
 410         File input = new File(inputDir);
 411         if (!input.exists()) {
 412             input.mkdir();
 413         }
 414 
 415         File module = new File("module" + File.separator + "com." + aName);
 416         if (!module.exists()) {
 417             module.mkdirs();
 418         }
 419 
 420         File javacLog = new File("javac.log");
 421         try {
 422             List<String> args = new ArrayList<>();
 423             args.add(JAVAC.toString());
 424             args.add("-d");
 425             args.add("module" + File.separator + "com." + aName);
 426             args.add(TEST_SRC_ROOT + File.separator + "apps" + File.separator
 427                     + "com." + aName + File.separator + "module-info.java");
 428             args.add(TEST_SRC_ROOT + File.separator + "apps"
 429                     + File.separator + "com." + aName + File.separator + "com"
 430                     + File.separator + aName + File.separator + javaFile);
 431             retVal = execute(javacLog, args.stream().toArray(String[]::new));
 432         } catch (Exception ex) {
 433             if (javacLog.exists()) {
 434                 System.err.println(Files.readString(javacLog.toPath()));
 435             }
 436             throw ex;
 437         }
 438 
 439         if (retVal != 0) {
 440             if (javacLog.exists()) {
 441                 System.err.println(Files.readString(javacLog.toPath()));
 442             }
 443             throw new AssertionError("javac exited with error: " + retVal);
 444         }
 445 
 446         if (createModularJar) {
 447             File jarLog = new File("jar.log");
 448             try {
 449                 List<String> args = new ArrayList<>();
 450                 args.add(JAR.toString());
 451                 args.add("--create");
 452                 args.add("--file");
 453                 args.add(inputDir + File.separator + "com." + aName + ".jar");
 454                 if (moduleArgs != null) {
 455                     if (moduleArgs.getVersion() != null) {
 456                         args.add("--module-version");
 457                         args.add(moduleArgs.getVersion());
 458                     }
 459 
 460                     if (moduleArgs.getMainClass()!= null) {
 461                         args.add("--main-class");
 462                         args.add(moduleArgs.getMainClass());
 463                     }
 464                 }
 465                 args.add("-C");
 466                 args.add("module" + File.separator + "com." + aName);
 467                 args.add(".");
 468 
 469                 retVal = execute(jarLog, args.stream().toArray(String[]::new));
 470             } catch (Exception ex) {
 471                 if (jarLog.exists()) {
 472                     System.err.println(Files.readString(jarLog.toPath()));
 473                 }
 474                 throw ex;
 475             }
 476 
 477             if (retVal != 0) {
 478                 if (jarLog.exists()) {
 479                     System.err.println(Files.readString(jarLog.toPath()));
 480                 }
 481                 throw new AssertionError("jar exited with error: " + retVal);
 482             }
 483         }
 484     }
 485 
 486     public static void createRuntime() throws Exception {
 487         List<String> moreArgs = new ArrayList<>();
 488         createRuntime(moreArgs);
 489     }
 490 
 491     public static void createRuntime(List<String> moreArgs) throws Exception {
 492         int retVal;
 493 
 494         File jlinkLog = new File("jlink.log");
 495         try {
 496             List<String> args = new ArrayList<>();
 497             args.add(JLINK.toString());
 498             args.add("--output");
 499             args.add("runtime");
 500             args.add("--add-modules");
 501             args.add("java.base");
 502             args.addAll(moreArgs);
 503 
 504             retVal = execute(jlinkLog, args.stream().toArray(String[]::new));
 505         } catch (Exception ex) {
 506             if (jlinkLog.exists()) {
 507                 System.err.println(Files.readString(jlinkLog.toPath()));
 508             }
 509             throw ex;
 510         }
 511 
 512         if (retVal != 0) {
 513             if (jlinkLog.exists()) {
 514                 System.err.println(Files.readString(jlinkLog.toPath()));
 515             }
 516             throw new AssertionError("jlink exited with error: " + retVal);
 517         }
 518     }
 519 
 520     public static String listToArgumentsMap(List<String> arguments, boolean toolProvider) {
 521         if (arguments.isEmpty()) {
 522             return "";
 523         }


 525         String argsStr = "";
 526         for (int i = 0; i < arguments.size(); i++) {
 527             String arg = arguments.get(i);
 528             argsStr += quote(arg, toolProvider);
 529             if ((i + 1) != arguments.size()) {
 530                 argsStr += " ";
 531             }
 532         }
 533 
 534         if (!toolProvider && isWindows()) {
 535             if (argsStr.contains(" ")) {
 536                 if (argsStr.contains("\"")) {
 537                     argsStr = escapeQuote(argsStr, toolProvider);
 538                 }
 539                 argsStr = "\"" + argsStr + "\"";
 540             }
 541         }
 542         return argsStr;
 543     }
 544 
 545     public static String[] cmdWithAtFilename(String [] cmd, int ndx, int len)
 546                 throws IOException {
 547         ArrayList<String> newAList = new ArrayList<>();
 548         String fileString = null;
 549         for (int i=0; i<cmd.length; i++) {
 550             if (i == ndx) {
 551                 newAList.add("@argfile.cmds");
 552                 fileString = cmd[i];
 553             } else if (i > ndx && i < ndx + len) {
 554                 fileString += " " + cmd[i];
 555             } else {
 556                 newAList.add(cmd[i]);
 557             }
 558         }
 559         if (fileString != null) {
 560             Path path = new File("argfile.cmds").toPath();
 561             try (BufferedWriter bw = Files.newBufferedWriter(path);
 562                     PrintWriter out = new PrintWriter(bw)) {
 563                 out.println(fileString);
 564             }
 565         }
 566         return newAList.toArray(new String[0]);
 567     }
 568 
 569     public static String [] splitAndFilter(String output) {
 570         if (output == null) {
 571             return null;
 572         }
 573 
 574         return Stream.of(output.split("\\R"))
 575                 .filter(str -> !str.startsWith("Picked up"))
 576                 .filter(str -> !str.startsWith("WARNING: Using incubator"))
 577                 .filter(str -> !str.startsWith("hello: "))
 578                 .collect(Collectors.toList()).toArray(String[]::new);
 579     }
 580 
 581     private static String quote(String in, boolean toolProvider) {
 582         if (in == null) {
 583             return null;
 584         }
 585 
 586         if (in.isEmpty()) {
 587             return "";
 588         }
 589 
 590         if (!in.contains("=")) {
 591             // Not a property
 592             if (in.contains(" ")) {
 593                 in = escapeQuote(in, toolProvider);
 594                 return "\"" + in + "\"";
 595             }
 596             return in;
 597         }
 598 
 599         if (!in.contains(" ")) {
 600             return in; // No need to quote


 664                                 sb.appendCodePoint('\\');
 665                                 sb.appendCodePoint(nextCode);
 666                             } else {
 667                                 sb.appendCodePoint('\\');
 668                                 sb.appendCodePoint(code);
 669                             }
 670                         } else {
 671                             sb.appendCodePoint(code);
 672                         }
 673                         break;
 674                     default:
 675                         sb.appendCodePoint(code);
 676                         break;
 677                 }
 678             }
 679             return sb.toString();
 680         }
 681 
 682         return in;
 683     }

 684 }
< prev index next >