author | lfoltan |
Tue, 20 Feb 2018 07:46:40 -0500 | |
changeset 49026 | 844bf1deff1a |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
27034
diff
changeset
|
2 |
* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 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 |
|
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
36511
diff
changeset
|
30 |
* @modules java.management.rmi |
2 | 31 |
* @run clean NotificationInfoTest |
32 |
* @run build NotificationInfoTest |
|
33 |
* @run main NotificationInfoTest |
|
34 |
*/ |
|
35 |
||
36 |
import java.io.*; |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
37 |
import java.lang.management.*; |
2 | 38 |
import java.lang.reflect.*; |
39 |
import java.net.*; |
|
36511 | 40 |
import java.nio.file.FileSystem; |
41 |
import java.nio.file.FileSystems; |
|
42 |
import java.nio.file.Files; |
|
43 |
import java.nio.file.Path; |
|
44 |
import java.nio.file.Paths; |
|
2 | 45 |
import java.util.*; |
36511 | 46 |
import java.util.stream.Collectors; |
2 | 47 |
import javax.management.*; |
48 |
import javax.management.relation.*; |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
49 |
import javax.management.remote.*; |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
50 |
import javax.management.remote.rmi.*; |
2 | 51 |
|
52 |
/* |
|
53 |
* This test finds all classes in the same code-base as the JMX |
|
54 |
* classes that look like Standard MBeans, and checks that if they are |
|
55 |
* NotificationBroadcasters they declare existent notification types. |
|
56 |
* A class looks like a Standard MBean if both Thing and ThingMBean |
|
57 |
* classes exist. So for example javax.management.timer.Timer looks |
|
58 |
* like a Standard MBean because javax.management.timer.TimerMBean |
|
59 |
* exists. Timer is instanceof NotificationBroadcaster, so we expect |
|
60 |
* that ((NotificationBroadcaster) timer).getNotificationInfo() will |
|
61 |
* return an array of MBeanNotificationInfo where each entry has a |
|
62 |
* getName() that names an existent Java class that is a Notification. |
|
63 |
* |
|
64 |
* An MBean is "suspicious" if it is a NotificationBroadcaster but its |
|
65 |
* MBeanNotificationInfo[] is empty. This is legal, but surprising. |
|
66 |
* |
|
67 |
* In order to call getNotificationInfo(), we need an instance of the |
|
68 |
* class. We attempt to make one by calling a public no-arg |
|
69 |
* constructor. But the "construct" method below can be extended to |
|
70 |
* construct specific MBean classes for which the no-arg constructor |
|
71 |
* doesn't exist. |
|
72 |
* |
|
73 |
* The test is obviously not exhaustive, but does catch the cases that |
|
74 |
* failed in 5012634. |
|
75 |
*/ |
|
76 |
public class NotificationInfoTest { |
|
77 |
// 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
|
78 |
private static final Set<String> failed = new TreeSet<String>(); |
2 | 79 |
|
80 |
// 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
|
81 |
private static final Set<String> suspicious = new TreeSet<String>(); |
2 | 82 |
|
83 |
public static void main(String[] args) throws Exception { |
|
84 |
System.out.println("Checking that all known MBeans that are " + |
|
85 |
"NotificationBroadcasters have sane " + |
|
86 |
"MBeanInfo.getNotifications()"); |
|
87 |
||
88 |
System.out.println("Checking platform MBeans..."); |
|
89 |
checkPlatformMBeans(); |
|
90 |
||
36511 | 91 |
URL codeBase; |
92 |
String home = System.getProperty("java.home"); |
|
93 |
Path classFile = Paths.get(home, "modules", "java.management"); |
|
94 |
if (Files.isDirectory(classFile)) { |
|
95 |
codeBase = classFile.toUri().toURL(); |
|
96 |
} else { |
|
97 |
codeBase = URI.create("jrt:/java.management").toURL(); |
|
27034
ac2dd06dba4a
8060166: javax/management/MBeanInfo/NotificationInfoTest.java fails with modular image
alanb
parents:
5506
diff
changeset
|
98 |
} |
2 | 99 |
|
100 |
System.out.println(); |
|
101 |
System.out.println("Looking for standard MBeans..."); |
|
102 |
String[] classes = findStandardMBeans(codeBase); |
|
103 |
||
104 |
System.out.println("Testing standard MBeans..."); |
|
105 |
for (int i = 0; i < classes.length; i++) { |
|
106 |
String name = classes[i]; |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
107 |
Class<?> c; |
2 | 108 |
try { |
109 |
c = Class.forName(name); |
|
110 |
} catch (Throwable e) { |
|
111 |
System.out.println(name + ": cannot load (not public?): " + e); |
|
112 |
continue; |
|
113 |
} |
|
114 |
if (!NotificationBroadcaster.class.isAssignableFrom(c)) { |
|
115 |
System.out.println(name + ": not a NotificationBroadcaster"); |
|
116 |
continue; |
|
117 |
} |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
118 |
if (Modifier.isAbstract(c.getModifiers())) { |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
119 |
System.out.println(name + ": abstract class"); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
120 |
continue; |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
121 |
} |
2 | 122 |
|
123 |
NotificationBroadcaster mbean; |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
124 |
Constructor<?> constr; |
2 | 125 |
try { |
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
126 |
constr = c.getConstructor(); |
2 | 127 |
} catch (Exception e) { |
128 |
System.out.println(name + ": no public no-arg constructor: " |
|
129 |
+ e); |
|
130 |
continue; |
|
131 |
} |
|
132 |
try { |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
133 |
mbean = (NotificationBroadcaster) constr.newInstance(); |
2 | 134 |
} catch (Exception e) { |
135 |
System.out.println(name + ": no-arg constructor failed: " + e); |
|
136 |
continue; |
|
137 |
} |
|
138 |
||
139 |
check(mbean); |
|
140 |
} |
|
141 |
||
142 |
System.out.println(); |
|
143 |
System.out.println("Testing some explicit cases..."); |
|
144 |
||
145 |
check(new RelationService(false)); |
|
146 |
/* |
|
147 |
We can't do this: |
|
148 |
check(new RequiredModelMBean()); |
|
149 |
because the Model MBean spec more or less forces us to use the |
|
150 |
names GENERIC and ATTRIBUTE_CHANGE for its standard notifs. |
|
151 |
*/ |
|
152 |
checkRMIConnectorServer(); |
|
153 |
||
154 |
System.out.println(); |
|
155 |
if (!suspicious.isEmpty()) |
|
156 |
System.out.println("SUSPICIOUS CLASSES: " + suspicious); |
|
157 |
||
158 |
if (failed.isEmpty()) |
|
159 |
System.out.println("TEST PASSED"); |
|
160 |
else { |
|
161 |
System.out.println("TEST FAILED: " + failed); |
|
162 |
System.exit(1); |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
private static void check(NotificationBroadcaster mbean) |
|
167 |
throws Exception { |
|
168 |
System.out.print(mbean.getClass().getName() + ": "); |
|
169 |
||
170 |
check(mbean.getClass().getName(), mbean.getNotificationInfo()); |
|
171 |
} |
|
172 |
||
173 |
private static void checkPlatformMBeans() throws Exception { |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
174 |
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
175 |
Set<ObjectName> mbeanNames = mbs.queryNames(null, null); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
176 |
for (ObjectName name : mbeanNames) { |
2 | 177 |
if (!mbs.isInstanceOf(name, |
178 |
NotificationBroadcaster.class.getName())) { |
|
179 |
System.out.println(name + ": not a NotificationBroadcaster"); |
|
180 |
} else { |
|
181 |
MBeanInfo mbi = mbs.getMBeanInfo(name); |
|
182 |
check(name.toString(), mbi.getNotifications()); |
|
183 |
} |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
private static void checkRMIConnectorServer() throws Exception { |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
188 |
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://"); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
189 |
RMIConnectorServer connector = new RMIConnectorServer(url, null); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
190 |
check(connector); |
2 | 191 |
} |
192 |
||
193 |
private static void check(String what, MBeanNotificationInfo[] mbnis) { |
|
194 |
System.out.print(what + ": checking notification info: "); |
|
195 |
||
196 |
if (mbnis.length == 0) { |
|
197 |
System.out.println("NONE (suspicious)"); |
|
198 |
suspicious.add(what); |
|
199 |
return; |
|
200 |
} |
|
201 |
||
202 |
// Each MBeanNotificationInfo.getName() should be an existent |
|
203 |
// Java class that is Notification or a subclass of it |
|
204 |
for (int j = 0; j < mbnis.length; j++) { |
|
205 |
String notifClassName = mbnis[j].getName(); |
|
206 |
Class notifClass; |
|
207 |
try { |
|
208 |
notifClass = Class.forName(notifClassName); |
|
209 |
} catch (Exception e) { |
|
210 |
System.out.print("FAILED(" + notifClassName + ": " + e + |
|
211 |
") "); |
|
212 |
failed.add(what); |
|
213 |
continue; |
|
214 |
} |
|
215 |
if (!Notification.class.isAssignableFrom(notifClass)) { |
|
216 |
System.out.print("FAILED(" + notifClassName + |
|
217 |
": not a Notification) "); |
|
218 |
failed.add(what); |
|
219 |
continue; |
|
220 |
} |
|
221 |
System.out.print("OK(" + notifClassName + ") "); |
|
222 |
} |
|
223 |
System.out.println(); |
|
224 |
} |
|
225 |
||
36511 | 226 |
private static String[] findStandardMBeans(URL codeBase) throws Exception { |
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
227 |
Set<String> names; |
36511 | 228 |
if (codeBase.getProtocol().equalsIgnoreCase("jrt")) { |
229 |
names = findStandardMBeansFromRuntime(); |
|
230 |
} else { |
|
2 | 231 |
names = findStandardMBeansFromDir(codeBase); |
36511 | 232 |
} |
2 | 233 |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
234 |
Set<String> standardMBeanNames = new TreeSet<String>(); |
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
235 |
for (String name : names) { |
2 | 236 |
if (name.endsWith("MBean")) { |
237 |
String prefix = name.substring(0, name.length() - 5); |
|
238 |
if (names.contains(prefix)) |
|
239 |
standardMBeanNames.add(prefix); |
|
240 |
} |
|
241 |
} |
|
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
242 |
return standardMBeanNames.toArray(new String[0]); |
2 | 243 |
} |
244 |
||
36511 | 245 |
private static Set<String> findStandardMBeansFromRuntime() throws Exception { |
246 |
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/")); |
|
247 |
Path modules = fs.getPath("/modules"); |
|
248 |
return Files.walk(modules) |
|
249 |
.filter(path -> path.toString().endsWith(".class")) |
|
250 |
.map(path -> path.subpath(2, path.getNameCount())) |
|
251 |
.map(Path::toString) |
|
252 |
.map(s -> s.substring(0, s.length() - 6)) // drop .class |
|
253 |
.filter(s -> !s.equals("module-info")) |
|
254 |
.map(s -> s.replace('/', '.')) |
|
255 |
.collect(Collectors.toSet()); |
|
2 | 256 |
} |
257 |
||
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
258 |
private static Set<String> findStandardMBeansFromDir(URL codeBase) |
2 | 259 |
throws Exception { |
260 |
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
|
261 |
Set<String> names = new TreeSet<String>(); |
2 | 262 |
scanDir(dir, "", names); |
263 |
return names; |
|
264 |
} |
|
265 |
||
1155
a9a142fcf1b5
6744132: Spurious failures from test/javax/management/MBeanInfo/NotificationInfoTest.java
emcmanus
parents:
2
diff
changeset
|
266 |
private static void scanDir(File dir, String prefix, Set<String> names) |
2 | 267 |
throws Exception { |
268 |
File[] files = dir.listFiles(); |
|
269 |
if (files == null) |
|
270 |
return; |
|
271 |
for (int i = 0; i < files.length; i++) { |
|
272 |
File f = files[i]; |
|
273 |
String name = f.getName(); |
|
274 |
String p = (prefix.equals("")) ? name : prefix + "." + name; |
|
275 |
if (f.isDirectory()) |
|
276 |
scanDir(f, p, names); |
|
277 |
else if (name.endsWith(".class")) { |
|
278 |
p = p.substring(0, p.length() - 6); |
|
279 |
names.add(p); |
|
280 |
} |
|
281 |
} |
|
282 |
} |
|
283 |
} |