Path: blob/master/test/jdk/javax/management/MBeanServer/MBeanExceptionTest.java
41152 views
/*1* Copyright (c) 2004, 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 5035217 676617326* @summary Test that MBean's RuntimeException is wrapped in27* RuntimeMBeanException and (for Standard MBeans) that checked exceptions28* are wrapped in MBeanException29* @author Eamonn McManus30*31* @run main MBeanExceptionTest32*/3334import java.util.Collections;35import java.util.Set;36import javax.management.Attribute;37import javax.management.AttributeList;38import javax.management.DynamicMBean;39import javax.management.MBeanException;40import javax.management.MBeanInfo;41import javax.management.MBeanServer;42import javax.management.MBeanServerFactory;43import javax.management.ObjectName;44import javax.management.RuntimeMBeanException;45import javax.management.StandardMBean;4647public class MBeanExceptionTest {48public static void main(String[] args) throws Exception {49System.out.println("Test that if an MBean throws RuntimeException " +50"it is wrapped in RuntimeMBeanException,");51System.out.println("and if a Standard MBean throws Exception " +52"it is wrapped in MBeanException");53MBeanServer mbs = MBeanServerFactory.newMBeanServer();54Object standard = new Except();55ObjectName standardName = new ObjectName(":name=Standard MBean");56Object standardMBean =57new StandardMBean(new Except(), ExceptMBean.class);58ObjectName standardMBeanName =59new ObjectName(":name=Instance of StandardMBean");60Object dynamic = new DynamicExcept();61ObjectName dynamicName = new ObjectName(":name=Dynamic MBean");62mbs.registerMBean(standard, standardName);63mbs.registerMBean(standardMBean, standardMBeanName);64mbs.registerMBean(dynamic, dynamicName);65int failures = 0;66failures += test(mbs, standardName, true);67failures += test(mbs, standardMBeanName, true);68failures += test(mbs, dynamicName, false);6970final boolean[] booleans = {false, true};7172for (boolean runtimeX : booleans) {73Class<? extends Exception> excC =74runtimeX ? RuntimeMBeanException.class : MBeanException.class;75String excS =76runtimeX ? "a RuntimeMBeanException" : "an MBeanException";77String mbsS = "a plain MBeanServer";78System.out.println(79"Test that, with " + mbsS + ", " + excS + " is wrapped " +80"in " + excS);81// E.g. "Test that, with a plain MBeanServer, an MBeanException82// is wrapped in an MBeanException".83try {84mbs.createMBean(85Except.class.getName(), new ObjectName(":name=Oops"),86new Object[] {runtimeX},87new String[] {boolean.class.getName()});88System.out.println(89"FAIL: createMBean succeeded but should not have");90failures++;91} catch (Exception e) {92if (!excC.isInstance(e)) {93System.out.println(94"FAIL: expected " + excC.getName() + " from " +95"createMBean, got " + e);96failures++;97} else {98Throwable cause = e.getCause();99if (!excC.isInstance(cause)) {100System.out.println(101"FAIL: expected " + excC.getName() +102" as cause of " + excC.getName() +103", got " + e);104failures++;105} else106System.out.println("...ok");107}108}109}110111if (failures == 0)112System.out.println("Test passed");113else {114System.out.println("TEST FAILED: " + failures + " failure(s)");115System.exit(1);116}117}118119private static int test(MBeanServer mbs, ObjectName name,120boolean testChecked)121throws Exception {122System.out.println("--------" + name + "--------");123124int failures = 0;125final String[] ops = {"getAttribute", "setAttribute", "invoke"};126final int GET = 0, SET = 1, INVOKE = 2;127final String[] targets = {"UncheckedException", "CheckedException"};128final int UNCHECKED = 0, CHECKED = 1;129130for (int i = 0; i < ops.length; i++) {131for (int j = 0; j < targets.length; j++) {132133if (j == CHECKED && !testChecked)134continue;135136String target = targets[j];137String what = ops[i] + "/" + target;138System.out.println(what);139140try {141switch (i) {142case GET:143mbs.getAttribute(name, target);144break;145case SET:146mbs.setAttribute(name, new Attribute(target, "x"));147break;148case INVOKE:149mbs.invoke(name, target, null, null);150break;151default:152throw new AssertionError();153}154System.out.println("failure: " + what + " returned!");155failures++;156} catch (RuntimeMBeanException e) {157if (j == CHECKED) {158System.out.println("failure: RuntimeMBeanException " +159"when checked expected: " + e);160failures++;161} else {162Throwable cause = e.getCause();163if (cause == theUncheckedException)164System.out.println("ok: " + what);165else {166System.out.println("failure: " + what +167" wrapped " + cause);168failures++;169}170}171} catch (MBeanException e) {172if (j == UNCHECKED) {173System.out.println("failure: checked exception " +174"when unchecked expected: " + e);175failures++;176} else {177Throwable cause = e.getCause();178if (cause == theCheckedException)179System.out.println("ok: " + what);180else {181System.out.println("failure: " + what +182" wrapped " + cause);183failures++;184}185}186} catch (Throwable t) {187System.out.println("failure: " + what + " threw: " + t);188while ((t = t.getCause()) != null)189System.out.println(" ... " + t);190failures++;191}192}193}194195return failures;196}197198public static interface ExceptMBean {199public String getUncheckedException();200public void setUncheckedException(String x);201public void UncheckedException();202public String getCheckedException() throws Exception;203public void setCheckedException(String x) throws Exception;204public void CheckedException() throws Exception;205}206207public static class Except implements ExceptMBean {208public Except() {}209210public Except(boolean runtimeX) throws MBeanException {211if (runtimeX)212throw new RuntimeMBeanException(new RuntimeException(), "Bang");213else214throw new MBeanException(new Exception(), "Bang");215}216217public String getUncheckedException() {218throw theUncheckedException;219}220public void setUncheckedException(String x) {221throw theUncheckedException;222}223public void UncheckedException() {224throw theUncheckedException;225}226public String getCheckedException() throws Exception {227throw theCheckedException;228}229public void setCheckedException(String x) throws Exception {230throw theCheckedException;231}232public void CheckedException() throws Exception {233throw theCheckedException;234}235}236237public static class DynamicExcept implements DynamicMBean {238public Object getAttribute(String attrName)239throws MBeanException {240if (attrName.equals("UncheckedException"))241throw theUncheckedException;242else243throw new AssertionError();244}245public void setAttribute(Attribute attr)246throws MBeanException {247String attrName = attr.getName();248if (attrName.equals("UncheckedException"))249throw theUncheckedException;250else251throw new AssertionError();252}253public Object invoke(String opName, Object[] params, String[] sig)254throws MBeanException {255assert params == null && sig == null;256if (opName.equals("UncheckedException"))257throw theUncheckedException;258else259throw new AssertionError();260}261public AttributeList getAttributes(String[] names) {262assert false;263return null;264}265public AttributeList setAttributes(AttributeList attrs) {266assert false;267return null;268}269public MBeanInfo getMBeanInfo() {270try {271return new StandardMBean(new Except(), ExceptMBean.class)272.getMBeanInfo();273} catch (Exception e) {274assert false;275return null;276}277}278}279280private static final Exception theCheckedException =281new Exception("The checked exception that should be seen");282private static final RuntimeException theUncheckedException =283new UnsupportedOperationException("The unchecked exception " +284"that should be seen");285}286287288