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 import java.io.BufferedReader; |
|
24 import java.io.IOException; |
|
25 import java.io.InputStreamReader; |
|
26 import java.math.BigDecimal; |
|
27 import java.util.ArrayList; |
|
28 import java.util.Arrays; |
|
29 import java.util.HashMap; |
|
30 import java.util.List; |
|
31 import java.util.Map; |
|
32 import java.util.logging.Level; |
|
33 import java.util.logging.Logger; |
|
34 |
|
35 import jdk.test.lib.apps.LingeredApp; |
|
36 import jdk.testlibrary.JDKToolLauncher; |
|
37 import jdk.testlibrary.Utils; |
|
38 |
|
39 public class TmtoolTestScenario { |
|
40 |
|
41 private final ArrayList<String> toolOutput = new ArrayList<String>(); |
|
42 private LingeredApp theApp = null; |
|
43 private final String toolName; |
|
44 private final String[] toolArgs; |
|
45 |
|
46 /** |
|
47 * @param toolName - name of tool to test |
|
48 * @param toolArgs - tool arguments |
|
49 * @return the object |
|
50 */ |
|
51 public static TmtoolTestScenario create(String toolName, String... toolArgs) { |
|
52 return new TmtoolTestScenario(toolName, toolArgs); |
|
53 } |
|
54 |
|
55 /** |
|
56 * @return STDOUT of tool |
|
57 */ |
|
58 public List<String> getToolOutput() { |
|
59 return toolOutput; |
|
60 } |
|
61 |
|
62 /** |
|
63 * |
|
64 * @return STDOUT of test app |
|
65 */ |
|
66 public List<String> getAppOutput() { |
|
67 return theApp.getAppOutput(); |
|
68 } |
|
69 |
|
70 /** |
|
71 * @return Value of the app output with -XX:+PrintFlagsFinal as a map. |
|
72 */ |
|
73 public Map<String, String> parseFlagsFinal() { |
|
74 List<String> astr = theApp.getAppOutput(); |
|
75 Map<String, String> vmMap = new HashMap<String, String>(); |
|
76 |
|
77 for (String line : astr) { |
|
78 String[] lv = line.trim().split("\\s+"); |
|
79 try { |
|
80 vmMap.put(lv[1], lv[3]); |
|
81 } catch (ArrayIndexOutOfBoundsException ex) { |
|
82 // ignore mailformed lines |
|
83 } |
|
84 } |
|
85 return vmMap; |
|
86 } |
|
87 |
|
88 /** |
|
89 * |
|
90 * @param vmArgs - vm and java arguments to launch test app |
|
91 * @return exit code of tool |
|
92 */ |
|
93 public int launch(List<String> vmArgs) { |
|
94 System.out.println("Starting LingeredApp"); |
|
95 try { |
|
96 try { |
|
97 List<String> vmArgsExtended = new ArrayList<String>(); |
|
98 vmArgsExtended.add("-XX:+UsePerfData"); |
|
99 vmArgsExtended.addAll(vmArgs); |
|
100 theApp = LingeredApp.startApp(vmArgsExtended); |
|
101 |
|
102 System.out.println("Starting " + toolName + " against " + theApp.getPid()); |
|
103 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(toolName); |
|
104 |
|
105 for (String cmd : toolArgs) { |
|
106 launcher.addToolArg(cmd); |
|
107 } |
|
108 launcher.addToolArg(Long.toString(theApp.getPid())); |
|
109 |
|
110 ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); |
|
111 processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); |
|
112 Process toolProcess = processBuilder.start(); |
|
113 |
|
114 // By default child process output stream redirected to pipe, so we are reading it in foreground. |
|
115 BufferedReader reader = new BufferedReader(new InputStreamReader(toolProcess.getInputStream())); |
|
116 |
|
117 String line; |
|
118 while ((line = reader.readLine()) != null) { |
|
119 toolOutput.add(line.trim()); |
|
120 } |
|
121 |
|
122 toolProcess.waitFor(); |
|
123 |
|
124 return toolProcess.exitValue(); |
|
125 } finally { |
|
126 LingeredApp.stopApp(theApp); |
|
127 } |
|
128 } catch (IOException | InterruptedException ex) { |
|
129 throw new RuntimeException("Test ERROR " + ex, ex); |
|
130 } |
|
131 } |
|
132 |
|
133 public void launch(String... appArgs) throws IOException { |
|
134 launch(Arrays.asList(appArgs)); |
|
135 } |
|
136 |
|
137 private TmtoolTestScenario(String toolName, String[] toolArgs) { |
|
138 this.toolName = toolName; |
|
139 this.toolArgs = toolArgs; |
|
140 } |
|
141 |
|
142 } |
|