Path: blob/master/test/jdk/sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
41153 views
/*1* Copyright (c) 2008, 2015, 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*/2223/*24* @test25* @bug 498121526* @summary Tests that the jvmstat counters published by the out-of-the-box27* management agent for the JMX connection details are correct.28* @author Luis-Miguel Alventosa29*30* @modules java.management31* jdk.attach32* jdk.management.agent/jdk.internal.agent33*34* @run clean JvmstatCountersTest35* @run build JvmstatCountersTest36* @run main/othervm/timeout=600 -XX:+UsePerfData JvmstatCountersTest 137* @run main/othervm/timeout=600 -XX:+UsePerfData -Dcom.sun.management.jmxremote JvmstatCountersTest 238* @run main/othervm/timeout=600 -XX:+UsePerfData -Dcom.sun.management.jmxremote.port=0 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false JvmstatCountersTest 339* @run main/othervm/timeout=600 -XX:+UsePerfData -Djdk.attach.allowAttachSelf JvmstatCountersTest 440*/4142import java.io.*;43import java.lang.management.*;44import java.util.*;45import javax.management.*;46import javax.management.remote.*;47import com.sun.tools.attach.*;48import jdk.internal.agent.ConnectorAddressLink;4950public class JvmstatCountersTest {5152public static void checkAddress(String address) throws IOException {53System.out.println("Address = " + address);54JMXServiceURL url = new JMXServiceURL(address);55JMXConnector jmxc = JMXConnectorFactory.connect(url);56MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();57System.out.println("MBean Count = " + mbsc.getMBeanCount());58}5960public static void checkKey(Map<String, String> data, int index,61String key, String expectedValue) throws Exception {62String counter = "sun.management.JMXConnectorServer." + index + "." + key;63if (!data.containsKey(counter)) {64System.out.println("Test FAILED! Missing counter " + counter);65throw new IllegalArgumentException("Test case failed");66}67String value = data.get(counter);68if (key.equals("remoteAddress")) {69checkAddress(value);70} else if (!expectedValue.equals(value)) {71System.out.println("Test FAILED! Invalid counter " +72counter + "=" + value);73throw new IllegalArgumentException("Test case failed");74}75System.out.println("OK: " + counter + "=" + value);76}7778public static void main(String args[]) throws Exception {79String localAddress = ConnectorAddressLink.importFrom(0);80Map<String, String> remoteData = ConnectorAddressLink.importRemoteFrom(0);81final int testCase = Integer.parseInt(args[0]);82switch (testCase) {83case 1:84if (localAddress == null && remoteData.isEmpty()) {85System.out.println("Test PASSED! The OOTB management " +86"agent didn't publish any jvmstat counter.");87} else {88System.out.println("Test FAILED! The OOTB management " +89"agent unexpectedly published jvmstat counters.");90throw new IllegalArgumentException("Test case 1 failed");91}92break;93case 2:94if (localAddress == null) {95System.out.println("Test FAILED! The OOTB management " +96"agent didn't publish the local connector.");97throw new IllegalArgumentException("Test case 2 failed");98}99checkAddress(localAddress);100if (!remoteData.isEmpty()) {101System.out.println("Test FAILED! The OOTB management " +102"agent shouldn't publish the remote connector.");103throw new IllegalArgumentException("Test case 2 failed");104}105System.out.println("Test PASSED! The OOTB management " +106"agent only publishes the local connector through " +107"a jvmstat counter.");108break;109case 3:110if (localAddress == null) {111System.out.println("Test FAILED! The OOTB management " +112"agent didn't publish the local connector.");113throw new IllegalArgumentException("Test case 3 failed");114}115checkAddress(localAddress);116if (remoteData.isEmpty()) {117System.out.println("Test FAILED! The OOTB management " +118"agent didnn't publish the remote connector.");119throw new IllegalArgumentException("Test case 3 failed");120}121for (String key : remoteData.keySet()) {122if (!isKeyAcceptable(key)) {123System.out.println("Test FAILED! The OOTB management " +124"agent shouldn't publish anything which isn't " +125"related to the remote connector (" + key + ").");126throw new IllegalArgumentException("Test case 3 failed");127}128}129checkKey(remoteData, 0, "remoteAddress", null);130checkKey(remoteData, 0, "authenticate", "false");131checkKey(remoteData, 0, "ssl", "false");132checkKey(remoteData, 0, "sslRegistry", "false");133checkKey(remoteData, 0, "sslNeedClientAuth", "false");134System.out.println("Test PASSED! The OOTB management " +135"agent publishes both the local and remote " +136"connector info through jvmstat counters.");137break;138case 4:139if (localAddress != null || !remoteData.isEmpty()) {140System.out.println("Test FAILED! The OOTB management " +141"agent unexpectedly published jvmstat counters.");142throw new IllegalArgumentException("Test case 4 failed");143}144RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();145String name = rt.getName();146System.out.println("name = " + name);147String vmid = name.substring(0, name.indexOf("@"));148System.out.println("vmid = " + vmid);149VirtualMachine vm = VirtualMachine.attach(vmid);150Properties p = new Properties();151p.put("com.sun.management.jmxremote.port", "0");152p.put("com.sun.management.jmxremote.authenticate", "false");153p.put("com.sun.management.jmxremote.ssl", "false");154vm.startManagementAgent(p);155vm.startLocalManagementAgent();156vm.detach();157String localAddress2 = ConnectorAddressLink.importFrom(0);158if (localAddress2 == null) {159System.out.println("Test FAILED! The OOTB management " +160"agent didn't publish the local connector.");161throw new IllegalArgumentException("Test case 4 failed");162}163checkAddress(localAddress2);164Map<String, String> remoteData2 = ConnectorAddressLink.importRemoteFrom(0);165if (remoteData2.isEmpty()) {166System.out.println("Test FAILED! The OOTB management " +167"agent didnn't publish the remote connector.");168throw new IllegalArgumentException("Test case 4 failed");169}170for (String key : remoteData2.keySet()) {171if (!isKeyAcceptable(key)) {172System.out.println("Test FAILED! The OOTB management " +173"agent shouldn't publish anything which isn't " +174"related to the remote connector (" + key + ").");175throw new IllegalArgumentException("Test case 4 failed");176}177}178checkKey(remoteData2, 0, "remoteAddress", null);179checkKey(remoteData2, 0, "authenticate", "false");180checkKey(remoteData2, 0, "ssl", "false");181checkKey(remoteData2, 0, "sslRegistry", "false");182checkKey(remoteData2, 0, "sslNeedClientAuth", "false");183System.out.println("Test PASSED! The OOTB management agent " +184"publishes both the local and remote connector " +185"info through jvmstat counters when the agent is " +186"loaded through the Attach API.");187}188System.out.println("Bye! Bye!");189}190191private static boolean isKeyAcceptable(String key) {192return key.startsWith("sun.management.JMXConnectorServer.0.") ||193key.startsWith("sun.management.JMXConnectorServer.remote.enabled");194}195}196197198