Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ThreadMXBean/ThreadInfoArray.java
38821 views
/*1* Copyright (c) 2004, 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 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* @build ThreadInfoArray33* @run main ThreadInfoArray34*/3536import java.lang.management.*;37import javax.management.*;38import static java.lang.management.ManagementFactory.*;3940public class ThreadInfoArray {41public static void main(String[] argv) throws Exception {42MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();43ObjectName on = new ObjectName(THREAD_MXBEAN_NAME);4445ThreadMXBean mbean = ManagementFactory.getThreadMXBean();46ThreadMXBean proxy = newPlatformMXBeanProxy(mbs,47on.toString(),48ThreadMXBean.class);4950checkNullElement(mbean, proxy, mbs, on);51checkEmptyArray(mbean, proxy, mbs, on);52System.out.println("Test passed");53}5455private static void checkNullElement(ThreadMXBean mbean, ThreadMXBean proxy,56MBeanServer mbs, ObjectName on)57throws Exception {58System.out.println("--- Check null element");59// ID for a new thread60long [] ids = {new Thread().getId()};61// direct call62ThreadInfo[] tinfos = mbean.getThreadInfo(ids);6364if (tinfos[0] != null) {65throw new RuntimeException("TEST FAILED: " +66"Expected to have a null element");67}6869// call getThreadInfo through MBeanServer70Object[] params = {ids};71String[] sigs = {"[J"};72Object[] result = (Object[]) mbs.invoke(on, "getThreadInfo", params, sigs);7374if (result[0] != null) {75throw new RuntimeException("TEST FAILED: " +76"Expected to have a null element via MBeanServer");77}7879// call getThreadInfo through proxy80tinfos = proxy.getThreadInfo(ids);81if (tinfos[0] != null) {82throw new RuntimeException("TEST FAILED: " +83"Expected to have a null element");84}85System.out.println("--- PASSED");86}8788private static void checkEmptyArray(ThreadMXBean mbean, ThreadMXBean proxy,89MBeanServer mbs, ObjectName on)90throws Exception {91System.out.println("--- Check empty TID array");9293long[] ids = new long[0];94// direct call95assertEmptyArray(mbean.getThreadInfo(ids), "Expected empty ThreadInfo array");96assertEmptyArray(mbean.getThreadInfo(ids, 1), "Expected empty ThreadInfo array");97assertEmptyArray(mbean.getThreadInfo(ids, true, true), "Expected empty ThreadInfo array");9899// call getThreadInfo through MBeanServer100assertEmptyArray(101(Object[]) mbs.invoke(102on, "getThreadInfo", new Object[]{ids}, new String[]{"[J"}103),104"Expected empty ThreadInfo array via MBeanServer"105);106assertEmptyArray(107(Object[]) mbs.invoke(108on, "getThreadInfo", new Object[]{ids, 1},109new String[]{"[J", "int"}110),111"Expected empty ThreadInfo array via MBeanServer"112);113assertEmptyArray(114(Object[]) mbs.invoke(115on, "getThreadInfo", new Object[]{ids, true, true},116new String[]{"[J", "boolean", "boolean"}117),118"Expected empty ThreadInfo array via MBeanServer"119);120121// call getThreadInfo through proxy122assertEmptyArray(proxy.getThreadInfo(ids), "Expected empty ThreadInfo array");123assertEmptyArray(proxy.getThreadInfo(ids, 1), "Expected empty ThreadInfo array");124assertEmptyArray(proxy.getThreadInfo(ids, true, true), "Expected empty ThreadInfo array");125System.out.println("--- PASSED");126}127128private static void assertEmptyArray(Object[] arr, String message) throws Exception {129if (arr.length > 0) {130throw new RuntimeException("TEST FAILED: " + message);131}132}133}134135136