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