Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ManagementFactory/MXBeanException.java
38828 views
/*1* Copyright (c) 2004, 2005, 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 505831926* @summary Check if a RuntimeException is wrapped by RuntimeMBeanException27* only once.28*29* @author Mandy Chung30*31* @build MXBeanException32* @run main MXBeanException33*/3435import java.lang.management.*;36import javax.management.*;37import java.util.*;38import static java.lang.management.ManagementFactory.*;3940public class MXBeanException {41private static MemoryPoolMXBean pool;4243public static void main(String[] argv) throws Exception {44List<MemoryPoolMXBean> pools =45ManagementFactory.getMemoryPoolMXBeans();46for (MemoryPoolMXBean p : pools) {47if (!p.isUsageThresholdSupported()) {48pool = p;49break;50}51}5253// check if UnsupportedOperationException is thrown54try {55pool.setUsageThreshold(1000);56throw new RuntimeException("TEST FAILED: " +57"UnsupportedOperationException not thrown");58} catch (UnsupportedOperationException e) {59// expected60}6162// check if UnsupportedOperationException is thrown63// when calling through MBeanServer64MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();65ObjectName on = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE +66",name=" + pool.getName());67Attribute att = new Attribute("UsageThreshold", 1000);68try {69mbs.setAttribute(on, att);70} catch (RuntimeMBeanException e) {71checkMBeanException(e);72} catch (RuntimeOperationsException e) {73checkMBeanException(e);74}7576// check if UnsupportedOperationException is thrown77// when calling through proxy7879MemoryPoolMXBean proxy = newPlatformMXBeanProxy(mbs,80on.toString(),81MemoryPoolMXBean.class);82try {83proxy.setUsageThreshold(1000);84throw new RuntimeException("TEST FAILED: " +85"UnsupportedOperationException not thrown via proxy");86} catch (UnsupportedOperationException e) {87// expected88}8990System.out.println("Test passed");91}9293private static void checkMBeanException(JMRuntimeException e) {94Throwable cause = e.getCause();95if (!(cause instanceof UnsupportedOperationException)) {96throw new RuntimeException("TEST FAILED: " + cause +97"is not UnsupportedOperationException");98}99}100}101102103