Path: blob/master/test/jdk/java/lang/management/ManagementFactory/MXBeanException.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 505831926* @summary Check if a RuntimeException is wrapped by RuntimeMBeanException27* only once.28*29* @requires vm.gc != "Z"30* @author Mandy Chung31*32* @build MXBeanException33* @run main MXBeanException34*/3536import java.lang.management.*;37import javax.management.*;38import java.util.*;39import static java.lang.management.ManagementFactory.*;4041public class MXBeanException {42private static MemoryPoolMXBean pool;4344public static void main(String[] argv) throws Exception {45List<MemoryPoolMXBean> pools =46ManagementFactory.getMemoryPoolMXBeans();47for (MemoryPoolMXBean p : pools) {48if (!p.isUsageThresholdSupported()) {49pool = p;50break;51}52}5354// check if UnsupportedOperationException is thrown55try {56pool.setUsageThreshold(1000);57throw new RuntimeException("TEST FAILED: " +58"UnsupportedOperationException not thrown");59} catch (UnsupportedOperationException e) {60// expected61}6263// check if UnsupportedOperationException is thrown64// when calling through MBeanServer65MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();66ObjectName on = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE +67",name=" + pool.getName());68Attribute att = new Attribute("UsageThreshold", 1000);69try {70mbs.setAttribute(on, att);71} catch (RuntimeMBeanException e) {72checkMBeanException(e);73} catch (RuntimeOperationsException e) {74checkMBeanException(e);75}7677// check if UnsupportedOperationException is thrown78// when calling through proxy7980MemoryPoolMXBean proxy = newPlatformMXBeanProxy(mbs,81on.toString(),82MemoryPoolMXBean.class);83try {84proxy.setUsageThreshold(1000);85throw new RuntimeException("TEST FAILED: " +86"UnsupportedOperationException not thrown via proxy");87} catch (UnsupportedOperationException e) {88// expected89}9091System.out.println("Test passed");92}9394private static void checkMBeanException(JMRuntimeException e) {95Throwable cause = e.getCause();96if (!(cause instanceof UnsupportedOperationException)) {97throw new RuntimeException("TEST FAILED: " + cause +98"is not UnsupportedOperationException");99}100}101}102103104