test/jdk/tools/jpackage/helpers/JPackageHelper.java
branchJDK-8200758-branch
changeset 57395 521c02b9eed0
parent 57324 c1d3935fbb79
child 57405 539d8b3f9e1e
equal deleted inserted replaced
57394:17c43babfc2f 57395:521c02b9eed0
    23 
    23 
    24 import java.io.File;
    24 import java.io.File;
    25 import java.io.IOException;
    25 import java.io.IOException;
    26 import java.io.PrintWriter;
    26 import java.io.PrintWriter;
    27 import java.io.StringWriter;
    27 import java.io.StringWriter;
       
    28 import java.io.FileWriter;
       
    29 import java.io.BufferedWriter;
    28 import java.nio.file.FileVisitResult;
    30 import java.nio.file.FileVisitResult;
    29 
    31 
    30 import java.nio.file.Files;
    32 import java.nio.file.Files;
    31 import java.nio.file.Path;
    33 import java.nio.file.Path;
    32 import java.nio.file.SimpleFileVisitor;
    34 import java.nio.file.SimpleFileVisitor;
   162         Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
   164         Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
   163             @Override
   165             @Override
   164             public FileVisitResult visitFile(Path file,
   166             public FileVisitResult visitFile(Path file,
   165                     BasicFileAttributes attr) throws IOException {
   167                     BasicFileAttributes attr) throws IOException {
   166                 if (OS.startsWith("win")) {
   168                 if (OS.startsWith("win")) {
   167                     Files.setAttribute(file, "dos:readonly", false);
   169                     try {
       
   170                         Files.setAttribute(file, "dos:readonly", false);
       
   171                     } catch (Exception ioe) {
       
   172                         // just report and try to contune
       
   173                         System.err.println("IOException: " + ioe);
       
   174                         ioe.printStackTrace(System.err);
       
   175                     }
   168                 }
   176                 }
   169                 Files.delete(file);
   177                 Files.delete(file);
   170                 return FileVisitResult.CONTINUE;
   178                 return FileVisitResult.CONTINUE;
   171             }
   179             }
   172 
   180 
   192         File outputFolder = new File(output);
   200         File outputFolder = new File(output);
   193         System.out.println("AMDEBUG output: " + outputFolder.getAbsolutePath());
   201         System.out.println("AMDEBUG output: " + outputFolder.getAbsolutePath());
   194         try {
   202         try {
   195             deleteRecursive(outputFolder);
   203             deleteRecursive(outputFolder);
   196         } catch (IOException ioe) {
   204         } catch (IOException ioe) {
   197             System.out.println("IOException: " + ioe);
   205             System.err.println("IOException: " + ioe);
   198             ioe.printStackTrace();
   206             ioe.printStackTrace(System.err);
   199             deleteRecursive(outputFolder);
   207             deleteRecursive(outputFolder);
   200         }
   208         }
   201     }
   209     }
   202 
   210 
   203     public static String executeCLI(boolean retValZero, String... args) throws Exception {
   211     public static String executeCLI(boolean retValZero, String... args) throws Exception {
   270 
   278 
   271     public static boolean isLinux() {
   279     public static boolean isLinux() {
   272         return ((OS.contains("nix") || OS.contains("nux")));
   280         return ((OS.contains("nix") || OS.contains("nux")));
   273     }
   281     }
   274 
   282 
       
   283     public static void createHelloImageJar(String inputDir) throws Exception {
       
   284         createJar(false, "Hello", "image", inputDir);
       
   285     }
       
   286 
   275     public static void createHelloImageJar() throws Exception {
   287     public static void createHelloImageJar() throws Exception {
   276         createJar(false, "Hello", "image");
   288         createJar(false, "Hello", "image", "input");
   277     }
   289     }
   278 
   290 
   279     public static void createHelloImageJarWithMainClass() throws Exception {
   291     public static void createHelloImageJarWithMainClass() throws Exception {
   280         createJar(true, "Hello", "image");
   292         createJar(true, "Hello", "image", "input");
   281     }
   293     }
   282 
   294 
   283     public static void createHelloInstallerJar() throws Exception {
   295     public static void createHelloInstallerJar() throws Exception {
   284         createJar(false, "Hello", "installer");
   296         createJar(false, "Hello", "installer", "input");
   285     }
   297     }
   286 
   298 
   287     public static void createHelloInstallerJarWithMainClass() throws Exception {
   299     public static void createHelloInstallerJarWithMainClass() throws Exception {
   288         createJar(true, "Hello", "installer");
   300         createJar(true, "Hello", "installer", "input");
   289     }
   301     }
   290 
   302 
   291     private static void createJar(boolean mainClassAttribute, String name,
   303     private static void createJar(boolean mainClassAttribute, String name,
   292                                   String testType) throws Exception {
   304         String testType, String inputDir) throws Exception {
   293         int retVal;
   305         int retVal;
   294 
   306 
   295         File input = new File("input");
   307         File input = new File(inputDir);
   296         if (!input.exists()) {
   308         if (!input.exists()) {
   297             input.mkdir();
   309             input.mkdirs();
   298         }
   310         }
   299 
   311 
   300         Files.copy(Path.of(TEST_SRC_ROOT + File.separator + "apps" + File.separator
   312         Path src = Path.of(TEST_SRC_ROOT + File.separator + "apps"
   301                 + testType + File.separator + name + ".java"), Path.of(name + ".java"));
   313                 + File.separator + testType + File.separator + name + ".java");
       
   314         Path dst = Path.of(name + ".java");
       
   315         
       
   316         if (dst.toFile().exists()) {
       
   317             Files.delete(dst);
       
   318         }
       
   319         Files.copy(src, dst);
       
   320 
   302 
   321 
   303         File javacLog = new File("javac.log");
   322         File javacLog = new File("javac.log");
   304         try {
   323         try {
   305             retVal = execute(javacLog, JAVAC.toString(), name + ".java");
   324             retVal = execute(javacLog, JAVAC.toString(), name + ".java");
   306         } catch (Exception ex) {
   325         } catch (Exception ex) {
   322             List<String> args = new ArrayList<>();
   341             List<String> args = new ArrayList<>();
   323             args.add(JAR.toString());
   342             args.add(JAR.toString());
   324             args.add("-c");
   343             args.add("-c");
   325             args.add("-v");
   344             args.add("-v");
   326             args.add("-f");
   345             args.add("-f");
   327             args.add("input" + File.separator + name.toLowerCase() + ".jar");
   346             args.add(inputDir + File.separator + name.toLowerCase() + ".jar");
   328             if (mainClassAttribute) {
   347             if (mainClassAttribute) {
   329                 args.add("-e");
   348                 args.add("-e");
   330                 args.add(name);
   349                 args.add(name);
   331             }
   350             }
   332             args.add(name + ".class");
   351             args.add(name + ".class");
   345             throw new AssertionError("jar exited with error: " + retVal);
   364             throw new AssertionError("jar exited with error: " + retVal);
   346         }
   365         }
   347     }
   366     }
   348 
   367 
   349     public static void createHelloModule() throws Exception {
   368     public static void createHelloModule() throws Exception {
   350         createModule("Hello.java", "input", "hello");
   369         createModule("Hello.java", "input", "hello", true);
   351     }
   370     }
   352 
   371 
   353     public static void createOtherModule() throws Exception {
   372     public static void createOtherModule() throws Exception {
   354         createModule("Other.java", "input-other", "other");
   373         createModule("Other.java", "input-other", "other", false);
   355     }
   374     }
   356 
   375 
   357     private static void createModule(String javaFile, String inputDir,
   376     private static void createModule(String javaFile, String inputDir,
   358             String aName) throws Exception {
   377             String aName, boolean createModularJar) throws Exception {
   359         int retVal;
   378         int retVal;
   360 
   379 
   361         File input = new File(inputDir);
   380         File input = new File(inputDir);
   362         if (!input.exists()) {
   381         if (!input.exists()) {
   363             input.mkdir();
   382             input.mkdir();
   392                 System.err.println(Files.readString(javacLog.toPath()));
   411                 System.err.println(Files.readString(javacLog.toPath()));
   393             }
   412             }
   394             throw new AssertionError("javac exited with error: " + retVal);
   413             throw new AssertionError("javac exited with error: " + retVal);
   395         }
   414         }
   396 
   415 
   397         File jarLog = new File("jar.log");
   416         if (createModularJar) {
   398         try {
   417             File jarLog = new File("jar.log");
   399             List<String> args = new ArrayList<>();
   418             try {
   400             args.add(JAR.toString());
   419                 List<String> args = new ArrayList<>();
   401             args.add("--create");
   420                 args.add(JAR.toString());
   402             args.add("--file");
   421                 args.add("--create");
   403             args.add(inputDir + File.separator + "com." + aName + ".jar");
   422                 args.add("--file");
   404             args.add("-C");
   423                 args.add(inputDir + File.separator + "com." + aName + ".jar");
   405             args.add("module" + File.separator + "com." + aName);
   424                 args.add("-C");
   406             args.add(".");
   425                 args.add("module" + File.separator + "com." + aName);
   407 
   426                 args.add(".");
   408             retVal = execute(jarLog, args.stream().toArray(String[]::new));
   427 
   409         } catch (Exception ex) {
   428                 retVal = execute(jarLog, args.stream().toArray(String[]::new));
   410             if (jarLog.exists()) {
   429             } catch (Exception ex) {
   411                 System.err.println(Files.readString(jarLog.toPath()));
   430                 if (jarLog.exists()) {
   412             }
   431                     System.err.println(Files.readString(jarLog.toPath()));
   413             throw ex;
   432                 }
   414         }
   433                 throw ex;
   415 
   434             }
   416         if (retVal != 0) {
   435 
   417             if (jarLog.exists()) {
   436             if (retVal != 0) {
   418                 System.err.println(Files.readString(jarLog.toPath()));
   437                 if (jarLog.exists()) {
   419             }
   438                     System.err.println(Files.readString(jarLog.toPath()));
   420             throw new AssertionError("jar exited with error: " + retVal);
   439                 }
       
   440                 throw new AssertionError("jar exited with error: " + retVal);
       
   441             }
   421         }
   442         }
   422     }
   443     }
   423 
   444 
   424     public static void createRuntime() throws Exception {
   445     public static void createRuntime() throws Exception {
       
   446         List<String> moreArgs = new ArrayList<>();
       
   447         createRuntime(moreArgs);
       
   448     }
       
   449 
       
   450     public static void createRuntime(List<String> moreArgs) throws Exception {
   425         int retVal;
   451         int retVal;
   426 
   452 
   427         File jlinkLog = new File("jlink.log");
   453         File jlinkLog = new File("jlink.log");
   428         try {
   454         try {
   429             List<String> args = new ArrayList<>();
   455             List<String> args = new ArrayList<>();
   430             args.add(JLINK.toString());
   456             args.add(JLINK.toString());
   431             args.add("--output");
   457             args.add("--output");
   432             args.add("runtime");
   458             args.add("runtime");
   433             args.add("--add-modules");
   459             args.add("--add-modules");
   434             args.add("java.base");
   460             args.add("java.base");
       
   461             args.addAll(moreArgs);
       
   462 
   435             retVal = execute(jlinkLog, args.stream().toArray(String[]::new));
   463             retVal = execute(jlinkLog, args.stream().toArray(String[]::new));
   436         } catch (Exception ex) {
   464         } catch (Exception ex) {
   437             if (jlinkLog.exists()) {
   465             if (jlinkLog.exists()) {
   438                 System.err.println(Files.readString(jlinkLog.toPath()));
   466                 System.err.println(Files.readString(jlinkLog.toPath()));
   439             }
   467             }
   469                 }
   497                 }
   470                 argsStr = "\"" + argsStr + "\"";
   498                 argsStr = "\"" + argsStr + "\"";
   471             }
   499             }
   472         }
   500         }
   473         return argsStr;
   501         return argsStr;
       
   502     }
       
   503 
       
   504     public static String[] cmdWithAtFilename(String [] cmd, int ndx, int len)
       
   505                 throws IOException {
       
   506         ArrayList<String> newAList = new ArrayList<>();
       
   507         String fileString = null;
       
   508         for (int i=0; i<cmd.length; i++) {
       
   509             if (i == ndx) {
       
   510                 newAList.add("@argfile.cmds");
       
   511                 fileString = cmd[i];
       
   512             } else if (i > ndx && i < ndx + len) {
       
   513                 fileString += " " + cmd[i];
       
   514             } else {
       
   515                 newAList.add(cmd[i]);
       
   516             }
       
   517         }
       
   518         if (fileString != null) {
       
   519             Path path = new File("argfile.cmds").toPath();
       
   520             try (BufferedWriter bw = Files.newBufferedWriter(path);
       
   521                     PrintWriter out = new PrintWriter(bw)) {
       
   522                 out.println(fileString);
       
   523             }
       
   524         }
       
   525         return newAList.toArray(new String[0]);
   474     }
   526     }
   475 
   527 
   476     private static String quote(String in, boolean toolProvider) {
   528     private static String quote(String in, boolean toolProvider) {
   477         if (in == null) {
   529         if (in == null) {
   478             return null;
   530             return null;