jdk/test/com/sun/management/HotSpotDiagnosticMXBean/SetVMOption.java
changeset 2 90ce3da70b43
child 401 ef01e0dccd63
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug     6314913
       
    27  * @summary Basic Test for HotSpotDiagnosticMXBean.setVMOption()
       
    28  *          and getDiagnosticOptions().
       
    29  * @author  Mandy Chung
       
    30  *
       
    31  * @run main/othervm -XX:+PrintGCDetails SetVMOption
       
    32  */
       
    33 
       
    34 import java.util.*;
       
    35 import com.sun.management.HotSpotDiagnosticMXBean;
       
    36 import com.sun.management.VMOption;
       
    37 import com.sun.management.VMOption.Origin;
       
    38 import sun.management.ManagementFactory;
       
    39 import sun.misc.Version;
       
    40 
       
    41 public class SetVMOption {
       
    42     private static String PRINT_GC_DETAILS = "PrintGCDetails";
       
    43     private static String EXPECTED_VALUE = "true";
       
    44     private static String BAD_VALUE = "yes";
       
    45     private static String NEW_VALUE = "false";
       
    46     private static String MANAGEMENT_SERVER = "ManagementServer";
       
    47     private static HotSpotDiagnosticMXBean mbean =
       
    48         ManagementFactory.getDiagnosticMXBean();
       
    49 
       
    50     public static void main(String[] args) throws Exception {
       
    51 
       
    52         // The following test is transitional only and should be removed
       
    53         // once build 52 is promoted.
       
    54         int build = Version.jvmBuildNumber();
       
    55         if (build > 0 && build < 52) {
       
    56              // JVM support is integrated in build 52
       
    57              // this test is skipped if running with VM earlier than 52
       
    58              return;
       
    59         }
       
    60 
       
    61         VMOption option = findPrintGCDetailsOption();
       
    62         if (!option.getValue().equalsIgnoreCase(EXPECTED_VALUE)) {
       
    63             throw new RuntimeException("Unexpected value: " +
       
    64                 option.getValue() + " expected: " + EXPECTED_VALUE);
       
    65         }
       
    66         if (option.getOrigin() != Origin.VM_CREATION) {
       
    67             throw new RuntimeException("Unexpected origin: " +
       
    68                 option.getOrigin() + " expected: VM_CREATION");
       
    69         }
       
    70         if (!option.isWriteable()) {
       
    71             throw new RuntimeException("Expected " + PRINT_GC_DETAILS +
       
    72                 " to be writeable");
       
    73         }
       
    74 
       
    75         // set VM option to a new value
       
    76         mbean.setVMOption(PRINT_GC_DETAILS, NEW_VALUE);
       
    77 
       
    78         option = findPrintGCDetailsOption();
       
    79         if (!option.getValue().equalsIgnoreCase(NEW_VALUE)) {
       
    80             throw new RuntimeException("Unexpected value: " +
       
    81                 option.getValue() + " expected: " + NEW_VALUE);
       
    82         }
       
    83         if (option.getOrigin() != Origin.MANAGEMENT) {
       
    84             throw new RuntimeException("Unexpected origin: " +
       
    85                 option.getOrigin() + " expected: MANAGEMENT");
       
    86         }
       
    87         VMOption o = mbean.getVMOption(PRINT_GC_DETAILS);
       
    88         if (!option.getValue().equals(o.getValue())) {
       
    89             throw new RuntimeException("Unmatched value: " +
       
    90                 option.getValue() + " expected: " + o.getValue());
       
    91         }
       
    92         if (!option.getValue().equals(o.getValue())) {
       
    93             throw new RuntimeException("Unmatched value: " +
       
    94                 option.getValue() + " expected: " + o.getValue());
       
    95         }
       
    96         if (option.getOrigin() != o.getOrigin()) {
       
    97             throw new RuntimeException("Unmatched origin: " +
       
    98                 option.getOrigin() + " expected: " + o.getOrigin());
       
    99         }
       
   100         if (option.isWriteable() != o.isWriteable()) {
       
   101             throw new RuntimeException("Unmatched writeable: " +
       
   102                 option.isWriteable() + " expected: " + o.isWriteable());
       
   103         }
       
   104 
       
   105         // check if ManagementServer is not writeable
       
   106         List<VMOption> options = mbean.getDiagnosticOptions();
       
   107         VMOption mgmtServerOption = null;
       
   108         for (VMOption o1 : options) {
       
   109             if (o1.getName().equals(MANAGEMENT_SERVER)) {
       
   110                  mgmtServerOption = o1;
       
   111                  break;
       
   112             }
       
   113         }
       
   114         if (mgmtServerOption != null) {
       
   115             throw new RuntimeException(MANAGEMENT_SERVER +
       
   116                 " is not expected to be writeable");
       
   117         }
       
   118         mgmtServerOption = mbean.getVMOption(MANAGEMENT_SERVER);
       
   119         if (mgmtServerOption == null) {
       
   120             throw new RuntimeException(MANAGEMENT_SERVER +
       
   121                 " should exist.");
       
   122         }
       
   123         if (mgmtServerOption.getOrigin() != Origin.DEFAULT) {
       
   124             throw new RuntimeException(MANAGEMENT_SERVER +
       
   125                 " should have the default value.");
       
   126         }
       
   127         if (mgmtServerOption.isWriteable()) {
       
   128             throw new RuntimeException(MANAGEMENT_SERVER +
       
   129                 " is not expected to be writeable");
       
   130         }
       
   131     }
       
   132 
       
   133     public static VMOption findPrintGCDetailsOption() {
       
   134         List<VMOption> options = mbean.getDiagnosticOptions();
       
   135         VMOption gcDetails = null;
       
   136         for (VMOption o : options) {
       
   137             if (o.getName().equals(PRINT_GC_DETAILS)) {
       
   138                  gcDetails = o;
       
   139                  break;
       
   140             }
       
   141         }
       
   142         if (gcDetails == null) {
       
   143             throw new RuntimeException("VM option " + PRINT_GC_DETAILS +
       
   144                 " not found");
       
   145         }
       
   146         return gcDetails;
       
   147     }
       
   148 }