aboutsummaryrefslogtreecommitdiff
path: root/test/sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
blob: 442f2b4c5bd944fff54c12810a94154e7bac712f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4981215
 * @summary Tests that the jvmstat counters published by the out-of-the-box
 *          management agent for the JMX connection details are correct.
 * @author Luis-Miguel Alventosa
 * @run clean JvmstatCountersTest
 * @run build JvmstatCountersTest
 * @run main/othervm/timeout=600 JvmstatCountersTest 1
 * @run main/othervm/timeout=600 -Dcom.sun.management.jmxremote JvmstatCountersTest 2
 * @run main/othervm/timeout=600 -Dcom.sun.management.jmxremote.port=0 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false JvmstatCountersTest 3
 * @run main/othervm/timeout=600 JvmstatCountersTest 4
 */

import java.io.*;
import java.lang.management.*;
import java.util.*;
import javax.management.*;
import javax.management.remote.*;
import com.sun.tools.attach.*;
import sun.management.ConnectorAddressLink;

public class JvmstatCountersTest {

    public static void checkAddress(String address) throws IOException {
        System.out.println("Address = " + address);
        JMXServiceURL url = new JMXServiceURL(address);
        JMXConnector jmxc = JMXConnectorFactory.connect(url);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        System.out.println("MBean Count = " + mbsc.getMBeanCount());
    }

    public static void checkKey(Map<String, String> data, int index,
            String key, String expectedValue) throws Exception {
        String counter = "sun.management.JMXConnectorServer." + index + "." + key;
        if (!data.containsKey(counter)) {
            System.out.println("Test FAILED! Missing counter " + counter);
            throw new IllegalArgumentException("Test case failed");
        }
        String value = data.get(counter);
        if (key.equals("remoteAddress")) {
            checkAddress(value);
        } else if (!expectedValue.equals(value)) {
            System.out.println("Test FAILED! Invalid counter " +
                    counter + "=" + value);
            throw new IllegalArgumentException("Test case failed");
        }
        System.out.println("OK: " + counter + "=" + value);
    }

    public static void main(String args[]) throws Exception {
        String localAddress = ConnectorAddressLink.importFrom(0);
        Map<String, String> remoteData = ConnectorAddressLink.importRemoteFrom(0);
        final int testCase = Integer.parseInt(args[0]);
        switch (testCase) {
            case 1:
                if (localAddress == null && remoteData.isEmpty()) {
                    System.out.println("Test PASSED! The OOTB management " +
                            "agent didn't publish any jvmstat counter.");
                } else {
                    System.out.println("Test FAILED! The OOTB management " +
                            "agent unexpectedly published jvmstat counters.");
                    throw new IllegalArgumentException("Test case 1 failed");
                }
                break;
            case 2:
                if (localAddress == null) {
                    System.out.println("Test FAILED! The OOTB management " +
                            "agent didn't publish the local connector.");
                    throw new IllegalArgumentException("Test case 2 failed");
                }
                checkAddress(localAddress);
                if (!remoteData.isEmpty()) {
                    System.out.println("Test FAILED! The OOTB management " +
                            "agent shouldn't publish the remote connector.");
                    throw new IllegalArgumentException("Test case 2 failed");
                }
                System.out.println("Test PASSED! The OOTB management " +
                        "agent only publishes the local connector through " +
                        "a jvmstat counter.");
                break;
            case 3:
                if (localAddress == null) {
                    System.out.println("Test FAILED! The OOTB management " +
                            "agent didn't publish the local connector.");
                    throw new IllegalArgumentException("Test case 3 failed");
                }
                checkAddress(localAddress);
                if (remoteData.isEmpty()) {
                    System.out.println("Test FAILED! The OOTB management " +
                            "agent didnn't publish the remote connector.");
                    throw new IllegalArgumentException("Test case 3 failed");
                }
                for (String key : remoteData.keySet()) {
                    if (!key.startsWith("sun.management.JMXConnectorServer.0.")) {
                        System.out.println("Test FAILED! The OOTB management " +
                                "agent shouldn't publish anything which isn't " +
                                "related to the remote connector.");
                        throw new IllegalArgumentException("Test case 3 failed");
                    }
                }
                checkKey(remoteData, 0, "remoteAddress", null);
                checkKey(remoteData, 0, "authenticate", "false");
                checkKey(remoteData, 0, "ssl", "false");
                checkKey(remoteData, 0, "sslRegistry", "false");
                checkKey(remoteData, 0, "sslNeedClientAuth", "false");
                System.out.println("Test PASSED! The OOTB management " +
                        "agent publishes both the local and remote " +
                        "connector info through jvmstat counters.");
                break;
            case 4:
                if (localAddress != null || !remoteData.isEmpty()) {
                    System.out.println("Test FAILED! The OOTB management " +
                            "agent unexpectedly published jvmstat counters.");
                    throw new IllegalArgumentException("Test case 4 failed");
                }
                RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
                String name = rt.getName();
                System.out.println("name = " + name);
                String vmid = name.substring(0, name.indexOf("@"));
                System.out.println("vmid = " + vmid);
                VirtualMachine vm = VirtualMachine.attach(vmid);
                String agent = vm.getSystemProperties().getProperty("java.home") +
                        File.separator + "lib" + File.separator + "management-agent.jar";
                vm.loadAgent(agent, "com.sun.management.jmxremote.port=0,com.sun.management.jmxremote.authenticate=false,com.sun.management.jmxremote.ssl=false");
                vm.detach();
                String localAddress2 = ConnectorAddressLink.importFrom(0);
                if (localAddress2 == null) {
                    System.out.println("Test FAILED! The OOTB management " +
                            "agent didn't publish the local connector.");
                    throw new IllegalArgumentException("Test case 4 failed");
                }
                checkAddress(localAddress2);
                Map<String, String> remoteData2 = ConnectorAddressLink.importRemoteFrom(0);
                if (remoteData2.isEmpty()) {
                    System.out.println("Test FAILED! The OOTB management " +
                            "agent didnn't publish the remote connector.");
                    throw new IllegalArgumentException("Test case 4 failed");
                }
                for (String key : remoteData2.keySet()) {
                    if (!key.startsWith("sun.management.JMXConnectorServer.0.")) {
                        System.out.println("Test FAILED! The OOTB management " +
                                "agent shouldn't publish anything which isn't " +
                                "related to the remote connector.");
                        throw new IllegalArgumentException("Test case 4 failed");
                    }
                }
                checkKey(remoteData2, 0, "remoteAddress", null);
                checkKey(remoteData2, 0, "authenticate", "false");
                checkKey(remoteData2, 0, "ssl", "false");
                checkKey(remoteData2, 0, "sslRegistry", "false");
                checkKey(remoteData2, 0, "sslNeedClientAuth", "false");
                System.out.println("Test PASSED! The OOTB management agent " +
                        "publishes both the local and remote connector " +
                        "info through jvmstat counters when the agent is " +
                        "loaded through the Attach API.");
        }
        System.out.println("Bye! Bye!");
    }
}