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 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.
22 */
23
24 /*
25 * @test
26 * @bug 4982289 8198253
27 * @summary Test ThreadInfo.from to return a valid
28 * ThreadInfo object. Or throw exception if
29 * the input CompositeData is invalid.
30 * @author Mandy Chung
31 *
32 * @build ThreadInfoCompositeData OpenTypeConverter
33 * @run main ThreadInfoCompositeData
34 */
35
36
37 import javax.management.openmbean.*;
38 import java.lang.management.LockInfo;
39 import java.lang.management.MonitorInfo;
40 import java.lang.management.ThreadInfo;
41 import java.util.Arrays;
42 import java.util.Objects;
43 import java.util.stream.Stream;
44
45 public class ThreadInfoCompositeData {
46 private static String lockClassName = "myClass";
47 private static int lockIdentityHashCode = 123456;
48 private static String lockName = lockClassName + '@' +
49 Integer.toHexString(lockIdentityHashCode);
50 private static LockInfo lockInfo =
51 new LockInfo(lockClassName, lockIdentityHashCode);
52
53 public static void main(String[] argv) throws Exception {
54 // A valid CompositeData is passed to ThreadInfo
55 createGoodCompositeData();
56 // A valid CompositeData for JDK 5 ThreadInfo
57 // is passed to ThreadInfo
58 createV5ThreadInfo();
59 // ThreadInfo of version N can accept lockedMonitors of version >= N
60 withNewMonitorInfoCompositeData();
61
62 // An invalid CompositeData is passed to ThreadInfo.from()
63 badNameCompositeData();
64 badTypeCompositeData();
65 badMissingCompositeData();
66 withV5StackTraceCompositeData();
67 withInvalidMonitorInfoCompositeData();
68 System.out.println("Test passed");
69 }
70
71 public static void createGoodCompositeData() throws Exception {
72 CompositeData cd = Factory.makeThreadInfoCompositeData();
73 ThreadInfo info = ThreadInfo.from(cd);
74 checkThreadInfo(info);
75 }
76
77 /*
78 * An invalid CompositeData with JDK 9 attributes but missing JDK 6 attributes
79 */
80 public static void badMissingCompositeData() throws Exception {
81 CompositeData cd = Factory.makeCompositeDataMissingV6();
82 try {
83 ThreadInfo info = ThreadInfo.from(cd);
84 throw new RuntimeException("IllegalArgumentException not thrown");
85 } catch (IllegalArgumentException e) {}
86 }
87
88 static final StackTraceElement STE =
89 new StackTraceElement("FooClass", "getFoo", "Foo.java", 100);
90
91
92 /*
93 * Current version of ThreadInfo but an older version of StackTraceElement
94 */
95 public static void withV5StackTraceCompositeData() throws Exception {
96 CompositeData cd = Factory.makeThreadInfoWithV5StackTrace();
97 try {
98 ThreadInfo info = ThreadInfo.from(cd);
99 throw new RuntimeException("IllegalArgumentException not thrown");
100 } catch (IllegalArgumentException e) {}
101 }
102
103 /*
104 * Current version of ThreadInfo but an older version of MonitorInfo
105 * and the value of "lockedStackFrame" attribute is null.
106 */
107 public static void withInvalidMonitorInfoCompositeData() throws Exception {
108 CompositeData cd = Factory.makeThreadInfoWithIncompatibleMonitorInfo();
109
110 // verify MonitorInfo is valid
111 CompositeData[] monitors = (CompositeData[])cd.get("lockedMonitors");
112 CompositeData ste = (CompositeData)monitors[0].get("lockedStackFrame");
113 if (((Integer)monitors[0].get("lockedStackDepth")) >= 0 || ste != null) {
114 throw new RuntimeException("Expected negative stack depth and null stack frame");
115 }
116 MonitorInfo minfo = MonitorInfo.from(monitors[0]);
117 checkLockInfo(minfo);
118 if (minfo.getLockedStackFrame() != null) {
119 throw new RuntimeException("Expected null stack frame");
120 }
121
122 try {
123 ThreadInfo info = ThreadInfo.from(cd);
124 throw new RuntimeException("IllegalArgumentException not thrown");
125 } catch (IllegalArgumentException e) {}
126 }
127
128 /*
129 * ThreadInfo of version N can accept lockedMonitors of version >= N
130 */
131 public static void withNewMonitorInfoCompositeData() throws Exception {
132 CompositeData cd = Factory.makeThreadInfoWithNewMonitorInfo();
133 ThreadInfo info = ThreadInfo.from(cd);
134 checkThreadInfo(info);
135 }
136
137 /*
138 * Test CompositeData representing JDK 5 ThreadInfo
139 */
140 public static void createV5ThreadInfo() throws Exception {
141 CompositeData cd = Factory.makeThreadInfoV5CompositeData();
142 ThreadInfo info = ThreadInfo.from(cd);
143 checkThreadInfoV5(info);
144 }
145
146 static void checkThreadInfoV5(ThreadInfo info) {
147 Object[] values = Factory.VALUES;
148
149 if (info.getThreadId() != ((Long) values[THREAD_ID]).longValue()) {
150 throw new RuntimeException("Thread Id = " + info.getThreadId() +
151 " expected = " + values[THREAD_ID]);
152 }
153 if (!info.getThreadName().equals(values[THREAD_NAME])) {
154 throw new RuntimeException("Thread Name = " +
155 info.getThreadName() + " expected = " + values[THREAD_NAME]);
156 }
157 if (info.getThreadState() != Thread.State.RUNNABLE) {
158 throw new RuntimeException("Thread Name = " +
159 info.getThreadName() + " expected = " + Thread.State.RUNNABLE);
160 }
161 if (info.getBlockedTime() != ((Long) values[BLOCKED_TIME]).longValue()) {
162 throw new RuntimeException("blocked time = " +
163 info.getBlockedTime() +
164 " expected = " + values[BLOCKED_TIME]);
165 }
245 s2.getFileName() + " expected = " + s1.getFileName());
246 }
247 if (s1.getLineNumber() != s2.getLineNumber()) {
248 throw new RuntimeException("Line number = " +
249 s2.getLineNumber() + " expected = " + s1.getLineNumber());
250 }
251 }
252
253 private static void checkLockInfo(LockInfo li) {
254 if (!li.getClassName().equals(lockInfo.getClassName())) {
255 throw new RuntimeException("Class Name = " +
256 li.getClassName() + " expected = " + lockInfo.getClassName());
257 }
258 if (li.getIdentityHashCode() != lockInfo.getIdentityHashCode()) {
259 throw new RuntimeException("Class Name = " +
260 li.getIdentityHashCode() + " expected = " +
261 lockInfo.getIdentityHashCode());
262 }
263 }
264
265 public static void badNameCompositeData() throws Exception {
266 CompositeData cd = Factory.makeCompositeDataWithBadNames();
267 try {
268 ThreadInfo info = ThreadInfo.from(cd);
269 throw new RuntimeException("IllegalArgumentException not thrown");
270 } catch (IllegalArgumentException e) { }
271 }
272
273 public static void badTypeCompositeData() throws Exception {
274 CompositeData cd = Factory.makeCompositeDataWithBadTypes();
275
276 try {
277 ThreadInfo info = ThreadInfo.from(cd);
278 throw new RuntimeException("IllegalArgumentException not thrown");
279 } catch (IllegalArgumentException e) { }
280 }
281
282 private static final int THREAD_ID = 0;
283 private static final int THREAD_NAME = 1;
284 private static final int THREAD_STATE = 2;
285 private static final int BLOCKED_TIME = 3;
286 private static final int BLOCKED_COUNT = 4;
287 private static final int WAITED_TIME = 5;
288 private static final int WAITED_COUNT = 6;
289 private static final int LOCK_NAME = 7;
290 private static final int LOCK_OWNER_ID = 8;
291 private static final int LOCK_OWNER_NAME = 9;
292 private static final int STACK_TRACE = 10;
293 private static final int SUSPENDED = 11;
294 private static final int IN_NATIVE = 12;
295 // JDK 6 ThreadInfo attributes
296 private static final int LOCK_INFO = 13;
297 private static final int LOCKED_MONITORS = 14;
298 private static final int LOCKED_SYNCS = 15;
299 // JDK 9 ThreadInfo attributes
300 private static final int DAEMON = 16;
301 private static final int PRIORITY = 17;
302
303 static class Factory {
304
305 static final CompositeType STE_COMPOSITE_TYPE;
306 static final CompositeType LOCK_INFO_COMPOSITE_TYPE;
307 static final CompositeType MONITOR_INFO_COMPOSITE_TYPE;
308 static final ArrayType STE_ARRAY_COMPOSITE_TYPE;
309 static final ArrayType LOCK_INFO_ARRAY_COMPOSITE_TYPE;
310 static final ArrayType MONITOR_INFO_ARRAY_COMPOSITE_TYPE;
311
312 static {
313 CompositeType steCType = null;
314 CompositeType lockInfoCType = null;
315 CompositeType monitorInfoCType = null;
316 ArrayType steArrayType = null;
317 ArrayType lockInfoArrayType = null;
318 ArrayType monitorInfoArrayType = null;
319
320 try {
321 steCType = (CompositeType) OpenTypeConverter.toOpenType(StackTraceElement.class);
322 lockInfoCType = (CompositeType) OpenTypeConverter.toOpenType(LockInfo.class);
323 monitorInfoCType = (CompositeType) OpenTypeConverter.toOpenType(MonitorInfo.class);
|
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 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.
22 */
23
24 /*
25 * @test
26 * @bug 4982289 8198253
27 * @summary Test ThreadInfo.from to return a valid
28 * ThreadInfo object. Or throw exception if
29 * the input CompositeData is invalid.
30 * @author Mandy Chung
31 *
32 * @modules java.management/sun.management
33 * @build ThreadInfoCompositeData OpenTypeConverter
34 * @run testng/othervm ThreadInfoCompositeData
35 */
36
37
38 import javax.management.openmbean.*;
39 import java.lang.management.LockInfo;
40 import java.lang.management.MonitorInfo;
41 import java.lang.management.ThreadInfo;
42 import java.util.Arrays;
43 import java.util.Objects;
44 import java.util.stream.Stream;
45
46 import org.testng.annotations.Test;
47 import static org.testng.Assert.*;
48
49 public class ThreadInfoCompositeData {
50 private static String lockClassName = "myClass";
51 private static int lockIdentityHashCode = 123456;
52 private static String lockName = lockClassName + '@' +
53 Integer.toHexString(lockIdentityHashCode);
54 private static LockInfo lockInfo =
55 new LockInfo(lockClassName, lockIdentityHashCode);
56
57 @Test
58 public static void createGoodCompositeData() throws Exception {
59 CompositeData cd = Factory.makeThreadInfoCompositeData();
60 ThreadInfo info = ThreadInfo.from(cd);
61 checkThreadInfo(info);
62 }
63
64 /*
65 * An invalid CompositeData with JDK 9 attributes but missing JDK 6 attributes
66 */
67 @Test
68 public static void badMissingCompositeData() throws Exception {
69 CompositeData cd = Factory.makeCompositeDataMissingV6();
70 try {
71 ThreadInfo info = ThreadInfo.from(cd);
72 throw new RuntimeException("IllegalArgumentException not thrown");
73 } catch (IllegalArgumentException e) {}
74 }
75
76 static final StackTraceElement STE =
77 new StackTraceElement("FooClass", "getFoo", "Foo.java", 100);
78
79
80 /*
81 * Current version of ThreadInfo but an older version of StackTraceElement
82 */
83 @Test
84 public static void withV5StackTraceCompositeData() throws Exception {
85 CompositeData cd = Factory.makeThreadInfoWithV5StackTrace();
86 try {
87 ThreadInfo info = ThreadInfo.from(cd);
88 throw new RuntimeException("IllegalArgumentException not thrown");
89 } catch (IllegalArgumentException e) {}
90 }
91
92 /*
93 * Current version of ThreadInfo but an older version of MonitorInfo
94 * and the value of "lockedStackFrame" attribute is null.
95 */
96 @Test
97 public static void withInvalidMonitorInfoCompositeData() throws Exception {
98 CompositeData cd = Factory.makeThreadInfoWithIncompatibleMonitorInfo();
99
100 // verify MonitorInfo is valid
101 CompositeData[] monitors = (CompositeData[])cd.get("lockedMonitors");
102 CompositeData ste = (CompositeData)monitors[0].get("lockedStackFrame");
103 if (((Integer)monitors[0].get("lockedStackDepth")) >= 0 || ste != null) {
104 throw new RuntimeException("Expected negative stack depth and null stack frame");
105 }
106 MonitorInfo minfo = MonitorInfo.from(monitors[0]);
107 checkLockInfo(minfo);
108 if (minfo.getLockedStackFrame() != null) {
109 throw new RuntimeException("Expected null stack frame");
110 }
111
112 try {
113 ThreadInfo info = ThreadInfo.from(cd);
114 throw new RuntimeException("IllegalArgumentException not thrown");
115 } catch (IllegalArgumentException e) {}
116 }
117
118 /*
119 * ThreadInfo of version N can accept lockedMonitors of version >= N
120 */
121 @Test
122 public static void withNewMonitorInfoCompositeData() throws Exception {
123 CompositeData cd = Factory.makeThreadInfoWithNewMonitorInfo();
124 ThreadInfo info = ThreadInfo.from(cd);
125 checkThreadInfo(info);
126 }
127
128 /*
129 * Test CompositeData representing JDK 5 ThreadInfo
130 */
131 @Test
132 public static void createV5ThreadInfo() throws Exception {
133 CompositeData cd = Factory.makeThreadInfoV5CompositeData();
134 ThreadInfo info = ThreadInfo.from(cd);
135 checkThreadInfoV5(info);
136 }
137
138 /*
139 * Test ThreadInfoCompositeData.toCompositeData
140 */
141 @Test
142 public static void internalToCompositeData() throws Exception {
143 CompositeData cd = Factory.makeThreadInfoCompositeData();
144 ThreadInfo info = ThreadInfo.from(cd);
145 cd = sun.management.ThreadInfoCompositeData.toCompositeData(info);
146 info = ThreadInfo.from(cd);
147 checkThreadInfo(info);
148 }
149
150 static void checkThreadInfoV5(ThreadInfo info) {
151 Object[] values = Factory.VALUES;
152
153 if (info.getThreadId() != ((Long) values[THREAD_ID]).longValue()) {
154 throw new RuntimeException("Thread Id = " + info.getThreadId() +
155 " expected = " + values[THREAD_ID]);
156 }
157 if (!info.getThreadName().equals(values[THREAD_NAME])) {
158 throw new RuntimeException("Thread Name = " +
159 info.getThreadName() + " expected = " + values[THREAD_NAME]);
160 }
161 if (info.getThreadState() != Thread.State.RUNNABLE) {
162 throw new RuntimeException("Thread Name = " +
163 info.getThreadName() + " expected = " + Thread.State.RUNNABLE);
164 }
165 if (info.getBlockedTime() != ((Long) values[BLOCKED_TIME]).longValue()) {
166 throw new RuntimeException("blocked time = " +
167 info.getBlockedTime() +
168 " expected = " + values[BLOCKED_TIME]);
169 }
249 s2.getFileName() + " expected = " + s1.getFileName());
250 }
251 if (s1.getLineNumber() != s2.getLineNumber()) {
252 throw new RuntimeException("Line number = " +
253 s2.getLineNumber() + " expected = " + s1.getLineNumber());
254 }
255 }
256
257 private static void checkLockInfo(LockInfo li) {
258 if (!li.getClassName().equals(lockInfo.getClassName())) {
259 throw new RuntimeException("Class Name = " +
260 li.getClassName() + " expected = " + lockInfo.getClassName());
261 }
262 if (li.getIdentityHashCode() != lockInfo.getIdentityHashCode()) {
263 throw new RuntimeException("Class Name = " +
264 li.getIdentityHashCode() + " expected = " +
265 lockInfo.getIdentityHashCode());
266 }
267 }
268
269 @Test
270 public static void badNameCompositeData() throws Exception {
271 CompositeData cd = Factory.makeCompositeDataWithBadNames();
272 try {
273 ThreadInfo info = ThreadInfo.from(cd);
274 throw new RuntimeException("IllegalArgumentException not thrown");
275 } catch (IllegalArgumentException e) { }
276 }
277
278 @Test
279 public static void badTypeCompositeData() throws Exception {
280 CompositeData cd = Factory.makeCompositeDataWithBadTypes();
281
282 try {
283 ThreadInfo info = ThreadInfo.from(cd);
284 throw new RuntimeException("IllegalArgumentException not thrown");
285 } catch (IllegalArgumentException e) { }
286 }
287
288 private static final int THREAD_ID = 0;
289 private static final int THREAD_NAME = 1;
290 private static final int THREAD_STATE = 2;
291 private static final int BLOCKED_TIME = 3;
292 private static final int BLOCKED_COUNT = 4;
293 private static final int WAITED_TIME = 5;
294 private static final int WAITED_COUNT = 6;
295 private static final int LOCK_NAME = 7;
296 private static final int LOCK_OWNER_ID = 8;
297 private static final int LOCK_OWNER_NAME = 9;
298 private static final int STACK_TRACE = 10;
299 private static final int SUSPENDED = 11;
300 private static final int IN_NATIVE = 12;
301 // JDK 6 ThreadInfo attributes
302 private static final int LOCK_INFO = 13;
303 private static final int LOCKED_MONITORS = 14;
304 private static final int LOCKED_SYNCS = 15;
305 // JDK 9 ThreadInfo attributes
306 private static final int DAEMON = 16;
307 private static final int PRIORITY = 17;
308
309 private static class Factory {
310
311 static final CompositeType STE_COMPOSITE_TYPE;
312 static final CompositeType LOCK_INFO_COMPOSITE_TYPE;
313 static final CompositeType MONITOR_INFO_COMPOSITE_TYPE;
314 static final ArrayType STE_ARRAY_COMPOSITE_TYPE;
315 static final ArrayType LOCK_INFO_ARRAY_COMPOSITE_TYPE;
316 static final ArrayType MONITOR_INFO_ARRAY_COMPOSITE_TYPE;
317
318 static {
319 CompositeType steCType = null;
320 CompositeType lockInfoCType = null;
321 CompositeType monitorInfoCType = null;
322 ArrayType steArrayType = null;
323 ArrayType lockInfoArrayType = null;
324 ArrayType monitorInfoArrayType = null;
325
326 try {
327 steCType = (CompositeType) OpenTypeConverter.toOpenType(StackTraceElement.class);
328 lockInfoCType = (CompositeType) OpenTypeConverter.toOpenType(LockInfo.class);
329 monitorInfoCType = (CompositeType) OpenTypeConverter.toOpenType(MonitorInfo.class);
|