Path: blob/master/test/jdk/javax/management/MBeanServer/MBeanServerInvocationHandlerExceptionTest.java
41149 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 509251526* @summary Test how to unwrap a user specific exception27* @author Shanliang JIANG28*29* @run clean MBeanServerInvocationHandlerExceptionTest30* @run build MBeanServerInvocationHandlerExceptionTest31* @run main MBeanServerInvocationHandlerExceptionTest32*/333435import java.io.IOException;36import javax.management.*;3738public class MBeanServerInvocationHandlerExceptionTest {39public static void main(String[] args) throws Exception {40System.out.println(">>> Test how for the MBeanServerInvocationHandler to "+41"unwrap a user specific exception.");4243final MBeanServer mbs = MBeanServerFactory.newMBeanServer();44final ObjectName name = new ObjectName("a:b=c");45mbs.registerMBean(new Test(), name);46TestMBean proxy = (TestMBean)47MBeanServerInvocationHandler.newProxyInstance(mbs,48name,49TestMBean.class,50false);5152// test the method "getter"53System.out.println(">>> Test the method getter to get an IOException.");54try {55proxy.getIOException();56} catch (IOException e) {57System.out.println(">>> Test passed: got expected exception:");58// e.printStackTrace(System.out);59} catch (Throwable t) {60System.out.println(">>> Test failed: got wrong exception:");61t.printStackTrace(System.out);6263throw new RuntimeException("Did not get an expected IOException.");64}6566// test the method "setter"67System.out.println(">>> Test the method setter to get a RuntimeException.");68try {69proxy.setRuntimeException("coucou");70} catch (UnsupportedOperationException ue) {71System.out.println(">>> Test passed: got expected exception:");72//ue.printStackTrace(System.out);73} catch (Throwable t) {74System.out.println(">>> Test failed: got wrong exception:");75t.printStackTrace(System.out);7677throw new RuntimeException("Did not get an expected Runtimeexception.");78}7980// test the method "invoke"81System.out.println(">>> Test the method invoke to get an Error.");82try {83proxy.invokeError();84} catch (AssertionError ae) {85System.out.println(">>> Test passed: got expected exception:");86//ue.printStackTrace(System.out);87} catch (Throwable t) {88System.out.println(">>> Test failed: got wrong exception:");89t.printStackTrace(System.out);9091throw new RuntimeException("Did not get an expected Error.");92}93}9495public static interface TestMBean {96public Object getIOException() throws IOException;9798public void setRuntimeException(String s) throws RuntimeException;99100public void invokeError() throws Error;101102}103104public static class Test implements TestMBean {105public Object getIOException() throws IOException {106throw new IOException("oops");107}108109public void setRuntimeException(String s) throws RuntimeException {110throw new UnsupportedOperationException(s);111}112113public void invokeError() throws Error {114throw new AssertionError();115}116}117}118119120