jdk/test/javax/management/MBeanServer/MBeanTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 18805 b359f8adc8ad
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 18805
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     4
 *
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     8
 *
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    13
 * accompanied this code).
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    14
 *
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    18
 *
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    21
 * questions.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    22
 */
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    23
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    24
import javax.management.MBeanServer;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    25
import javax.management.MBeanServerFactory;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    26
import javax.management.NotCompliantMBeanException;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    27
import javax.management.ObjectName;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    28
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    29
/*
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    30
 * @test
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    31
 * @bug 8010285
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    32
 * @summary General MBean test.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    33
 * @author Jaroslav Bachorik
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 18805
diff changeset
    34
 * @modules java.management
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    35
 * @run clean MBeanTest
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    36
 * @run build MBeanTest
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    37
 * @run main MBeanTest
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    38
 */
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    39
public class MBeanTest {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    40
    private static interface PrivateMBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    41
        public int[] getInts();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    42
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    43
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    44
    public static class Private implements PrivateMBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    45
        public int[] getInts() {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    46
            return new int[]{1,2,3};
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    47
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    48
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    49
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    50
    public static interface NonCompliantMBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    51
        public boolean getInt();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    52
        public boolean isInt();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    53
        public void setInt(int a);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    54
        public void setInt(long b);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    55
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    56
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    57
    public static class NonCompliant implements NonCompliantMBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    58
        public boolean getInt() {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    59
            return false;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    60
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    61
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    62
        public boolean isInt() {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    63
            return true;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    64
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    65
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    66
        public void setInt(int a) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    67
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    68
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    69
        public void setInt(long b) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    70
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    71
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    72
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    73
    public static interface CompliantMBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    74
        public boolean isFlag();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    75
        public int getInt();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    76
        public void setInt(int value);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    77
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    78
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    79
    public static class Compliant implements CompliantMBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    80
        public boolean isFlag() {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    81
            return false;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    82
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    83
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    84
        public int getInt() {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    85
            return 1;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    86
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    87
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    88
        public void setInt(int value) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    89
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    90
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    91
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    92
    private static int failures = 0;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    93
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    94
    public static void main(String[] args) throws Exception {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    95
        testCompliant(CompliantMBean.class, new Compliant());
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    96
        testNonCompliant(PrivateMBean.class, new Private());
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    97
        testNonCompliant(NonCompliantMBean.class, new NonCompliant());
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    98
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    99
        if (failures == 0)
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   100
            System.out.println("Test passed");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   101
        else
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   102
            throw new Exception("TEST FAILURES: " + failures);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   103
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   104
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   105
    private static void fail(String msg) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   106
        failures++;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   107
        System.out.println("FAIL: " + msg);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   108
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   109
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   110
    private static void success(String msg) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   111
        System.out.println("OK: " + msg);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   112
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   113
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   114
    private static void testNonCompliant(Class<?> iface, Object bean) throws Exception {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   115
        try {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   116
            System.out.println("Registering a non-compliant MBean " +
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   117
                                iface.getName() + " ...");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   118
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   119
            MBeanServer mbs = MBeanServerFactory.newMBeanServer();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   120
            ObjectName on = new ObjectName("test:type=NonCompliant");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   121
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   122
            mbs.registerMBean(bean, on);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   123
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   124
            fail("Registered a non-compliant MBean - " + iface.getName());
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   125
        } catch (Exception e) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   126
            Throwable t = e;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   127
            while (t != null && !(t instanceof NotCompliantMBeanException)) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   128
                t = t.getCause();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   129
            }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   130
            if (t != null) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   131
                success("MBean not registered");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   132
            } else {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   133
                throw e;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   134
            }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   135
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   136
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   137
    private static void testCompliant(Class<?> iface, Object bean) throws Exception {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   138
        try {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   139
            System.out.println("Registering a compliant MBean " +
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   140
                                iface.getName() + " ...");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   141
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   142
            MBeanServer mbs = MBeanServerFactory.newMBeanServer();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   143
            ObjectName on = new ObjectName("test:type=Compliant");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   144
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   145
            mbs.registerMBean(bean, on);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   146
            success("Registered a compliant MBean - " + iface.getName());
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   147
        } catch (Exception e) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   148
            Throwable t = e;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   149
            while (t != null && !(t instanceof NotCompliantMBeanException)) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   150
                t = t.getCause();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   151
            }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   152
            if (t != null) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   153
                fail("MBean not registered");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   154
            } else {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   155
                throw e;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   156
            }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   157
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   158
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
   159
}