jdk/test/com/sun/management/HotSpotDiagnosticMXBean/SetVMOption.java
author iignatyev
Wed, 15 Mar 2017 22:48:59 -0700
changeset 44423 306c020eb154
parent 35246 05ced3076fd8
permissions -rw-r--r--
8176176: fix @modules in jdk_svc tests Reviewed-by: shurailine, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 27493
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * questions.
2
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
18801
ff4643bb836d 8019826: Test com/sun/management/HotSpotDiagnosticMXBean/SetVMOption.java fails with NPE
jbachorik
parents: 5506
diff changeset
    30
 * @author  Jaroslav Bachorik
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
    32
 * @run main/othervm -XX:+HeapDumpOnOutOfMemoryError SetVMOption
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    35
import java.lang.management.ManagementFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.management.HotSpotDiagnosticMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.management.VMOption;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.management.VMOption.Origin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class SetVMOption {
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
    42
    private static final String HEAP_DUMP_ON_OOM = "HeapDumpOnOutOfMemoryError";
27493
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 23010
diff changeset
    43
    private static final String EXPECTED_VALUE = "true";
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 23010
diff changeset
    44
    private static final String BAD_VALUE = "yes";
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 23010
diff changeset
    45
    private static final String NEW_VALUE = "false";
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 23010
diff changeset
    46
    private static final String MANAGEMENT_SERVER = "ManagementServer";
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    47
    private static HotSpotDiagnosticMXBean mbean;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
18801
ff4643bb836d 8019826: Test com/sun/management/HotSpotDiagnosticMXBean/SetVMOption.java fails with NPE
jbachorik
parents: 5506
diff changeset
    50
        mbean =
ff4643bb836d 8019826: Test com/sun/management/HotSpotDiagnosticMXBean/SetVMOption.java fails with NPE
jbachorik
parents: 5506
diff changeset
    51
            ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
    53
        VMOption option = findHeapDumpOnOomOption();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (!option.getValue().equalsIgnoreCase(EXPECTED_VALUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            throw new RuntimeException("Unexpected value: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                option.getValue() + " expected: " + EXPECTED_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (option.getOrigin() != Origin.VM_CREATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throw new RuntimeException("Unexpected origin: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                option.getOrigin() + " expected: VM_CREATION");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (!option.isWriteable()) {
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
    63
            throw new RuntimeException("Expected " + HEAP_DUMP_ON_OOM +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                " to be writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // set VM option to a new value
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
    68
        mbean.setVMOption(HEAP_DUMP_ON_OOM, NEW_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
    70
        option = findHeapDumpOnOomOption();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (!option.getValue().equalsIgnoreCase(NEW_VALUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new RuntimeException("Unexpected value: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                option.getValue() + " expected: " + NEW_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (option.getOrigin() != Origin.MANAGEMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new RuntimeException("Unexpected origin: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                option.getOrigin() + " expected: MANAGEMENT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
    79
        VMOption o = mbean.getVMOption(HEAP_DUMP_ON_OOM);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (!option.getValue().equals(o.getValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new RuntimeException("Unmatched value: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                option.getValue() + " expected: " + o.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (!option.getValue().equals(o.getValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new RuntimeException("Unmatched value: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                option.getValue() + " expected: " + o.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (option.getOrigin() != o.getOrigin()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new RuntimeException("Unmatched origin: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                option.getOrigin() + " expected: " + o.getOrigin());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (option.isWriteable() != o.isWriteable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new RuntimeException("Unmatched writeable: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                option.isWriteable() + " expected: " + o.isWriteable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // check if ManagementServer is not writeable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        List<VMOption> options = mbean.getDiagnosticOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        VMOption mgmtServerOption = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        for (VMOption o1 : options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (o1.getName().equals(MANAGEMENT_SERVER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                 mgmtServerOption = o1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                 break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (mgmtServerOption != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new RuntimeException(MANAGEMENT_SERVER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                " is not expected to be writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        mgmtServerOption = mbean.getVMOption(MANAGEMENT_SERVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (mgmtServerOption == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new RuntimeException(MANAGEMENT_SERVER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                " should exist.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (mgmtServerOption.getOrigin() != Origin.DEFAULT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new RuntimeException(MANAGEMENT_SERVER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                " should have the default value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (mgmtServerOption.isWriteable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new RuntimeException(MANAGEMENT_SERVER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                " is not expected to be writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
   125
    public static VMOption findHeapDumpOnOomOption() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        List<VMOption> options = mbean.getDiagnosticOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        VMOption gcDetails = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        for (VMOption o : options) {
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
   129
            if (o.getName().equals(HEAP_DUMP_ON_OOM)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                 gcDetails = o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                 break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (gcDetails == null) {
35246
05ced3076fd8 8145092: Use Unified Logging for the GC logging
david
parents: 30376
diff changeset
   135
            throw new RuntimeException("VM option " + HEAP_DUMP_ON_OOM +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return gcDetails;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}