Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/ThreadInfoArray.java
41152 views
/*1* Copyright (c) 2004, 2016, 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 5058327 807436826* @summary Tests the correct behaviour of getThreadInfo(long[]) for non-existent27* thread IDs and the empty thread id array.28*29* @author Mandy Chung30* @author Jaroslav Bachorik31*32* @modules jdk.management33* @build ThreadInfoArray34* @run main ThreadInfoArray35*/3637import java.lang.management.*;38import javax.management.*;39import static java.lang.management.ManagementFactory.*;4041public class ThreadInfoArray {42public static void main(String[] argv) throws Exception {43MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();44ObjectName on = new ObjectName(THREAD_MXBEAN_NAME);4546ThreadMXBean mbean = ManagementFactory.getThreadMXBean();47ThreadMXBean proxy = newPlatformMXBeanProxy(mbs,48on.toString(),49ThreadMXBean.class);5051checkNullElement(mbean, proxy, mbs, on);52checkEmptyArray(mbean, proxy, mbs, on);53System.out.println("Test passed");54}5556private static void checkNullElement(ThreadMXBean mbean, ThreadMXBean proxy,57MBeanServer mbs, ObjectName on)58throws Exception {59System.out.println("--- Check null element");60// ID for a new thread61long [] ids = {new Thread().getId()};62// direct call63ThreadInfo[] tinfos = mbean.getThreadInfo(ids);6465if (tinfos[0] != null) {66throw new RuntimeException("TEST FAILED: " +67"Expected to have a null element");68}6970// call getThreadInfo through MBeanServer71Object[] params = {ids};72String[] sigs = {"[J"};73Object[] result = (Object[]) mbs.invoke(on, "getThreadInfo", params, sigs);7475if (result[0] != null) {76throw new RuntimeException("TEST FAILED: " +77"Expected to have a null element via MBeanServer");78}7980// call getThreadInfo through proxy81tinfos = proxy.getThreadInfo(ids);82if (tinfos[0] != null) {83throw new RuntimeException("TEST FAILED: " +84"Expected to have a null element");85}86System.out.println("--- PASSED");87}8889private static void checkEmptyArray(ThreadMXBean mbean, ThreadMXBean proxy,90MBeanServer mbs, ObjectName on)91throws Exception {92System.out.println("--- Check empty TID array");9394long[] ids = new long[0];95// direct call96assertEmptyArray(mbean.getThreadInfo(ids), "Expected empty ThreadInfo array");97assertEmptyArray(mbean.getThreadInfo(ids, 1), "Expected empty ThreadInfo array");98assertEmptyArray(mbean.getThreadInfo(ids, true, true), "Expected empty ThreadInfo array");99100// call getThreadInfo through MBeanServer101assertEmptyArray(102(Object[]) mbs.invoke(103on, "getThreadInfo", new Object[]{ids}, new String[]{"[J"}104),105"Expected empty ThreadInfo array via MBeanServer"106);107assertEmptyArray(108(Object[]) mbs.invoke(109on, "getThreadInfo", new Object[]{ids, 1},110new String[]{"[J", "int"}111),112"Expected empty ThreadInfo array via MBeanServer"113);114assertEmptyArray(115(Object[]) mbs.invoke(116on, "getThreadInfo", new Object[]{ids, true, true},117new String[]{"[J", "boolean", "boolean"}118),119"Expected empty ThreadInfo array via MBeanServer"120);121122// call getThreadInfo through proxy123assertEmptyArray(proxy.getThreadInfo(ids), "Expected empty ThreadInfo array");124assertEmptyArray(proxy.getThreadInfo(ids, 1), "Expected empty ThreadInfo array");125assertEmptyArray(proxy.getThreadInfo(ids, true, true), "Expected empty ThreadInfo array");126System.out.println("--- PASSED");127}128129private static void assertEmptyArray(Object[] arr, String message) throws Exception {130if (arr.length > 0) {131throw new RuntimeException("TEST FAILED: " + message);132}133}134}135136137