Path: blob/master/test/jdk/javax/management/Introspector/GetMBeanInfoExceptionTest.java
41149 views
/*1* Copyright (c) 2005, 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 628144626* @summary Test that the exception thrown by DynamicMBean.getMBeanInfo()27* keeps the init cause.28* @author Luis-Miguel Alventosa29*30* @run clean GetMBeanInfoExceptionTest31* @run build GetMBeanInfoExceptionTest32* @run main GetMBeanInfoExceptionTest33*/3435import javax.management.Attribute;36import javax.management.AttributeList;37import javax.management.AttributeNotFoundException;38import javax.management.DynamicMBean;39import javax.management.InvalidAttributeValueException;40import javax.management.MBeanException;41import javax.management.MBeanInfo;42import javax.management.MBeanServer;43import javax.management.MBeanServerFactory;44import javax.management.NotCompliantMBeanException;45import javax.management.ObjectName;46import javax.management.ReflectionException;4748public class GetMBeanInfoExceptionTest {4950public static class TestDynamicMBean implements DynamicMBean {5152public Object getAttribute(String attribute) throws53AttributeNotFoundException,54MBeanException,55ReflectionException {56return null;57}5859public void setAttribute(Attribute attribute) throws60AttributeNotFoundException,61InvalidAttributeValueException,62MBeanException,63ReflectionException {64}6566public AttributeList getAttributes(String[] attributes) {67return null;68}6970public AttributeList setAttributes(AttributeList attributes) {71return null;72}7374public Object invoke(String op, Object params[], String sign[]) throws75MBeanException,76ReflectionException {77return null;78}7980public MBeanInfo getMBeanInfo() {81throw new IllegalArgumentException("GetMBeanInfoExceptionTest");82}83}8485public static void main(String[] args) throws Exception {8687int error = 0;8889// Instantiate the MBean server90//91System.out.println("Create the MBean server");92MBeanServer mbs = MBeanServerFactory.createMBeanServer();9394// Register the MBean95//96System.out.println("Create a TestDynamicMBean");97TestDynamicMBean obj = new TestDynamicMBean();98ObjectName n = new ObjectName("d:k=v");99try {100mbs.registerMBean(obj, n);101System.out.println("Didn't get expected NotCompliantMBeanException");102error++;103} catch (NotCompliantMBeanException e) {104boolean found = false;105Throwable t = e.getCause();106while (t != null) {107if (t instanceof IllegalArgumentException &&108"GetMBeanInfoExceptionTest".equals(t.getMessage())) {109found = true;110}111t = t.getCause();112}113if (found) {114System.out.println("Found expected IllegalArgumentException");115} else {116System.out.println("Didn't find expected IllegalArgumentException");117error++;118}119} catch (Exception e) {120System.out.println("Got " + e.getClass().getName() +121"instead of expected NotCompliantMBeanException");122error++;123}124125if (error > 0) {126System.out.println("Test failed");127throw new IllegalArgumentException("Test failed");128} else {129System.out.println("Test passed");130}131}132}133134135