blob: c38aaf6f863bb892bdfdb154734c1b7e4a30d449 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 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 5043245
27 * @summary Test the following in RequiredModelMBean.getAttribute():
28 * The declared type of the attribute is the String returned by
29 * ModelMBeanAttributeInfo.getType(). A value is compatible
30 * with this type if one of the following is true:
31 * - the value is null;
32 * - the declared name is a primitive type name (such as "int")
33 * and the value is an instance of the corresponding wrapper
34 * type (such as java.lang.Integer);
35 * - the name of the value's class is identical to the declared name;
36 * - the declared name can be loaded by the value's class loader and
37 * produces a class to which the value can be assigned.
38 * @author Luis-Miguel Alventosa
39 * @run clean RequiredModelMBeanGetAttributeTest
40 * @run build RequiredModelMBeanGetAttributeTest
41 * @run main RequiredModelMBeanGetAttributeTest
42 */
43
44import java.lang.reflect.Method;
45import java.util.Hashtable;
46import java.util.Map;
47import javax.management.Descriptor;
48import javax.management.MBeanServer;
49import javax.management.MBeanServerFactory;
50import javax.management.ObjectName;
51import javax.management.modelmbean.DescriptorSupport;
52import javax.management.modelmbean.ModelMBean;
53import javax.management.modelmbean.ModelMBeanAttributeInfo;
54import javax.management.modelmbean.ModelMBeanInfo;
55import javax.management.modelmbean.ModelMBeanInfoSupport;
56import javax.management.modelmbean.ModelMBeanOperationInfo;
57import javax.management.modelmbean.RequiredModelMBean;
58
59public class RequiredModelMBeanGetAttributeTest {
60
61 public static void main(String[] args) throws Exception {
62
63 boolean ok = true;
64
65 MBeanServer mbs = MBeanServerFactory.createMBeanServer();
66
67 // Resource methods
68
69 Method nullGetter =
70 Resource.class.getMethod("getNull", (Class[]) null);
71 Method integerGetter =
72 Resource.class.getMethod("getInteger", (Class[]) null);
73 Method hashtableGetter =
74 Resource.class.getMethod("getHashtable", (Class[]) null);
75 Method mapGetter =
76 Resource.class.getMethod("getMap", (Class[]) null);
77
78 // ModelMBeanOperationInfo
79
80 Descriptor nullOperationDescriptor =
81 new DescriptorSupport(new String[] {
82 "name=getNull",
83 "descriptorType=operation",
84 "role=getter"
85 });
86 ModelMBeanOperationInfo nullOperationInfo =
87 new ModelMBeanOperationInfo("Null attribute",
88 nullGetter,
89 nullOperationDescriptor);
90
91 Descriptor integerOperationDescriptor =
92 new DescriptorSupport(new String[] {
93 "name=getInteger",
94 "descriptorType=operation",
95 "role=getter"
96 });
97 ModelMBeanOperationInfo integerOperationInfo =
98 new ModelMBeanOperationInfo("Integer attribute",
99 integerGetter,
100 integerOperationDescriptor);
101
102 Descriptor hashtableOperationDescriptor =
103 new DescriptorSupport(new String[] {
104 "name=getHashtable",
105 "descriptorType=operation",
106 "role=getter"
107 });
108 ModelMBeanOperationInfo hashtableOperationInfo =
109 new ModelMBeanOperationInfo("Hashtable attribute",
110 hashtableGetter,
111 hashtableOperationDescriptor);
112
113 Descriptor mapOperationDescriptor =
114 new DescriptorSupport(new String[] {
115 "name=getMap",
116 "descriptorType=operation",
117 "role=getter"
118 });
119 ModelMBeanOperationInfo mapOperationInfo =
120 new ModelMBeanOperationInfo("Map attribute",
121 mapGetter,
122 mapOperationDescriptor);
123
124 // ModelMBeanAttributeInfo
125
126 Descriptor nullAttributeDescriptor =
127 new DescriptorSupport(new String[] {
128 "name=Null",
129 "descriptorType=attribute",
130 "getMethod=getNull"
131 });
132 ModelMBeanAttributeInfo nullAttributeInfo =
133 new ModelMBeanAttributeInfo("Null",
134 "java.lang.Object",
135 "Null attribute",
136 true,
137 false,
138 false,
139 nullAttributeDescriptor);
140
141 Descriptor integerAttributeDescriptor =
142 new DescriptorSupport(new String[] {
143 "name=Integer",
144 "descriptorType=attribute",
145 "getMethod=getInteger"
146 });
147 ModelMBeanAttributeInfo integerAttributeInfo =
148 new ModelMBeanAttributeInfo("Integer",
149 "int",
150 "Integer attribute",
151 true,
152 false,
153 false,
154 integerAttributeDescriptor);
155
156 Descriptor hashtableAttributeDescriptor =
157 new DescriptorSupport(new String[] {
158 "name=Hashtable",
159 "descriptorType=attribute",
160 "getMethod=getHashtable"
161 });
162 ModelMBeanAttributeInfo hashtableAttributeInfo =
163 new ModelMBeanAttributeInfo("Hashtable",
164 "java.util.Hashtable",
165 "Hashtable attribute",
166 true,
167 false,
168 false,
169 hashtableAttributeDescriptor);
170
171 Descriptor mapAttributeDescriptor =
172 new DescriptorSupport(new String[] {
173 "name=Map",
174 "descriptorType=attribute",
175 "getMethod=getMap"
176 });
177 ModelMBeanAttributeInfo mapAttributeInfo =
178 new ModelMBeanAttributeInfo("Map",
179 "java.util.Map",
180 "Map attribute",
181 true,
182 false,
183 false,
184 mapAttributeDescriptor);
185
186 Descriptor null2AttributeDescriptor =
187 new DescriptorSupport(new String[] {
188 "name=Null2",
189 "descriptorType=attribute"
190 });
191 null2AttributeDescriptor.setField("default", null);
192 ModelMBeanAttributeInfo null2AttributeInfo =
193 new ModelMBeanAttributeInfo("Null2",
194 "java.lang.Object",
195 "Null2 attribute",
196 true,
197 false,
198 false,
199 null2AttributeDescriptor);
200
201 Descriptor integer2AttributeDescriptor =
202 new DescriptorSupport(new String[] {
203 "name=Integer2",
204 "descriptorType=attribute"
205 });
206 integer2AttributeDescriptor.setField("default", 10);
207 ModelMBeanAttributeInfo integer2AttributeInfo =
208 new ModelMBeanAttributeInfo("Integer2",
209 "int",
210 "Integer2 attribute",
211 true,
212 false,
213 false,
214 integer2AttributeDescriptor);
215
216 Descriptor hashtable2AttributeDescriptor =
217 new DescriptorSupport(new String[] {
218 "name=Hashtable2",
219 "descriptorType=attribute"
220 });
221 hashtable2AttributeDescriptor.setField("default", new Hashtable());
222 ModelMBeanAttributeInfo hashtable2AttributeInfo =
223 new ModelMBeanAttributeInfo("Hashtable2",
224 "java.util.Hashtable",
225 "Hashtable2 attribute",
226 true,
227 false,
228 false,
229 hashtable2AttributeDescriptor);
230
231 Descriptor map2AttributeDescriptor =
232 new DescriptorSupport(new String[] {
233 "name=Map2",
234 "descriptorType=attribute"
235 });
236 map2AttributeDescriptor.setField("default", new Hashtable());
237 ModelMBeanAttributeInfo map2AttributeInfo =
238 new ModelMBeanAttributeInfo("Map2",
239 "java.util.Map",
240 "Map2 attribute",
241 true,
242 false,
243 false,
244 map2AttributeDescriptor);
245
246 // ModelMBeanInfo
247
248 ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(
249 Resource.class.getName(),
250 "Resource MBean",
251 new ModelMBeanAttributeInfo[] { nullAttributeInfo,
252 integerAttributeInfo,
253 hashtableAttributeInfo,
254 mapAttributeInfo,
255 null2AttributeInfo,
256 integer2AttributeInfo,
257 hashtable2AttributeInfo,
258 map2AttributeInfo },
259 null,
260 new ModelMBeanOperationInfo[] { nullOperationInfo,
261 integerOperationInfo,
262 hashtableOperationInfo,
263 mapOperationInfo },
264 null);
265
266 // RequiredModelMBean
267
268 ModelMBean mmb = new RequiredModelMBean(mmbi);
269 mmb.setManagedResource(resource, "ObjectReference");
270 ObjectName mmbName = new ObjectName(":type=ResourceMBean");
271 mbs.registerMBean(mmb, mmbName);
272
273 // Run tests
274
275 System.out.println("\nTesting that we can call getNull()... ");
276 try {
277 Object o = mbs.getAttribute(mmbName, "Null");
278 System.out.println("getNull() = " + o);
279 System.out.println("Attribute's declared type = java.lang.Object");
280 System.out.println("Returned value's type = null");
281 } catch (Exception e) {
282 System.out.println("TEST FAILED: Caught exception:");
283 e.printStackTrace(System.out);
284 ok = false;
285 }
286
287 System.out.println("\nTesting that we can call getInteger()... ");
288 try {
289 Integer i = (Integer) mbs.getAttribute(mmbName, "Integer");
290 System.out.println("getInteger() = " + i);
291 System.out.println("Attribute's declared type = int");
292 System.out.println("Returned value's type = " +
293 i.getClass().getName());
294 } catch (Exception e) {
295 System.out.println("TEST FAILED: Caught exception:");
296 e.printStackTrace(System.out);
297 ok = false;
298 }
299
300 System.out.println("\nTesting that we can call getHashtable()... ");
301 try {
302 Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable");
303 System.out.println("getHashtable() = " + h);
304 System.out.println("Attribute's declared type = " +
305 "java.util.Hashtable");
306 System.out.println("Returned value's type = " +
307 h.getClass().getName());
308 } catch (Exception e) {
309 System.out.println("TEST FAILED: Caught exception:");
310 e.printStackTrace(System.out);
311 ok = false;
312 }
313
314 System.out.println("\nTesting that we can call getMap()... ");
315 try {
316 Map m = (Map) mbs.getAttribute(mmbName, "Map");
317 System.out.println("getMap() = " + m);
318 System.out.println("Attribute's declared type = " +
319 "java.util.Map");
320 System.out.println("Returned value's type = " +
321 m.getClass().getName());
322 } catch (Exception e) {
323 System.out.println("TEST FAILED: Caught exception:");
324 e.printStackTrace(System.out);
325 ok = false;
326 }
327
328 System.out.println("\nTesting that we can call getNull2()... ");
329 try {
330 Object o = mbs.getAttribute(mmbName, "Null2");
331 System.out.println("getNull2() = " + o);
332 System.out.println("Attribute's declared type = java.lang.Object");
333 System.out.println("Returned value's type = null");
334 } catch (Exception e) {
335 System.out.println("TEST FAILED: Caught exception:");
336 e.printStackTrace(System.out);
337 ok = false;
338 }
339
340 System.out.println("\nTesting that we can call getInteger2()... ");
341 try {
342 Integer i = (Integer) mbs.getAttribute(mmbName, "Integer2");
343 System.out.println("getInteger2() = " + i);
344 System.out.println("Attribute's declared type = int");
345 System.out.println("Returned value's type = " +
346 i.getClass().getName());
347 } catch (Exception e) {
348 System.out.println("TEST FAILED: Caught exception:");
349 e.printStackTrace(System.out);
350 ok = false;
351 }
352
353 System.out.println("\nTesting that we can call getHashtable2()... ");
354 try {
355 Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable2");
356 System.out.println("getHashtable2() = " + h);
357 System.out.println("Attribute's declared type = " +
358 "java.util.Hashtable");
359 System.out.println("Returned value's type = " +
360 h.getClass().getName());
361 } catch (Exception e) {
362 System.out.println("TEST FAILED: Caught exception:");
363 e.printStackTrace(System.out);
364 ok = false;
365 }
366
367 System.out.println("\nTesting that we can call getMap2()... ");
368 try {
369 Map m = (Map) mbs.getAttribute(mmbName, "Map2");
370 System.out.println("getMap2() = " + m);
371 System.out.println("Attribute's declared type = " +
372 "java.util.Map");
373 System.out.println("Returned value's type = " +
374 m.getClass().getName());
375 } catch (Exception e) {
376 System.out.println("TEST FAILED: Caught exception:");
377 e.printStackTrace(System.out);
378 ok = false;
379 }
380
381 if (ok)
382 System.out.println("\nTest passed.\n");
383 else {
384 System.out.println("\nTest failed.\n");
385 System.exit(1);
386 }
387 }
388
389 public static class Resource {
390 public Object getNull() {
391 return null;
392 }
393 public int getInteger() {
394 return 10;
395 }
396 public Hashtable getHashtable() {
397 return new Hashtable();
398 }
399 public Map getMap() {
400 return new Hashtable();
401 }
402 }
403
404 private static Resource resource = new Resource();
405}