|
1 /* |
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
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 |
|
7 * published by the Free Software Foundation. |
|
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 package common; |
|
24 |
|
25 import java.io.BufferedReader; |
|
26 import java.io.IOException; |
|
27 import java.io.StringReader; |
|
28 import java.util.ArrayList; |
|
29 import java.util.LinkedList; |
|
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 |
|
35 /** |
|
36 * 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 |
|
38 * output as ToolResults |
|
39 */ |
|
40 class ToolRunner { |
|
41 |
|
42 private final List<String> cmdArgs = new LinkedList<>(); |
|
43 |
|
44 ToolRunner(String cmdLine) { |
|
45 StringTokenizer st = new StringTokenizer(cmdLine); |
|
46 while (st.hasMoreTokens()) { |
|
47 cmdArgs.add(st.nextToken()); |
|
48 } |
|
49 } |
|
50 |
|
51 /** |
|
52 * Starts the process, waits for the process completion and returns the |
|
53 * results |
|
54 * |
|
55 * @return process results |
|
56 * @throws Exception if anything goes wrong |
|
57 */ |
|
58 ToolResults runToCompletion() throws Exception { |
|
59 |
|
60 ProcessBuilder pb = new ProcessBuilder(cmdArgs); |
|
61 OutputAnalyzer oa = ProcessTools.executeProcess(pb); |
|
62 |
|
63 return new ToolResults(oa.getExitValue(), |
|
64 stringToList(oa.getStdout()), |
|
65 stringToList(oa.getStderr())); |
|
66 |
|
67 } |
|
68 |
|
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 } |
|
78 } |