author | emcmanus |
Wed, 03 Sep 2008 14:31:17 +0200 | |
changeset 1155 | a9a142fcf1b5 |
parent 2 | 90ce3da70b43 |
child 1247 | b4c26443dee5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2004 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 5012634 |
|
27 |
* @summary Test that JMX classes use fully-qualified class names |
|
28 |
* in MBeanNotificationInfo |
|
29 |
* @author Eamonn McManus |
|
30 |
* @run clean NotificationInfoTest |
|
31 |
* @run build NotificationInfoTest |
|
32 |
* @run main NotificationInfoTest |
|
33 |
*/ |
|
34 |
||
35 |
import java.io.*; |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
36 |
import java.lang.management.*; |
2 | 37 |
import java.lang.reflect.*; |
38 |
import java.net.*; |
|
39 |
import java.security.CodeSource; |
|
40 |
import java.util.*; |
|
41 |
import java.util.jar.*; |
|
42 |
import javax.management.*; |
|
43 |
import javax.management.relation.*; |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
44 |
import javax.management.remote.*; |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
45 |
import javax.management.remote.rmi.*; |
2 | 46 |
|
47 |
/* |
|
48 |
* This test finds all classes in the same code-base as the JMX |
|
49 |
* classes that look like Standard MBeans, and checks that if they are |
|
50 |
* NotificationBroadcasters they declare existent notification types. |
|
51 |
* A class looks like a Standard MBean if both Thing and ThingMBean |
|
52 |
* classes exist. So for example javax.management.timer.Timer looks |
|
53 |
* like a Standard MBean because javax.management.timer.TimerMBean |
|
54 |
* exists. Timer is instanceof NotificationBroadcaster, so we expect |
|
55 |
* that ((NotificationBroadcaster) timer).getNotificationInfo() will |
|
56 |
* return an array of MBeanNotificationInfo where each entry has a |
|
57 |
* getName() that names an existent Java class that is a Notification. |
|
58 |
* |
|
59 |
* An MBean is "suspicious" if it is a NotificationBroadcaster but its |
|
60 |
* MBeanNotificationInfo[] is empty. This is legal, but surprising. |
|
61 |
* |
|
62 |
* In order to call getNotificationInfo(), we need an instance of the |
|
63 |
* class. We attempt to make one by calling a public no-arg |
|
64 |
* constructor. But the "construct" method below can be extended to |
|
65 |
* construct specific MBean classes for which the no-arg constructor |
|
66 |
* doesn't exist. |
|
67 |
* |
|
68 |
* The test is obviously not exhaustive, but does catch the cases that |
|
69 |
* failed in 5012634. |
|
70 |
*/ |
|
71 |
public class NotificationInfoTest { |
|
72 |
// class or object names where the test failed |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
73 |
private static final Set<String> failed = new TreeSet<String>(); |
2 | 74 |
|
75 |
// class or object names where there were no MBeanNotificationInfo entries |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
76 |
private static final Set<String> suspicious = new TreeSet<String>(); |
2 | 77 |
|
78 |
public static void main(String[] args) throws Exception { |
|
79 |
System.out.println("Checking that all known MBeans that are " + |
|
80 |
"NotificationBroadcasters have sane " + |
|
81 |
"MBeanInfo.getNotifications()"); |
|
82 |
||
83 |
System.out.println("Checking platform MBeans..."); |
|
84 |
checkPlatformMBeans(); |
|
85 |
||
86 |
CodeSource cs = |
|
87 |
javax.management.MBeanServer.class.getProtectionDomain() |
|
88 |
.getCodeSource(); |
|
89 |
URL codeBase; |
|
90 |
if (cs == null) { |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
91 |
String javaHome = System.getProperty("java.home"); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
92 |
String[] candidates = {"/lib/rt.jar", "/classes/"}; |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
93 |
codeBase = null; |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
94 |
for (String candidate : candidates) { |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
95 |
File file = new File(javaHome + candidate); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
96 |
if (file.exists()) { |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
97 |
codeBase = file.toURI().toURL(); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
98 |
break; |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
99 |
} |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
100 |
} |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
101 |
if (codeBase == null) { |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
102 |
throw new Exception( |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
103 |
"Could not determine codeBase for java.home=" + javaHome); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
104 |
} |
2 | 105 |
} else |
106 |
codeBase = cs.getLocation(); |
|
107 |
||
108 |
System.out.println(); |
|
109 |
System.out.println("Looking for standard MBeans..."); |
|
110 |
String[] classes = findStandardMBeans(codeBase); |
|
111 |
||
112 |
System.out.println("Testing standard MBeans..."); |
|
113 |
for (int i = 0; i < classes.length; i++) { |
|
114 |
String name = classes[i]; |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
115 |
Class<?> c; |
2 | 116 |
try { |
117 |
c = Class.forName(name); |
|
118 |
} catch (Throwable e) { |
|
119 |
System.out.println(name + ": cannot load (not public?): " + e); |
|
120 |
continue; |
|
121 |
} |
|
122 |
if (!NotificationBroadcaster.class.isAssignableFrom(c)) { |
|
123 |
System.out.println(name + ": not a NotificationBroadcaster"); |
|
124 |
continue; |
|
125 |
} |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
126 |
if (Modifier.isAbstract(c.getModifiers())) { |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
127 |
System.out.println(name + ": abstract class"); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
128 |
continue; |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
129 |
} |
2 | 130 |
|
131 |
NotificationBroadcaster mbean; |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
132 |
Constructor<?> constr; |
2 | 133 |
try { |
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
134 |
constr = c.getConstructor(); |
2 | 135 |
} catch (Exception e) { |
136 |
System.out.println(name + ": no public no-arg constructor: " |
|
137 |
+ e); |
|
138 |
continue; |
|
139 |
} |
|
140 |
try { |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
141 |
mbean = (NotificationBroadcaster) constr.newInstance(); |
2 | 142 |
} catch (Exception e) { |
143 |
System.out.println(name + ": no-arg constructor failed: " + e); |
|
144 |
continue; |
|
145 |
} |
|
146 |
||
147 |
check(mbean); |
|
148 |
} |
|
149 |
||
150 |
System.out.println(); |
|
151 |
System.out.println("Testing some explicit cases..."); |
|
152 |
||
153 |
check(new RelationService(false)); |
|
154 |
/* |
|
155 |
We can't do this: |
|
156 |
check(new RequiredModelMBean()); |
|
157 |
because the Model MBean spec more or less forces us to use the |
|
158 |
names GENERIC and ATTRIBUTE_CHANGE for its standard notifs. |
|
159 |
*/ |
|
160 |
checkRMIConnectorServer(); |
|
161 |
||
162 |
System.out.println(); |
|
163 |
if (!suspicious.isEmpty()) |
|
164 |
System.out.println("SUSPICIOUS CLASSES: " + suspicious); |
|
165 |
||
166 |
if (failed.isEmpty()) |
|
167 |
System.out.println("TEST PASSED"); |
|
168 |
else { |
|
169 |
System.out.println("TEST FAILED: " + failed); |
|
170 |
System.exit(1); |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
private static void check(NotificationBroadcaster mbean) |
|
175 |
throws Exception { |
|
176 |
System.out.print(mbean.getClass().getName() + ": "); |
|
177 |
||
178 |
check(mbean.getClass().getName(), mbean.getNotificationInfo()); |
|
179 |
} |
|
180 |
||
181 |
private static void checkPlatformMBeans() throws Exception { |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
182 |
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
183 |
Set<ObjectName> mbeanNames = mbs.queryNames(null, null); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
184 |
for (ObjectName name : mbeanNames) { |
2 | 185 |
if (!mbs.isInstanceOf(name, |
186 |
NotificationBroadcaster.class.getName())) { |
|
187 |
System.out.println(name + ": not a NotificationBroadcaster"); |
|
188 |
} else { |
|
189 |
MBeanInfo mbi = mbs.getMBeanInfo(name); |
|
190 |
check(name.toString(), mbi.getNotifications()); |
|
191 |
} |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
private static void checkRMIConnectorServer() throws Exception { |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
196 |
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://"); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
197 |
RMIConnectorServer connector = new RMIConnectorServer(url, null); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
198 |
check(connector); |
2 | 199 |
} |
200 |
||
201 |
private static void check(String what, MBeanNotificationInfo[] mbnis) { |
|
202 |
System.out.print(what + ": checking notification info: "); |
|
203 |
||
204 |
if (mbnis.length == 0) { |
|
205 |
System.out.println("NONE (suspicious)"); |
|
206 |
suspicious.add(what); |
|
207 |
return; |
|
208 |
} |
|
209 |
||
210 |
// Each MBeanNotificationInfo.getName() should be an existent |
|
211 |
// Java class that is Notification or a subclass of it |
|
212 |
for (int j = 0; j < mbnis.length; j++) { |
|
213 |
String notifClassName = mbnis[j].getName(); |
|
214 |
Class notifClass; |
|
215 |
try { |
|
216 |
notifClass = Class.forName(notifClassName); |
|
217 |
} catch (Exception e) { |
|
218 |
System.out.print("FAILED(" + notifClassName + ": " + e + |
|
219 |
") "); |
|
220 |
failed.add(what); |
|
221 |
continue; |
|
222 |
} |
|
223 |
if (!Notification.class.isAssignableFrom(notifClass)) { |
|
224 |
System.out.print("FAILED(" + notifClassName + |
|
225 |
": not a Notification) "); |
|
226 |
failed.add(what); |
|
227 |
continue; |
|
228 |
} |
|
229 |
System.out.print("OK(" + notifClassName + ") "); |
|
230 |
} |
|
231 |
System.out.println(); |
|
232 |
} |
|
233 |
||
234 |
private static String[] findStandardMBeans(URL codeBase) |
|
235 |
throws Exception { |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
236 |
Set<String> names; |
2 | 237 |
if (codeBase.getProtocol().equalsIgnoreCase("file") |
238 |
&& codeBase.toString().endsWith("/")) |
|
239 |
names = findStandardMBeansFromDir(codeBase); |
|
240 |
else |
|
241 |
names = findStandardMBeansFromJar(codeBase); |
|
242 |
||
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
243 |
Set<String> standardMBeanNames = new TreeSet<String>(); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
244 |
for (String name : names) { |
2 | 245 |
if (name.endsWith("MBean")) { |
246 |
String prefix = name.substring(0, name.length() - 5); |
|
247 |
if (names.contains(prefix)) |
|
248 |
standardMBeanNames.add(prefix); |
|
249 |
} |
|
250 |
} |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
251 |
return standardMBeanNames.toArray(new String[0]); |
2 | 252 |
} |
253 |
||
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
254 |
private static Set<String> findStandardMBeansFromJar(URL codeBase) |
2 | 255 |
throws Exception { |
256 |
InputStream is = codeBase.openStream(); |
|
257 |
JarInputStream jis = new JarInputStream(is); |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
258 |
Set<String> names = new TreeSet<String>(); |
2 | 259 |
JarEntry entry; |
260 |
while ((entry = jis.getNextJarEntry()) != null) { |
|
261 |
String name = entry.getName(); |
|
262 |
if (!name.endsWith(".class")) |
|
263 |
continue; |
|
264 |
name = name.substring(0, name.length() - 6); |
|
265 |
name = name.replace('/', '.'); |
|
266 |
names.add(name); |
|
267 |
} |
|
268 |
return names; |
|
269 |
} |
|
270 |
||
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
271 |
private static Set<String> findStandardMBeansFromDir(URL codeBase) |
2 | 272 |
throws Exception { |
273 |
File dir = new File(new URI(codeBase.toString())); |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
274 |
Set<String> names = new TreeSet<String>(); |
2 | 275 |
scanDir(dir, "", names); |
276 |
return names; |
|
277 |
} |
|
278 |
||
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
279 |
private static void scanDir(File dir, String prefix, Set<String> names) |
2 | 280 |
throws Exception { |
281 |
File[] files = dir.listFiles(); |
|
282 |
if (files == null) |
|
283 |
return; |
|
284 |
for (int i = 0; i < files.length; i++) { |
|
285 |
File f = files[i]; |
|
286 |
String name = f.getName(); |
|
287 |
String p = (prefix.equals("")) ? name : prefix + "." + name; |
|
288 |
if (f.isDirectory()) |
|
289 |
scanDir(f, p, names); |
|
290 |
else if (name.endsWith(".class")) { |
|
291 |
p = p.substring(0, p.length() - 6); |
|
292 |
names.add(p); |
|
293 |
} |
|
294 |
} |
|
295 |
} |
|
296 |
} |