|
Java example source code file (ExceptionDiagnosisTest.java)
The ExceptionDiagnosisTest.java Java example source code/* * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 6713777 * @summary Test that exception messages include all relevant information * @author Eamonn McManus */ import java.beans.ConstructorProperties; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.management.JMX; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; public class ExceptionDiagnosisTest { private static volatile String failure; // ------ Illegal MXBeans ------ // Test that all of BdelloidMXBean, Rotifer, and File appear in the // exception messages. File is not an allowed type because of recursive // getters like "File getParentFile()". public static interface BdelloidMXBean { public Rotifer getRotifer(); } public static class Bdelloid implements BdelloidMXBean { public Rotifer getRotifer() { return null; } } public static class Rotifer { public File getFile() { return null; } } // Test that all of IndirectHashMapMXBean, HashMapContainer, and // HashMap<String,String> appear in the exception messages. // HashMap<String,String> is not an allowed type because only the // java.util interface such as Map are allowed with generic parameters, // not their concrete implementations like HashMap. public static interface IndirectHashMapMXBean { public HashMapContainer getContainer(); } public static class IndirectHashMap implements IndirectHashMapMXBean { public HashMapContainer getContainer() { return null; } } public static class HashMapContainer { public HashMap<String, String> getHashMap() {return null;} } // ------ MXBeans that are legal but where proxies are not ------ // Test that all of BlimMXBean, BlimContainer, Blim, and Blam appear // in the exception messages for a proxy for this MXBean. Blam is // legal in MXBeans but is not reconstructible so you cannot make // a proxy for BlimMXBean. public static interface BlimMXBean { public BlimContainer getBlimContainer(); } public static class BlimImpl implements BlimMXBean { public BlimContainer getBlimContainer() { return null; } } public static class BlimContainer { public Blim getBlim() {return null;} public void setBlim(Blim blim) {} } public static class Blim { public Blam getBlam() {return null;} public void setBlam(Blam blam) {} } public static class Blam { public Blam(int x) {} public int getX() {return 0;} } // ------ Property name differing only in case ------ public static interface CaseProbMXBean { public CaseProb getCaseProb(); } public static class CaseProbImpl implements CaseProbMXBean { public CaseProb getCaseProb() {return null;} } public static class CaseProb { @ConstructorProperties({"urlPath"}) public CaseProb(String urlPath) {} public String getURLPath() {return null;} } public static void main(String[] args) throws Exception { testMXBeans(new Bdelloid(), BdelloidMXBean.class, Rotifer.class, File.class); testMXBeans(new IndirectHashMap(), IndirectHashMapMXBean.class, HashMapContainer.class, HashMapContainer.class.getMethod("getHashMap").getGenericReturnType()); testProxies(new BlimImpl(), BlimMXBean.class, BlimMXBean.class, BlimContainer.class, Blim.class, Blam.class); testCaseProb(); if (failure == null) System.out.println("TEST PASSED"); else throw new Exception("TEST FAILED: " + failure); } private static void testMXBeans(Object mbean, Type... expectedTypes) throws Exception { try { MBeanServer mbs = MBeanServerFactory.newMBeanServer(); ObjectName name = new ObjectName("a:b=c"); mbs.registerMBean(mbean, name); fail("No exception from registerMBean for " + mbean); } catch (NotCompliantMBeanException e) { checkExceptionChain("MBean " + mbean, e, expectedTypes); } } private static <T> void testProxies( Object mbean, Class<T> mxbeanClass, Type... expectedTypes) throws Exception { MBeanServer mbs = MBeanServerFactory.newMBeanServer(); ObjectName name = new ObjectName("a:b=c"); mbs.registerMBean(mbean, name); T proxy = JMX.newMXBeanProxy(mbs, name, mxbeanClass); List<Method> methods = new ArrayList Other Java examples (source code examples)Here is a short list of links related to this Java ExceptionDiagnosisTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.