Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/UnregisterMBeanExceptionTest.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 632868226* @summary Test that MBeanServer.unregisterMBean() correctly unregisters27* the supplied MBean although DynamicMBean.getMBeanInfo() throws28* a runtime exception.29* @author Luis-Miguel Alventosa30* @run clean UnregisterMBeanExceptionTest31* @run build UnregisterMBeanExceptionTest32* @run main UnregisterMBeanExceptionTest33*/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.ObjectName;45import javax.management.ReflectionException;4647public class UnregisterMBeanExceptionTest {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() {80if (throwException)81throw new RuntimeException("UnregisterMBeanExceptionTest");82else83return new MBeanInfo(this.getClass().getName(), "Test",84null, null, null, null);85}8687public boolean throwException;88}8990public static void main(String[] args) throws Exception {9192// Instantiate the MBean server93//94System.out.println("Create the MBean server");95MBeanServer mbs = MBeanServerFactory.createMBeanServer();9697// Register the MBean98//99System.out.println("Create a TestDynamicMBean");100TestDynamicMBean obj = new TestDynamicMBean();101ObjectName n = new ObjectName("d:k=v");102System.out.println("Register a TestDynamicMBean");103mbs.registerMBean(obj, n);104obj.throwException = true;105System.out.println("Unregister a TestDynamicMBean");106try {107mbs.unregisterMBean(n);108} catch (Exception e) {109throw new IllegalArgumentException("Test failed", e);110}111boolean isRegistered = mbs.isRegistered(n);112System.out.println("Is MBean Registered? " + isRegistered);113114if (isRegistered) {115throw new IllegalArgumentException(116"Test failed: the MBean is still registered");117} else {118System.out.println("Test passed");119}120}121}122123124