Path: blob/master/test/jdk/java/lang/management/ManagementFactory/GetPlatformMXBeans.java
41152 views
/*1* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6610094 702417226* @summary Basic unit test of ManagementFactory.getPlatformMXBean(s)27* methods and PlatformManagedObject.getObjectName()28* @author Mandy Chung29*30* @run main GetPlatformMXBeans31*/3233import java.lang.management.*;34import java.io.IOException;35import java.util.*;36import javax.management.*;3738import static java.lang.management.ManagementFactory.*;3940public class GetPlatformMXBeans {41private static MBeanServer platformMBeanServer =42getPlatformMBeanServer();43public static void main(String[] argv) throws Exception {44// singleton platform MXBean45checkPlatformMXBean(getClassLoadingMXBean(),46ClassLoadingMXBean.class,47CLASS_LOADING_MXBEAN_NAME);48checkPlatformMXBean(getCompilationMXBean(),49CompilationMXBean.class,50COMPILATION_MXBEAN_NAME);51checkPlatformMXBean(getMemoryMXBean(),52MemoryMXBean.class,53MEMORY_MXBEAN_NAME);54checkPlatformMXBean(getOperatingSystemMXBean(),55OperatingSystemMXBean.class,56OPERATING_SYSTEM_MXBEAN_NAME);57checkPlatformMXBean(getRuntimeMXBean(),58RuntimeMXBean.class,59RUNTIME_MXBEAN_NAME);60checkPlatformMXBean(getThreadMXBean(),61ThreadMXBean.class,62THREAD_MXBEAN_NAME);6364// the following MXBean can have more than one instances65checkGarbageCollectorMXBeans(getGarbageCollectorMXBeans());66checkMemoryManagerMXBeans(getMemoryManagerMXBeans());67checkMemoryPoolMXBeans(getMemoryPoolMXBeans());6869// check invalid platform MXBean70checkInvalidPlatformMXBean();71}7273private static <T extends PlatformManagedObject>74void checkPlatformMXBean(T obj, Class<T> mxbeanInterface,75String mxbeanName)76throws Exception77{78// getPlatformMXBean may return null if the mxbean is not implemented79PlatformManagedObject mxbean = getPlatformMXBean(mxbeanInterface);80if (obj != mxbean) {81throw new RuntimeException("Singleton MXBean returned not matched");82}8384int numElements = obj == null ? 0 : 1;85List<? extends PlatformManagedObject> mxbeans =86getPlatformMXBeans(mxbeanInterface);87if (mxbeans.size() != numElements) {88throw new RuntimeException("Unmatched number of platform MXBeans "89+ mxbeans.size() + ". Expected = " + numElements);90}9192if (obj != null) {93if (obj != mxbeans.get(0)) {94throw new RuntimeException("The list returned by getPlatformMXBeans"95+ " not matched");96}97ObjectName on = new ObjectName(mxbeanName);98if (!on.equals(mxbean.getObjectName())) {99throw new RuntimeException("Unmatched ObjectName " +100mxbean.getObjectName() + " Expected = " + on);101}102checkRemotePlatformMXBean(obj, platformMBeanServer,103mxbeanInterface, mxbeanName);104}105}106107// verify platform MXBeans in the platform MBeanServer108private static <T extends PlatformManagedObject>109void checkRemotePlatformMXBean(T obj,110MBeanServerConnection mbs,111Class<T> mxbeanInterface,112String mxbeanName)113throws Exception114{115PlatformManagedObject mxbean = getPlatformMXBean(mbs, mxbeanInterface);116if ((obj == null && mxbean != null) || (obj != null && mxbean == null)) {117throw new RuntimeException("Singleton MXBean returned not matched");118}119120int numElements = obj == null ? 0 : 1;121List<? extends PlatformManagedObject> mxbeans =122getPlatformMXBeans(mbs, mxbeanInterface);123if (mxbeans.size() != numElements) {124throw new RuntimeException("Unmatched number of platform MXBeans "125+ mxbeans.size() + ". Expected = " + numElements);126}127128ObjectName on = new ObjectName(mxbeanName);129if (!on.equals(mxbean.getObjectName())) {130throw new RuntimeException("Unmatched ObjectName " +131mxbean.getObjectName() + " Expected = " + on);132}133}134135private static void checkMemoryManagerMXBeans(List<MemoryManagerMXBean> objs)136throws Exception137{138checkPlatformMXBeans(objs, MemoryManagerMXBean.class);139for (MemoryManagerMXBean mxbean : objs) {140String domainAndType;141if (mxbean instanceof GarbageCollectorMXBean) {142domainAndType = GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE;143} else {144domainAndType = MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE;145}146ObjectName on = new ObjectName(domainAndType +147",name=" + mxbean.getName());148if (!on.equals(mxbean.getObjectName())) {149throw new RuntimeException("Unmatched ObjectName " +150mxbean.getObjectName() + " Expected = " + on);151}152}153}154private static void checkMemoryPoolMXBeans(List<MemoryPoolMXBean> objs)155throws Exception156{157checkPlatformMXBeans(objs, MemoryPoolMXBean.class);158for (MemoryPoolMXBean mxbean : objs) {159ObjectName on = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE +160",name=" + mxbean.getName());161if (!on.equals(mxbean.getObjectName())) {162throw new RuntimeException("Unmatched ObjectName " +163mxbean.getObjectName() + " Expected = " + on);164}165}166}167168private static void checkGarbageCollectorMXBeans(List<GarbageCollectorMXBean> objs)169throws Exception170{171checkPlatformMXBeans(objs, GarbageCollectorMXBean.class);172for (GarbageCollectorMXBean mxbean : objs) {173ObjectName on = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +174",name=" + mxbean.getName());175if (!on.equals(mxbean.getObjectName())) {176throw new RuntimeException("Unmatched ObjectName " +177mxbean.getObjectName() + " Expected = " + on);178}179}180}181182private static <T extends PlatformManagedObject>183void checkPlatformMXBeans(List<T> objs, Class<T> mxbeanInterface)184throws Exception185{186try {187getPlatformMXBean(mxbeanInterface);188// mxbeanInterface is not a singleton189throw new RuntimeException(mxbeanInterface + ": not a singleton MXBean");190} catch (IllegalArgumentException e) {191// expect IAE192}193194// verify local list of platform MXBeans195List<? extends PlatformManagedObject> mxbeans =196getPlatformMXBeans(mxbeanInterface);197if (objs.size() != mxbeans.size()) {198throw new RuntimeException("Unmatched number of platform MXBeans "199+ mxbeans.size() + ". Expected = " + objs.size());200}201List<T> list = new ArrayList<T>(objs);202for (PlatformManagedObject pmo : mxbeans) {203if (list.contains(pmo)) {204list.remove(pmo);205} else {206throw new RuntimeException(pmo +207" not in the platform MXBean list");208}209}210211if (!list.isEmpty()) {212throw new RuntimeException("The list returned by getPlatformMXBeans"213+ " not matched");214}215216// verify platform MXBeans in the platform MBeanServer217mxbeans = getPlatformMXBeans(platformMBeanServer, mxbeanInterface);218if (objs.size() != mxbeans.size()) {219throw new RuntimeException("Unmatched number of platform MXBeans "220+ mxbeans.size() + ". Expected = " + objs.size());221}222}223224interface FakeMXBean extends PlatformManagedObject {};225226private static void checkInvalidPlatformMXBean() throws IOException {227try {228getPlatformMXBean(FakeMXBean.class);229// mxbeanInterface is not a singleton230throw new RuntimeException("Expect IllegalArgumentException but not thrown");231} catch (IllegalArgumentException e) {232// expect IAE233}234235try {236getPlatformMXBeans(FakeMXBean.class);237// mxbeanInterface is not a singleton238throw new RuntimeException("Expect IllegalArgumentException but not thrown");239} catch (IllegalArgumentException e) {240// expect IAE241}242243try {244getPlatformMXBean(platformMBeanServer, FakeMXBean.class);245// mxbeanInterface is not a singleton246throw new RuntimeException("Expect IllegalArgumentException but not thrown");247} catch (IllegalArgumentException e) {248// expect IAE249}250251try {252getPlatformMXBeans(platformMBeanServer, FakeMXBean.class);253// mxbeanInterface is not a singleton254throw new RuntimeException("Expect IllegalArgumentException but not thrown");255} catch (IllegalArgumentException e) {256// expect IAE257}258}259}260261262