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
178N/A * @bug 6610094
178N/A * @summary Test the OperatingSystemMXBean instance returned by
178N/A * ManagementFactory.getPlatformMXBeans()
178N/A * @author Mandy Chung
178N/A *
178N/A * @run main PlatformMXBeanTest
178N/A */
178N/A
178N/Aimport java.lang.management.*;
178N/Aimport java.util.List;
178N/A
178N/Apublic class PlatformMXBeanTest {
178N/A public static void main(String[] argv) throws Exception {
178N/A OperatingSystemMXBean osMBean = getOSPlatformMXBean(OperatingSystemMXBean.class);
178N/A
178N/A // There should have only one single MXBean for the OS MXBean interfaces:
178N/A // java.lang.management.OperatingSystemMXBean
178N/A // com.sun.management.OperatingSystemMXBean
178N/A // com.sun.management.UnixOperatingSystemMXBean
178N/A if (osMBean != getOSPlatformMXBean(com.sun.management.OperatingSystemMXBean.class)) {
178N/A throw new RuntimeException(
178N/A "Invalid com.sun.management.OperatingSystemMXBean instance");
178N/A }
178N/A
178N/A if (!System.getProperty("os.name").startsWith("Windows") &&
178N/A osMBean != getOSPlatformMXBean(com.sun.management.UnixOperatingSystemMXBean.class)) {
178N/A throw new RuntimeException(
178N/A "Invalid com.sun.management.UnixOperatingSystemMXBean instance");
178N/A }
178N/A }
178N/A
178N/A private static <T extends OperatingSystemMXBean>
178N/A T getOSPlatformMXBean(Class<T> c) {
178N/A List<T> result = ManagementFactory.getPlatformMXBeans(c);
178N/A if (result.isEmpty()) {
178N/A return null;
178N/A } else if (result.size() == 1) {
178N/A return result.get(0);
178N/A } else {
178N/A throw new RuntimeException(c.getName() + " has " +
178N/A result.size() + " number of instances");
178N/A }
178N/A }
178N/A}