Path: blob/master/test/jdk/sun/management/jmxremote/bootstrap/LocalManagementTest.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 java.lang.reflect.Method;24import java.lang.reflect.Modifier;25import java.util.ArrayList;26import java.util.Collections;27import java.util.List;28import java.util.concurrent.atomic.AtomicReference;2930import jdk.test.lib.process.ProcessTools;31import jdk.test.lib.Utils;3233/**34* @test35* @bug 5016507 6173612 6319776 6342019 6484550 800492636* @summary Start a managed VM and test that a management tool can connect37* without connection or username/password details.38* TestManager will attempt a connection to the address obtained from39* both agent properties and jvmstat buffer.40*41* @library /test/lib42* @modules java.management43* jdk.attach44* jdk.management.agent/jdk.internal.agent45*46* @build TestManager TestApplication47* @run main/othervm/timeout=300 LocalManagementTest48*/49public class LocalManagementTest {50private static final String TEST_CLASSPATH = System.getProperty("test.class.path");5152public static void main(String[] args) throws Exception {53int failures = 0;54for(Method m : LocalManagementTest.class.getDeclaredMethods()) {55if (Modifier.isStatic(m.getModifiers()) &&56m.getName().startsWith("test")) {57m.setAccessible(true);58try {59System.out.println(m.getName());60System.out.println("==========");61Boolean rslt = (Boolean)m.invoke(null);62if (!rslt) {63System.err.println(m.getName() + " failed");64failures++;65}66} catch (Exception e) {67e.printStackTrace();68failures++;69}70}71}72if (failures > 0) {73throw new Error("Test failed");74}75}7677@SuppressWarnings("unused")78private static boolean test1() throws Exception {79return doTest("1", "-Dcom.sun.management.jmxremote");80}8182/**83* no args (blank) - manager should attach and start agent84*/85@SuppressWarnings("unused")86private static boolean test3() throws Exception {87return doTest("3", null);88}8990/**91* use DNS-only name service92*/93@SuppressWarnings("unused")94private static boolean test5() throws Exception {95return doTest("5", "-Dsun.net.spi.namservice.provider.1=\"dns,sun\"");96}9798private static boolean doTest(String testId, String arg) throws Exception {99List<String> args = new ArrayList<>();100args.add("-XX:+UsePerfData");101Collections.addAll(args, Utils.getTestJavaOpts());102args.add("-cp");103args.add(TEST_CLASSPATH);104105if (arg != null) {106args.add(arg);107}108args.add("TestApplication");109ProcessBuilder server = ProcessTools.createJavaProcessBuilder(110args.toArray(new String[args.size()])111);112113Process serverPrc = null, clientPrc = null;114try {115final AtomicReference<String> port = new AtomicReference<>();116117serverPrc = ProcessTools.startProcess(118"TestApplication(" + testId + ")",119server,120(String line) -> {121if (line.startsWith("port:")) {122port.set(line.split("\\:")[1]);123} else if (line.startsWith("waiting")) {124return true;125}126return false;127}128);129130System.out.println("Attaching test manager:");131System.out.println("=========================");132System.out.println(" PID : " + serverPrc.pid());133System.out.println(" shutdown port : " + port.get());134135ProcessBuilder client = ProcessTools.createJavaProcessBuilder(136"-cp",137TEST_CLASSPATH,138"--add-exports", "jdk.management.agent/jdk.internal.agent=ALL-UNNAMED",139"TestManager",140String.valueOf(serverPrc.pid()),141port.get(),142"true"143);144145clientPrc = ProcessTools.startProcess(146"TestManager",147client,148(String line) -> line.startsWith("Starting TestManager for PID")149);150151int clientExitCode = clientPrc.waitFor();152int serverExitCode = serverPrc.waitFor();153return clientExitCode == 0 && serverExitCode == 0;154} finally {155if (clientPrc != null) {156System.out.println("Stopping process " + clientPrc);157clientPrc.destroy();158clientPrc.waitFor();159}160if (serverPrc != null) {161System.out.println("Stopping process " + serverPrc);162serverPrc.destroy();163serverPrc.waitFor();164}165}166}167}168169170