Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanServer/MBeanServerInvocationHandlerExceptionTest.java
38838 views
/*1* Copyright (c) 2004, 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* @run clean MBeanServerInvocationHandlerExceptionTest29* @run build MBeanServerInvocationHandlerExceptionTest30* @run main MBeanServerInvocationHandlerExceptionTest31*/323334import java.io.IOException;35import javax.management.*;3637public class MBeanServerInvocationHandlerExceptionTest {38public static void main(String[] args) throws Exception {39System.out.println(">>> Test how for the MBeanServerInvocationHandler to "+40"unwrap a user specific exception.");4142final MBeanServer mbs = MBeanServerFactory.newMBeanServer();43final ObjectName name = new ObjectName("a:b=c");44mbs.registerMBean(new Test(), name);45TestMBean proxy = (TestMBean)46MBeanServerInvocationHandler.newProxyInstance(mbs,47name,48TestMBean.class,49false);5051// test the method "getter"52System.out.println(">>> Test the method getter to get an IOException.");53try {54proxy.getIOException();55} catch (IOException e) {56System.out.println(">>> Test passed: got expected exception:");57// e.printStackTrace(System.out);58} catch (Throwable t) {59System.out.println(">>> Test failed: got wrong exception:");60t.printStackTrace(System.out);6162throw new RuntimeException("Did not get an expected IOException.");63}6465// test the method "setter"66System.out.println(">>> Test the method setter to get a RuntimeException.");67try {68proxy.setRuntimeException("coucou");69} catch (UnsupportedOperationException ue) {70System.out.println(">>> Test passed: got expected exception:");71//ue.printStackTrace(System.out);72} catch (Throwable t) {73System.out.println(">>> Test failed: got wrong exception:");74t.printStackTrace(System.out);7576throw new RuntimeException("Did not get an expected Runtimeexception.");77}7879// test the method "invoke"80System.out.println(">>> Test the method invoke to get an Error.");81try {82proxy.invokeError();83} catch (AssertionError ae) {84System.out.println(">>> Test passed: got expected exception:");85//ue.printStackTrace(System.out);86} catch (Throwable t) {87System.out.println(">>> Test failed: got wrong exception:");88t.printStackTrace(System.out);8990throw new RuntimeException("Did not get an expected Error.");91}92}9394public static interface TestMBean {95public Object getIOException() throws IOException;9697public void setRuntimeException(String s) throws RuntimeException;9899public void invokeError() throws Error;100101}102103public static class Test implements TestMBean {104public Object getIOException() throws IOException {105throw new IOException("oops");106}107108public void setRuntimeException(String s) throws RuntimeException {109throw new UnsupportedOperationException(s);110}111112public void invokeError() throws Error {113throw new AssertionError();114}115}116}117118119