Path: blob/master/test/jdk/sun/management/jmxremote/bootstrap/CustomLauncherTest.java
41153 views
/*1* Copyright (c) 2013, 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*/2223import jdk.test.lib.Utils;24import jdk.test.lib.Platform;25import jdk.test.lib.process.ProcessTools;2627import java.nio.file.Files;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.util.concurrent.TimeUnit;31import java.util.concurrent.atomic.AtomicReference;3233/**34* @test35* @bug 6434402 800492636* @author Jaroslav Bachorik37*38* @library /test/lib39* @modules java.management40* jdk.attach41* jdk.management.agent/jdk.internal.agent42*43* @requires os.family == "linux"44* @build TestManager TestApplication CustomLauncherTest45* @run main/othervm/native CustomLauncherTest46*/47public class CustomLauncherTest {4849public static final String TEST_NATIVE_PATH = System.getProperty("test.nativepath");5051public static void main(String[] args) throws Exception {52if (".".equals(Utils.TEST_CLASS_PATH)) {53System.out.println("Test is designed to be run from jtreg only");54return;55}5657Path libjvm = Platform.jvmLibDir().resolve("libjvm.so");58Process serverPrc = null, clientPrc = null;5960try {61String launcher = getLauncher();6263System.out.println("Starting custom launcher:");64System.out.println("=========================");65System.out.println(" launcher : " + launcher);66System.out.println(" libjvm : " + libjvm);67System.out.println(" classpath : " + Utils.TEST_CLASS_PATH);68ProcessBuilder server = new ProcessBuilder(69launcher,70libjvm.toString(),71Utils.TEST_CLASS_PATH,72"TestApplication"73);7475final AtomicReference<String> port = new AtomicReference<>();7677serverPrc = ProcessTools.startProcess(78"Launcher",79server,80(String line) -> {81if (line.startsWith("port:")) {82port.set(line.split("\\:")[1]);83} else if (line.startsWith("waiting")) {84return true;85}86return false;87},885,89TimeUnit.SECONDS90);9192System.out.println("Attaching test manager:");93System.out.println("=========================");94System.out.println(" PID : " + serverPrc.pid());95System.out.println(" shutdown port : " + port.get());9697ProcessBuilder client = ProcessTools.createJavaProcessBuilder(98"-cp",99Utils.TEST_CLASS_PATH,100"--add-exports", "jdk.management.agent/jdk.internal.agent=ALL-UNNAMED",101"TestManager",102String.valueOf(serverPrc.pid()),103port.get(),104"true"105);106107clientPrc = ProcessTools.startProcess(108"TestManager",109client,110(String line) -> line.startsWith("Starting TestManager for PID"),11110,112TimeUnit.SECONDS113);114115int clientExitCode = clientPrc.waitFor();116int serverExitCode = serverPrc.waitFor();117118if (clientExitCode != 0 || serverExitCode != 0) {119throw new Error("Test failed");120}121} finally {122if (clientPrc != null) {123clientPrc.destroy();124clientPrc.waitFor();125}126if (serverPrc != null) {127serverPrc.destroy();128serverPrc.waitFor();129}130}131}132133private static String getLauncher() {134Path launcherPath = Paths.get(TEST_NATIVE_PATH, "launcher");135return launcherPath.toAbsolutePath().toString();136}137}138139140