test/jdk/com/sun/management/VMOptionOpenDataTest.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
     1
/*
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
     4
 *
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
     8
 *
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    13
 * accompanied this code).
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    14
 *
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    18
 *
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    21
 * questions.
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    22
 */
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    23
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    24
import com.sun.management.HotSpotDiagnosticMXBean;
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    25
import com.sun.management.VMOption;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    26
import java.lang.management.ManagementFactory;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    27
import java.util.Arrays;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    28
import java.util.Set;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    29
import java.util.stream.Collectors;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    30
import javax.management.MBeanServerConnection;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    31
import javax.management.openmbean.CompositeData;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    32
import javax.management.openmbean.CompositeType;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    33
import javax.management.openmbean.OpenType;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    34
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    35
import static javax.management.openmbean.SimpleType.*;
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    36
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    37
/*
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    38
 * @test
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    39
 * @bug     8042901
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    40
 * @summary Check that MappedMXBeanType.toOpenTypeData supports VMOption
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    41
 * @author  Shanliang Jiang
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    42
 */
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    43
public class VMOptionOpenDataTest {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    44
    private static final String[] names = new String[] {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    45
        "name", "value", "origin", "writeable"
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    46
    };
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    47
    private static final OpenType[] types = new OpenType[] {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    48
        STRING, STRING, STRING, BOOLEAN
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    49
    };
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    50
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    51
    public static void main(String... args) throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    52
        MBeanServerConnection msc = ManagementFactory.getPlatformMBeanServer();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    53
        HotSpotDiagnosticMXBean mxbean =
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    54
            ManagementFactory.getPlatformMXBean(msc, HotSpotDiagnosticMXBean.class);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    55
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    56
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    57
        String[] signatures = new String[] {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    58
            String.class.getName()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    59
        };
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    60
        Object obj = msc.invoke(mxbean.getObjectName(), "getVMOption",
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    61
            new String[] { "PrintVMOptions"}, signatures);
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    62
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    63
        CompositeData data = (CompositeData)obj;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    64
        validateType(data);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    65
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    66
        VMOption option = mxbean.getVMOption("PrintVMOptions");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    67
        VMOption o = VMOption.from(data);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    68
        assertEquals(option, o);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    69
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    70
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    71
    private static void validateType(CompositeData data) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    72
        CompositeType type = data.getCompositeType();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    73
        Set<String> keys = Arrays.stream(names).collect(Collectors.toSet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    74
        if (!type.keySet().equals(keys)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    75
            throw new RuntimeException("key not matched: " + type.keySet().toString());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    76
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    77
        for (int i=0; i < names.length; i++) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    78
            OpenType t = type.getType(names[i]);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    79
            if (t != types[i]) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    80
                throw new AssertionError(names[i] + ": type not matched: " +
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    81
                    t + " expected: " + types[i]);
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    82
            }
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    83
        }
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    84
    }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    85
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    86
    private static void assertEquals(VMOption o1, VMOption o2) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    87
        if (!o1.getName().equals(o2.getName()) ||
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    88
            !o1.getOrigin().equals(o2.getOrigin()) ||
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    89
            !o1.getValue().equals(o2.getValue()) ||
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    90
            o1.isWriteable() != o2.isWriteable()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    91
            throw new AssertionError(o1 + " != " + o2);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    92
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    93
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    94
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 30820
diff changeset
    95
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff changeset
    96
}