MBeanExceptionTest.java revision 11884:b45c81ca8671
1/*
2 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 5035217 6766173
27 * @summary Test that MBean's RuntimeException is wrapped in
28 * RuntimeMBeanException and (for Standard MBeans) that checked exceptions
29 * are wrapped in MBeanException
30 * @author Eamonn McManus
31 * @modules java.management
32 * @compile MBeanExceptionTest.java
33 * @run main MBeanExceptionTest
34 */
35
36import java.util.Collections;
37import java.util.Set;
38import javax.management.Attribute;
39import javax.management.AttributeList;
40import javax.management.DynamicMBean;
41import javax.management.MBeanException;
42import javax.management.MBeanInfo;
43import javax.management.MBeanServer;
44import javax.management.MBeanServerFactory;
45import javax.management.ObjectName;
46import javax.management.RuntimeMBeanException;
47import javax.management.StandardMBean;
48
49public class MBeanExceptionTest {
50    public static void main(String[] args) throws Exception {
51        System.out.println("Test that if an MBean throws RuntimeException " +
52                           "it is wrapped in RuntimeMBeanException,");
53        System.out.println("and if a Standard MBean throws Exception " +
54                           "it is wrapped in MBeanException");
55        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
56        Object standard = new Except();
57        ObjectName standardName = new ObjectName(":name=Standard MBean");
58        Object standardMBean =
59            new StandardMBean(new Except(), ExceptMBean.class);
60        ObjectName standardMBeanName =
61            new ObjectName(":name=Instance of StandardMBean");
62        Object dynamic = new DynamicExcept();
63        ObjectName dynamicName = new ObjectName(":name=Dynamic MBean");
64        mbs.registerMBean(standard, standardName);
65        mbs.registerMBean(standardMBean, standardMBeanName);
66        mbs.registerMBean(dynamic, dynamicName);
67        int failures = 0;
68        failures += test(mbs, standardName, true);
69        failures += test(mbs, standardMBeanName, true);
70        failures += test(mbs, dynamicName, false);
71
72        final boolean[] booleans = {false, true};
73
74        for (boolean runtimeX : booleans) {
75            Class<? extends Exception> excC =
76                    runtimeX ? RuntimeMBeanException.class : MBeanException.class;
77            String excS =
78                    runtimeX ? "a RuntimeMBeanException" : "an MBeanException";
79            String mbsS = "a plain MBeanServer";
80            System.out.println(
81                    "Test that, with " + mbsS + ", " + excS + " is wrapped " +
82                    "in " + excS);
83            // E.g. "Test that, with a plain MBeanServer, an MBeanException
84            // is wrapped in an MBeanException".
85            try {
86                mbs.createMBean(
87                        Except.class.getName(), new ObjectName(":name=Oops"),
88                        new Object[] {runtimeX},
89                        new String[] {boolean.class.getName()});
90                System.out.println(
91                        "FAIL: createMBean succeeded but should not have");
92                failures++;
93            } catch (Exception e) {
94                if (!excC.isInstance(e)) {
95                    System.out.println(
96                            "FAIL: expected " + excC.getName() + " from " +
97                            "createMBean, got " + e);
98                    failures++;
99                } else {
100                    Throwable cause = e.getCause();
101                    if (!excC.isInstance(cause)) {
102                        System.out.println(
103                                "FAIL: expected " + excC.getName() +
104                                " as cause of " + excC.getName() +
105                                ", got " + e);
106                        failures++;
107                    } else
108                        System.out.println("...ok");
109                }
110            }
111        }
112
113        if (failures == 0)
114            System.out.println("Test passed");
115        else {
116            System.out.println("TEST FAILED: " + failures + " failure(s)");
117            System.exit(1);
118        }
119    }
120
121    private static int test(MBeanServer mbs, ObjectName name,
122                            boolean testChecked)
123            throws Exception {
124        System.out.println("--------" + name + "--------");
125
126        int failures = 0;
127        final String[] ops = {"getAttribute", "setAttribute", "invoke"};
128        final int GET = 0, SET = 1, INVOKE = 2;
129        final String[] targets = {"UncheckedException", "CheckedException"};
130        final int UNCHECKED = 0, CHECKED = 1;
131
132        for (int i = 0; i < ops.length; i++) {
133            for (int j = 0; j < targets.length; j++) {
134
135                if (j == CHECKED && !testChecked)
136                    continue;
137
138                String target = targets[j];
139                String what = ops[i] + "/" + target;
140                System.out.println(what);
141
142                try {
143                    switch (i) {
144                    case GET:
145                        mbs.getAttribute(name, target);
146                        break;
147                    case SET:
148                        mbs.setAttribute(name, new Attribute(target, "x"));
149                        break;
150                    case INVOKE:
151                        mbs.invoke(name, target, null, null);
152                        break;
153                    default:
154                        throw new AssertionError();
155                    }
156                    System.out.println("failure: " + what + " returned!");
157                    failures++;
158                } catch (RuntimeMBeanException e) {
159                    if (j == CHECKED) {
160                        System.out.println("failure: RuntimeMBeanException " +
161                                           "when checked expected: " + e);
162                        failures++;
163                    } else {
164                        Throwable cause = e.getCause();
165                        if (cause == theUncheckedException)
166                            System.out.println("ok: " + what);
167                        else {
168                            System.out.println("failure: " + what +
169                                               " wrapped " + cause);
170                            failures++;
171                        }
172                    }
173                } catch (MBeanException e) {
174                    if (j == UNCHECKED) {
175                        System.out.println("failure: checked exception " +
176                                           "when unchecked expected: " + e);
177                        failures++;
178                    } else {
179                        Throwable cause = e.getCause();
180                        if (cause == theCheckedException)
181                            System.out.println("ok: " + what);
182                        else {
183                            System.out.println("failure: " + what +
184                                               " wrapped " + cause);
185                            failures++;
186                        }
187                    }
188                } catch (Throwable t) {
189                    System.out.println("failure: " + what + " threw: " + t);
190                    while ((t = t.getCause()) != null)
191                        System.out.println("  ... " + t);
192                    failures++;
193                }
194            }
195        }
196
197        return failures;
198    }
199
200    public static interface ExceptMBean {
201        public String getUncheckedException();
202        public void setUncheckedException(String x);
203        public void UncheckedException();
204        public String getCheckedException() throws Exception;
205        public void setCheckedException(String x) throws Exception;
206        public void CheckedException() throws Exception;
207    }
208
209    public static class Except implements ExceptMBean {
210        public Except() {}
211
212        public Except(boolean runtimeX) throws MBeanException {
213            if (runtimeX)
214                throw new RuntimeMBeanException(new RuntimeException(), "Bang");
215            else
216                throw new MBeanException(new Exception(), "Bang");
217        }
218
219        public String getUncheckedException() {
220            throw theUncheckedException;
221        }
222        public void setUncheckedException(String x) {
223            throw theUncheckedException;
224        }
225        public void UncheckedException() {
226            throw theUncheckedException;
227        }
228        public String getCheckedException() throws Exception {
229            throw theCheckedException;
230        }
231        public void setCheckedException(String x) throws Exception {
232            throw theCheckedException;
233        }
234        public void CheckedException() throws Exception {
235            throw theCheckedException;
236        }
237    }
238
239    public static class DynamicExcept implements DynamicMBean {
240        public Object getAttribute(String attrName)
241                throws MBeanException {
242            if (attrName.equals("UncheckedException"))
243                throw theUncheckedException;
244            else
245                throw new AssertionError();
246        }
247        public void setAttribute(Attribute attr)
248                throws MBeanException {
249            String attrName = attr.getName();
250            if (attrName.equals("UncheckedException"))
251                throw theUncheckedException;
252            else
253                throw new AssertionError();
254        }
255        public Object invoke(String opName, Object[] params, String[] sig)
256                throws MBeanException {
257            assert params == null && sig == null;
258            if (opName.equals("UncheckedException"))
259                throw theUncheckedException;
260            else
261                throw new AssertionError();
262        }
263        public AttributeList getAttributes(String[] names) {
264            assert false;
265            return null;
266        }
267        public AttributeList setAttributes(AttributeList attrs) {
268            assert false;
269            return null;
270        }
271        public MBeanInfo getMBeanInfo() {
272            try {
273                return new StandardMBean(new Except(), ExceptMBean.class)
274                    .getMBeanInfo();
275            } catch (Exception e) {
276                assert false;
277                return null;
278            }
279        }
280    }
281
282    private static final Exception theCheckedException =
283        new Exception("The checked exception that should be seen");
284    private static final RuntimeException theUncheckedException =
285        new UnsupportedOperationException("The unchecked exception " +
286                                          "that should be seen");
287}
288