test/java/lang/management/MXBean/MXBeanBehavior.java

Print this page


   1 /*
   2  * Copyright (c) 2005, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6320211
  27  * @summary Check that java.lang.management MXBeans have the same behavior
  28  *          as user MXBeans
  29  * @author  Eamonn McManus
  30  * @run     main/othervm MXBeanBehavior
  31  */
  32 
  33 import java.lang.management.*;
  34 import java.lang.reflect.*;
  35 import java.util.*;
  36 import javax.management.*;
  37 
  38 public class MXBeanBehavior {



  39     public static void main(String[] args) throws Exception {
  40         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  41 
  42         /* Test that all the MBeans in the java.* and com.sun.management*
  43            domains are MXBeans with the appropriate behavior.  */
  44         Set<ObjectName> names = mbs.queryNames(new ObjectName("java.*:*"),
  45                                                null);
  46         names.addAll(mbs.queryNames(new ObjectName("com.sun.management*:*"),
  47                                     null));
  48         for (ObjectName name : names)
  49             test(mbs, name);
  50 
  51         /* Now do some rudimentary testing of inter-MXBean references.
  52            It should be possible for a user MXBean to return e.g.  the
  53            CompilationMXBean from the platform from an attribute of
  54            type CompilationMXBean, and have the MXBean infrastructure
  55            map this into that MXBean's standard ObjectName.  It should
  56            also be possible for a proxy for this user MXBean to have
  57            this attribute's value mapped back into a CompilationMXBean
  58            instance, which however will be another proxy rather than


  82        javax.management.MXBean.  Its MBeanInfo should have a
  83        Descriptor with the fields mxbean and interfaceClassName, and
  84        furthermore we know that our implementation sets immutableInfo
  85        here.  Each attribute should have Descriptor with the fields
  86        openType and originalType that have appropriate values.  We
  87        don't currently check operations though the same considerations
  88        would apply there.  (If the MBeanInfo and MBeanAttributeInfo
  89        tests pass we can reasonably suppose that this MXBean will
  90        behave the same as all other MXBeans, so MBeanOperationInfo,
  91        MBeanNotificationInfo, and MBeanConstructorInfo will be covered
  92        by generic MXBean tests.
  93      */
  94     private static void test(MBeanServer mbs, ObjectName name) throws Exception {
  95         System.out.println("Testing: " + name);
  96 
  97         MBeanInfo mbi = mbs.getMBeanInfo(name);
  98         Descriptor mbid = mbi.getDescriptor();
  99         Object[] values = mbid.getFieldValues("immutableInfo",
 100                                               "interfaceClassName",
 101                                               "mxbean");




 102         checkEqual(values[0], "true", name + " immutableInfo field");
 103         checkEqual(values[2], "true", name + " mxbean field");

 104         String interfaceClassName = (String) values[1];
 105         if (!mbs.isInstanceOf(name, interfaceClassName)) {
 106             throw new RuntimeException(name + " not instance of " +
 107                                        interfaceClassName);
 108         }
 109         Class interfaceClass = Class.forName(interfaceClassName);
 110         for (MBeanAttributeInfo mbai : mbi.getAttributes()) {
 111             Descriptor mbaid = mbai.getDescriptor();
 112             Object[] avalues = mbaid.getFieldValues("openType",
 113                                                     "originalType");
 114             if (avalues[0] == null || avalues[1] == null) {
 115                 throw new RuntimeException("Null attribute descriptor fields: " +
 116                                            Arrays.toString(avalues));
 117             }
 118             if (mbai.isReadable()) {
 119                 String mname = (mbai.isIs() ? "is" : "get") + mbai.getName();
 120                 Method m = interfaceClass.getMethod(mname);
 121                 Type t = m.getGenericReturnType();
 122                 String ret =
 123                     (t instanceof Class) ? ((Class) t).getName() : t.toString();


   1 /*
   2  * Copyright (c) 2005, 2012 Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6320211
  27  * @summary Check that java.lang.management MXBeans have the same behavior
  28  *          as user MXBeans
  29  * @author  Eamonn McManus
  30  * @run     main/othervm MXBeanBehavior
  31  */
  32 
  33 import java.lang.management.*;
  34 import java.lang.reflect.*;
  35 import java.util.*;
  36 import javax.management.*;
  37 
  38 public class MXBeanBehavior {
  39     public static final HashSet<String> dynamicMBeanList = new HashSet<String>(
  40             Arrays.asList("com.sun.management:type=DiagnosticCommand"));
  41 
  42     public static void main(String[] args) throws Exception {
  43         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  44 
  45         /* Test that all the MBeans in the java.* and com.sun.management*
  46            domains are MXBeans with the appropriate behavior.  */
  47         Set<ObjectName> names = mbs.queryNames(new ObjectName("java.*:*"),
  48                                                null);
  49         names.addAll(mbs.queryNames(new ObjectName("com.sun.management*:*"),
  50                                     null));
  51         for (ObjectName name : names)
  52             test(mbs, name);
  53 
  54         /* Now do some rudimentary testing of inter-MXBean references.
  55            It should be possible for a user MXBean to return e.g.  the
  56            CompilationMXBean from the platform from an attribute of
  57            type CompilationMXBean, and have the MXBean infrastructure
  58            map this into that MXBean's standard ObjectName.  It should
  59            also be possible for a proxy for this user MXBean to have
  60            this attribute's value mapped back into a CompilationMXBean
  61            instance, which however will be another proxy rather than


  85        javax.management.MXBean.  Its MBeanInfo should have a
  86        Descriptor with the fields mxbean and interfaceClassName, and
  87        furthermore we know that our implementation sets immutableInfo
  88        here.  Each attribute should have Descriptor with the fields
  89        openType and originalType that have appropriate values.  We
  90        don't currently check operations though the same considerations
  91        would apply there.  (If the MBeanInfo and MBeanAttributeInfo
  92        tests pass we can reasonably suppose that this MXBean will
  93        behave the same as all other MXBeans, so MBeanOperationInfo,
  94        MBeanNotificationInfo, and MBeanConstructorInfo will be covered
  95        by generic MXBean tests.
  96      */
  97     private static void test(MBeanServer mbs, ObjectName name) throws Exception {
  98         System.out.println("Testing: " + name);
  99 
 100         MBeanInfo mbi = mbs.getMBeanInfo(name);
 101         Descriptor mbid = mbi.getDescriptor();
 102         Object[] values = mbid.getFieldValues("immutableInfo",
 103                                               "interfaceClassName",
 104                                               "mxbean");
 105         if(dynamicMBeanList.contains(name.getCanonicalName())) {
 106             checkEqual(values[0], "false", name + " immutableInfo field");
 107             checkEqual(values[2], "false", name + " mxbean field");
 108         } else {
 109             checkEqual(values[0], "true", name + " immutableInfo field");
 110             checkEqual(values[2], "true", name + " mxbean field");
 111         }
 112         String interfaceClassName = (String) values[1];
 113         if (!mbs.isInstanceOf(name, interfaceClassName)) {
 114             throw new RuntimeException(name + " not instance of " +
 115                                        interfaceClassName);
 116         }
 117         Class interfaceClass = Class.forName(interfaceClassName);
 118         for (MBeanAttributeInfo mbai : mbi.getAttributes()) {
 119             Descriptor mbaid = mbai.getDescriptor();
 120             Object[] avalues = mbaid.getFieldValues("openType",
 121                                                     "originalType");
 122             if (avalues[0] == null || avalues[1] == null) {
 123                 throw new RuntimeException("Null attribute descriptor fields: " +
 124                                            Arrays.toString(avalues));
 125             }
 126             if (mbai.isReadable()) {
 127                 String mname = (mbai.isIs() ? "is" : "get") + mbai.getName();
 128                 Method m = interfaceClass.getMethod(mname);
 129                 Type t = m.getGenericReturnType();
 130                 String ret =
 131                     (t instanceof Class) ? ((Class) t).getName() : t.toString();