jdk/test/javax/management/MBeanServer/MBeanExceptionTest.java
author lana
Thu, 04 Nov 2010 15:39:43 -0700
changeset 7205 e6560614a608
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     2
 * Copyright (c) 2004, 2008, 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: 4156
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
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
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    26
 * @bug 5035217 6766173
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that MBean's RuntimeException is wrapped in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * RuntimeMBeanException and (for Standard MBeans) that checked exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * are wrapped in MBeanException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Eamonn McManus
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    31
 * @compile MBeanExceptionTest.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main MBeanExceptionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    35
import java.util.Collections;
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    36
import java.util.Set;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    37
import javax.management.Attribute;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    38
import javax.management.AttributeList;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    39
import javax.management.DynamicMBean;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    40
import javax.management.MBeanException;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    41
import javax.management.MBeanInfo;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    42
import javax.management.MBeanServer;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    43
import javax.management.MBeanServerFactory;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    44
import javax.management.ObjectName;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    45
import javax.management.RuntimeMBeanException;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    46
import javax.management.StandardMBean;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class MBeanExceptionTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        System.out.println("Test that if an MBean throws RuntimeException " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                           "it is wrapped in RuntimeMBeanException,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        System.out.println("and if a Standard MBean throws Exception " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                           "it is wrapped in MBeanException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Object standard = new Except();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ObjectName standardName = new ObjectName(":name=Standard MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Object standardMBean =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            new StandardMBean(new Except(), ExceptMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        ObjectName standardMBeanName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            new ObjectName(":name=Instance of StandardMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Object dynamic = new DynamicExcept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ObjectName dynamicName = new ObjectName(":name=Dynamic MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        mbs.registerMBean(standard, standardName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        mbs.registerMBean(standardMBean, standardMBeanName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        mbs.registerMBean(dynamic, dynamicName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        failures += test(mbs, standardName, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        failures += test(mbs, standardMBeanName, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        failures += test(mbs, dynamicName, false);
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    70
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    71
        final boolean[] booleans = {false, true};
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    72
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    73
        for (boolean runtimeX : booleans) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    74
            Class<? extends Exception> excC =
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    75
                    runtimeX ? RuntimeMBeanException.class : MBeanException.class;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    76
            String excS =
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    77
                    runtimeX ? "a RuntimeMBeanException" : "an MBeanException";
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    78
            String mbsS = "a plain MBeanServer";
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    79
            System.out.println(
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    80
                    "Test that, with " + mbsS + ", " + excS + " is wrapped " +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    81
                    "in " + excS);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    82
            // E.g. "Test that, with a plain MBeanServer, an MBeanException
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    83
            // is wrapped in an MBeanException".
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    84
            try {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    85
                mbs.createMBean(
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    86
                        Except.class.getName(), new ObjectName(":name=Oops"),
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    87
                        new Object[] {runtimeX},
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    88
                        new String[] {boolean.class.getName()});
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    89
                System.out.println(
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    90
                        "FAIL: createMBean succeeded but should not have");
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    91
                failures++;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    92
            } catch (Exception e) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    93
                if (!excC.isInstance(e)) {
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    94
                    System.out.println(
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    95
                            "FAIL: expected " + excC.getName() + " from " +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    96
                            "createMBean, got " + e);
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
    97
                    failures++;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    98
                } else {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
    99
                    Throwable cause = e.getCause();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
   100
                    if (!excC.isInstance(cause)) {
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   101
                        System.out.println(
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
   102
                                "FAIL: expected " + excC.getName() +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
   103
                                " as cause of " + excC.getName() +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
   104
                                ", got " + e);
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   105
                        failures++;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
   106
                    } else
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1639
diff changeset
   107
                        System.out.println("...ok");
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   108
                }
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   109
            }
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   110
        }
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (failures == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println("TEST FAILED: " + failures + " failure(s)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static int test(MBeanServer mbs, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            boolean testChecked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println("--------" + name + "--------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        final String[] ops = {"getAttribute", "setAttribute", "invoke"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        final int GET = 0, SET = 1, INVOKE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        final String[] targets = {"UncheckedException", "CheckedException"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        final int UNCHECKED = 0, CHECKED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        for (int i = 0; i < ops.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            for (int j = 0; j < targets.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                if (j == CHECKED && !testChecked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                String target = targets[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                String what = ops[i] + "/" + target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                System.out.println(what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    case GET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        mbs.getAttribute(name, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    case SET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        mbs.setAttribute(name, new Attribute(target, "x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    case INVOKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        mbs.invoke(name, target, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    System.out.println("failure: " + what + " returned!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                } catch (RuntimeMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    if (j == CHECKED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        System.out.println("failure: RuntimeMBeanException " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                           "when checked expected: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        if (cause == theUncheckedException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            System.out.println("ok: " + what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            System.out.println("failure: " + what +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                               " wrapped " + cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                            failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                } catch (MBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    if (j == UNCHECKED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        System.out.println("failure: checked exception " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                           "when unchecked expected: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        if (cause == theCheckedException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            System.out.println("ok: " + what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            System.out.println("failure: " + what +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                               " wrapped " + cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    System.out.println("failure: " + what + " threw: " + t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    while ((t = t.getCause()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        System.out.println("  ... " + t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return failures;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public static interface ExceptMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        public String getUncheckedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        public void setUncheckedException(String x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        public void UncheckedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        public String getCheckedException() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        public void setCheckedException(String x) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        public void CheckedException() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public static class Except implements ExceptMBean {
1517
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   209
        public Except() {}
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   210
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   211
        public Except(boolean runtimeX) throws MBeanException {
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   212
            if (runtimeX)
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   213
                throw new RuntimeMBeanException(new RuntimeException(), "Bang");
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   214
            else
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   215
                throw new MBeanException(new Exception(), "Bang");
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   216
        }
4ca99a6fc74e 6766173: Spec should say that createMBean wraps a constructor RuntimeException in a RuntimeMBeanException
emcmanus
parents: 2
diff changeset
   217
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        public String getUncheckedException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw theUncheckedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        public void setUncheckedException(String x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw theUncheckedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        public void UncheckedException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw theUncheckedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        public String getCheckedException() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw theCheckedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        public void setCheckedException(String x) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw theCheckedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        public void CheckedException() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw theCheckedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public static class DynamicExcept implements DynamicMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        public Object getAttribute(String attrName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                throws MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (attrName.equals("UncheckedException"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                throw theUncheckedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        public void setAttribute(Attribute attr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                throws MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            String attrName = attr.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (attrName.equals("UncheckedException"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                throw theUncheckedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        public Object invoke(String opName, Object[] params, String[] sig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                throws MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            assert params == null && sig == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (opName.equals("UncheckedException"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                throw theUncheckedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        public AttributeList getAttributes(String[] names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        public AttributeList setAttributes(AttributeList attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        public MBeanInfo getMBeanInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                return new StandardMBean(new Except(), ExceptMBean.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    .getMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private static final Exception theCheckedException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        new Exception("The checked exception that should be seen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private static final RuntimeException theUncheckedException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        new UnsupportedOperationException("The unchecked exception " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                          "that should be seen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}