Path: blob/master/test/jdk/sun/tools/jhsdb/heapconfig/TmtoolTestScenario.java
41153 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.io.BufferedReader;23import java.io.IOException;24import java.io.InputStreamReader;25import java.math.BigDecimal;26import java.util.ArrayList;27import java.util.Arrays;28import java.util.Collections;29import java.util.HashMap;30import java.util.List;31import java.util.Map;32import java.util.logging.Level;33import java.util.logging.Logger;3435import jdk.test.lib.apps.LingeredApp;36import jdk.test.lib.JDKToolLauncher;37import jdk.test.lib.SA.SATestUtils;38import jdk.test.lib.Utils;3940public class TmtoolTestScenario {4142private final ArrayList<String> toolOutput = new ArrayList<String>();43private LingeredApp theApp = null;44private final String toolName;45private final String[] toolArgs;4647/**48* @param toolName - name of tool to test49* @param toolArgs - tool arguments50* @return the object51*/52public static TmtoolTestScenario create(String toolName, String... toolArgs) {53return new TmtoolTestScenario(toolName, toolArgs);54}5556/**57* @return STDOUT of tool58*/59public List<String> getToolOutput() {60return toolOutput;61}6263/**64*65* @return STDOUT of test app66*/67public List<String> getAppOutput() {68return theApp.getOutput().getStdoutAsList();69}7071/**72* @return Value of the app output with -XX:+PrintFlagsFinal as a map.73*/74public Map<String, String> parseFlagsFinal() {75List<String> astr = getAppOutput();76Map<String, String> vmMap = new HashMap<String, String>();7778for (String line : astr) {79String[] lv = line.trim().split("\\s+");80try {81vmMap.put(lv[1], lv[3]);82} catch (ArrayIndexOutOfBoundsException ex) {83// ignore mailformed lines84}85}86return vmMap;87}8889/**90*91* @param vmArgs - vm and java arguments to launch test app92* @return exit code of tool93*/94public int launch(String... vmArgs) {95System.out.println("Starting LingeredApp");96try {97try {98List<String> vmArgsExtended = new ArrayList<String>();99vmArgsExtended.add("-XX:+UsePerfData");100Collections.addAll(vmArgsExtended, vmArgs);101theApp = new LingeredApp();102LingeredApp.startAppExactJvmOpts(theApp, vmArgsExtended.toArray(new String[0]));103104System.out.println("Starting " + toolName + " against " + theApp.getPid());105JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");106launcher.addToolArg(toolName);107108for (String cmd : toolArgs) {109launcher.addToolArg(cmd);110}111launcher.addToolArg("--pid");112launcher.addToolArg(Long.toString(theApp.getPid()));113114ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);115processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);116Process toolProcess = processBuilder.start();117118// By default child process output stream redirected to pipe, so we are reading it in foreground.119BufferedReader reader = new BufferedReader(new InputStreamReader(toolProcess.getInputStream()));120121String line;122while ((line = reader.readLine()) != null) {123toolOutput.add(line.trim());124}125126toolProcess.waitFor();127128return toolProcess.exitValue();129} finally {130LingeredApp.stopApp(theApp);131}132} catch (IOException | InterruptedException ex) {133throw new RuntimeException("Test ERROR " + ex, ex);134}135}136137private TmtoolTestScenario(String toolName, String[] toolArgs) {138this.toolName = toolName;139this.toolArgs = toolArgs;140}141142}143144145