178N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
178N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
178N/A *
178N/A * This code is free software; you can redistribute it and/or modify it
178N/A * under the terms of the GNU General Public License version 2 only, as
178N/A * published by the Free Software Foundation.
178N/A *
178N/A * This code is distributed in the hope that it will be useful, but WITHOUT
178N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
178N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
178N/A * version 2 for more details (a copy is included in the LICENSE file that
178N/A * accompanied this code).
178N/A *
178N/A * You should have received a copy of the GNU General Public License version
178N/A * 2 along with this work; if not, write to the Free Software Foundation,
178N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
178N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
178N/A */
178N/A
178N/A/*
178N/A * @test
3887N/A * @bug 6610094 7024172
3887N/A * @summary Basic unit test of ManagementFactory.getPlatformMXBean(s)
3887N/A * methods and PlatformManagedObject.getObjectName()
178N/A * @author Mandy Chung
178N/A *
178N/A * @run main GetPlatformMXBeans
178N/A */
178N/A
178N/Aimport java.lang.management.*;
3887N/Aimport java.io.IOException;
178N/Aimport java.util.*;
178N/Aimport javax.management.*;
178N/A
3887N/Aimport static java.lang.management.ManagementFactory.*;
3887N/A
178N/Apublic class GetPlatformMXBeans {
178N/A private static MBeanServer platformMBeanServer =
178N/A getPlatformMBeanServer();
178N/A public static void main(String[] argv) throws Exception {
3887N/A // singleton platform MXBean
178N/A checkPlatformMXBean(getClassLoadingMXBean(),
178N/A ClassLoadingMXBean.class,
178N/A CLASS_LOADING_MXBEAN_NAME);
178N/A checkPlatformMXBean(getCompilationMXBean(),
178N/A CompilationMXBean.class,
178N/A COMPILATION_MXBEAN_NAME);
178N/A checkPlatformMXBean(getMemoryMXBean(),
178N/A MemoryMXBean.class,
178N/A MEMORY_MXBEAN_NAME);
178N/A checkPlatformMXBean(getOperatingSystemMXBean(),
178N/A OperatingSystemMXBean.class,
178N/A OPERATING_SYSTEM_MXBEAN_NAME);
178N/A checkPlatformMXBean(getRuntimeMXBean(),
178N/A RuntimeMXBean.class,
178N/A RUNTIME_MXBEAN_NAME);
178N/A checkPlatformMXBean(getThreadMXBean(),
178N/A ThreadMXBean.class,
178N/A THREAD_MXBEAN_NAME);
3887N/A
3887N/A // the following MXBean can have more than one instances
178N/A checkGarbageCollectorMXBeans(getGarbageCollectorMXBeans());
178N/A checkMemoryManagerMXBeans(getMemoryManagerMXBeans());
178N/A checkMemoryPoolMXBeans(getMemoryPoolMXBeans());
3887N/A
3887N/A // check invalid platform MXBean
3887N/A checkInvalidPlatformMXBean();
178N/A }
178N/A
178N/A private static <T extends PlatformManagedObject>
3887N/A void checkPlatformMXBean(T obj, Class<T> mxbeanInterface,
3887N/A String mxbeanName)
3887N/A throws Exception
178N/A {
3887N/A // getPlatformMXBean may return null if the mxbean is not implemented
3887N/A PlatformManagedObject mxbean = getPlatformMXBean(mxbeanInterface);
3887N/A if (obj != mxbean) {
3887N/A throw new RuntimeException("Singleton MXBean returned not matched");
3887N/A }
3887N/A
3887N/A int numElements = obj == null ? 0 : 1;
178N/A List<? extends PlatformManagedObject> mxbeans =
178N/A getPlatformMXBeans(mxbeanInterface);
178N/A if (mxbeans.size() != numElements) {
178N/A throw new RuntimeException("Unmatched number of platform MXBeans "
178N/A + mxbeans.size() + ". Expected = " + numElements);
178N/A }
178N/A
178N/A if (obj != null) {
3887N/A if (obj != mxbeans.get(0)) {
178N/A throw new RuntimeException("The list returned by getPlatformMXBeans"
178N/A + " not matched");
178N/A }
178N/A ObjectName on = new ObjectName(mxbeanName);
3887N/A if (!on.equals(mxbean.getObjectName())) {
178N/A throw new RuntimeException("Unmatched ObjectName " +
3887N/A mxbean.getObjectName() + " Expected = " + on);
178N/A }
3887N/A checkRemotePlatformMXBean(obj, platformMBeanServer,
3887N/A mxbeanInterface, mxbeanName);
3887N/A }
3887N/A }
3887N/A
3887N/A // verify platform MXBeans in the platform MBeanServer
3887N/A private static <T extends PlatformManagedObject>
3887N/A void checkRemotePlatformMXBean(T obj,
3887N/A MBeanServerConnection mbs,
3887N/A Class<T> mxbeanInterface,
3887N/A String mxbeanName)
3887N/A throws Exception
3887N/A {
3887N/A PlatformManagedObject mxbean = getPlatformMXBean(mbs, mxbeanInterface);
3887N/A if ((obj == null && mxbean != null) || (obj != null && mxbean == null)) {
3887N/A throw new RuntimeException("Singleton MXBean returned not matched");
178N/A }
178N/A
3887N/A int numElements = obj == null ? 0 : 1;
3887N/A List<? extends PlatformManagedObject> mxbeans =
3887N/A getPlatformMXBeans(mbs, mxbeanInterface);
178N/A if (mxbeans.size() != numElements) {
178N/A throw new RuntimeException("Unmatched number of platform MXBeans "
178N/A + mxbeans.size() + ". Expected = " + numElements);
178N/A }
3887N/A
3887N/A ObjectName on = new ObjectName(mxbeanName);
3887N/A if (!on.equals(mxbean.getObjectName())) {
3887N/A throw new RuntimeException("Unmatched ObjectName " +
3887N/A mxbean.getObjectName() + " Expected = " + on);
3887N/A }
178N/A }
178N/A
178N/A private static void checkMemoryManagerMXBeans(List<MemoryManagerMXBean> objs)
178N/A throws Exception
178N/A {
178N/A checkPlatformMXBeans(objs, MemoryManagerMXBean.class);
178N/A for (MemoryManagerMXBean mxbean : objs) {
178N/A String domainAndType;
178N/A if (mxbean instanceof GarbageCollectorMXBean) {
178N/A domainAndType = GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE;
178N/A } else {
178N/A domainAndType = MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE;
178N/A }
178N/A ObjectName on = new ObjectName(domainAndType +
178N/A ",name=" + mxbean.getName());
178N/A if (!on.equals(mxbean.getObjectName())) {
178N/A throw new RuntimeException("Unmatched ObjectName " +
178N/A mxbean.getObjectName() + " Expected = " + on);
178N/A }
178N/A }
178N/A }
178N/A private static void checkMemoryPoolMXBeans(List<MemoryPoolMXBean> objs)
178N/A throws Exception
178N/A {
178N/A checkPlatformMXBeans(objs, MemoryPoolMXBean.class);
178N/A for (MemoryPoolMXBean mxbean : objs) {
178N/A ObjectName on = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE +
178N/A ",name=" + mxbean.getName());
178N/A if (!on.equals(mxbean.getObjectName())) {
178N/A throw new RuntimeException("Unmatched ObjectName " +
178N/A mxbean.getObjectName() + " Expected = " + on);
178N/A }
178N/A }
178N/A }
178N/A
178N/A private static void checkGarbageCollectorMXBeans(List<GarbageCollectorMXBean> objs)
178N/A throws Exception
178N/A {
178N/A checkPlatformMXBeans(objs, GarbageCollectorMXBean.class);
178N/A for (GarbageCollectorMXBean mxbean : objs) {
178N/A ObjectName on = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
178N/A ",name=" + mxbean.getName());
178N/A if (!on.equals(mxbean.getObjectName())) {
178N/A throw new RuntimeException("Unmatched ObjectName " +
178N/A mxbean.getObjectName() + " Expected = " + on);
178N/A }
178N/A }
178N/A }
178N/A
178N/A private static <T extends PlatformManagedObject>
178N/A void checkPlatformMXBeans(List<T> objs, Class<T> mxbeanInterface)
178N/A throws Exception
178N/A {
3887N/A try {
3887N/A getPlatformMXBean(mxbeanInterface);
3887N/A // mxbeanInterface is not a singleton
3887N/A throw new RuntimeException(mxbeanInterface + ": not a singleton MXBean");
3887N/A } catch (IllegalArgumentException e) {
3887N/A // expect IAE
3887N/A }
3887N/A
178N/A // verify local list of platform MXBeans
178N/A List<? extends PlatformManagedObject> mxbeans =
178N/A getPlatformMXBeans(mxbeanInterface);
178N/A if (objs.size() != mxbeans.size()) {
178N/A throw new RuntimeException("Unmatched number of platform MXBeans "
178N/A + mxbeans.size() + ". Expected = " + objs.size());
178N/A }
178N/A List<T> list = new ArrayList<T>(objs);
178N/A for (PlatformManagedObject pmo : mxbeans) {
178N/A if (list.contains(pmo)) {
178N/A list.remove(pmo);
178N/A } else {
178N/A throw new RuntimeException(pmo +
178N/A " not in the platform MXBean list");
178N/A }
178N/A }
178N/A
178N/A if (!list.isEmpty()) {
178N/A throw new RuntimeException("The list returned by getPlatformMXBeans"
178N/A + " not matched");
178N/A }
178N/A
178N/A // verify platform MXBeans in the platform MBeanServer
178N/A mxbeans = getPlatformMXBeans(platformMBeanServer, mxbeanInterface);
178N/A if (objs.size() != mxbeans.size()) {
178N/A throw new RuntimeException("Unmatched number of platform MXBeans "
178N/A + mxbeans.size() + ". Expected = " + objs.size());
178N/A }
178N/A }
3887N/A
3887N/A interface FakeMXBean extends PlatformManagedObject {};
3887N/A
3887N/A private static void checkInvalidPlatformMXBean() throws IOException {
3887N/A try {
3887N/A getPlatformMXBean(FakeMXBean.class);
3887N/A // mxbeanInterface is not a singleton
3887N/A throw new RuntimeException("Expect IllegalArgumentException but not thrown");
3887N/A } catch (IllegalArgumentException e) {
3887N/A // expect IAE
3887N/A }
3887N/A
3887N/A try {
3887N/A getPlatformMXBeans(FakeMXBean.class);
3887N/A // mxbeanInterface is not a singleton
3887N/A throw new RuntimeException("Expect IllegalArgumentException but not thrown");
3887N/A } catch (IllegalArgumentException e) {
3887N/A // expect IAE
3887N/A }
3887N/A
3887N/A try {
3887N/A getPlatformMXBean(platformMBeanServer, FakeMXBean.class);
3887N/A // mxbeanInterface is not a singleton
3887N/A throw new RuntimeException("Expect IllegalArgumentException but not thrown");
3887N/A } catch (IllegalArgumentException e) {
3887N/A // expect IAE
3887N/A }
3887N/A
3887N/A try {
3887N/A getPlatformMXBeans(platformMBeanServer, FakeMXBean.class);
3887N/A // mxbeanInterface is not a singleton
3887N/A throw new RuntimeException("Expect IllegalArgumentException but not thrown");
3887N/A } catch (IllegalArgumentException e) {
3887N/A // expect IAE
3887N/A }
3887N/A }
178N/A}