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