1 /* 2 * Copyright (c) 2004, 2015, 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 * @modules java.management 30 * @run clean MBeanServerInvocationHandlerExceptionTest 31 * @run build MBeanServerInvocationHandlerExceptionTest 32 * @run main MBeanServerInvocationHandlerExceptionTest 33 */ 34 35 36 import java.io.IOException; 37 import javax.management.*; 38 39 public class MBeanServerInvocationHandlerExceptionTest { 40 public static void main(String[] args) throws Exception { 41 System.out.println(">>> Test how for the MBeanServerInvocationHandler to "+ 42 "unwrap a user specific exception."); 43 44 final MBeanServer mbs = MBeanServerFactory.newMBeanServer(); 45 final ObjectName name = new ObjectName("a:b=c"); 46 mbs.registerMBean(new Test(), name); 47 TestMBean proxy = (TestMBean) 48 MBeanServerInvocationHandler.newProxyInstance(mbs, 49 name, 50 TestMBean.class, 51 false); 52 53 // test the method "getter" 54 System.out.println(">>> Test the method getter to get an IOException."); 55 try { 56 proxy.getIOException(); 57 } catch (IOException e) { 58 System.out.println(">>> Test passed: got expected exception:"); 59 // e.printStackTrace(System.out); 60 } catch (Throwable t) { 61 System.out.println(">>> Test failed: got wrong exception:"); 62 t.printStackTrace(System.out); 63 64 throw new RuntimeException("Did not get an expected IOException."); 65 } 66 67 // test the method "setter" 68 System.out.println(">>> Test the method setter to get a RuntimeException."); 69 try { 70 proxy.setRuntimeException("coucou"); 71 } catch (UnsupportedOperationException ue) { 72 System.out.println(">>> Test passed: got expected exception:"); 73 //ue.printStackTrace(System.out); 74 } catch (Throwable t) { 75 System.out.println(">>> Test failed: got wrong exception:"); 76 t.printStackTrace(System.out); 77 78 throw new RuntimeException("Did not get an expected Runtimeexception."); 79 } 80 81 // test the method "invoke" 82 System.out.println(">>> Test the method invoke to get an Error."); 83 try { 84 proxy.invokeError(); 85 } catch (AssertionError ae) { 86 System.out.println(">>> Test passed: got expected exception:"); 87 //ue.printStackTrace(System.out); 88 } catch (Throwable t) { 89 System.out.println(">>> Test failed: got wrong exception:"); 90 t.printStackTrace(System.out); 91 92 throw new RuntimeException("Did not get an expected Error."); 93 } 94 } 95 96 public static interface TestMBean { 97 public Object getIOException() throws IOException; 98 99 public void setRuntimeException(String s) throws RuntimeException; 100 101 public void invokeError() throws Error; 102 103 } 104 105 public static class Test implements TestMBean { 106 public Object getIOException() throws IOException { 107 throw new IOException("oops"); 108 } 109 110 public void setRuntimeException(String s) throws RuntimeException { 111 throw new UnsupportedOperationException(s); 112 } 113 114 public void invokeError() throws Error { 115 throw new AssertionError(); 116 } 117 } 118 }