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