J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6316491 |
| 27 | * @summary Check that the MXBean annotation works as advertised |
| 28 | * @author Eamonn McManus |
| 29 | * @run clean MXBeanAnnotationTest |
| 30 | * @run build MXBeanAnnotationTest |
| 31 | * @run main MXBeanAnnotationTest |
| 32 | */ |
| 33 | |
| 34 | import javax.management.*; |
| 35 | |
| 36 | public class MXBeanAnnotationTest { |
| 37 | @MXBean |
| 38 | public static interface Empty {} |
| 39 | |
| 40 | public static class EmptyImpl implements Empty {} |
| 41 | |
| 42 | @MXBean(false) |
| 43 | public static interface NotMXBean {} |
| 44 | |
| 45 | public static class NotImpl implements NotMXBean {} |
| 46 | |
| 47 | public static void main(String[] args) throws Exception { |
| 48 | MBeanServer mbs = MBeanServerFactory.newMBeanServer(); |
| 49 | ObjectName on = new ObjectName("a:b=c"); |
| 50 | |
| 51 | // Test empty interface |
| 52 | |
| 53 | try { |
| 54 | mbs.registerMBean(new EmptyImpl(), on); |
| 55 | boolean ok = checkMXBean(mbs.getMBeanInfo(on), true, |
| 56 | "empty MXBean interface"); |
| 57 | mbs.unregisterMBean(on); |
| 58 | if (ok) |
| 59 | System.out.println("OK: empty MXBean interface"); |
| 60 | } catch (Exception e) { |
| 61 | failure = "MXBean with empty interface got exception: " + e; |
| 62 | System.out.println("FAILED: " + failure); |
| 63 | e.printStackTrace(System.out); |
| 64 | } |
| 65 | |
| 66 | // Test @MXBean(false) |
| 67 | |
| 68 | try { |
| 69 | mbs.registerMBean(new NotImpl(), on); |
| 70 | failure = "Registered a non-Standard MBean with @MXBean(false)"; |
| 71 | System.out.println("FAILED: " + failure); |
| 72 | } catch (NotCompliantMBeanException e) { |
| 73 | System.out.println("OK: non-Standard MBean with @MXBean(false) " + |
| 74 | "rejected"); |
| 75 | } |
| 76 | |
| 77 | if (failure == null) |
| 78 | System.out.println("TEST PASSED"); |
| 79 | else |
| 80 | throw new Exception("TEST FAILED: " + failure); |
| 81 | } |
| 82 | |
| 83 | private static boolean checkMXBean(MBeanInfo mbi, boolean expected, |
| 84 | String what) { |
| 85 | Descriptor d = mbi.getDescriptor(); |
| 86 | String mxbean = (String) d.getFieldValue("mxbean"); |
| 87 | boolean is = (mxbean != null && mxbean.equals("true")); |
| 88 | if (is == expected) |
| 89 | return true; |
| 90 | else { |
| 91 | failure = "MBean should " + (expected ? "" : "not ") + |
| 92 | "have mxbean=true: " + d; |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private static String failure; |
| 98 | } |