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


  75         mbs.setAttribute(refName, new Attribute("CompilationMXBean", on));
  76 
  77         System.out.println("TEST PASSED");
  78     }
  79 
  80     /* Check the behavior of this MXBean to ensure that it conforms to
  81        what is expected of all MXBeans as detailed in
  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) {


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


  79         mbs.setAttribute(refName, new Attribute("CompilationMXBean", on));
  80 
  81         System.out.println("TEST PASSED");
  82     }
  83 
  84     /* Check the behavior of this MXBean to ensure that it conforms to
  85        what is expected of all MXBeans as detailed in
  86        javax.management.MXBean.  Its MBeanInfo should have a
  87        Descriptor with the fields mxbean and interfaceClassName, and
  88        furthermore we know that our implementation sets immutableInfo
  89        here.  Each attribute should have Descriptor with the fields
  90        openType and originalType that have appropriate values.  We
  91        don't currently check operations though the same considerations
  92        would apply there.  (If the MBeanInfo and MBeanAttributeInfo
  93        tests pass we can reasonably suppose that this MXBean will
  94        behave the same as all other MXBeans, so MBeanOperationInfo,
  95        MBeanNotificationInfo, and MBeanConstructorInfo will be covered
  96        by generic MXBean tests.
  97      */
  98     private static void test(MBeanServer mbs, ObjectName name) throws Exception {
  99         if(excludeList.contains(name.getCanonicalName())) {
 100             // Skipping not MXBean objects.
 101             return;
 102         }
 103         System.out.println("Testing: " + name);
 104 
 105         MBeanInfo mbi = mbs.getMBeanInfo(name);
 106         Descriptor mbid = mbi.getDescriptor();
 107         Object[] values = mbid.getFieldValues("immutableInfo",
 108                                               "interfaceClassName",
 109                                               "mxbean");
 110         checkEqual(values[0], "true", name + " immutableInfo field");
 111         checkEqual(values[2], "true", name + " mxbean field");
 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) {