1 /* 2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 5092515 27 * @summary Test how to unwrap a user specific exception 28 * @author Shanliang JIANG 29 * @run clean MBeanServerInvocationHandlerExceptionTest 30 * @run build MBeanServerInvocationHandlerExceptionTest 31 * @run main MBeanServerInvocationHandlerExceptionTest 32 */ 33 34 35 import java.io.IOException; 36 import javax.management.*; 37 38 public class MBeanServerInvocationHandlerExceptionTest { 39 public static void main(String[] args) throws Exception { 40 System.out.println(">>> Test how for the MBeanServerInvocationHandler to "+ 41 "unwrap a user specific exception."); 42 43 final MBeanServer mbs = MBeanServerFactory.newMBeanServer(); 44 final ObjectName name = new ObjectName("a:b=c"); 45 mbs.registerMBean(new Test(), name); 46 TestMBean proxy = (TestMBean) 47 MBeanServerInvocationHandler.newProxyInstance(mbs, 48 name, 49 TestMBean.class, 50 false); 51 52 // test the method "getter" 53 System.out.println(">>> Test the method getter to get an IOException."); 54 try { 55 proxy.getIOException(); 56 } catch (IOException e) { 57 System.out.println(">>> Test passed: got expected exception:"); 58 // e.printStackTrace(System.out); 59 } catch (Throwable t) { 60 System.out.println(">>> Test failed: got wrong exception:"); 61 t.printStackTrace(System.out); 62 63 throw new RuntimeException("Did not get an expected IOException."); 64 } 65 66 // test the method "setter" 67 System.out.println(">>> Test the method setter to get a RuntimeException."); 68 try { 69 proxy.setRuntimeException("coucou"); 70 } catch (UnsupportedOperationException ue) { 71 System.out.println(">>> Test passed: got expected exception:"); 72 //ue.printStackTrace(System.out); 73 } catch (Throwable t) { 74 System.out.println(">>> Test failed: got wrong exception:"); 75 t.printStackTrace(System.out); 76 77 throw new RuntimeException("Did not get an expected Runtimeexception."); 78 } 79 80 // test the method "invoke" 81 System.out.println(">>> Test the method invoke to get an Error."); 82 try { 83 proxy.invokeError(); 84 } catch (AssertionError ae) { 85 System.out.println(">>> Test passed: got expected exception:"); 86 //ue.printStackTrace(System.out); 87 } catch (Throwable t) { 88 System.out.println(">>> Test failed: got wrong exception:"); 89 t.printStackTrace(System.out); 90 91 throw new RuntimeException("Did not get an expected Error."); 92 } 93 } 94 95 public static interface TestMBean { 96 public Object getIOException() throws IOException; 97 98 public void setRuntimeException(String s) throws RuntimeException; 99 100 public void invokeError() throws Error; 101 102 } 103 104 public static class Test implements TestMBean { 105 public Object getIOException() throws IOException { 106 throw new IOException("oops"); 107 } 108 109 public void setRuntimeException(String s) throws RuntimeException { 110 throw new UnsupportedOperationException(s); 111 } 112 113 public void invokeError() throws Error { 114 throw new AssertionError(); 115 } 116 } 117 }