blob: a3d69bec952bcc8e6782629797588c20c1ea33e9 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4954062
27 * @summary Test that a custom ModelMBean implementation can have custom
28 * targetType values in its ModelMBeanOperationInfo descriptors.
29 * @author Eamonn McManus
30 * @run clean ExoticTargetTypeTest
31 * @run build ExoticTargetTypeTest
32 * @run main ExoticTargetTypeTest
33 */
34
35import javax.management.*;
36import javax.management.modelmbean.*;
37
38public class ExoticTargetTypeTest {
39 public static void main(String[] args) throws Exception {
40 System.out.println("Testing that ModelMBeanOperationInfo can contain" +
41 " a nonstandard targetType provided that the " +
42 "ModelMBean understands it");
43
44 boolean ok = true;
45
46 final Descriptor noddyDescr =
47 new DescriptorSupport(new String[] {
48 "name=noddy",
49 "descriptorType=operation",
50 "role=operation",
51 "targetType=noddy",
52 });
53 final ModelMBeanOperationInfo opInfo =
54 new ModelMBeanOperationInfo("noddy",
55 "noddy description",
56 new MBeanParameterInfo[0],
57 "void",
58 ModelMBeanOperationInfo.ACTION,
59 noddyDescr);
60 final ModelMBeanInfo mmbInfo =
61 new ModelMBeanInfoSupport(ExoticModelMBean.class.getName(),
62 "Noddy ModelMBean description",
63 null, null,
64 new ModelMBeanOperationInfo[] {opInfo},
65 null);
66
67 System.out.println("Testing nonstandard ModelMBean with nonstandard " +
68 "ModelMBeanOperationInfo...");
69 final ExoticModelMBean exoticMMB = new ExoticModelMBean(mmbInfo);
70 try {
71 test(exoticMMB);
72 if (exoticMMB.noddyCalled)
73 System.out.println("...OK");
74 else {
75 System.out.println("...TEST FAILS: invoke worked but did " +
76 "not invoke method?!");
77 ok = false;
78 }
79 } catch (Exception e) {
80 System.out.println("...TEST FAILS: exception:");
81 e.printStackTrace(System.out);
82 ok = false;
83 }
84
85 System.out.println("Testing standard ModelMBean with nonstandard " +
86 "ModelMBeanOperationInfo...");
87 final ModelMBean standardMMB = new RequiredModelMBean(mmbInfo);
88 // RequiredModelMBean's constructor could legitimately throw
89 // an exception here because of the nonstandard descriptor.
90 // If some day it does, we will have to update this test.
91 try {
92 test(standardMMB);
93 System.out.println("...TEST FAILS: invoke worked but should not");
94 ok = false;
95 } catch (MBeanException e) {
96 Throwable cause = e.getCause();
97 if (cause instanceof InvalidTargetObjectTypeException) {
98 System.out.println("...OK: got exception MBeanException/" +
99 "InvalidTargetObjectTypeException: " +
100 cause.getMessage());
101 } else {
102 System.out.println("...TEST FAILS: got MBeanException with " +
103 "wrong cause: " + cause);
104 ok = false;
105 }
106 } catch (Exception e) {
107 System.out.println("...TEST FAILS: exception:");
108 e.printStackTrace(System.out);
109 ok = false;
110 }
111
112 if (ok)
113 System.out.println("Test passed");
114 else {
115 System.out.println("TEST FAILED");
116 System.exit(1);
117 }
118 }
119
120 private static void test(ModelMBean mmb) throws Exception {
121 final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
122 final ObjectName name = new ObjectName("d:k=v");
123 mbs.registerMBean(mmb, name);
124 try {
125 mbs.invoke(name, "noddy", null, null);
126 } finally {
127 mbs.unregisterMBean(name);
128 }
129 }
130
131 public static class ExoticModelMBean extends RequiredModelMBean {
132 public ExoticModelMBean(ModelMBeanInfo mmbInfo) throws MBeanException {
133 super();
134 this.mmbInfo = mmbInfo;
135 }
136
137 public Object invoke(String opName, Object[] opArgs, String[] sig)
138 throws MBeanException, ReflectionException {
139 if (opName.equals("noddy")) {
140 noddyCalled = true;
141 return null;
142 } else
143 throw new IllegalArgumentException("Not noddy: " + opName);
144 }
145
146 private boolean noddyCalled;
147 private final ModelMBeanInfo mmbInfo;
148 }
149}