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 */
23import java.io.BufferedReader;
24import java.io.IOException;
25import java.io.InputStreamReader;
26import java.math.BigDecimal;
27import java.util.ArrayList;
28import java.util.Arrays;
29import java.util.HashMap;
30import java.util.List;
31import java.util.Map;
32import java.util.logging.Level;
33import java.util.logging.Logger;
34
35import jdk.test.lib.apps.LingeredApp;
36import jdk.testlibrary.JDKToolLauncher;
37import jdk.testlibrary.Utils;
38
39public 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("jhsdb");
104                launcher.addToolArg(toolName);
105
106                for (String cmd : toolArgs) {
107                    launcher.addToolArg(cmd);
108                }
109                launcher.addToolArg("--pid");
110                launcher.addToolArg(Long.toString(theApp.getPid()));
111
112                ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
113                processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
114                Process toolProcess = processBuilder.start();
115
116                // By default child process output stream redirected to pipe, so we are reading it in foreground.
117                BufferedReader reader = new BufferedReader(new InputStreamReader(toolProcess.getInputStream()));
118
119                String line;
120                while ((line = reader.readLine()) != null) {
121                    toolOutput.add(line.trim());
122                }
123
124                toolProcess.waitFor();
125
126                return toolProcess.exitValue();
127            } finally {
128                LingeredApp.stopApp(theApp);
129            }
130        } catch (IOException | InterruptedException ex) {
131            throw new RuntimeException("Test ERROR " + ex, ex);
132        }
133    }
134
135    public void launch(String... appArgs) throws IOException {
136        launch(Arrays.asList(appArgs));
137    }
138
139    private TmtoolTestScenario(String toolName, String[] toolArgs) {
140        this.toolName = toolName;
141        this.toolArgs = toolArgs;
142    }
143
144}
145