Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/NotCompliantCauseTest.java
38839 views
/*1* Copyright (c) 2006, 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* @run clean NotCompliantCauseTest30* @run build NotCompliantCauseTest31* @run main NotCompliantCauseTest32*/33/*34* NotCompliantCauseTest.java35*36* Created on January 20, 2006, 2:56 PM / dfuchs37*38*/3940import java.util.Random;41import java.util.logging.Logger;4243import javax.management.MBeanServer;44import javax.management.MBeanServerFactory;45import javax.management.NotCompliantMBeanException;46import javax.management.ObjectName;47import javax.management.openmbean.OpenDataException;4849/**50*51* @author Sun Microsystems, 2005 - All rights reserved.52*/53public class NotCompliantCauseTest {5455/**56* A logger for this class.57**/58private static final Logger LOG =59Logger.getLogger(NotCompliantCauseTest.class.getName());6061/**62* Creates a new instance of NotCompliantCauseTest63*/64public NotCompliantCauseTest() {65}6667/**68* Test that NotCompliantMBeanException has a cause in case of69* type mapping problems.70**/71public static void main(String[] args) {72NotCompliantCauseTest instance = new NotCompliantCauseTest();7374instance.test1();75}7677public static class RuntimeTestException extends RuntimeException {78public RuntimeTestException(String msg) {79super(msg);80}81public RuntimeTestException(String msg, Throwable cause) {82super(msg,cause);83}84public RuntimeTestException(Throwable cause) {85super(cause);86}87}8889/**90* Test that NotCompliantMBeanException has a cause in case of91* type mapping problems.92**/93void test1() {94try {95MBeanServer mbs = MBeanServerFactory.createMBeanServer();96ObjectName oname = new ObjectName("domain:type=test");9798mbs.createMBean(NotCompliant.class.getName(), oname);99System.err.println("ERROR: expected " +100"NotCompliantMBeanException not thrown");101throw new RuntimeTestException("NotCompliantMBeanException not thrown");102} catch (RuntimeTestException e) {103throw e;104} catch (NotCompliantMBeanException e) {105Throwable cause = e.getCause();106if (cause == null)107throw new RuntimeTestException("NotCompliantMBeanException " +108"doesn't have any cause.", e);109while (cause.getCause() != null) {110if (cause instanceof OpenDataException) break;111cause = cause.getCause();112}113if (! (cause instanceof OpenDataException))114throw new RuntimeTestException("NotCompliantMBeanException " +115"doesn't have expected cause ("+116OpenDataException.class.getName()+"): "+cause, e);117System.err.println("SUCCESS: Found expected cause: " + cause);118} catch (Exception e) {119System.err.println("Unexpected exception: " + e);120throw new RuntimeException("Unexpected exception: " + e,e);121}122}123124public interface NotCompliantMXBean {125Random returnRandom();126}127128public static class NotCompliant implements NotCompliantMXBean {129public Random returnRandom() {130return new Random();131}132}133134}135136137