1 /* 2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.File; 25 import java.lang.reflect.Method; 26 import java.lang.reflect.Modifier; 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.concurrent.TimeUnit; 30 import java.util.concurrent.atomic.AtomicReference; 31 32 /** 33 * @test 34 * @library /lib/testlibrary 35 * @bug 5016507 6173612 6319776 6342019 6484550 8004926 36 * @summary Start a managed VM and test that a management tool can connect 37 * without connection or username/password details. 38 * TestManager will attempt a connection to the address obtained from 39 * both agent properties and jvmstat buffer. 40 * @modules java.management/sun.management 41 * @build jdk.testlibrary.* TestManager TestApplication 42 * @run main/othervm/timeout=300 -XX:+UsePerfData LocalManagementTest 43 */ 44 45 import jdk.testlibrary.ProcessTools; 46 47 public class LocalManagementTest { 48 private static final String TEST_CLASSPATH = System.getProperty("test.class.path"); 49 private static final String TEST_JDK = System.getProperty("test.jdk"); 50 51 public static void main(String[] args) throws Exception { 52 int failures = 0; 53 for(Method m : LocalManagementTest.class.getDeclaredMethods()) { 54 if (Modifier.isStatic(m.getModifiers()) && 55 m.getName().startsWith("test")) { 56 m.setAccessible(true); 57 try { 58 System.out.println(m.getName()); 59 System.out.println("=========="); 60 Boolean rslt = (Boolean)m.invoke(null); 61 if (!rslt) { 62 System.err.println(m.getName() + " failed"); 63 failures++; 64 } 65 } catch (Exception e) { 66 e.printStackTrace(); 67 failures++; 68 } 69 } 70 } 71 if (failures > 0) { 72 throw new Error("Test failed"); 73 } 74 } 75 76 @SuppressWarnings("unused") 77 private static boolean test1() throws Exception { 78 return doTest("1", "-Dcom.sun.management.jmxremote"); 79 } 80 81 /** 82 * no args (blank) - manager should attach and start agent 83 */ 84 @SuppressWarnings("unused") 85 private static boolean test3() throws Exception { 86 return doTest("3", null); 87 } 88 89 /** 90 * use DNS-only name service 91 */ 92 @SuppressWarnings("unused") 93 private static boolean test5() throws Exception { 94 return doTest("5", "-Dsun.net.spi.namservice.provider.1=\"dns,sun\""); 95 } 96 97 private static boolean doTest(String testId, String arg) throws Exception { 98 List<String> args = new ArrayList<>(); 99 args.add("-cp"); 100 args.add(TEST_CLASSPATH); 101 102 if (arg != null) { 103 args.add(arg); 104 } 105 args.add("TestApplication"); 106 ProcessBuilder server = ProcessTools.createJavaProcessBuilder( 107 args.toArray(new String[args.size()]) 108 ); 109 110 Process serverPrc = null, clientPrc = null; 111 try { 112 final AtomicReference<String> port = new AtomicReference<>(); 113 114 serverPrc = ProcessTools.startProcess( 115 "TestApplication(" + testId + ")", 116 server, 117 (String line) -> { 118 if (line.startsWith("port:")) { 119 port.set(line.split("\\:")[1]); 120 } else if (line.startsWith("waiting")) { 121 return true; 122 } 123 return false; 124 }, 125 5, 126 TimeUnit.SECONDS 127 ); 128 129 System.out.println("Attaching test manager:"); 130 System.out.println("========================="); 131 System.out.println(" PID : " + serverPrc.getPid()); 132 System.out.println(" shutdown port : " + port.get()); 133 134 ProcessBuilder client = ProcessTools.createJavaProcessBuilder( 135 "-cp", 136 TEST_CLASSPATH, 137 "TestManager", 138 String.valueOf(serverPrc.getPid()), 139 port.get(), 140 "true" 141 ); 142 143 clientPrc = ProcessTools.startProcess( 144 "TestManager", 145 client, 146 (String line) -> line.startsWith("Starting TestManager for PID"), 147 10, 148 TimeUnit.SECONDS 149 ); 150 151 int clientExitCode = clientPrc.waitFor(); 152 int serverExitCode = serverPrc.waitFor(); 153 return clientExitCode == 0 && serverExitCode == 0; 154 } finally { 155 if (clientPrc != null) { 156 System.out.println("Stopping process " + clientPrc); 157 clientPrc.destroy(); 158 clientPrc.waitFor(); 159 } 160 if (serverPrc != null) { 161 System.out.println("Stopping process " + serverPrc); 162 serverPrc.destroy(); 163 serverPrc.waitFor(); 164 } 165 } 166 } 167 }