Path: blob/master/test/jdk/javax/management/mxbean/ThreadMXBeanTest.java
41152 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 630574626* @key randomness27* @summary Test that the null values returned by the ThreadMXBean work.28* @author Eamonn McManus29*30* @run clean ThreadMXBeanTest31* @run build ThreadMXBeanTest32* @run main ThreadMXBeanTest33*/3435import java.lang.management.*;36import java.util.*;37import javax.management.*;3839public class ThreadMXBeanTest {40public static void main(String[] args) throws Exception {41MBeanServer mbs = MBeanServerFactory.newMBeanServer();42ThreadMXBean tmb = ManagementFactory.getThreadMXBean();43StandardMBean smb = new StandardMBean(tmb, ThreadMXBean.class, true);44ObjectName on = new ObjectName("a:type=ThreadMXBean");45mbs.registerMBean(smb, on);46ThreadMXBean proxy = JMX.newMXBeanProxy(mbs, on, ThreadMXBean.class);47long[] ids1 = proxy.getAllThreadIds();4849// Add some random ids to the list so we'll get back null ThreadInfo50long[] ids2 = new long[ids1.length + 10];51System.arraycopy(ids1, 0, ids2, 0, ids1.length);52Random r = new Random();53for (int i = ids1.length; i < ids2.length; i++)54ids2[i] = Math.abs(r.nextLong());55// Following line produces an exception if null values not handled56ThreadInfo[] info = proxy.getThreadInfo(ids2);57boolean sawNull = false;58for (ThreadInfo ti : info) {59if (ti == null)60sawNull = true;61}62if (!sawNull)63throw new Exception("No null value in returned array");64System.out.println("TEST PASSED");65}66}676869