Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/MaxDepthForThreadInfoTest.java
41152 views
/*1* Copyright (c) 2017, 2020, 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 818500326* @build ThreadDump27* @run main MaxDepthForThreadInfoTest28* @summary verifies the functionality of ThreadMXBean.dumpAllThreads29* and ThreadMXBean.getThreadInfo with maxDepth argument30*/3132import java.lang.management.ManagementFactory;33import java.lang.management.ThreadInfo;34import java.lang.management.ThreadMXBean;35363738public class MaxDepthForThreadInfoTest {394041public static void main(String[] Args) {4243ThreadMXBean tmxb = ManagementFactory.getThreadMXBean();4445long[] threadIds = tmxb.getAllThreadIds();4647ThreadInfo[] tinfos = tmxb.getThreadInfo(threadIds, true, true, 0);48for (ThreadInfo ti : tinfos) {49if (ti != null && ti.getStackTrace().length > 0) {50ThreadDump.printThreadInfo(ti);51throw new RuntimeException("more than requested " +52"number of frames dumped");53}54}5556tinfos = tmxb.getThreadInfo(threadIds, true, true, 3);57for (ThreadInfo ti : tinfos) {58if (ti != null && ti.getStackTrace().length > 3) {59ThreadDump.printThreadInfo(ti);60throw new RuntimeException("more than requested " +61"number of frames dumped");62}63}6465try {66tmxb.getThreadInfo(threadIds, true, true, -1);67throw new RuntimeException("Didn't throw IllegalArgumentException " +68"for negative maxdepth value");69} catch (IllegalArgumentException e) {70System.out.println("Throwed IllegalArgumentException as expected");71}7273tinfos = tmxb.dumpAllThreads(true, true, 0);74for (ThreadInfo ti : tinfos) {75if (ti.getStackTrace().length > 0) {76ThreadDump.printThreadInfo(ti);77throw new RuntimeException("more than requested " +78"number of frames dumped");79}80}81tinfos = tmxb.dumpAllThreads(true, true, 2);82for (ThreadInfo ti : tinfos) {83if (ti.getStackTrace().length > 2) {84ThreadDump.printThreadInfo(ti);85throw new RuntimeException("more than requested " +86"number of frames dumped");87}88}8990try {91tmxb.dumpAllThreads(true, true, -1);92throw new RuntimeException("Didn't throw IllegalArgumentException " +93"for negative maxdepth value");94} catch (IllegalArgumentException e) {95System.out.println("Throwed IllegalArgumentException as expected");96}9798System.out.println("Test passed");99}100}101102103