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