Path: blob/master/test/hotspot/jtreg/serviceability/tmtools/share/common/TmTool.java
41159 views
/*1* Copyright (c) 2015, 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*/2223package common;2425import java.nio.file.Path;26import java.nio.file.Paths;27import jdk.test.lib.Platform;2829/**30* A tool, such as jstat, jmap, etc Specific tools are defined as subclasses31* parameterized by their corresponding ToolResults subclasses32*/33public abstract class TmTool<T extends ToolResults> {3435private final Class<T> resultsClz;36private final String cmdLine;3738public TmTool(Class<T> resultsClz, String toolName, String otherArgs) {39this.resultsClz = resultsClz;40this.cmdLine = adjustForTestJava(toolName) + " " + otherArgs;41}4243/**44* Runs the tool to completion and returns the results45*46* @return the tool results47* @throws Exception if anything goes wrong48*/49public T measure() throws Exception {50ToolRunner runner = new ToolRunner(cmdLine);51ToolResults rawResults = runner.runToCompletion();52System.out.println("Process output: " + rawResults);53return resultsClz.getDeclaredConstructor(ToolResults.class).newInstance(rawResults);54}5556private String adjustForTestJava(String toolName) {57// We need to make sure we are running the tol from the JDK under testing58String jdkPath = System.getProperty("test.jdk");59if (jdkPath == null || !Paths.get(jdkPath).toFile().exists()) {60throw new RuntimeException("property test.jdk not not set");61}62Path toolPath = Paths.get("bin", toolName + (Platform.isWindows() ? ".exe" : ""));63return Paths.get(jdkPath, toolPath.toString()).toString();64}6566}676869