Path: blob/master/test/jdk/java/lang/management/ManagementFactory/PlatformMBeanServerTest.java
41152 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 494753626* @summary Basic unit test of ManagementFactory.getPlatformMBeanServer()27* @author Mandy Chung28*29* @modules java.logging30* jdk.management31*/3233import java.lang.management.*;34import static java.lang.management.ManagementFactory.*;35import java.util.*;36import java.util.logging.LogManager;3738import javax.management.MBeanServer;39import javax.management.ObjectName;4041public class PlatformMBeanServerTest {42public static void main(String[] argv) throws Exception {43MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();44printMBeans(mbs);4546// validate if all standard JVM MBeans are registered47checkStandardMBeans(mbs);4849// validate if all platform MBeans are registered50checkPlatformMBeans(mbs);5152MBeanServer mbs1 = ManagementFactory.getPlatformMBeanServer();53if (mbs != mbs1) {54throw new RuntimeException("Second call to getPlatformMBeanServer()"55+ " returns a different MBeanServer.");56}5758System.out.println("Test passed.");59}6061private static void checkMBean(MBeanServer mbs, String mbeanName)62throws Exception {63try {64ObjectName objName = new ObjectName(mbeanName);65// We could call mbs.isRegistered(objName) here.66// Calling getMBeanInfo will throw exception if not found.67mbs.getMBeanInfo(objName);68} catch (Exception e) {69throw e;70}71}72private static void checkStandardMBeans(MBeanServer mbs) throws Exception {73checkMBean(mbs, CLASS_LOADING_MXBEAN_NAME);74checkMBean(mbs, MEMORY_MXBEAN_NAME);75checkMBean(mbs, OPERATING_SYSTEM_MXBEAN_NAME);76checkMBean(mbs, RUNTIME_MXBEAN_NAME);77checkMBean(mbs, THREAD_MXBEAN_NAME);78if (ManagementFactory.getCompilationMXBean() != null) {79checkMBean(mbs, COMPILATION_MXBEAN_NAME);80}8182List pools = ManagementFactory.getMemoryPoolMXBeans();83for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {84MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();85checkMBean(mbs, MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName());86}8788// Check the number of memory pools in the mbs89Set set = mbs.queryNames(new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*"), null);90if (set.size() != pools.size()) {91throw new RuntimeException("Unexpected number of memory pools:" +92"MBeanServer has " + set.size() +93". Expected = " + pools.size());94}9596List mgrs = ManagementFactory.getMemoryManagerMXBeans();97int num_mgrs = 0;98for (ListIterator iter = mgrs.listIterator(); iter.hasNext(); ) {99MemoryManagerMXBean m = (MemoryManagerMXBean) iter.next();100if (m instanceof GarbageCollectorMXBean) {101checkMBean(mbs, GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE102+ ",name=" + m.getName());103} else {104checkMBean(mbs, MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE105+ ",name=" + m.getName());106num_mgrs++;107}108}109110List gcs = ManagementFactory.getGarbageCollectorMXBeans();111for (ListIterator iter = gcs.listIterator(); iter.hasNext(); ) {112GarbageCollectorMXBean gc = (GarbageCollectorMXBean) iter.next();113checkMBean(mbs, GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE114+ ",name=" + gc.getName());115}116117// Check the number of memory managers and garbage collectors in the mbs118set = mbs.queryNames(new ObjectName(MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE + ",*"), null);119if (set.size() != num_mgrs) {120throw new RuntimeException("Unexpected number of memory managers:" +121"MBeanServer has " + set.size() +122". Expected = " + num_mgrs);123}124125set = mbs.queryNames(new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*"), null);126if (set.size() != gcs.size()) {127throw new RuntimeException("Unexpected number of garbage collectors:" +128"MBeanServer has " + set.size() +129". Expected = " + gcs.size());130}131}132private static void checkPlatformMBeans(MBeanServer mbs) throws Exception {133checkMBean(mbs, LogManager.LOGGING_MXBEAN_NAME);134}135136private static void printMBeans(MBeanServer mbs) throws Exception {137Set set = mbs.queryNames(null, null);138for (Iterator iter = set.iterator(); iter.hasNext(); ) {139System.out.println(iter.next());140}141}142}143144145