0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4947536
0N/A * @summary Basic unit test of ManagementFactory.getPlatformMBeanServer()
0N/A * @author Mandy Chung
0N/A */
0N/A
0N/Aimport java.lang.management.*;
0N/Aimport static java.lang.management.ManagementFactory.*;
0N/Aimport java.util.*;
0N/Aimport java.util.logging.LogManager;
0N/A
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.ObjectName;
0N/A
0N/Apublic class PlatformMBeanServerTest {
0N/A public static void main(String[] argv) throws Exception {
0N/A MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
0N/A printMBeans(mbs);
0N/A
0N/A // validate if all standard JVM MBeans are registered
0N/A checkStandardMBeans(mbs);
0N/A
0N/A // validate if all platform MBeans are registered
0N/A checkPlatformMBeans(mbs);
0N/A
0N/A MBeanServer mbs1 = ManagementFactory.getPlatformMBeanServer();
0N/A if (mbs != mbs1) {
0N/A throw new RuntimeException("Second call to getPlatformMBeanServer()"
0N/A + " returns a different MBeanServer.");
0N/A }
0N/A
0N/A System.out.println("Test passed.");
0N/A }
0N/A
0N/A private static void checkMBean(MBeanServer mbs, String mbeanName)
0N/A throws Exception {
0N/A try {
0N/A ObjectName objName = new ObjectName(mbeanName);
0N/A // We could call mbs.isRegistered(objName) here.
0N/A // Calling getMBeanInfo will throw exception if not found.
0N/A mbs.getMBeanInfo(objName);
0N/A } catch (Exception e) {
0N/A throw e;
0N/A }
0N/A }
0N/A private static void checkStandardMBeans(MBeanServer mbs) throws Exception {
0N/A checkMBean(mbs, CLASS_LOADING_MXBEAN_NAME);
0N/A checkMBean(mbs, MEMORY_MXBEAN_NAME);
0N/A checkMBean(mbs, OPERATING_SYSTEM_MXBEAN_NAME);
0N/A checkMBean(mbs, RUNTIME_MXBEAN_NAME);
0N/A checkMBean(mbs, THREAD_MXBEAN_NAME);
0N/A if (ManagementFactory.getCompilationMXBean() != null) {
0N/A checkMBean(mbs, COMPILATION_MXBEAN_NAME);
0N/A }
0N/A
0N/A List pools = ManagementFactory.getMemoryPoolMXBeans();
0N/A for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {
0N/A MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
0N/A checkMBean(mbs, MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName());
0N/A }
0N/A
0N/A // Check the number of memory pools in the mbs
0N/A Set set = mbs.queryNames(new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*"), null);
0N/A if (set.size() != pools.size()) {
0N/A throw new RuntimeException("Unexpected number of memory pools:" +
0N/A "MBeanServer has " + set.size() +
0N/A ". Expected = " + pools.size());
0N/A }
0N/A
0N/A List mgrs = ManagementFactory.getMemoryManagerMXBeans();
0N/A int num_mgrs = 0;
0N/A for (ListIterator iter = mgrs.listIterator(); iter.hasNext(); ) {
0N/A MemoryManagerMXBean m = (MemoryManagerMXBean) iter.next();
0N/A if (m instanceof GarbageCollectorMXBean) {
0N/A checkMBean(mbs, GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
0N/A + ",name=" + m.getName());
0N/A } else {
0N/A checkMBean(mbs, MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
0N/A + ",name=" + m.getName());
0N/A num_mgrs++;
0N/A }
0N/A }
0N/A
0N/A List gcs = ManagementFactory.getGarbageCollectorMXBeans();
0N/A for (ListIterator iter = gcs.listIterator(); iter.hasNext(); ) {
0N/A GarbageCollectorMXBean gc = (GarbageCollectorMXBean) iter.next();
0N/A checkMBean(mbs, GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
0N/A + ",name=" + gc.getName());
0N/A }
0N/A
0N/A // Check the number of memory managers and garbage collectors in the mbs
0N/A set = mbs.queryNames(new ObjectName(MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE + ",*"), null);
0N/A if (set.size() != num_mgrs) {
0N/A throw new RuntimeException("Unexpected number of memory managers:" +
0N/A "MBeanServer has " + set.size() +
0N/A ". Expected = " + num_mgrs);
0N/A }
0N/A
0N/A set = mbs.queryNames(new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*"), null);
0N/A if (set.size() != gcs.size()) {
0N/A throw new RuntimeException("Unexpected number of garbage collectors:" +
0N/A "MBeanServer has " + set.size() +
0N/A ". Expected = " + gcs.size());
0N/A }
0N/A }
0N/A private static void checkPlatformMBeans(MBeanServer mbs) throws Exception {
0N/A checkMBean(mbs, LogManager.LOGGING_MXBEAN_NAME);
0N/A }
0N/A
0N/A private static void printMBeans(MBeanServer mbs) throws Exception {
0N/A Set set = mbs.queryNames(null, null);
0N/A for (Iterator iter = set.iterator(); iter.hasNext(); ) {
0N/A System.out.println(iter.next());
0N/A }
0N/A }
0N/A}