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