Path: blob/master/test/jdk/javax/management/Introspector/NotCompliantCauseTest.java
41149 views
/*1* Copyright (c) 2006, 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* @test NotCompliantCauseTest.java25* @bug 637429026* @summary Test that NotCompliantMBeanException has a cause in case of27* type mapping problems.28* @author Daniel Fuchs, Alexander Shusherov29*30* @run clean NotCompliantCauseTest31* @run build NotCompliantCauseTest32* @run main NotCompliantCauseTest33*/34/*35* NotCompliantCauseTest.java36*37* Created on January 20, 2006, 2:56 PM / dfuchs38*39*/4041import javax.management.MBeanServer;42import javax.management.MBeanServerFactory;43import javax.management.NotCompliantMBeanException;44import javax.management.ObjectName;45import javax.management.openmbean.OpenDataException;4647/**48*49* @author Sun Microsystems, 2005 - All rights reserved.50*/51public class NotCompliantCauseTest {5253/**54* Creates a new instance of NotCompliantCauseTest55*/56public NotCompliantCauseTest() {57}5859/**60* Test that NotCompliantMBeanException has a cause in case of61* type mapping problems.62**/63public static void main(String[] args) {64NotCompliantCauseTest instance = new NotCompliantCauseTest();6566instance.test1();67}6869public static class RuntimeTestException extends RuntimeException {70public RuntimeTestException(String msg) {71super(msg);72}73public RuntimeTestException(String msg, Throwable cause) {74super(msg,cause);75}76public RuntimeTestException(Throwable cause) {77super(cause);78}79}8081/**82* Test that NotCompliantMBeanException has a cause in case of83* type mapping problems.84**/85void test1() {86try {87MBeanServer mbs = MBeanServerFactory.createMBeanServer();88ObjectName oname = new ObjectName("domain:type=test");8990mbs.createMBean(NotCompliant.class.getName(), oname);91System.err.println("ERROR: expected " +92"NotCompliantMBeanException not thrown");93throw new RuntimeTestException("NotCompliantMBeanException not thrown");94} catch (RuntimeTestException e) {95throw e;96} catch (NotCompliantMBeanException e) {97Throwable cause = e.getCause();98if (cause == null)99throw new RuntimeTestException("NotCompliantMBeanException " +100"doesn't have any cause.", e);101while (cause.getCause() != null) {102if (cause instanceof OpenDataException) break;103cause = cause.getCause();104}105if (! (cause instanceof OpenDataException))106throw new RuntimeTestException("NotCompliantMBeanException " +107"doesn't have expected cause ("+108OpenDataException.class.getName()+"): "+cause, e);109System.err.println("SUCCESS: Found expected cause: " + cause);110} catch (Exception e) {111System.err.println("Unexpected exception: " + e);112throw new RuntimeException("Unexpected exception: " + e,e);113}114}115116public interface NotCompliantMXBean {117Object returnObject();118}119120public static class NotCompliant implements NotCompliantMXBean {121public Object returnObject() {122return new Object();123}124}125126}127128129