Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/management/HotSpotDiagnosticMXBean/SetVMOption.java
38855 views
/*1* Copyright (c) 2005, 2014, 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 631491326* @summary Basic Test for HotSpotDiagnosticMXBean.setVMOption()27* and getDiagnosticOptions().28* @author Mandy Chung29* @author Jaroslav Bachorik30*31* @run main/othervm -XX:+PrintGCDetails SetVMOption32*/3334import java.lang.management.ManagementFactory;35import java.util.*;36import com.sun.management.HotSpotDiagnosticMXBean;37import com.sun.management.VMOption;38import com.sun.management.VMOption.Origin;3940public class SetVMOption {41private static final String PRINT_GC_DETAILS = "PrintGCDetails";42private static final String EXPECTED_VALUE = "true";43private static final String BAD_VALUE = "yes";44private static final String NEW_VALUE = "false";45private static final String MANAGEMENT_SERVER = "ManagementServer";46private static HotSpotDiagnosticMXBean mbean;4748public static void main(String[] args) throws Exception {49mbean =50ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);5152VMOption option = findPrintGCDetailsOption();53if (!option.getValue().equalsIgnoreCase(EXPECTED_VALUE)) {54throw new RuntimeException("Unexpected value: " +55option.getValue() + " expected: " + EXPECTED_VALUE);56}57if (option.getOrigin() != Origin.VM_CREATION) {58throw new RuntimeException("Unexpected origin: " +59option.getOrigin() + " expected: VM_CREATION");60}61if (!option.isWriteable()) {62throw new RuntimeException("Expected " + PRINT_GC_DETAILS +63" to be writeable");64}6566// set VM option to a new value67mbean.setVMOption(PRINT_GC_DETAILS, NEW_VALUE);6869option = findPrintGCDetailsOption();70if (!option.getValue().equalsIgnoreCase(NEW_VALUE)) {71throw new RuntimeException("Unexpected value: " +72option.getValue() + " expected: " + NEW_VALUE);73}74if (option.getOrigin() != Origin.MANAGEMENT) {75throw new RuntimeException("Unexpected origin: " +76option.getOrigin() + " expected: MANAGEMENT");77}78VMOption o = mbean.getVMOption(PRINT_GC_DETAILS);79if (!option.getValue().equals(o.getValue())) {80throw new RuntimeException("Unmatched value: " +81option.getValue() + " expected: " + o.getValue());82}83if (!option.getValue().equals(o.getValue())) {84throw new RuntimeException("Unmatched value: " +85option.getValue() + " expected: " + o.getValue());86}87if (option.getOrigin() != o.getOrigin()) {88throw new RuntimeException("Unmatched origin: " +89option.getOrigin() + " expected: " + o.getOrigin());90}91if (option.isWriteable() != o.isWriteable()) {92throw new RuntimeException("Unmatched writeable: " +93option.isWriteable() + " expected: " + o.isWriteable());94}9596// check if ManagementServer is not writeable97List<VMOption> options = mbean.getDiagnosticOptions();98VMOption mgmtServerOption = null;99for (VMOption o1 : options) {100if (o1.getName().equals(MANAGEMENT_SERVER)) {101mgmtServerOption = o1;102break;103}104}105if (mgmtServerOption != null) {106throw new RuntimeException(MANAGEMENT_SERVER +107" is not expected to be writeable");108}109mgmtServerOption = mbean.getVMOption(MANAGEMENT_SERVER);110if (mgmtServerOption == null) {111throw new RuntimeException(MANAGEMENT_SERVER +112" should exist.");113}114if (mgmtServerOption.getOrigin() != Origin.DEFAULT) {115throw new RuntimeException(MANAGEMENT_SERVER +116" should have the default value.");117}118if (mgmtServerOption.isWriteable()) {119throw new RuntimeException(MANAGEMENT_SERVER +120" is not expected to be writeable");121}122}123124public static VMOption findPrintGCDetailsOption() {125List<VMOption> options = mbean.getDiagnosticOptions();126VMOption gcDetails = null;127for (VMOption o : options) {128if (o.getName().equals(PRINT_GC_DETAILS)) {129gcDetails = o;130break;131}132}133if (gcDetails == null) {134throw new RuntimeException("VM option " + PRINT_GC_DETAILS +135" not found");136}137return gcDetails;138}139}140141142