Path: blob/master/test/jdk/javax/management/Introspector/UnregisterMBeanExceptionTest.java
51504 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 632868226* @summary Test that MBeanServer.unregisterMBean() correctly unregisters27* the supplied MBean although DynamicMBean.getMBeanInfo() throws28* a runtime exception.29* @author Luis-Miguel Alventosa30*31* @run clean UnregisterMBeanExceptionTest32* @run build UnregisterMBeanExceptionTest33* @run main UnregisterMBeanExceptionTest34*/3536import javax.management.Attribute;37import javax.management.AttributeList;38import javax.management.AttributeNotFoundException;39import javax.management.DynamicMBean;40import javax.management.InvalidAttributeValueException;41import javax.management.MBeanException;42import javax.management.MBeanInfo;43import javax.management.MBeanServer;44import javax.management.MBeanServerFactory;45import javax.management.ObjectName;46import javax.management.ReflectionException;4748public class UnregisterMBeanExceptionTest {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() {81if (throwException)82throw new RuntimeException("UnregisterMBeanExceptionTest");83else84return new MBeanInfo(this.getClass().getName(), "Test",85null, null, null, null);86}8788public boolean throwException;89}9091public static void main(String[] args) throws Exception {9293// Instantiate the MBean server94//95System.out.println("Create the MBean server");96MBeanServer mbs = MBeanServerFactory.createMBeanServer();9798// Register the MBean99//100System.out.println("Create a TestDynamicMBean");101TestDynamicMBean obj = new TestDynamicMBean();102ObjectName n = new ObjectName("d:k=v");103System.out.println("Register a TestDynamicMBean");104mbs.registerMBean(obj, n);105obj.throwException = true;106System.out.println("Unregister a TestDynamicMBean");107try {108mbs.unregisterMBean(n);109} catch (Exception e) {110throw new IllegalArgumentException("Test failed", e);111}112boolean isRegistered = mbs.isRegistered(n);113System.out.println("Is MBean Registered? " + isRegistered);114115if (isRegistered) {116throw new IllegalArgumentException(117"Test failed: the MBean is still registered");118} else {119System.out.println("Test passed");120}121}122}123124125