jdk/test/sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
changeset 52 752ebbd838ad
child 5506 202f599c92aa
equal deleted inserted replaced
42:6c846a8c97d3 52:752ebbd838ad
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4981215
       
    27  * @summary Tests that the jvmstat counters published by the out-of-the-box
       
    28  *          management agent for the JMX connection details are correct.
       
    29  * @author Luis-Miguel Alventosa
       
    30  * @run clean JvmstatCountersTest
       
    31  * @run build JvmstatCountersTest
       
    32  * @run main/othervm JvmstatCountersTest 1
       
    33  * @run main/othervm -Dcom.sun.management.jmxremote JvmstatCountersTest 2
       
    34  * @run main/othervm -Dcom.sun.management.jmxremote.port=0 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false JvmstatCountersTest 3
       
    35  * @run main/othervm JvmstatCountersTest 4
       
    36  */
       
    37 
       
    38 import java.io.*;
       
    39 import java.lang.management.*;
       
    40 import java.util.*;
       
    41 import javax.management.*;
       
    42 import javax.management.remote.*;
       
    43 import com.sun.tools.attach.*;
       
    44 import sun.management.ConnectorAddressLink;
       
    45 
       
    46 public class JvmstatCountersTest {
       
    47 
       
    48     public static void checkAddress(String address) throws IOException {
       
    49         System.out.println("Address = " + address);
       
    50         JMXServiceURL url = new JMXServiceURL(address);
       
    51         JMXConnector jmxc = JMXConnectorFactory.connect(url);
       
    52         MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
       
    53         System.out.println("MBean Count = " + mbsc.getMBeanCount());
       
    54     }
       
    55 
       
    56     public static void checkKey(Map<String, String> data, int index,
       
    57             String key, String expectedValue) throws Exception {
       
    58         String counter = "sun.management.JMXConnectorServer." + index + "." + key;
       
    59         if (!data.containsKey(counter)) {
       
    60             System.out.println("Test FAILED! Missing counter " + counter);
       
    61             throw new IllegalArgumentException("Test case failed");
       
    62         }
       
    63         String value = data.get(counter);
       
    64         if (key.equals("remoteAddress")) {
       
    65             checkAddress(value);
       
    66         } else if (!expectedValue.equals(value)) {
       
    67             System.out.println("Test FAILED! Invalid counter " +
       
    68                     counter + "=" + value);
       
    69             throw new IllegalArgumentException("Test case failed");
       
    70         }
       
    71         System.out.println("OK: " + counter + "=" + value);
       
    72     }
       
    73 
       
    74     public static void main(String args[]) throws Exception {
       
    75         String localAddress = ConnectorAddressLink.importFrom(0);
       
    76         Map<String, String> remoteData = ConnectorAddressLink.importRemoteFrom(0);
       
    77         final int testCase = Integer.parseInt(args[0]);
       
    78         switch (testCase) {
       
    79             case 1:
       
    80                 if (localAddress == null && remoteData.isEmpty()) {
       
    81                     System.out.println("Test PASSED! The OOTB management " +
       
    82                             "agent didn't publish any jvmstat counter.");
       
    83                 } else {
       
    84                     System.out.println("Test FAILED! The OOTB management " +
       
    85                             "agent unexpectedly published jvmstat counters.");
       
    86                     throw new IllegalArgumentException("Test case 1 failed");
       
    87                 }
       
    88                 break;
       
    89             case 2:
       
    90                 if (localAddress == null) {
       
    91                     System.out.println("Test FAILED! The OOTB management " +
       
    92                             "agent didn't publish the local connector.");
       
    93                     throw new IllegalArgumentException("Test case 2 failed");
       
    94                 }
       
    95                 checkAddress(localAddress);
       
    96                 if (!remoteData.isEmpty()) {
       
    97                     System.out.println("Test FAILED! The OOTB management " +
       
    98                             "agent shouldn't publish the remote connector.");
       
    99                     throw new IllegalArgumentException("Test case 2 failed");
       
   100                 }
       
   101                 System.out.println("Test PASSED! The OOTB management " +
       
   102                         "agent only publishes the local connector through " +
       
   103                         "a jvmstat counter.");
       
   104                 break;
       
   105             case 3:
       
   106                 if (localAddress == null) {
       
   107                     System.out.println("Test FAILED! The OOTB management " +
       
   108                             "agent didn't publish the local connector.");
       
   109                     throw new IllegalArgumentException("Test case 3 failed");
       
   110                 }
       
   111                 checkAddress(localAddress);
       
   112                 if (remoteData.isEmpty()) {
       
   113                     System.out.println("Test FAILED! The OOTB management " +
       
   114                             "agent didnn't publish the remote connector.");
       
   115                     throw new IllegalArgumentException("Test case 3 failed");
       
   116                 }
       
   117                 for (String key : remoteData.keySet()) {
       
   118                     if (!key.startsWith("sun.management.JMXConnectorServer.0.")) {
       
   119                         System.out.println("Test FAILED! The OOTB management " +
       
   120                                 "agent shouldn't publish anything which isn't " +
       
   121                                 "related to the remote connector.");
       
   122                         throw new IllegalArgumentException("Test case 3 failed");
       
   123                     }
       
   124                 }
       
   125                 checkKey(remoteData, 0, "remoteAddress", null);
       
   126                 checkKey(remoteData, 0, "authenticate", "false");
       
   127                 checkKey(remoteData, 0, "ssl", "false");
       
   128                 checkKey(remoteData, 0, "sslRegistry", "false");
       
   129                 checkKey(remoteData, 0, "sslNeedClientAuth", "false");
       
   130                 System.out.println("Test PASSED! The OOTB management " +
       
   131                         "agent publishes both the local and remote " +
       
   132                         "connector info through jvmstat counters.");
       
   133                 break;
       
   134             case 4:
       
   135                 if (localAddress != null || !remoteData.isEmpty()) {
       
   136                     System.out.println("Test FAILED! The OOTB management " +
       
   137                             "agent unexpectedly published jvmstat counters.");
       
   138                     throw new IllegalArgumentException("Test case 4 failed");
       
   139                 }
       
   140                 RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
       
   141                 String name = rt.getName();
       
   142                 System.out.println("name = " + name);
       
   143                 String vmid = name.substring(0, name.indexOf("@"));
       
   144                 System.out.println("vmid = " + vmid);
       
   145                 VirtualMachine vm = VirtualMachine.attach(vmid);
       
   146                 String agent = vm.getSystemProperties().getProperty("java.home") +
       
   147                         File.separator + "lib" + File.separator + "management-agent.jar";
       
   148                 vm.loadAgent(agent, "com.sun.management.jmxremote.port=0,com.sun.management.jmxremote.authenticate=false,com.sun.management.jmxremote.ssl=false");
       
   149                 vm.detach();
       
   150                 String localAddress2 = ConnectorAddressLink.importFrom(0);
       
   151                 if (localAddress2 == null) {
       
   152                     System.out.println("Test FAILED! The OOTB management " +
       
   153                             "agent didn't publish the local connector.");
       
   154                     throw new IllegalArgumentException("Test case 4 failed");
       
   155                 }
       
   156                 checkAddress(localAddress2);
       
   157                 Map<String, String> remoteData2 = ConnectorAddressLink.importRemoteFrom(0);
       
   158                 if (remoteData2.isEmpty()) {
       
   159                     System.out.println("Test FAILED! The OOTB management " +
       
   160                             "agent didnn't publish the remote connector.");
       
   161                     throw new IllegalArgumentException("Test case 4 failed");
       
   162                 }
       
   163                 for (String key : remoteData2.keySet()) {
       
   164                     if (!key.startsWith("sun.management.JMXConnectorServer.0.")) {
       
   165                         System.out.println("Test FAILED! The OOTB management " +
       
   166                                 "agent shouldn't publish anything which isn't " +
       
   167                                 "related to the remote connector.");
       
   168                         throw new IllegalArgumentException("Test case 4 failed");
       
   169                     }
       
   170                 }
       
   171                 checkKey(remoteData2, 0, "remoteAddress", null);
       
   172                 checkKey(remoteData2, 0, "authenticate", "false");
       
   173                 checkKey(remoteData2, 0, "ssl", "false");
       
   174                 checkKey(remoteData2, 0, "sslRegistry", "false");
       
   175                 checkKey(remoteData2, 0, "sslNeedClientAuth", "false");
       
   176                 System.out.println("Test PASSED! The OOTB management agent " +
       
   177                         "publishes both the local and remote connector " +
       
   178                         "info through jvmstat counters when the agent is " +
       
   179                         "loaded through the Attach API.");
       
   180         }
       
   181         System.out.println("Bye! Bye!");
       
   182     }
       
   183 }