Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/jmxremote/bootstrap/LocalManagementTest.java
38867 views
/*1* Copyright (c) 2013, 2014, 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.File;24import java.lang.reflect.Method;25import java.lang.reflect.Modifier;26import java.nio.file.FileSystem;27import java.nio.file.FileSystems;28import java.nio.file.Files;29import java.nio.file.Path;30import java.util.ArrayList;31import java.util.List;32import java.util.concurrent.TimeUnit;33import java.util.concurrent.atomic.AtomicReference;34import jdk.testlibrary.ProcessTools;3536/**37* @test38* @library /lib/testlibrary39* @bug 5016507 6173612 6319776 6342019 6484550 800492640* @summary Start a managed VM and test that a management tool can connect41* without connection or username/password details.42* TestManager will attempt a connection to the address obtained from43* both agent properties and jvmstat buffer.44* @build jdk.testlibrary.* TestManager TestApplication45* @run main/othervm/timeout=300 -XX:+UsePerfData LocalManagementTest46*/4748public class LocalManagementTest {49private static final String TEST_CLASSPATH = System.getProperty("test.class.path");50private static final String TEST_JDK = System.getProperty("test.jdk");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}7677private static boolean test1() throws Exception {78return doTest("1", "-Dcom.sun.management.jmxremote");79}8081/**82* no args (blank) - manager should attach and start agent83*/84private static boolean test3() throws Exception {85return doTest("3", null);86}8788/**89* use DNS-only name service90*/91private static boolean test5() throws Exception {92return doTest("5", "-Dsun.net.spi.namservice.provider.1=\"dns,sun\"");93}9495private static boolean doTest(String testId, String arg) throws Exception {96List<String> args = new ArrayList<>();97args.add("-cp");98args.add(TEST_CLASSPATH);99100if (arg != null) {101args.add(arg);102}103args.add("TestApplication");104ProcessBuilder server = ProcessTools.createJavaProcessBuilder(105args.toArray(new String[args.size()])106);107108Process serverPrc = null, clientPrc = null;109try {110final AtomicReference<String> port = new AtomicReference<>();111final AtomicReference<String> pid = new AtomicReference<>();112113serverPrc = ProcessTools.startProcess(114"TestApplication(" + testId + ")",115server,116(String line) -> {117if (line.startsWith("port:")) {118port.set(line.split("\\:")[1]);119} else if (line.startsWith("pid:")) {120pid.set(line.split("\\:")[1]);121} else if (line.startsWith("waiting")) {122return true;123}124return false;125},1265,127TimeUnit.SECONDS128);129130System.out.println("Attaching test manager:");131System.out.println("=========================");132System.out.println(" PID : " + pid.get());133System.out.println(" shutdown port : " + port.get());134135ProcessBuilder client = ProcessTools.createJavaProcessBuilder(136"-cp",137TEST_CLASSPATH +138File.pathSeparator +139TEST_JDK +140File.separator +141"lib" +142File.separator +143"tools.jar",144"TestManager",145pid.get(),146port.get(),147"true"148);149150clientPrc = ProcessTools.startProcess(151"TestManager",152client,153(String line) -> line.startsWith("Starting TestManager for PID"),15410,155TimeUnit.SECONDS156);157158int clientExitCode = clientPrc.waitFor();159int serverExitCode = serverPrc.waitFor();160return clientExitCode == 0 && serverExitCode == 0;161} finally {162if (clientPrc != null) {163System.out.println("Stopping process " + clientPrc);164clientPrc.destroy();165clientPrc.waitFor();166}167if (serverPrc != null) {168System.out.println("Stopping process " + serverPrc);169serverPrc.destroy();170serverPrc.waitFor();171}172}173}174}175176177