29N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
29N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29N/A *
29N/A * This code is free software; you can redistribute it and/or modify it
29N/A * under the terms of the GNU General Public License version 2 only, as
29N/A * published by the Free Software Foundation.
29N/A *
29N/A * This code is distributed in the hope that it will be useful, but WITHOUT
29N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29N/A * version 2 for more details (a copy is included in the LICENSE file that
29N/A * accompanied this code).
29N/A *
29N/A * You should have received a copy of the GNU General Public License version
29N/A * 2 along with this work; if not, write to the Free Software Foundation,
29N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
29N/A */
29N/A
29N/A/*
29N/A * @test
29N/A * @bug 4981215
29N/A * @summary Tests that the jvmstat counters published by the out-of-the-box
29N/A * management agent for the JMX connection details are correct.
29N/A * @author Luis-Miguel Alventosa
29N/A * @run clean JvmstatCountersTest
29N/A * @run build JvmstatCountersTest
29N/A * @run main/othervm JvmstatCountersTest 1
29N/A * @run main/othervm -Dcom.sun.management.jmxremote JvmstatCountersTest 2
29N/A * @run main/othervm -Dcom.sun.management.jmxremote.port=0 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false JvmstatCountersTest 3
29N/A * @run main/othervm JvmstatCountersTest 4
29N/A */
29N/A
29N/Aimport java.io.*;
29N/Aimport java.lang.management.*;
29N/Aimport java.util.*;
29N/Aimport javax.management.*;
29N/Aimport javax.management.remote.*;
29N/Aimport com.sun.tools.attach.*;
29N/Aimport sun.management.ConnectorAddressLink;
29N/A
29N/Apublic class JvmstatCountersTest {
29N/A
29N/A public static void checkAddress(String address) throws IOException {
29N/A System.out.println("Address = " + address);
29N/A JMXServiceURL url = new JMXServiceURL(address);
29N/A JMXConnector jmxc = JMXConnectorFactory.connect(url);
29N/A MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
29N/A System.out.println("MBean Count = " + mbsc.getMBeanCount());
29N/A }
29N/A
29N/A public static void checkKey(Map<String, String> data, int index,
29N/A String key, String expectedValue) throws Exception {
29N/A String counter = "sun.management.JMXConnectorServer." + index + "." + key;
29N/A if (!data.containsKey(counter)) {
29N/A System.out.println("Test FAILED! Missing counter " + counter);
29N/A throw new IllegalArgumentException("Test case failed");
29N/A }
29N/A String value = data.get(counter);
29N/A if (key.equals("remoteAddress")) {
29N/A checkAddress(value);
29N/A } else if (!expectedValue.equals(value)) {
29N/A System.out.println("Test FAILED! Invalid counter " +
29N/A counter + "=" + value);
29N/A throw new IllegalArgumentException("Test case failed");
29N/A }
29N/A System.out.println("OK: " + counter + "=" + value);
29N/A }
29N/A
29N/A public static void main(String args[]) throws Exception {
29N/A String localAddress = ConnectorAddressLink.importFrom(0);
29N/A Map<String, String> remoteData = ConnectorAddressLink.importRemoteFrom(0);
29N/A final int testCase = Integer.parseInt(args[0]);
29N/A switch (testCase) {
29N/A case 1:
29N/A if (localAddress == null && remoteData.isEmpty()) {
29N/A System.out.println("Test PASSED! The OOTB management " +
29N/A "agent didn't publish any jvmstat counter.");
29N/A } else {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent unexpectedly published jvmstat counters.");
29N/A throw new IllegalArgumentException("Test case 1 failed");
29N/A }
29N/A break;
29N/A case 2:
29N/A if (localAddress == null) {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent didn't publish the local connector.");
29N/A throw new IllegalArgumentException("Test case 2 failed");
29N/A }
29N/A checkAddress(localAddress);
29N/A if (!remoteData.isEmpty()) {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent shouldn't publish the remote connector.");
29N/A throw new IllegalArgumentException("Test case 2 failed");
29N/A }
29N/A System.out.println("Test PASSED! The OOTB management " +
29N/A "agent only publishes the local connector through " +
29N/A "a jvmstat counter.");
29N/A break;
29N/A case 3:
29N/A if (localAddress == null) {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent didn't publish the local connector.");
29N/A throw new IllegalArgumentException("Test case 3 failed");
29N/A }
29N/A checkAddress(localAddress);
29N/A if (remoteData.isEmpty()) {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent didnn't publish the remote connector.");
29N/A throw new IllegalArgumentException("Test case 3 failed");
29N/A }
29N/A for (String key : remoteData.keySet()) {
29N/A if (!key.startsWith("sun.management.JMXConnectorServer.0.")) {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent shouldn't publish anything which isn't " +
29N/A "related to the remote connector.");
29N/A throw new IllegalArgumentException("Test case 3 failed");
29N/A }
29N/A }
29N/A checkKey(remoteData, 0, "remoteAddress", null);
29N/A checkKey(remoteData, 0, "authenticate", "false");
29N/A checkKey(remoteData, 0, "ssl", "false");
29N/A checkKey(remoteData, 0, "sslRegistry", "false");
29N/A checkKey(remoteData, 0, "sslNeedClientAuth", "false");
29N/A System.out.println("Test PASSED! The OOTB management " +
29N/A "agent publishes both the local and remote " +
29N/A "connector info through jvmstat counters.");
29N/A break;
29N/A case 4:
29N/A if (localAddress != null || !remoteData.isEmpty()) {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent unexpectedly published jvmstat counters.");
29N/A throw new IllegalArgumentException("Test case 4 failed");
29N/A }
29N/A RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
29N/A String name = rt.getName();
29N/A System.out.println("name = " + name);
29N/A String vmid = name.substring(0, name.indexOf("@"));
29N/A System.out.println("vmid = " + vmid);
29N/A VirtualMachine vm = VirtualMachine.attach(vmid);
29N/A String agent = vm.getSystemProperties().getProperty("java.home") +
29N/A File.separator + "lib" + File.separator + "management-agent.jar";
29N/A vm.loadAgent(agent, "com.sun.management.jmxremote.port=0,com.sun.management.jmxremote.authenticate=false,com.sun.management.jmxremote.ssl=false");
29N/A vm.detach();
29N/A String localAddress2 = ConnectorAddressLink.importFrom(0);
29N/A if (localAddress2 == null) {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent didn't publish the local connector.");
29N/A throw new IllegalArgumentException("Test case 4 failed");
29N/A }
29N/A checkAddress(localAddress2);
29N/A Map<String, String> remoteData2 = ConnectorAddressLink.importRemoteFrom(0);
29N/A if (remoteData2.isEmpty()) {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent didnn't publish the remote connector.");
29N/A throw new IllegalArgumentException("Test case 4 failed");
29N/A }
29N/A for (String key : remoteData2.keySet()) {
29N/A if (!key.startsWith("sun.management.JMXConnectorServer.0.")) {
29N/A System.out.println("Test FAILED! The OOTB management " +
29N/A "agent shouldn't publish anything which isn't " +
29N/A "related to the remote connector.");
29N/A throw new IllegalArgumentException("Test case 4 failed");
29N/A }
29N/A }
29N/A checkKey(remoteData2, 0, "remoteAddress", null);
29N/A checkKey(remoteData2, 0, "authenticate", "false");
29N/A checkKey(remoteData2, 0, "ssl", "false");
29N/A checkKey(remoteData2, 0, "sslRegistry", "false");
29N/A checkKey(remoteData2, 0, "sslNeedClientAuth", "false");
29N/A System.out.println("Test PASSED! The OOTB management agent " +
29N/A "publishes both the local and remote connector " +
29N/A "info through jvmstat counters when the agent is " +
29N/A "loaded through the Attach API.");
29N/A }
29N/A System.out.println("Bye! Bye!");
29N/A }
29N/A}