test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java
branchJDK-8200758-branch
changeset 58648 3bf53ffa9ae7
parent 58463 4e71249f291c
equal deleted inserted replaced
58647:2c43b89b1679 58648:3bf53ffa9ae7
    57         toolProvider = Objects.requireNonNull(v);
    57         toolProvider = Objects.requireNonNull(v);
    58         executable = null;
    58         executable = null;
    59         return this;
    59         return this;
    60     }
    60     }
    61 
    61 
       
    62     public Executor setToolProvider(JavaTool v) {
       
    63         return setToolProvider(v.asToolProvider());
       
    64     }
       
    65 
    62     public Executor setDirectory(Path v) {
    66     public Executor setDirectory(Path v) {
    63         directory = v;
    67         directory = v;
    64         return this;
    68         return this;
    65     }
    69     }
    66 
    70 
   165         final int exitCode;
   169         final int exitCode;
   166         private List<String> output;
   170         private List<String> output;
   167     }
   171     }
   168 
   172 
   169     public Result execute() {
   173     public Result execute() {
       
   174         if (toolProvider != null && directory != null) {
       
   175             throw new IllegalArgumentException(
       
   176                     "Can't change directory when using tool provider");
       
   177         }
       
   178 
   170         return ThrowingSupplier.toSupplier(() -> {
   179         return ThrowingSupplier.toSupplier(() -> {
   171             if (toolProvider != null) {
   180             if (toolProvider != null) {
   172                 return runToolProvider();
   181                 return runToolProvider();
   173             }
   182             }
   174 
   183 
   191     private boolean withSavedOutput() {
   200     private boolean withSavedOutput() {
   192         return saveOutputType.contains(SaveOutputType.FULL) || saveOutputType.contains(
   201         return saveOutputType.contains(SaveOutputType.FULL) || saveOutputType.contains(
   193                 SaveOutputType.FIRST_LINE);
   202                 SaveOutputType.FIRST_LINE);
   194     }
   203     }
   195 
   204 
       
   205     private Path executablePath() {
       
   206         if (directory == null || executable.isAbsolute()) {
       
   207             return executable;
       
   208         }
       
   209 
       
   210         // If relative path to executable is used it seems to be broken when
       
   211         // ProcessBuilder changes the directory. On Windows it changes the
       
   212         // directory first and on Linux it looks up for executable before
       
   213         // changing the directory. So to stay of safe side, use absolute path
       
   214         // to executable.
       
   215         return executable.toAbsolutePath();
       
   216     }
       
   217 
   196     private Result runExecutable() throws IOException, InterruptedException {
   218     private Result runExecutable() throws IOException, InterruptedException {
   197         List<String> command = new ArrayList<>();
   219         List<String> command = new ArrayList<>();
   198         command.add(executable.toString());
   220         command.add(executablePath().toString());
   199         command.addAll(args);
   221         command.addAll(args);
   200         ProcessBuilder builder = new ProcessBuilder(command);
   222         ProcessBuilder builder = new ProcessBuilder(command);
   201         StringBuilder sb = new StringBuilder(getPrintableCommandLine());
   223         StringBuilder sb = new StringBuilder(getPrintableCommandLine());
   202         if (withSavedOutput()) {
   224         if (withSavedOutput()) {
   203             builder.redirectErrorStream(true);
   225             builder.redirectErrorStream(true);
   313             exec = "<null>";
   335             exec = "<null>";
   314         } else if (toolProvider != null) {
   336         } else if (toolProvider != null) {
   315             format = "tool provider " + format;
   337             format = "tool provider " + format;
   316             exec = toolProvider.name();
   338             exec = toolProvider.name();
   317         } else {
   339         } else {
   318             exec = executable.toString();
   340             exec = executablePath().toString();
   319         }
   341         }
   320 
   342 
   321         return String.format(format, printCommandLine(exec, args),
   343         return String.format(format, printCommandLine(exec, args),
   322                 args.size() + 1);
   344                 args.size() + 1);
   323     }
   345     }