1 /* 2 * Copyright (c) 2008, 2016, 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 7024172 27 * @summary Basic unit test of ManagementFactory.getPlatformMXBean(s) 28 * methods and PlatformManagedObject.getObjectName() 29 * @author Mandy Chung 30 * 31 * @run main GetPlatformMXBeans 32 */ 33 34 import java.lang.management.*; 35 import java.io.IOException; 36 import java.util.*; 37 import javax.management.*; 38 39 import static java.lang.management.ManagementFactory.*; 40 41 public class GetPlatformMXBeans { 42 private static MBeanServer platformMBeanServer = 43 getPlatformMBeanServer(); 44 public static void main(String[] argv) throws Exception { 45 // singleton platform MXBean 46 checkPlatformMXBean(getClassLoadingMXBean(), 47 ClassLoadingMXBean.class, 48 CLASS_LOADING_MXBEAN_NAME); 49 checkPlatformMXBean(getCompilationMXBean(), 50 CompilationMXBean.class, 51 COMPILATION_MXBEAN_NAME); 52 checkPlatformMXBean(getMemoryMXBean(), 53 MemoryMXBean.class, 54 MEMORY_MXBEAN_NAME); 55 checkPlatformMXBean(getOperatingSystemMXBean(), 56 OperatingSystemMXBean.class, 57 OPERATING_SYSTEM_MXBEAN_NAME); 58 checkPlatformMXBean(getRuntimeMXBean(), 59 RuntimeMXBean.class, 60 RUNTIME_MXBEAN_NAME); 61 checkPlatformMXBean(getThreadMXBean(), 62 ThreadMXBean.class, 63 THREAD_MXBEAN_NAME); 64 65 // the following MXBean can have more than one instances 66 checkGarbageCollectorMXBeans(getGarbageCollectorMXBeans()); 67 checkMemoryManagerMXBeans(getMemoryManagerMXBeans()); 68 checkMemoryPoolMXBeans(getMemoryPoolMXBeans()); 69 70 // check invalid platform MXBean 71 checkInvalidPlatformMXBean(); 72 } 73 74 private static <T extends PlatformManagedObject> 75 void checkPlatformMXBean(T obj, Class<T> mxbeanInterface, 76 String mxbeanName) 77 throws Exception 78 { 79 // getPlatformMXBean may return null if the mxbean is not implemented 80 PlatformManagedObject mxbean = getPlatformMXBean(mxbeanInterface); 81 if (obj != mxbean) { 82 throw new RuntimeException("Singleton MXBean returned not matched"); 83 } 84 85 int numElements = obj == null ? 0 : 1; 86 List<? extends PlatformManagedObject> mxbeans = 87 getPlatformMXBeans(mxbeanInterface); 88 if (mxbeans.size() != numElements) { 89 throw new RuntimeException("Unmatched number of platform MXBeans " 90 + mxbeans.size() + ". Expected = " + numElements); 91 } 92 93 if (obj != null) { 94 if (obj != mxbeans.get(0)) { 95 throw new RuntimeException("The list returned by getPlatformMXBeans" 96 + " not matched"); 97 } 98 ObjectName on = new ObjectName(mxbeanName); 99 if (!on.equals(mxbean.getObjectName())) { 100 throw new RuntimeException("Unmatched ObjectName " + 101 mxbean.getObjectName() + " Expected = " + on); 102 } 103 checkRemotePlatformMXBean(obj, platformMBeanServer, 104 mxbeanInterface, mxbeanName); 105 } 106 } 107 108 // verify platform MXBeans in the platform MBeanServer 109 private static <T extends PlatformManagedObject> 110 void checkRemotePlatformMXBean(T obj, 111 MBeanServerConnection mbs, 112 Class<T> mxbeanInterface, 113 String mxbeanName) 114 throws Exception 115 { 116 PlatformManagedObject mxbean = getPlatformMXBean(mbs, mxbeanInterface); 117 if ((obj == null && mxbean != null) || (obj != null && mxbean == null)) { 118 throw new RuntimeException("Singleton MXBean returned not matched"); 119 } 120 121 int numElements = obj == null ? 0 : 1; 122 List<? extends PlatformManagedObject> mxbeans = 123 getPlatformMXBeans(mbs, mxbeanInterface); 124 if (mxbeans.size() != numElements) { 125 throw new RuntimeException("Unmatched number of platform MXBeans " 126 + mxbeans.size() + ". Expected = " + numElements); 127 } 128 129 ObjectName on = new ObjectName(mxbeanName); 130 if (!on.equals(mxbean.getObjectName())) { 131 throw new RuntimeException("Unmatched ObjectName " + 132 mxbean.getObjectName() + " Expected = " + on); 133 } 134 } 135 136 private static void checkMemoryManagerMXBeans(List<MemoryManagerMXBean> objs) 137 throws Exception 138 { 139 checkPlatformMXBeans(objs, MemoryManagerMXBean.class); 140 for (MemoryManagerMXBean mxbean : objs) { 141 String domainAndType; 142 if (mxbean instanceof GarbageCollectorMXBean) { 143 domainAndType = GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE; 144 } else { 145 domainAndType = MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE; 146 } 147 ObjectName on = new ObjectName(domainAndType + 148 ",name=" + mxbean.getName()); 149 if (!on.equals(mxbean.getObjectName())) { 150 throw new RuntimeException("Unmatched ObjectName " + 151 mxbean.getObjectName() + " Expected = " + on); 152 } 153 } 154 } 155 private static void checkMemoryPoolMXBeans(List<MemoryPoolMXBean> objs) 156 throws Exception 157 { 158 checkPlatformMXBeans(objs, MemoryPoolMXBean.class); 159 for (MemoryPoolMXBean mxbean : objs) { 160 ObjectName on = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + 161 ",name=" + mxbean.getName()); 162 if (!on.equals(mxbean.getObjectName())) { 163 throw new RuntimeException("Unmatched ObjectName " + 164 mxbean.getObjectName() + " Expected = " + on); 165 } 166 } 167 } 168 169 private static void checkGarbageCollectorMXBeans(List<GarbageCollectorMXBean> objs) 170 throws Exception 171 { 172 checkPlatformMXBeans(objs, GarbageCollectorMXBean.class); 173 for (GarbageCollectorMXBean mxbean : objs) { 174 ObjectName on = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + 175 ",name=" + mxbean.getName()); 176 if (!on.equals(mxbean.getObjectName())) { 177 throw new RuntimeException("Unmatched ObjectName " + 178 mxbean.getObjectName() + " Expected = " + on); 179 } 180 } 181 } 182 183 private static <T extends PlatformManagedObject> 184 void checkPlatformMXBeans(List<T> objs, Class<T> mxbeanInterface) 185 throws Exception 186 { 187 try { 188 getPlatformMXBean(mxbeanInterface); 189 // mxbeanInterface is not a singleton 190 throw new RuntimeException(mxbeanInterface + ": not a singleton MXBean"); 191 } catch (IllegalArgumentException e) { 192 // expect IAE 193 } 194 195 // verify local list of platform MXBeans 196 List<? extends PlatformManagedObject> mxbeans = 197 getPlatformMXBeans(mxbeanInterface); 198 if (objs.size() != mxbeans.size()) { 199 throw new RuntimeException("Unmatched number of platform MXBeans " 200 + mxbeans.size() + ". Expected = " + objs.size()); 201 } 202 List<T> list = new ArrayList<T>(objs); 203 for (PlatformManagedObject pmo : mxbeans) { 204 if (list.contains(pmo)) { 205 list.remove(pmo); 206 } else { 207 throw new RuntimeException(pmo + 208 " not in the platform MXBean list"); 209 } 210 } 211 212 if (!list.isEmpty()) { 213 throw new RuntimeException("The list returned by getPlatformMXBeans" 214 + " not matched"); 215 } 216 217 // verify platform MXBeans in the platform MBeanServer 218 mxbeans = getPlatformMXBeans(platformMBeanServer, mxbeanInterface); 219 if (objs.size() != mxbeans.size()) { 220 throw new RuntimeException("Unmatched number of platform MXBeans " 221 + mxbeans.size() + ". Expected = " + objs.size()); 222 } 223 } 224 225 interface FakeMXBean extends PlatformManagedObject {}; 226 227 private static void checkInvalidPlatformMXBean() throws IOException { 228 try { 229 getPlatformMXBean(FakeMXBean.class); 230 // mxbeanInterface is not a singleton 231 throw new RuntimeException("Expect IllegalArgumentException but not thrown"); 232 } catch (IllegalArgumentException e) { 233 // expect IAE 234 } 235 236 try { 237 getPlatformMXBeans(FakeMXBean.class); 238 // mxbeanInterface is not a singleton 239 throw new RuntimeException("Expect IllegalArgumentException but not thrown"); 240 } catch (IllegalArgumentException e) { 241 // expect IAE 242 } 243 244 try { 245 getPlatformMXBean(platformMBeanServer, FakeMXBean.class); 246 // mxbeanInterface is not a singleton 247 throw new RuntimeException("Expect IllegalArgumentException but not thrown"); 248 } catch (IllegalArgumentException e) { 249 // expect IAE 250 } 251 252 try { 253 getPlatformMXBeans(platformMBeanServer, FakeMXBean.class); 254 // mxbeanInterface is not a singleton 255 throw new RuntimeException("Expect IllegalArgumentException but not thrown"); 256 } catch (IllegalArgumentException e) { 257 // expect IAE 258 } 259 } 260 }