Path: blob/master/test/jdk/java/lang/management/OperatingSystemMXBean/PlatformMXBeanTest.java
41153 views
/*1* Copyright (c) 2008, 2015, 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 661009426* @summary Test the OperatingSystemMXBean instance returned by27* ManagementFactory.getPlatformMXBeans()28* @author Mandy Chung29*30* @modules java.management31* jdk.management32* @run main PlatformMXBeanTest33*/3435import java.lang.management.*;36import java.util.List;3738public class PlatformMXBeanTest {39public static void main(String[] argv) throws Exception {40OperatingSystemMXBean osMBean = getOSPlatformMXBean(OperatingSystemMXBean.class);4142// There should have only one single MXBean for the OS MXBean interfaces:43// java.lang.management.OperatingSystemMXBean44// com.sun.management.OperatingSystemMXBean45// com.sun.management.UnixOperatingSystemMXBean46if (osMBean != getOSPlatformMXBean(com.sun.management.OperatingSystemMXBean.class)) {47throw new RuntimeException(48"Invalid com.sun.management.OperatingSystemMXBean instance");49}5051if (!System.getProperty("os.name").startsWith("Windows") &&52osMBean != getOSPlatformMXBean(com.sun.management.UnixOperatingSystemMXBean.class)) {53throw new RuntimeException(54"Invalid com.sun.management.UnixOperatingSystemMXBean instance");55}56}5758private static <T extends OperatingSystemMXBean>59T getOSPlatformMXBean(Class<T> c) {60List<T> result = ManagementFactory.getPlatformMXBeans(c);61if (result.isEmpty()) {62return null;63} else if (result.size() == 1) {64return result.get(0);65} else {66throw new RuntimeException(c.getName() + " has " +67result.size() + " number of instances");68}69}70}717273