1 /*
   2  * Copyright (c) 2003, 2016, 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     4947536
  27  * @summary Basic unit test of ManagementFactory.getPlatformMBeanServer()
  28  * @author  Mandy Chung
  29  * @modules jdk.management
  30  */
  31 
  32 import java.lang.management.*;
  33 import static java.lang.management.ManagementFactory.*;
  34 import java.util.*;
  35 import java.util.logging.LogManager;
  36 
  37 import javax.management.MBeanServer;
  38 import javax.management.ObjectName;
  39 
  40 public class PlatformMBeanServerTest {
  41     public static void main(String[] argv) throws Exception {
  42         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  43         printMBeans(mbs);
  44 
  45         // validate if all standard JVM MBeans are registered
  46         checkStandardMBeans(mbs);
  47 
  48         // validate if all platform MBeans are registered
  49         checkPlatformMBeans(mbs);
  50 
  51         MBeanServer mbs1 = ManagementFactory.getPlatformMBeanServer();
  52         if (mbs != mbs1) {
  53             throw new RuntimeException("Second call to getPlatformMBeanServer()"
  54                 + " returns a different MBeanServer.");
  55         }
  56 
  57         System.out.println("Test passed.");
  58     }
  59 
  60     private static void checkMBean(MBeanServer mbs, String mbeanName)
  61             throws Exception {
  62         try {
  63             ObjectName objName = new ObjectName(mbeanName);
  64             // We could call mbs.isRegistered(objName) here.
  65             // Calling getMBeanInfo will throw exception if not found.
  66             mbs.getMBeanInfo(objName);
  67         } catch (Exception e) {
  68             throw e;
  69         }
  70     }
  71     private static void checkStandardMBeans(MBeanServer mbs) throws Exception {
  72         checkMBean(mbs, CLASS_LOADING_MXBEAN_NAME);
  73         checkMBean(mbs, MEMORY_MXBEAN_NAME);
  74         checkMBean(mbs, OPERATING_SYSTEM_MXBEAN_NAME);
  75         checkMBean(mbs, RUNTIME_MXBEAN_NAME);
  76         checkMBean(mbs, THREAD_MXBEAN_NAME);
  77         if (ManagementFactory.getCompilationMXBean() != null) {
  78             checkMBean(mbs, COMPILATION_MXBEAN_NAME);
  79         }
  80 
  81         List pools = ManagementFactory.getMemoryPoolMXBeans();
  82         for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {
  83             MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
  84             checkMBean(mbs, MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName());
  85         }
  86 
  87         // Check the number of memory pools in the mbs
  88         Set set = mbs.queryNames(new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*"), null);
  89         if (set.size() != pools.size()) {
  90             throw new RuntimeException("Unexpected number of memory pools:" +
  91                 "MBeanServer has " + set.size() +
  92                 ". Expected = " + pools.size());
  93         }
  94 
  95         List mgrs = ManagementFactory.getMemoryManagerMXBeans();
  96         int num_mgrs = 0;
  97         for (ListIterator iter = mgrs.listIterator(); iter.hasNext(); ) {
  98             MemoryManagerMXBean m = (MemoryManagerMXBean) iter.next();
  99             if (m instanceof GarbageCollectorMXBean) {
 100                 checkMBean(mbs, GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
 101                                     + ",name=" + m.getName());
 102             } else {
 103                 checkMBean(mbs, MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
 104                                     + ",name=" + m.getName());
 105                 num_mgrs++;
 106             }
 107         }
 108 
 109         List gcs = ManagementFactory.getGarbageCollectorMXBeans();
 110         for (ListIterator iter = gcs.listIterator(); iter.hasNext(); ) {
 111             GarbageCollectorMXBean gc = (GarbageCollectorMXBean) iter.next();
 112             checkMBean(mbs, GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
 113                                 + ",name=" + gc.getName());
 114         }
 115 
 116         // Check the number of memory managers and garbage collectors in the mbs
 117         set = mbs.queryNames(new ObjectName(MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE + ",*"), null);
 118         if (set.size() != num_mgrs) {
 119             throw new RuntimeException("Unexpected number of memory managers:" +
 120                 "MBeanServer has " + set.size() +
 121                 ". Expected = " + num_mgrs);
 122         }
 123 
 124         set = mbs.queryNames(new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*"), null);
 125         if (set.size() != gcs.size()) {
 126             throw new RuntimeException("Unexpected number of garbage collectors:" +
 127                 "MBeanServer has " + set.size() +
 128                 ". Expected = " + gcs.size());
 129         }
 130     }
 131     private static void checkPlatformMBeans(MBeanServer mbs) throws Exception {
 132         checkMBean(mbs, LogManager.LOGGING_MXBEAN_NAME);
 133     }
 134 
 135     private static void printMBeans(MBeanServer mbs) throws Exception {
 136         Set set = mbs.queryNames(null, null);
 137         for (Iterator iter = set.iterator(); iter.hasNext(); ) {
 138             System.out.println(iter.next());
 139         }
 140     }
 141 }