test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java
branchJDK-8200758-branch
changeset 58113 885b0543f6e4
parent 58036 f7f10023f7c0
child 58301 e0efb29609bd
equal deleted inserted replaced
58037:c127c766fe8e 58113:885b0543f6e4
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 package jdk.jpackage.test;
    23 package jdk.jpackage.test;
    24 
    24 
    25 import java.io.File;
    25 import java.io.IOException;
       
    26 import java.io.PrintWriter;
       
    27 import java.io.StringWriter;
    26 import java.nio.file.Files;
    28 import java.nio.file.Files;
    27 import java.nio.file.Path;
    29 import java.nio.file.Path;
    28 import java.util.ArrayList;
    30 import java.util.ArrayList;
    29 import java.util.Arrays;
    31 import java.util.Arrays;
    30 import java.util.Collections;
    32 import java.util.Collections;
    31 import java.util.List;
    33 import java.util.List;
       
    34 import java.util.spi.ToolProvider;
       
    35 import java.util.stream.Stream;
    32 
    36 
    33 public final class Executor extends CommandArguments<Executor> {
    37 public final class Executor extends CommandArguments<Executor> {
    34 
    38 
    35     public Executor() {
    39     public Executor() {
    36         saveOutputType = SaveOutputType.NONE;
    40         saveOutputType = SaveOutputType.NONE;
    37     }
    41     }
    38 
    42 
    39     public Executor setExecutable(String v) {
    43     public Executor setExecutable(String v) {
    40         executable = v;
    44         executable = v;
       
    45         if (executable != null) {
       
    46             toolProvider = null;
       
    47         }
       
    48         return this;
       
    49     }
       
    50 
       
    51     public Executor setToolProvider(ToolProvider v) {
       
    52         toolProvider = v;
       
    53         if (toolProvider != null) {
       
    54             executable = null;
       
    55         }
    41         return this;
    56         return this;
    42     }
    57     }
    43 
    58 
    44     public Executor setDirectory(Path v) {
    59     public Executor setDirectory(Path v) {
    45         directory = v;
    60         directory = v;
    87 
   102 
    88         public Result assertExitCodeIsZero() {
   103         public Result assertExitCodeIsZero() {
    89             return assertExitCodeIs(0);
   104             return assertExitCodeIs(0);
    90         }
   105         }
    91 
   106 
    92         int exitCode;
   107         final int exitCode;
    93         List<String> output;
   108         private List<String> output;
    94     }
   109     }
    95 
   110 
    96     public Result execute() {
   111     public Result execute() {
       
   112         if (toolProvider != null) {
       
   113             return runToolProvider();
       
   114         }
       
   115 
    97         try {
   116         try {
    98             return executeImpl();
   117             if (executable != null) {
       
   118                 return runExecutable();
       
   119             }
    99         } catch (RuntimeException e) {
   120         } catch (RuntimeException e) {
   100             throw e;
   121             throw e;
   101         } catch (Exception e) {
   122         } catch (IOException | InterruptedException e) {
   102             throw new RuntimeException(e);
   123             throw new RuntimeException(e);
   103         }
   124         }
   104     }
   125 
   105 
   126         throw new IllegalStateException("No command to execute");
   106     private Result executeImpl() throws Exception {
   127     }
       
   128 
       
   129     private Result runExecutable() throws IOException, InterruptedException {
   107         List<String> command = new ArrayList<>();
   130         List<String> command = new ArrayList<>();
   108         command.add(executable);
   131         command.add(executable);
   109         command.addAll(args);
   132         command.addAll(args);
   110         Path outputFile = null;
   133         Path outputFile = null;
   111         ProcessBuilder builder = new ProcessBuilder(command);
   134         ProcessBuilder builder = new ProcessBuilder(command);
   139                 Files.deleteIfExists(outputFile);
   162                 Files.deleteIfExists(outputFile);
   140             }
   163             }
   141         }
   164         }
   142     }
   165     }
   143 
   166 
       
   167     private Result runToolProvider() {
       
   168         StringWriter writer = new StringWriter();
       
   169         PrintWriter pw = new PrintWriter(writer);
       
   170 
       
   171         Test.trace("Execute " + getPrintableCommandLine() + "...");
       
   172         Result reply = new Result(toolProvider.run(pw, pw, args.toArray(
       
   173                 String[]::new)));
       
   174         Test.trace("Done. Exit code: " + reply.exitCode);
       
   175 
       
   176         List lines = List.of(writer.toString().split("\\R", -1));
       
   177 
       
   178         if (saveOutputType == SaveOutputType.FIRST_LINE) {
       
   179             reply.output = Stream.of(lines).findFirst().get();
       
   180         } else if (saveOutputType == SaveOutputType.FULL) {
       
   181             reply.output = Collections.unmodifiableList(lines);
       
   182         }
       
   183         return reply;
       
   184     }
       
   185 
   144     public String getPrintableCommandLine() {
   186     public String getPrintableCommandLine() {
   145         return "[" + executable + "]; args(" + args.size() + ")=" + Arrays.toString(
   187         String argsStr = String.format("; args(%d)=%s", args.size(),
   146                 args.toArray());
   188                 Arrays.toString(args.toArray()));
   147     }
   189 
   148 
   190         if (toolProvider == null && executable == null) {
       
   191             return "[null]; " + argsStr;
       
   192         }
       
   193 
       
   194         if (toolProvider != null) {
       
   195             return String.format("tool provider=[%s]; ", toolProvider.name()) + argsStr;
       
   196         }
       
   197 
       
   198         return String.format("[%s]; ", executable) + argsStr;
       
   199     }
       
   200 
       
   201     private ToolProvider toolProvider;
   149     private String executable;
   202     private String executable;
   150     private SaveOutputType saveOutputType;
   203     private SaveOutputType saveOutputType;
   151     private Path directory;
   204     private Path directory;
   152 
   205 
   153     private static enum SaveOutputType {
   206     private static enum SaveOutputType {