1 /*
2  * Copyright (c) 2005, 2008, Oracle and/or its affiliates. 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 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 6305746
27  * @summary Test that the null values returned by the ThreadMXBean work.
28  * @author Eamonn McManus
29  * @run clean ThreadMXBeanTest
30  * @run build ThreadMXBeanTest
31  * @run main ThreadMXBeanTest
32  * @key randomness
33  */
34 
35 import java.lang.management.*;
36 import java.util.*;
37 import javax.management.*;
38 
39 public class ThreadMXBeanTest {
main(String[] args)40     public static void main(String[] args) throws Exception {
41         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
42         ThreadMXBean tmb = ManagementFactory.getThreadMXBean();
43         StandardMBean smb = new StandardMBean(tmb, ThreadMXBean.class, true);
44         ObjectName on = new ObjectName("a:type=ThreadMXBean");
45         mbs.registerMBean(smb, on);
46         ThreadMXBean proxy = JMX.newMXBeanProxy(mbs, on, ThreadMXBean.class);
47         long[] ids1 = proxy.getAllThreadIds();
48 
49         // Add some random ids to the list so we'll get back null ThreadInfo
50         long[] ids2 = new long[ids1.length + 10];
51         System.arraycopy(ids1, 0, ids2, 0, ids1.length);
52         Random r = new Random();
53         for (int i = ids1.length; i < ids2.length; i++)
54             ids2[i] = Math.abs(r.nextLong());
55         // Following line produces an exception if null values not handled
56         ThreadInfo[] info = proxy.getThreadInfo(ids2);
57         boolean sawNull = false;
58         for (ThreadInfo ti : info) {
59             if (ti == null)
60                 sawNull = true;
61         }
62         if (!sawNull)
63             throw new Exception("No null value in returned array");
64         System.out.println("TEST PASSED");
65     }
66 }
67