Path: blob/master/test/jdk/com/sun/tools/attach/RunnerUtil.java
41153 views
/*1* Copyright (c) 2013, 2018, 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*/2223import java.io.IOException;24import java.io.File;25import java.nio.file.Files;26import java.util.Arrays;2728import jdk.test.lib.thread.ProcessThread;29import jdk.test.lib.process.ProcessTools;30import jdk.test.lib.Utils;3132/*33* Utility functions for test runners.34* (Test runner = class that launch a test)35*/36public class RunnerUtil {3738/**39* The Application process must be run concurrently with our tests since40* the tests will attach to the Application.41* We will run the Application process in a separate thread.42*43* The Application must be started with flag "-Xshare:off" for the Retransform44* test in TestBasics to pass on all platforms.45*46* The Application will write its pid and shutdownPort in the given outFile.47*/48public static ProcessThread startApplication(String... additionalOpts) throws Throwable {49String classpath = System.getProperty("test.class.path", ".");50String[] myArgs = concat(additionalOpts, new String [] {51"-XX:+UsePerfData", "-XX:+EnableDynamicAgentLoading",52"-Dattach.test=true", "-classpath", classpath, "Application"53});54String[] args = Utils.addTestJavaOpts(myArgs);55ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);56ProcessThread pt = new ProcessThread("runApplication", (line) -> line.equals(Application.READY_MSG), pb);57pt.start();58return pt;59}6061public static String[] concat(String[] a, String[] b) {62if (a == null) {63return b;64}65if (b == null) {66return a;67}68int aLen = a.length;69int bLen = b.length;70String[] c = new String[aLen + bLen];71System.arraycopy(a, 0, c, 0, aLen);72System.arraycopy(b, 0, c, aLen, bLen);73return c;74}7576/**77* Will stop the running Application.78* First tries to shutdown nicely by connecting to the shut down port.79* If that fails, the process will be killed hard with stopProcess().80*81* If the nice shutdown fails, then an Exception is thrown and the test should fail.82*83* @param processThread The process to stop.84*/85public static void stopApplication(ProcessThread processThread) throws Throwable {86if (processThread == null) {87System.out.println("RunnerUtil.stopApplication ignored since proc is null");88return;89}90try {91System.out.println("RunnerUtil.stopApplication waiting for shutdown");92processThread.sendMessage(Application.SHUTDOWN_MSG);93processThread.joinAndThrow();94processThread.getOutput().shouldHaveExitValue(0);95} catch (Throwable t) {96System.out.println("RunnerUtil.stopApplication failed. Will kill it hard: " + t);97processThread.stopProcess();98throw t;99}100}101102/**103* Creates a jar file.104* @param args Command to the jar tool.105*/106public static void createJar(String... args) {107System.out.println("Running: jar " + Arrays.toString(args));108sun.tools.jar.Main jar = new sun.tools.jar.Main(System.out, System.err, "jar");109if (!jar.run(args)) {110throw new RuntimeException("jar failed: args=" + Arrays.toString(args));111}112}113114/**115* Read the content of a file.116* @param file The file to read.117* @return The file content or null if file does not exists.118*/119public static String readFile(File file) throws IOException {120if (!file.exists()) {121return null;122}123try {124byte[] bytes = Files.readAllBytes(file.toPath());125String content = new String(bytes);126return content;127} catch (IOException e) {128e.printStackTrace();129throw e;130}131}132}133134135