1 /* |
1 /* |
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
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 common; |
23 package common; |
24 |
24 |
25 import java.io.BufferedReader; |
25 import java.nio.file.Files; |
26 import java.io.IOException; |
26 import java.nio.file.Path; |
27 import java.io.StringReader; |
27 import java.nio.file.Paths; |
28 import java.util.ArrayList; |
28 import java.util.Arrays; |
29 import java.util.LinkedList; |
29 import java.time.Instant; |
30 import java.util.List; |
|
31 import java.util.StringTokenizer; |
|
32 import jdk.test.lib.process.OutputAnalyzer; |
|
33 import jdk.test.lib.process.ProcessTools; |
|
34 |
30 |
35 /** |
31 /** |
36 * This class starts a process specified by the passed command line waits till |
32 * This class starts a process specified by the passed command line waits till |
37 * the process completes and returns the process exit code and stdout and stderr |
33 * the process completes and returns the process exit code and stdout and stderr |
38 * output as ToolResults |
34 * output as ToolResults |
39 */ |
35 */ |
40 class ToolRunner { |
36 class ToolRunner { |
41 |
37 private final String[] cmdArgs; |
42 private final List<String> cmdArgs = new LinkedList<>(); |
|
43 |
38 |
44 ToolRunner(String cmdLine) { |
39 ToolRunner(String cmdLine) { |
45 StringTokenizer st = new StringTokenizer(cmdLine); |
40 cmdArgs = cmdLine.split(" +"); |
46 while (st.hasMoreTokens()) { |
|
47 cmdArgs.add(st.nextToken()); |
|
48 } |
|
49 } |
41 } |
50 |
42 |
51 /** |
43 /** |
52 * Starts the process, waits for the process completion and returns the |
44 * Starts the process, waits for the process completion and returns the |
53 * results |
45 * results |
54 * |
46 * |
55 * @return process results |
47 * @return process results |
56 * @throws Exception if anything goes wrong |
48 * @throws Exception if anything goes wrong |
57 */ |
49 */ |
58 ToolResults runToCompletion() throws Exception { |
50 ToolResults runToCompletion() throws Exception { |
|
51 ProcessBuilder pb = new ProcessBuilder(cmdArgs); |
|
52 Path out = Files.createTempFile(Paths.get("."), "out.", ".txt"); |
|
53 Path err = out.resolveSibling(out.getFileName().toString().replaceFirst("out", "err")); |
59 |
54 |
60 ProcessBuilder pb = new ProcessBuilder(cmdArgs); |
55 Process p = pb.redirectOutput(ProcessBuilder.Redirect.to(out.toFile())) |
61 OutputAnalyzer oa = ProcessTools.executeProcess(pb); |
56 .redirectError(ProcessBuilder.Redirect.to(err.toFile())) |
62 |
57 .start(); |
63 return new ToolResults(oa.getExitValue(), |
58 System.out.printf("[%s] started process %d %s with out/err redirected to '%s' and '%s'%n", |
64 stringToList(oa.getStdout()), |
59 Instant.now().toString(), p.pid(), pb.command(), out.toString(), err.toString()); |
65 stringToList(oa.getStderr())); |
60 int exitCode = p.waitFor(); |
66 |
61 System.out.printf("[%s] process %d finished with exit code = %d%n", |
67 } |
62 Instant.now().toString(), p.pid(), exitCode); |
68 |
63 return new ToolResults(exitCode, Files.readAllLines(out), Files.readAllLines(err)); |
69 private static List<String> stringToList(String s) throws IOException { |
|
70 BufferedReader reader = new BufferedReader(new StringReader(s)); |
|
71 List<String> strings = new ArrayList<>(); |
|
72 for (String line = reader.readLine(); line != null; line = reader.readLine()) { |
|
73 strings.add(line); |
|
74 } |
|
75 reader.close(); |
|
76 return strings; |
|
77 } |
64 } |
78 } |
65 } |