Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/GetMBeanInfoExceptionTest.java
38838 views
/*1* Copyright (c) 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 628144626* @summary Test that the exception thrown by DynamicMBean.getMBeanInfo()27* keeps the init cause.28* @author Luis-Miguel Alventosa29* @run clean GetMBeanInfoExceptionTest30* @run build GetMBeanInfoExceptionTest31* @run main GetMBeanInfoExceptionTest32*/3334import javax.management.Attribute;35import javax.management.AttributeList;36import javax.management.AttributeNotFoundException;37import javax.management.DynamicMBean;38import javax.management.InvalidAttributeValueException;39import javax.management.MBeanException;40import javax.management.MBeanInfo;41import javax.management.MBeanServer;42import javax.management.MBeanServerFactory;43import javax.management.NotCompliantMBeanException;44import javax.management.ObjectName;45import javax.management.ReflectionException;4647public class GetMBeanInfoExceptionTest {4849public static class TestDynamicMBean implements DynamicMBean {5051public Object getAttribute(String attribute) throws52AttributeNotFoundException,53MBeanException,54ReflectionException {55return null;56}5758public void setAttribute(Attribute attribute) throws59AttributeNotFoundException,60InvalidAttributeValueException,61MBeanException,62ReflectionException {63}6465public AttributeList getAttributes(String[] attributes) {66return null;67}6869public AttributeList setAttributes(AttributeList attributes) {70return null;71}7273public Object invoke(String op, Object params[], String sign[]) throws74MBeanException,75ReflectionException {76return null;77}7879public MBeanInfo getMBeanInfo() {80throw new IllegalArgumentException("GetMBeanInfoExceptionTest");81}82}8384public static void main(String[] args) throws Exception {8586int error = 0;8788// Instantiate the MBean server89//90System.out.println("Create the MBean server");91MBeanServer mbs = MBeanServerFactory.createMBeanServer();9293// Register the MBean94//95System.out.println("Create a TestDynamicMBean");96TestDynamicMBean obj = new TestDynamicMBean();97ObjectName n = new ObjectName("d:k=v");98try {99mbs.registerMBean(obj, n);100System.out.println("Didn't get expected NotCompliantMBeanException");101error++;102} catch (NotCompliantMBeanException e) {103boolean found = false;104Throwable t = e.getCause();105while (t != null) {106if (t instanceof IllegalArgumentException &&107"GetMBeanInfoExceptionTest".equals(t.getMessage())) {108found = true;109}110t = t.getCause();111}112if (found) {113System.out.println("Found expected IllegalArgumentException");114} else {115System.out.println("Didn't find expected IllegalArgumentException");116error++;117}118} catch (Exception e) {119System.out.println("Got " + e.getClass().getName() +120"instead of expected NotCompliantMBeanException");121error++;122}123124if (error > 0) {125System.out.println("Test failed");126throw new IllegalArgumentException("Test failed");127} else {128System.out.println("Test passed");129}130}131}132133134