Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/lang/management/MXBean/MXBeanBehavior.java
+++ new/test/java/lang/management/MXBean/MXBeanBehavior.java
1 1 /*
2 - * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2 + * Copyright (c) 2005, 2013 Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23
24 24 /*
25 25 * @test
26 26 * @bug 6320211
27 27 * @summary Check that java.lang.management MXBeans have the same behavior
28 28 * as user MXBeans
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
29 29 * @author Eamonn McManus
30 30 * @run main/othervm MXBeanBehavior
31 31 */
32 32
33 33 import java.lang.management.*;
34 34 import java.lang.reflect.*;
35 35 import java.util.*;
36 36 import javax.management.*;
37 37
38 38 public class MXBeanBehavior {
39 + // Exclude list: list of platform MBeans that are not MXBeans
40 + public static final HashSet<String> excludeList = new HashSet<>(
41 + Arrays.asList("com.sun.management:type=DiagnosticCommand"));
42 +
39 43 public static void main(String[] args) throws Exception {
40 44 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
41 45
42 46 /* Test that all the MBeans in the java.* and com.sun.management*
43 47 domains are MXBeans with the appropriate behavior. */
44 48 Set<ObjectName> names = mbs.queryNames(new ObjectName("java.*:*"),
45 49 null);
46 50 names.addAll(mbs.queryNames(new ObjectName("com.sun.management*:*"),
47 51 null));
48 52 for (ObjectName name : names)
49 53 test(mbs, name);
50 54
51 55 /* Now do some rudimentary testing of inter-MXBean references.
52 56 It should be possible for a user MXBean to return e.g. the
53 57 CompilationMXBean from the platform from an attribute of
54 58 type CompilationMXBean, and have the MXBean infrastructure
55 59 map this into that MXBean's standard ObjectName. It should
56 60 also be possible for a proxy for this user MXBean to have
57 61 this attribute's value mapped back into a CompilationMXBean
58 62 instance, which however will be another proxy rather than
59 63 the original object. Finally, it should be possible to set
60 64 the attribute in the user's MXBean through a proxy, giving
61 65 the real CompilationMXBean as an argument, and have this be
62 66 translated into that MXBean's standard ObjectName. The
63 67 user's MXBean will receive a proxy in this case, though we
64 68 don't check that. */
65 69 ObjectName refName = new ObjectName("d:type=CompilationRef");
66 70 mbs.registerMBean(new CompilationImpl(), refName);
67 71 CompilationRefMXBean refProxy =
68 72 JMX.newMXBeanProxy(mbs, refName, CompilationRefMXBean.class);
69 73 refProxy.getCompilationMXBean();
70 74 refProxy.setCompilationMXBean(ManagementFactory.getCompilationMXBean());
71 75 ObjectName on =
72 76 (ObjectName) mbs.getAttribute(refName, "CompilationMXBean");
73 77 checkEqual(on, new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME),
74 78 "Referenced object name");
75 79 mbs.setAttribute(refName, new Attribute("CompilationMXBean", on));
76 80
77 81 System.out.println("TEST PASSED");
78 82 }
79 83
80 84 /* Check the behavior of this MXBean to ensure that it conforms to
81 85 what is expected of all MXBeans as detailed in
82 86 javax.management.MXBean. Its MBeanInfo should have a
83 87 Descriptor with the fields mxbean and interfaceClassName, and
84 88 furthermore we know that our implementation sets immutableInfo
↓ open down ↓ |
36 lines elided |
↑ open up ↑ |
85 89 here. Each attribute should have Descriptor with the fields
86 90 openType and originalType that have appropriate values. We
87 91 don't currently check operations though the same considerations
88 92 would apply there. (If the MBeanInfo and MBeanAttributeInfo
89 93 tests pass we can reasonably suppose that this MXBean will
90 94 behave the same as all other MXBeans, so MBeanOperationInfo,
91 95 MBeanNotificationInfo, and MBeanConstructorInfo will be covered
92 96 by generic MXBean tests.
93 97 */
94 98 private static void test(MBeanServer mbs, ObjectName name) throws Exception {
99 + if(excludeList.contains(name.getCanonicalName())) {
100 + // Skipping not MXBean objects.
101 + return;
102 + }
95 103 System.out.println("Testing: " + name);
96 104
97 105 MBeanInfo mbi = mbs.getMBeanInfo(name);
98 106 Descriptor mbid = mbi.getDescriptor();
99 107 Object[] values = mbid.getFieldValues("immutableInfo",
100 108 "interfaceClassName",
101 109 "mxbean");
102 - checkEqual(values[0], "true", name + " immutableInfo field");
103 - checkEqual(values[2], "true", name + " mxbean field");
110 + checkEqual(values[0], "true", name + " immutableInfo field");
111 + checkEqual(values[2], "true", name + " mxbean field");
104 112 String interfaceClassName = (String) values[1];
105 113 if (!mbs.isInstanceOf(name, interfaceClassName)) {
106 114 throw new RuntimeException(name + " not instance of " +
107 115 interfaceClassName);
108 116 }
109 117 Class interfaceClass = Class.forName(interfaceClassName);
110 118 for (MBeanAttributeInfo mbai : mbi.getAttributes()) {
111 119 Descriptor mbaid = mbai.getDescriptor();
112 120 Object[] avalues = mbaid.getFieldValues("openType",
113 121 "originalType");
114 122 if (avalues[0] == null || avalues[1] == null) {
115 123 throw new RuntimeException("Null attribute descriptor fields: " +
116 124 Arrays.toString(avalues));
117 125 }
118 126 if (mbai.isReadable()) {
119 127 String mname = (mbai.isIs() ? "is" : "get") + mbai.getName();
120 128 Method m = interfaceClass.getMethod(mname);
121 129 Type t = m.getGenericReturnType();
122 130 String ret =
123 131 (t instanceof Class) ? ((Class) t).getName() : t.toString();
124 132 if (!ret.equals(avalues[1])) {
125 133 final String msg =
126 134 name + " attribute " + mbai.getName() + " has wrong " +
127 135 "originalType: " + avalues[1] + " vs " + ret;
128 136 throw new RuntimeException(msg);
129 137 }
130 138 }
131 139 }
132 140 }
133 141
134 142 private static void checkEqual(Object x, Object y, String what) {
135 143 final boolean eq;
136 144 if (x == y)
137 145 eq = true;
138 146 else if (x == null)
139 147 eq = false;
140 148 else
141 149 eq = x.equals(y);
142 150 if (!eq)
143 151 throw new RuntimeException(what + " should be " + y + ", is " + x);
144 152 }
145 153
146 154 public static interface CompilationRefMXBean {
147 155 public CompilationMXBean getCompilationMXBean();
148 156 public void setCompilationMXBean(CompilationMXBean mxb);
149 157 }
150 158
151 159 public static class CompilationImpl implements CompilationRefMXBean {
152 160 public CompilationMXBean getCompilationMXBean() {
153 161 return ManagementFactory.getCompilationMXBean();
154 162 }
155 163
156 164 public void setCompilationMXBean(CompilationMXBean mxb) {
157 165 if (mxb == ManagementFactory.getCompilationMXBean())
158 166 return;
159 167 MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler)
160 168 Proxy.getInvocationHandler(mxb);
161 169 ObjectName expectedName;
162 170 try {
163 171 expectedName =
164 172 new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);
165 173 } catch (MalformedObjectNameException e) {
166 174 throw new RuntimeException(e);
167 175 }
168 176 checkEqual(mbsih.getObjectName(), expectedName,
169 177 "Proxy name in setCompilationMXBean");
170 178 }
171 179 }
172 180 }
↓ open down ↓ |
59 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX