Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ManagementFactory/PlatformMBeanServerTest.java
38828 views
/*1* Copyright (c) 2003, 2004, 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*/2930import java.lang.management.*;31import static java.lang.management.ManagementFactory.*;32import java.util.*;33import java.util.logging.LogManager;3435import javax.management.MBeanServer;36import javax.management.ObjectName;3738public class PlatformMBeanServerTest {39public static void main(String[] argv) throws Exception {40MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();41printMBeans(mbs);4243// validate if all standard JVM MBeans are registered44checkStandardMBeans(mbs);4546// validate if all platform MBeans are registered47checkPlatformMBeans(mbs);4849MBeanServer mbs1 = ManagementFactory.getPlatformMBeanServer();50if (mbs != mbs1) {51throw new RuntimeException("Second call to getPlatformMBeanServer()"52+ " returns a different MBeanServer.");53}5455System.out.println("Test passed.");56}5758private static void checkMBean(MBeanServer mbs, String mbeanName)59throws Exception {60try {61ObjectName objName = new ObjectName(mbeanName);62// We could call mbs.isRegistered(objName) here.63// Calling getMBeanInfo will throw exception if not found.64mbs.getMBeanInfo(objName);65} catch (Exception e) {66throw e;67}68}69private static void checkStandardMBeans(MBeanServer mbs) throws Exception {70checkMBean(mbs, CLASS_LOADING_MXBEAN_NAME);71checkMBean(mbs, MEMORY_MXBEAN_NAME);72checkMBean(mbs, OPERATING_SYSTEM_MXBEAN_NAME);73checkMBean(mbs, RUNTIME_MXBEAN_NAME);74checkMBean(mbs, THREAD_MXBEAN_NAME);75if (ManagementFactory.getCompilationMXBean() != null) {76checkMBean(mbs, COMPILATION_MXBEAN_NAME);77}7879List pools = ManagementFactory.getMemoryPoolMXBeans();80for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {81MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();82checkMBean(mbs, MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName());83}8485// Check the number of memory pools in the mbs86Set set = mbs.queryNames(new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*"), null);87if (set.size() != pools.size()) {88throw new RuntimeException("Unexpected number of memory pools:" +89"MBeanServer has " + set.size() +90". Expected = " + pools.size());91}9293List mgrs = ManagementFactory.getMemoryManagerMXBeans();94int num_mgrs = 0;95for (ListIterator iter = mgrs.listIterator(); iter.hasNext(); ) {96MemoryManagerMXBean m = (MemoryManagerMXBean) iter.next();97if (m instanceof GarbageCollectorMXBean) {98checkMBean(mbs, GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE99+ ",name=" + m.getName());100} else {101checkMBean(mbs, MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE102+ ",name=" + m.getName());103num_mgrs++;104}105}106107List gcs = ManagementFactory.getGarbageCollectorMXBeans();108for (ListIterator iter = gcs.listIterator(); iter.hasNext(); ) {109GarbageCollectorMXBean gc = (GarbageCollectorMXBean) iter.next();110checkMBean(mbs, GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE111+ ",name=" + gc.getName());112}113114// Check the number of memory managers and garbage collectors in the mbs115set = mbs.queryNames(new ObjectName(MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE + ",*"), null);116if (set.size() != num_mgrs) {117throw new RuntimeException("Unexpected number of memory managers:" +118"MBeanServer has " + set.size() +119". Expected = " + num_mgrs);120}121122set = mbs.queryNames(new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*"), null);123if (set.size() != gcs.size()) {124throw new RuntimeException("Unexpected number of garbage collectors:" +125"MBeanServer has " + set.size() +126". Expected = " + gcs.size());127}128}129private static void checkPlatformMBeans(MBeanServer mbs) throws Exception {130checkMBean(mbs, LogManager.LOGGING_MXBEAN_NAME);131}132133private static void printMBeans(MBeanServer mbs) throws Exception {134Set set = mbs.queryNames(null, null);135for (Iterator iter = set.iterator(); iter.hasNext(); ) {136System.out.println(iter.next());137}138}139}140141142