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 } 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 } | 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 } 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 } |