Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
38867 views
/*1* Copyright (c) 2008, 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*/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* @run clean JvmstatCountersTest30* @run build JvmstatCountersTest31* @run main/othervm/timeout=600 JvmstatCountersTest 132* @run main/othervm/timeout=600 -Dcom.sun.management.jmxremote JvmstatCountersTest 233* @run main/othervm/timeout=600 -Dcom.sun.management.jmxremote.port=0 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false JvmstatCountersTest 334* @run main/othervm/timeout=600 JvmstatCountersTest 435*/3637import java.io.*;38import java.lang.management.*;39import java.util.*;40import javax.management.*;41import javax.management.remote.*;42import com.sun.tools.attach.*;43import sun.management.ConnectorAddressLink;4445public class JvmstatCountersTest {4647public static void checkAddress(String address) throws IOException {48System.out.println("Address = " + address);49JMXServiceURL url = new JMXServiceURL(address);50JMXConnector jmxc = JMXConnectorFactory.connect(url);51MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();52System.out.println("MBean Count = " + mbsc.getMBeanCount());53}5455public static void checkKey(Map<String, String> data, int index,56String key, String expectedValue) throws Exception {57String counter = "sun.management.JMXConnectorServer." + index + "." + key;58if (!data.containsKey(counter)) {59System.out.println("Test FAILED! Missing counter " + counter);60throw new IllegalArgumentException("Test case failed");61}62String value = data.get(counter);63if (key.equals("remoteAddress")) {64checkAddress(value);65} else if (!expectedValue.equals(value)) {66System.out.println("Test FAILED! Invalid counter " +67counter + "=" + value);68throw new IllegalArgumentException("Test case failed");69}70System.out.println("OK: " + counter + "=" + value);71}7273public static void main(String args[]) throws Exception {74String localAddress = ConnectorAddressLink.importFrom(0);75Map<String, String> remoteData = ConnectorAddressLink.importRemoteFrom(0);76final int testCase = Integer.parseInt(args[0]);77switch (testCase) {78case 1:79if (localAddress == null && remoteData.isEmpty()) {80System.out.println("Test PASSED! The OOTB management " +81"agent didn't publish any jvmstat counter.");82} else {83System.out.println("Test FAILED! The OOTB management " +84"agent unexpectedly published jvmstat counters.");85throw new IllegalArgumentException("Test case 1 failed");86}87break;88case 2:89if (localAddress == null) {90System.out.println("Test FAILED! The OOTB management " +91"agent didn't publish the local connector.");92throw new IllegalArgumentException("Test case 2 failed");93}94checkAddress(localAddress);95if (!remoteData.isEmpty()) {96System.out.println("Test FAILED! The OOTB management " +97"agent shouldn't publish the remote connector.");98throw new IllegalArgumentException("Test case 2 failed");99}100System.out.println("Test PASSED! The OOTB management " +101"agent only publishes the local connector through " +102"a jvmstat counter.");103break;104case 3:105if (localAddress == null) {106System.out.println("Test FAILED! The OOTB management " +107"agent didn't publish the local connector.");108throw new IllegalArgumentException("Test case 3 failed");109}110checkAddress(localAddress);111if (remoteData.isEmpty()) {112System.out.println("Test FAILED! The OOTB management " +113"agent didnn't publish the remote connector.");114throw new IllegalArgumentException("Test case 3 failed");115}116for (String key : remoteData.keySet()) {117if (!key.startsWith("sun.management.JMXConnectorServer.0.")) {118System.out.println("Test FAILED! The OOTB management " +119"agent shouldn't publish anything which isn't " +120"related to the remote connector.");121throw new IllegalArgumentException("Test case 3 failed");122}123}124checkKey(remoteData, 0, "remoteAddress", null);125checkKey(remoteData, 0, "authenticate", "false");126checkKey(remoteData, 0, "ssl", "false");127checkKey(remoteData, 0, "sslRegistry", "false");128checkKey(remoteData, 0, "sslNeedClientAuth", "false");129System.out.println("Test PASSED! The OOTB management " +130"agent publishes both the local and remote " +131"connector info through jvmstat counters.");132break;133case 4:134if (localAddress != null || !remoteData.isEmpty()) {135System.out.println("Test FAILED! The OOTB management " +136"agent unexpectedly published jvmstat counters.");137throw new IllegalArgumentException("Test case 4 failed");138}139RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();140String name = rt.getName();141System.out.println("name = " + name);142String vmid = name.substring(0, name.indexOf("@"));143System.out.println("vmid = " + vmid);144VirtualMachine vm = VirtualMachine.attach(vmid);145Properties p = new Properties();146p.put("com.sun.management.jmxremote.port", "0");147p.put("com.sun.management.jmxremote.authenticate", "false");148p.put("com.sun.management.jmxremote.ssl", "false");149vm.startManagementAgent(p);150vm.startLocalManagementAgent();151vm.detach();152String localAddress2 = ConnectorAddressLink.importFrom(0);153if (localAddress2 == null) {154System.out.println("Test FAILED! The OOTB management " +155"agent didn't publish the local connector.");156throw new IllegalArgumentException("Test case 4 failed");157}158checkAddress(localAddress2);159Map<String, String> remoteData2 = ConnectorAddressLink.importRemoteFrom(0);160if (remoteData2.isEmpty()) {161System.out.println("Test FAILED! The OOTB management " +162"agent didnn't publish the remote connector.");163throw new IllegalArgumentException("Test case 4 failed");164}165for (String key : remoteData2.keySet()) {166if (!key.startsWith("sun.management.JMXConnectorServer.0.")) {167System.out.println("Test FAILED! The OOTB management " +168"agent shouldn't publish anything which isn't " +169"related to the remote connector.");170throw new IllegalArgumentException("Test case 4 failed");171}172}173checkKey(remoteData2, 0, "remoteAddress", null);174checkKey(remoteData2, 0, "authenticate", "false");175checkKey(remoteData2, 0, "ssl", "false");176checkKey(remoteData2, 0, "sslRegistry", "false");177checkKey(remoteData2, 0, "sslNeedClientAuth", "false");178System.out.println("Test PASSED! The OOTB management agent " +179"publishes both the local and remote connector " +180"info through jvmstat counters when the agent is " +181"loaded through the Attach API.");182}183System.out.println("Bye! Bye!");184}185}186187188