1 /* 2 * Copyright (c) 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6610094 27 * @summary Basic unit test of ManagementFactory.getPlatformMXBeans() 28 * and also PlatformManagedObject.getObjectName() 29 * @author Mandy Chung 30 * 31 * @run main GetPlatformMXBeans 32 */ 33 34 import java.lang.management.*; 35 import static java.lang.management.ManagementFactory.*; 36 import java.util.*; 37 import javax.management.*; 38 39 public class GetPlatformMXBeans { 40 private static MBeanServer platformMBeanServer = 41 getPlatformMBeanServer(); 42 public static void main(String[] argv) throws Exception { 43 checkPlatformMXBean(getClassLoadingMXBean(), 44 ClassLoadingMXBean.class, 45 CLASS_LOADING_MXBEAN_NAME); 46 checkPlatformMXBean(getCompilationMXBean(), 47 CompilationMXBean.class, 48 COMPILATION_MXBEAN_NAME); 49 checkPlatformMXBean(getMemoryMXBean(), 50 MemoryMXBean.class, 51 MEMORY_MXBEAN_NAME); 52 checkPlatformMXBean(getOperatingSystemMXBean(), 53 OperatingSystemMXBean.class, 54 OPERATING_SYSTEM_MXBEAN_NAME); 55 checkPlatformMXBean(getRuntimeMXBean(), 56 RuntimeMXBean.class, 57 RUNTIME_MXBEAN_NAME); 58 checkPlatformMXBean(getThreadMXBean(), 59 ThreadMXBean.class, 60 THREAD_MXBEAN_NAME); 61 checkGarbageCollectorMXBeans(getGarbageCollectorMXBeans()); 62 checkMemoryManagerMXBeans(getMemoryManagerMXBeans()); 63 checkMemoryPoolMXBeans(getMemoryPoolMXBeans()); 64 } 65 66 private static <T extends PlatformManagedObject> 67 void checkPlatformMXBean(T obj, Class<T> mxbeanInterface, 68 String mxbeanName) throws Exception 69 { 70 int numElements = (obj != null ? 1 : 0); 71 // verify local list of platform MXBeans 72 List<? extends PlatformManagedObject> mxbeans = 73 getPlatformMXBeans(mxbeanInterface); 74 if (mxbeans.size() != numElements) { 75 throw new RuntimeException("Unmatched number of platform MXBeans " 76 + mxbeans.size() + ". Expected = " + numElements); 77 } 78 79 if (obj != null) { 80 PlatformManagedObject pmo = mxbeans.get(0); 81 if (obj != pmo) { 82 throw new RuntimeException("The list returned by getPlatformMXBeans" 83 + " not matched"); 84 } 85 ObjectName on = new ObjectName(mxbeanName); 86 if (!on.equals(pmo.getObjectName())) { 87 throw new RuntimeException("Unmatched ObjectName " + 88 pmo.getObjectName() + " Expected = " + on); 89 } 90 } 91 92 // verify platform MXBeans in the platform MBeanServer 93 mxbeans = getPlatformMXBeans(platformMBeanServer, mxbeanInterface); 94 if (mxbeans.size() != numElements) { 95 throw new RuntimeException("Unmatched number of platform MXBeans " 96 + mxbeans.size() + ". Expected = " + numElements); 97 } 98 } 99 100 private static void checkMemoryManagerMXBeans(List<MemoryManagerMXBean> objs) 101 throws Exception 102 { 103 checkPlatformMXBeans(objs, MemoryManagerMXBean.class); 104 for (MemoryManagerMXBean mxbean : objs) { 105 String domainAndType; 106 if (mxbean instanceof GarbageCollectorMXBean) { 107 domainAndType = GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE; 108 } else { 109 domainAndType = MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE; 110 } 111 ObjectName on = new ObjectName(domainAndType + 112 ",name=" + mxbean.getName()); 113 if (!on.equals(mxbean.getObjectName())) { 114 throw new RuntimeException("Unmatched ObjectName " + 115 mxbean.getObjectName() + " Expected = " + on); 116 } 117 } 118 } 119 private static void checkMemoryPoolMXBeans(List<MemoryPoolMXBean> objs) 120 throws Exception 121 { 122 checkPlatformMXBeans(objs, MemoryPoolMXBean.class); 123 for (MemoryPoolMXBean mxbean : objs) { 124 ObjectName on = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + 125 ",name=" + mxbean.getName()); 126 if (!on.equals(mxbean.getObjectName())) { 127 throw new RuntimeException("Unmatched ObjectName " + 128 mxbean.getObjectName() + " Expected = " + on); 129 } 130 } 131 } 132 133 private static void checkGarbageCollectorMXBeans(List<GarbageCollectorMXBean> objs) 134 throws Exception 135 { 136 checkPlatformMXBeans(objs, GarbageCollectorMXBean.class); 137 for (GarbageCollectorMXBean mxbean : objs) { 138 ObjectName on = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + 139 ",name=" + mxbean.getName()); 140 if (!on.equals(mxbean.getObjectName())) { 141 throw new RuntimeException("Unmatched ObjectName " + 142 mxbean.getObjectName() + " Expected = " + on); 143 } 144 } 145 } 146 147 private static <T extends PlatformManagedObject> 148 void checkPlatformMXBeans(List<T> objs, Class<T> mxbeanInterface) 149 throws Exception 150 { 151 // verify local list of platform MXBeans 152 List<? extends PlatformManagedObject> mxbeans = 153 getPlatformMXBeans(mxbeanInterface); 154 if (objs.size() != mxbeans.size()) { 155 throw new RuntimeException("Unmatched number of platform MXBeans " 156 + mxbeans.size() + ". Expected = " + objs.size()); 157 } 158 List<T> list = new ArrayList<T>(objs); 159 for (PlatformManagedObject pmo : mxbeans) { 160 if (list.contains(pmo)) { 161 list.remove(pmo); 162 } else { 163 throw new RuntimeException(pmo + 164 " not in the platform MXBean list"); 165 } 166 } 167 168 if (!list.isEmpty()) { 169 throw new RuntimeException("The list returned by getPlatformMXBeans" 170 + " not matched"); 171 } 172 173 // verify platform MXBeans in the platform MBeanServer 174 mxbeans = getPlatformMXBeans(platformMBeanServer, mxbeanInterface); 175 if (objs.size() != mxbeans.size()) { 176 throw new RuntimeException("Unmatched number of platform MXBeans " 177 + mxbeans.size() + ". Expected = " + objs.size()); 178 } 179 } 180 }