Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/OperatingSystemMXBean/PlatformMXBeanTest.java
38828 views
/*1* Copyright (c) 2008, 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* @run main PlatformMXBeanTest31*/3233import java.lang.management.*;34import java.util.List;3536public class PlatformMXBeanTest {37public static void main(String[] argv) throws Exception {38OperatingSystemMXBean osMBean = getOSPlatformMXBean(OperatingSystemMXBean.class);3940// There should have only one single MXBean for the OS MXBean interfaces:41// java.lang.management.OperatingSystemMXBean42// com.sun.management.OperatingSystemMXBean43// com.sun.management.UnixOperatingSystemMXBean44if (osMBean != getOSPlatformMXBean(com.sun.management.OperatingSystemMXBean.class)) {45throw new RuntimeException(46"Invalid com.sun.management.OperatingSystemMXBean instance");47}4849if (!System.getProperty("os.name").startsWith("Windows") &&50osMBean != getOSPlatformMXBean(com.sun.management.UnixOperatingSystemMXBean.class)) {51throw new RuntimeException(52"Invalid com.sun.management.UnixOperatingSystemMXBean instance");53}54}5556private static <T extends OperatingSystemMXBean>57T getOSPlatformMXBean(Class<T> c) {58List<T> result = ManagementFactory.getPlatformMXBeans(c);59if (result.isEmpty()) {60return null;61} else if (result.size() == 1) {62return result.get(0);63} else {64throw new RuntimeException(c.getName() + " has " +65result.size() + " number of instances");66}67}68}697071