Path: blob/master/test/jdk/com/sun/management/VMOptionOpenDataTest.java
41149 views
/*1* Copyright (c) 2015, 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*/2223import com.sun.management.HotSpotDiagnosticMXBean;24import com.sun.management.VMOption;25import java.lang.management.ManagementFactory;26import java.util.Arrays;27import java.util.Set;28import java.util.stream.Collectors;29import javax.management.MBeanServerConnection;30import javax.management.openmbean.CompositeData;31import javax.management.openmbean.CompositeType;32import javax.management.openmbean.OpenType;3334import static javax.management.openmbean.SimpleType.*;3536/*37* @test38* @bug 804290139* @summary Check that MappedMXBeanType.toOpenTypeData supports VMOption40* @author Shanliang Jiang41*/42public class VMOptionOpenDataTest {43private static final String[] names = new String[] {44"name", "value", "origin", "writeable"45};46private static final OpenType[] types = new OpenType[] {47STRING, STRING, STRING, BOOLEAN48};4950public static void main(String... args) throws Exception {51MBeanServerConnection msc = ManagementFactory.getPlatformMBeanServer();52HotSpotDiagnosticMXBean mxbean =53ManagementFactory.getPlatformMXBean(msc, HotSpotDiagnosticMXBean.class);545556String[] signatures = new String[] {57String.class.getName()58};59Object obj = msc.invoke(mxbean.getObjectName(), "getVMOption",60new String[] { "PrintVMOptions"}, signatures);6162CompositeData data = (CompositeData)obj;63validateType(data);6465VMOption option = mxbean.getVMOption("PrintVMOptions");66VMOption o = VMOption.from(data);67assertEquals(option, o);68}6970private static void validateType(CompositeData data) {71CompositeType type = data.getCompositeType();72Set<String> keys = Arrays.stream(names).collect(Collectors.toSet());73if (!type.keySet().equals(keys)) {74throw new RuntimeException("key not matched: " + type.keySet().toString());75}76for (int i=0; i < names.length; i++) {77OpenType t = type.getType(names[i]);78if (t != types[i]) {79throw new AssertionError(names[i] + ": type not matched: " +80t + " expected: " + types[i]);81}82}83}8485private static void assertEquals(VMOption o1, VMOption o2) {86if (!o1.getName().equals(o2.getName()) ||87!o1.getOrigin().equals(o2.getOrigin()) ||88!o1.getValue().equals(o2.getValue()) ||89o1.isWriteable() != o2.isWriteable()) {90throw new AssertionError(o1 + " != " + o2);91}9293}9495}969798