jdk/test/java/lang/management/ManagementFactory/MXBeanProxyTest.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5808 3a1f603c5ca7
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5808
diff changeset
     2
 * Copyright (c) 2004, 2010, 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: 4347
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
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     5024531
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic Test for ManagementFactory.newPlatformMXBean().
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    28
 * @run main/othervm MXBeanProxyTest
2
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
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.management.ClassLoadingMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.management.RuntimeMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static java.lang.management.ManagementFactory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class MXBeanProxyTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static MBeanServer server = getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        boolean iae = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            // Get a MXBean proxy with invalid name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            newPlatformMXBeanProxy(server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                                   "Invalid ObjectName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                                   RuntimeMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            // Expected exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            System.out.println("EXPECTED: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            iae = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (!iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            throw new RuntimeException("Invalid ObjectName " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                " was not detected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            // Get a MXBean proxy with non existent MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            newPlatformMXBeanProxy(server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                                   "java.lang:type=Foo",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                   RuntimeMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            iae = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            // Expected exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.println("EXPECTED: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            iae = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (!iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new RuntimeException("Non existent MXBean " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                " was not detected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            // Mismatch MXBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            newPlatformMXBeanProxy(server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                   RUNTIME_MXBEAN_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                   ClassLoadingMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            iae = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            // Expected exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.println("EXPECTED: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            iae = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (!iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new RuntimeException("Mismatched MXBean interface " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                " was not detected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        final FooMBean foo = new Foo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        final ObjectName objName = new ObjectName("java.lang:type=Foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        server.registerMBean(foo, objName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // non-platform MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            newPlatformMXBeanProxy(server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                   "java.lang:type=Foo",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                   FooMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            iae = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // Expected exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            System.out.println("EXPECTED: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            iae = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (!iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new RuntimeException("Non-platform MXBean " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                " was not detected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Successfully get MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        RuntimeMXBean rm = newPlatformMXBeanProxy(server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                                  RUNTIME_MXBEAN_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                                  RuntimeMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.println("VM uptime = " + rm.getUptime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public interface FooMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        public boolean isFoo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static class Foo implements FooMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        public boolean isFoo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}