KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > modelmbean > RequiredModelMBeanTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.modelmbean;
10
11 import java.util.List JavaDoc;
12 import javax.management.*;
13 import javax.management.modelmbean.*;
14
15 import test.MX4JTestCase;
16 import test.MutableBoolean;
17 import test.MutableInteger;
18 import test.javax.management.modelmbean.support.ModelMBeanTarget;
19
20 /**
21  * @version $Revision: 1.14 $
22  */

23 public class RequiredModelMBeanTest extends MX4JTestCase
24 {
25    private MBeanServer m_server;
26
27    public RequiredModelMBeanTest(String JavaDoc s)
28    {
29       super(s);
30    }
31
32    protected void setUp() throws Exception JavaDoc
33    {
34       super.setUp();
35       m_server = MBeanServerFactory.createMBeanServer("ModelMBean");
36    }
37
38    protected void tearDown() throws Exception JavaDoc
39    {
40       super.tearDown();
41       MBeanServerFactory.releaseMBeanServer(m_server);
42       m_server = null;
43    }
44
45    public void testCopyConstructor() throws Exception JavaDoc
46    {
47       try
48       {
49          new RequiredModelMBean(null);
50          fail("Expecting RuntimeOperationsException");
51       }
52       catch (RuntimeOperationsException x)
53       {
54          assertTrue(true); //success
55
}
56    }
57
58    public void testRegistration() throws Exception JavaDoc
59    {
60       RequiredModelMBean rmmb = new RequiredModelMBean();
61       ObjectName name = new ObjectName(":type=test");
62       try
63       {
64          m_server.registerMBean(rmmb, name);
65          m_server.unregisterMBean(name);
66       }
67       catch (NotCompliantMBeanException x)
68       {
69          fail("Default RequireModelMBean cannot be registered");
70       }
71
72       try
73       {
74          m_server.createMBean(RequiredModelMBean.class.getName(), name, null);
75          m_server.unregisterMBean(name);
76       }
77       catch (NotCompliantMBeanException x)
78       {
79          fail("Default RequireModelMBean cannot be created");
80       }
81
82       rmmb = (RequiredModelMBean)m_server.instantiate(RequiredModelMBean.class.getName(), null);
83       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(Object JavaDoc.class.getName(), "Test", null, null, null, null);
84       rmmb.setModelMBeanInfo(info);
85       m_server.registerMBean(rmmb, name);
86    }
87
88    public void testGetAttributeDefault() throws Exception JavaDoc
89    {
90       ObjectName name = new ObjectName(":type=test");
91
92       MutableInteger counter = new MutableInteger(0);
93       ModelMBeanTarget bean = new ModelMBeanTarget(counter);
94
95       String JavaDoc attrName = "FixedContent";
96
97       String JavaDoc[] names = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "default", "currencyTimeLimit"};
98       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
99
// currencyTimeLimit is now -1
100
Object JavaDoc[] values = new Object JavaDoc[]{attrName, "attribute", null, "false", "", "DEFAULT", "-1"};
101       DescriptorSupport attrDescr = new DescriptorSupport(names, values);
102       ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(attrName, String JavaDoc.class.getName(), "", true, false, false, attrDescr);
103       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo}, null, null, null);
104
105       RequiredModelMBean rmmb = new RequiredModelMBean();
106       rmmb.setModelMBeanInfo(info);
107       rmmb.setManagedResource(bean, "ObjectReference");
108       m_server.registerMBean(rmmb, name);
109
110       // No get method, should always get the default value back
111
int num = 5;
112       for (int i = 0; i < num; ++i)
113       {
114          String JavaDoc value = (String JavaDoc)m_server.getAttribute(name, attrName);
115          assertEquals("Returned value is not the default", value, "DEFAULT");
116       }
117
118       assertEquals("Wrong staleness algorithm", 0, counter.get());
119    }
120
121    public void testGetAttributeAlwaysStale() throws Exception JavaDoc
122    {
123       ObjectName name = new ObjectName(":type=test");
124
125       MutableInteger counter = new MutableInteger(0);
126       ModelMBeanTarget bean = new ModelMBeanTarget(counter);
127
128       String JavaDoc attrName = "FixedContent";
129
130       String JavaDoc[] names = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "default", "getMethod", "currencyTimeLimit"};
131       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
132
// if currencyTimeLimit is -1 then the value is always stale
133
// fix for bug #794313
134
Object JavaDoc[] values = new Object JavaDoc[]{attrName, "attribute", null, "false", "", "DEFAULT", "get" + attrName, "-1"};
135       DescriptorSupport attrDescr = new DescriptorSupport(names, values);
136       ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(attrName, String JavaDoc.class.getName(), "", true, false, false, attrDescr);
137       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo}, null, null, null);
138
139       RequiredModelMBean rmmb = new RequiredModelMBean();
140       rmmb.setModelMBeanInfo(info);
141       rmmb.setManagedResource(bean, "ObjectReference");
142       m_server.registerMBean(rmmb, name);
143
144       // We set the staleness to 0 (-> always stale) so test if the bean method is always called
145
String JavaDoc fixed = bean.getFixedContent();
146       counter.set(0);
147       int num = 5;
148       for (int i = 0; i < num; ++i)
149       {
150          String JavaDoc value = (String JavaDoc)m_server.getAttribute(name, attrName);
151          assertEquals("Method returned different value", value, fixed);
152       }
153
154       assertEquals("Wrong staleness algorithm: " + counter.get(), counter.get(), num);
155    }
156
157    public void testGetAttributeNeverStale() throws Exception JavaDoc
158    {
159       ObjectName name = new ObjectName(":type=test");
160
161       MutableInteger counter = new MutableInteger(0);
162       ModelMBeanTarget bean = new ModelMBeanTarget(counter);
163
164       String JavaDoc attrName = "FixedContent";
165
166       String JavaDoc[] names = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "default", "getMethod", "currencyTimeLimit", "value"};
167       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
168
// if currencyTimeLimit is 0 then the value is never stale
169
// fix for bug #794313
170
Object JavaDoc[] values = new Object JavaDoc[]{attrName, "attribute", null, "false", "", "DEFAULT", "get" + attrName, "0", "NEVER"};
171       DescriptorSupport attrDescr = new DescriptorSupport(names, values);
172       ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(attrName, String JavaDoc.class.getName(), "", true, false, false, attrDescr);
173       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo}, null, null, null);
174
175       RequiredModelMBean rmmb = new RequiredModelMBean();
176       rmmb.setModelMBeanInfo(info);
177       rmmb.setManagedResource(bean, "ObjectReference");
178       m_server.registerMBean(rmmb, name);
179
180       // We set the staleness to 0 (-> never stale) so test that the bean method is never called
181
int num = 5;
182       for (int i = 0; i < num; ++i)
183       {
184          String JavaDoc value = (String JavaDoc)m_server.getAttribute(name, attrName);
185          assertEquals("Method returned different value", value, "NEVER");
186       }
187
188       assertEquals("Wrong staleness algorithm", counter.get(), 0);
189    }
190
191    public void testGetAttributeStale() throws Exception JavaDoc
192    {
193       ObjectName name = new ObjectName(":type=test");
194
195       MutableInteger counter = new MutableInteger(0);
196       ModelMBeanTarget bean = new ModelMBeanTarget(counter);
197
198       String JavaDoc attrName = "MutableContent";
199
200       String JavaDoc[] names = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "default", "getMethod", "currencyTimeLimit"};
201       Object JavaDoc[] values = new Object JavaDoc[]{attrName, "attribute", null, "false", "", "DEFAULT", "get" + attrName, "2"};
202       DescriptorSupport attrDescr = new DescriptorSupport(names, values);
203       ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(attrName, "java.lang.String", "", true, false, false, attrDescr);
204       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport("test.javax.management.modelmbean.ModelMBeanTarget", "", new ModelMBeanAttributeInfo[]{attrInfo}, null, null, null);
205
206       RequiredModelMBean rmmb = new RequiredModelMBean();
207       rmmb.setModelMBeanInfo(info);
208       rmmb.setManagedResource(bean, "ObjectReference");
209       m_server.registerMBean(rmmb, name);
210
211       // We set the staleness to 2 seconds
212

213       // First time
214
bean.setMutableContent("First");
215       String JavaDoc attrValue = (String JavaDoc)m_server.getAttribute(name, attrName);
216       assertEquals("getAttribute does not work", attrValue, "First");
217
218       // Now value should be cached, check it
219
bean.setMutableContent("Second");
220       attrValue = (String JavaDoc)m_server.getAttribute(name, attrName);
221       assertEquals("Attribute value caching does not work", attrValue, "First");
222
223       // Now wait 2 seconds
224
Thread.sleep(2000);
225       attrValue = (String JavaDoc)m_server.getAttribute(name, attrName);
226       assertEquals("Attribute staleness algorithm does not work", attrValue, "Second");
227    }
228
229    public void testGetAttributes() throws Exception JavaDoc
230    {
231       String JavaDoc attrName1 = "FixedContent";
232       String JavaDoc attrName2 = "MutableContent";
233
234       ObjectName name = new ObjectName(":type=test");
235
236       MutableInteger counter = new MutableInteger(0);
237       ModelMBeanTarget bean = new ModelMBeanTarget(counter);
238
239       String JavaDoc[] names1 = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "getMethod", "currencyTimeLimit"};
240       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
241
// currencyTimeLimit is now -1
242
Object JavaDoc[] values1 = new Object JavaDoc[]{attrName1, "attribute", null, "false", "", "get" + attrName1, "-1"};
243       DescriptorSupport attrDescr1 = new DescriptorSupport(names1, values1);
244
245       String JavaDoc[] names2 = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "getMethod", "currencyTimeLimit"};
246       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
247
// currencyTimeLimit is now -1
248
Object JavaDoc[] values2 = new Object JavaDoc[]{attrName2, "attribute", null, "false", "", "get" + attrName2, "-1"};
249       DescriptorSupport attrDescr2 = new DescriptorSupport(names2, values2);
250
251       ModelMBeanAttributeInfo attrInfo1 = new ModelMBeanAttributeInfo(attrName1, String JavaDoc.class.getName(), "", true, false, false, attrDescr1);
252       ModelMBeanAttributeInfo attrInfo2 = new ModelMBeanAttributeInfo(attrName2, String JavaDoc.class.getName(), "", true, false, false, attrDescr2);
253       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo1, attrInfo2}, null, null, null);
254
255       RequiredModelMBean rmmb = new RequiredModelMBean();
256       rmmb.setModelMBeanInfo(info);
257       rmmb.setManagedResource(bean, "ObjectReference");
258       m_server.registerMBean(rmmb, name);
259
260       String JavaDoc[] attributes = new String JavaDoc[]{attrName1, attrName2};
261       AttributeList list = m_server.getAttributes(name, attributes);
262       assertEquals("Wrong number of attributes", list.size(), 2);
263
264       // Check that they're really the right ones
265
Attribute attr = (Attribute)list.get(0);
266       assertEquals(attr.getName(), attrName1);
267       attr = (Attribute)list.get(1);
268       assertEquals(attr.getName(), attrName2);
269
270       // Test also a wrong attribute
271
attributes = new String JavaDoc[]{attrName1, null, attrName2};
272       list = m_server.getAttributes(name, attributes);
273       assertEquals(list.size(), 2);
274
275       // Check that they're really the right ones
276
attr = (Attribute)list.get(0);
277       assertEquals(attr.getName(), attrName1);
278       attr = (Attribute)list.get(1);
279       assertEquals(attr.getName(), attrName2);
280
281       // Test also a wrong attribute
282
attributes = new String JavaDoc[]{"NonExisting", attrName1, attrName2};
283       list = m_server.getAttributes(name, attributes);
284       assertEquals(list.size(), 2);
285       // Check that they're really the right ones
286
attr = (Attribute)list.get(0);
287       assertEquals(attr.getName(), attrName1);
288       attr = (Attribute)list.get(1);
289       assertEquals(attr.getName(), attrName2);
290
291       // Test also a wrong attribute
292
attributes = new String JavaDoc[]{"NonExisting", attrName2};
293       list = m_server.getAttributes(name, attributes);
294       assertEquals(list.size(), 1);
295       // Check that it is really the right one
296
attr = (Attribute)list.get(0);
297       assertEquals(attr.getName(), attrName2);
298    }
299
300    public void testSetAttribute() throws Exception JavaDoc
301    {
302       String JavaDoc attrName1 = "MutableContent";
303
304       ObjectName name = new ObjectName(":type=test");
305
306       MutableInteger counter = new MutableInteger(0);
307       ModelMBeanTarget bean = new ModelMBeanTarget(counter);
308
309       String JavaDoc[] names1 = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "setMethod", "getMethod", "currencyTimeLimit"};
310       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
311
// currencyTimeLimit is now -1
312
Object JavaDoc[] values1 = new Object JavaDoc[]{attrName1, "attribute", null, "false", "", "set" + attrName1, "get" + attrName1, "-1"};
313       DescriptorSupport attrDescr1 = new DescriptorSupport(names1, values1);
314
315       ModelMBeanAttributeInfo attrInfo1 = new ModelMBeanAttributeInfo(attrName1, String JavaDoc.class.getName(), "", true, true, false, attrDescr1);
316       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo1}, null, null, null);
317
318       final MutableBoolean storeTester = new MutableBoolean(false);
319       RequiredModelMBean rmmb = new StoreTesterRMMB(storeTester);
320       rmmb.setModelMBeanInfo(info);
321       rmmb.setManagedResource(bean, "ObjectReference");
322       m_server.registerMBean(rmmb, name);
323
324       // Adding a attribute change notification listener
325
final MutableInteger listenerCount = new MutableInteger(0);
326       rmmb.addAttributeChangeNotificationListener(new NotificationListener()
327       {
328          public void handleNotification(Notification notification, Object JavaDoc handback)
329          {
330             listenerCount.set(listenerCount.get() + 1);
331          }
332       }, attrName1, null);
333
334       String JavaDoc value = "SET_FIRST_TIME";
335       Attribute attribute = new Attribute(attrName1, value);
336       m_server.setAttribute(name, attribute);
337
338       // check that has really been set
339
assertEquals(bean.getMutableContent(), value);
340       // check through MBeanServer
341
assertEquals(m_server.getAttribute(name, attrName1), value);
342       // check that listener has been called
343
assertEquals(listenerCount.get(), 1);
344       // There is no persistence settings, check that store was not called
345
assertFalse("Store should not have been called", storeTester.get());
346
347       // Adding a attribute change notification listener with
348
// null as attribute. test for bug #742389
349
NotificationListener dummyListener = new NotificationListener()
350       {
351          public void handleNotification(Notification notification, Object JavaDoc handback)
352          {
353          }
354       };
355
356       rmmb.addAttributeChangeNotificationListener(dummyListener, null, null);
357       rmmb.removeAttributeChangeNotificationListener(dummyListener, null);
358
359       // Change the persist policy - have to unregeister to call setModelMBeanInfo
360
m_server.unregisterMBean(name);
361       attrDescr1.setField("persistPolicy", "OnUpdate");
362       info.setDescriptor(attrDescr1, "attribute");
363       rmmb.setModelMBeanInfo(info);
364       storeTester.set(false);
365       m_server.registerMBean(rmmb, name);
366
367       value = "SET_SECOND_TIME";
368       attribute = new Attribute(attrName1, value);
369       m_server.setAttribute(name, attribute);
370
371       // check that listener has been called
372
assertEquals(listenerCount.get(), 2);
373       // There are persistence settings, check that store was called
374
assertTrue("Store should have been called", storeTester.get());
375
376       // Now remove setMethod - again we have to unregister
377
m_server.unregisterMBean(name);
378       names1 = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "getMethod", "currencyTimeLimit"};
379       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
380
// currencyTimeLimit is now -1
381
values1 = new Object JavaDoc[]{attrName1, "attribute", null, "false", "", "get" + attrName1, "-1"};
382       attrDescr1 = new DescriptorSupport(names1, values1);
383       attrDescr1.setField("persistPolicy", "OnUpdate");
384       info.setDescriptor(attrDescr1, "attribute");
385       rmmb.setModelMBeanInfo(info);
386       storeTester.set(false);
387       m_server.registerMBean(rmmb, name);
388
389       value = "SET_THIRD_TIME";
390       attribute = new Attribute(attrName1, value);
391       m_server.setAttribute(name, attribute);
392
393       // check that listener has been called
394
assertEquals(listenerCount.get(), 3);
395       // There are persistence settings, check that store was called
396
assertTrue("Store should have been called", storeTester.get());
397       // Check the attribute value
398
if (bean.getMutableContent().equals(value))
399       {
400          fail("No setMethod, bean should not have been modified");
401       }
402       if (info.getAttribute(attrName1).getDescriptor().getFieldValue("value") != null)
403       {
404          fail("New value should not have been cached since currencyTimeLimit is negative");
405       }
406
407       // Test attribute that takes array as parameters
408
m_server.unregisterMBean(name);
409       String JavaDoc attrName = "ArrayAttribute";
410       String JavaDoc[] names = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "getMethod", "setMethod", "currencyTimeLimit"};
411       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
412
// currencyTimeLimit is now -1
413
Object JavaDoc[] values = new Object JavaDoc[]{attrName, "attribute", null, "true", "", "get" + attrName, "set" + attrName, "-1"};
414       Descriptor attrDescr = new DescriptorSupport(names, values);
415
416       ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(attrName, new String JavaDoc[0].getClass().getName(), "", true, true, false, attrDescr);
417       info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo}, null, null, null);
418       rmmb.setModelMBeanInfo(info);
419       m_server.registerMBean(rmmb, name);
420
421       String JavaDoc[] v = new String JavaDoc[]{"one", "two"};
422       attribute = new Attribute(attrName, v);
423       m_server.setAttribute(name, attribute);
424    }
425
426    public void testSetAttributes() throws Exception JavaDoc
427    {
428       String JavaDoc attrName1 = "MutableContent";
429       String JavaDoc attrName2 = "MutableContent2";
430
431       ObjectName name = new ObjectName(":type=test");
432
433       MutableInteger counter = new MutableInteger(0);
434       ModelMBeanTarget bean = new ModelMBeanTarget(counter);
435
436       String JavaDoc[] names1 = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "getMethod", "setMethod", "currencyTimeLimit"};
437       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
438
// currencyTimeLimit is now -1
439
Object JavaDoc[] values1 = new Object JavaDoc[]{attrName1, "attribute", null, "false", "", "get" + attrName1, "set" + attrName1, "-1"};
440       DescriptorSupport attrDescr1 = new DescriptorSupport(names1, values1);
441
442       String JavaDoc[] names2 = new String JavaDoc[]{"name", "descriptorType", "value", "iterable", "displayName", "getMethod", "setMethod", "currencyTimeLimit"};
443       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
444
// currencyTimeLimit is now -1
445
Object JavaDoc[] values2 = new Object JavaDoc[]{attrName2, "attribute", null, "false", "", "get" + attrName2, "set" + attrName2, "-1"};
446       DescriptorSupport attrDescr2 = new DescriptorSupport(names2, values2);
447
448       ModelMBeanAttributeInfo attrInfo1 = new ModelMBeanAttributeInfo(attrName1, String JavaDoc.class.getName(), "", true, true, false, attrDescr1);
449       ModelMBeanAttributeInfo attrInfo2 = new ModelMBeanAttributeInfo(attrName2, "int", "", true, true, false, attrDescr2);
450       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo1, attrInfo2}, null, null, null);
451
452       RequiredModelMBean rmmb = new RequiredModelMBean();
453       rmmb.setModelMBeanInfo(info);
454       rmmb.setManagedResource(bean, "ObjectReference");
455       m_server.registerMBean(rmmb, name);
456
457       Attribute attr1 = new Attribute(attrName1, "FIRST");
458       Attribute attr2 = new Attribute(attrName2, new Integer JavaDoc(5));
459       AttributeList list = new AttributeList();
460       list.add(attr1);
461       list.add(attr2);
462       AttributeList result = m_server.setAttributes(name, list);
463       assertEquals("Wrong number of attributes were set", result.size(), 2);
464       // Check that they're really the right ones
465
Attribute attr = (Attribute)result.get(0);
466       assertEquals(attr, attr1);
467       attr = (Attribute)result.get(1);
468       assertEquals(attr, attr2);
469       // Check that they were really set
470
assertEquals(bean.getMutableContent(), attr1.getValue());
471       assertEquals(bean.getMutableContent2(), ((Integer JavaDoc)attr2.getValue()).intValue());
472
473       // Test non-existing attribute
474
attr = new Attribute("NonExisting", null);
475       attr2 = new Attribute(attrName2, new Integer JavaDoc(7));
476       list.clear();
477       list.add(attr);
478       list.add(attr2);
479       result = m_server.setAttributes(name, list);
480       assertEquals(result.size(), 1);
481       // Check that they're really the right ones
482
attr = (Attribute)result.get(0);
483       assertEquals(attr, attr2);
484       // Check that they were really set
485
assertEquals(bean.getMutableContent2(), ((Integer JavaDoc)attr2.getValue()).intValue());
486
487       attr = new Attribute("NonExisting", null);
488       list.clear();
489       list.add(attr);
490       result = m_server.setAttributes(name, list);
491       assertEquals(result.size(), 0);
492    }
493
494    public void testInvoke() throws Exception JavaDoc
495    {
496       String JavaDoc operation = "operation1";
497
498       ObjectName name = new ObjectName(":type=test");
499
500       MutableInteger counter = new MutableInteger(0);
501       ModelMBeanTarget bean = new ModelMBeanTarget(counter);
502
503       String JavaDoc[] names1 = new String JavaDoc[]{"name", "descriptorType", "displayName", "role", "targetObject", "targetObjectType", "currencyTimeLimit"};
504       // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
505
// currencyTimeLimit is now -1
506
Object JavaDoc[] values1 = new Object JavaDoc[]{operation, "operation", "", "operation", null, null, "-1"};
507       DescriptorSupport operDescr = new DescriptorSupport(names1, values1);
508
509       MBeanParameterInfo paramInfo1 = new MBeanParameterInfo("c", "char", "");
510       MBeanParameterInfo paramInfo2 = new MBeanParameterInfo("s", "short", "");
511       MBeanParameterInfo paramInfo3 = new MBeanParameterInfo("f", new float[0].getClass().getName(), "");
512       MBeanParameterInfo paramInfo4 = new MBeanParameterInfo("c", new Object JavaDoc[0][0].getClass().getName(), "");
513       ModelMBeanOperationInfo operInfo = new ModelMBeanOperationInfo(operation, "", new MBeanParameterInfo[]{paramInfo1, paramInfo2, paramInfo3, paramInfo4}, "java.util.List", ModelMBeanOperationInfo.UNKNOWN, operDescr);
514
515       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", null, null, new ModelMBeanOperationInfo[]{operInfo}, null);
516
517       RequiredModelMBean rmmb = new RequiredModelMBean();
518       rmmb.setModelMBeanInfo(info);
519       rmmb.setManagedResource(bean, "ObjectReference");
520       m_server.registerMBean(rmmb, name);
521
522       short s = 10;
523       Object JavaDoc[] args = new Object JavaDoc[]{new Character JavaDoc('z'), new Short JavaDoc(s), new float[]{1.0F}, new Object JavaDoc[][]{{"Hello"}, {"World"}}};
524       String JavaDoc[] params = new String JavaDoc[]{paramInfo1.getType(), paramInfo2.getType(), paramInfo3.getType(), paramInfo4.getType()};
525       List JavaDoc list = (List JavaDoc)m_server.invoke(name, operation, args, params);
526
527       // Test that was really called
528
assertEquals(counter.get(), 1);
529       // Right values ?
530
for (int i = 0; i < list.size(); ++i)
531       {
532          Object JavaDoc obj = list.get(i);
533          assertEquals("Returned value is different: " + obj, args[i], obj);
534       }
535
536       m_server.unregisterMBean(name);
537       ModelMBeanTarget.TargetBean target = new ModelMBeanTarget.TargetBean();
538       operDescr.setField("targetObject", target);
539       operDescr.setField("targetObjectType", "ObjectReference");
540
541       info.setDescriptor(operDescr, "operation");
542       rmmb.setModelMBeanInfo(info);
543       m_server.registerMBean(rmmb, name);
544
545       list = (List JavaDoc)m_server.invoke(name, operation, args, params);
546
547       // Test that was not called
548
assertEquals("Operation should not have been called", counter.get(), 1);
549       // Right values ?
550
for (int i = 0; i < list.size(); ++i)
551       {
552          Object JavaDoc obj = list.get(list.size() - 1 - i);
553          assertEquals("Returned value is different: " + obj, args[i], obj);
554       }
555    }
556
557    public void testInvokeModelMBeanMethods() throws Exception JavaDoc
558    {
559       ObjectName name = new ObjectName(":type=test");
560
561       ModelMBean mmb = (ModelMBean)m_server.instantiate(RequiredModelMBean.class.getName(), null);
562
563       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", null, null, null, null);
564
565       mmb.setModelMBeanInfo(info);
566
567       m_server.registerMBean(mmb, name);
568
569       // Now try to invoke methods that are part of the ModelMBean interface
570

571       try
572       {
573          m_server.setAttribute(name, new Attribute("ModelMBeanInfo", info));
574          fail("Cannot invoke a ModelMBean method via MBeanServer");
575       }
576       catch (Exception JavaDoc ignored)
577       {
578       }
579
580       // Bug #940161 Required ModelMBean methods are not invoked
581
m_server.invoke(name, "setManagedResource", new Object JavaDoc[]{new ModelMBeanTarget(new MutableInteger(0)), "ObjectReference"}, new String JavaDoc[]{Object JavaDoc.class.getName(), String JavaDoc.class.getName()});
582       m_server.invoke(name, "store", new Object JavaDoc[0], new String JavaDoc[0]);
583       m_server.invoke(name, "sendNotification", new Object JavaDoc[]{"generic"}, new String JavaDoc[]{String JavaDoc.class.getName()});
584
585       // Now specify setManagedResource as an operation in the MMBI
586
m_server.unregisterMBean(name);
587       String JavaDoc operation = "setManagedResource";
588       MBeanParameterInfo paramInfo1 = new MBeanParameterInfo("resource", Object JavaDoc.class.getName(), "");
589       MBeanParameterInfo paramInfo2 = new MBeanParameterInfo("type", String JavaDoc.class.getName(), "");
590       ModelMBeanOperationInfo operInfo = new ModelMBeanOperationInfo(operation, "", new MBeanParameterInfo[]{paramInfo1, paramInfo2}, null, ModelMBeanOperationInfo.ACTION, null);
591       info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", null, null, new ModelMBeanOperationInfo[]{operInfo}, null);
592       mmb.setModelMBeanInfo(info);
593       m_server.registerMBean(mmb, name);
594
595       // Spec says I must be able to invoke it
596
Object JavaDoc target = new ModelMBeanTarget(new MutableInteger(0));
597       m_server.invoke(name, operation, new Object JavaDoc[]{target, "ObjectReference"}, new String JavaDoc[]{Object JavaDoc.class.getName(), String JavaDoc.class.getName()});
598    }
599
600    public void testNotifications() throws Exception JavaDoc
601    {
602       ObjectName name = new ObjectName(":type=test");
603
604       ModelMBeanNotificationInfo notification[] = new ModelMBeanNotificationInfo[1];
605       notification[0] = new ModelMBeanNotificationInfo(new String JavaDoc[]{ModelMBeanTarget.class.getName() + ".notification"}, "name", "");
606
607       ModelMBeanAttributeInfo attributeInfo = new ModelMBeanAttributeInfo("MutableContent", String JavaDoc.class.getName(), "", true, true, false);
608       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attributeInfo}, null, null, notification);
609
610       RequiredModelMBean rmmb = new RequiredModelMBean();
611       rmmb.setModelMBeanInfo(info);
612       rmmb.setManagedResource(new ModelMBeanTarget(new MutableInteger(0)), "objectReference");
613       m_server.registerMBean(rmmb, name);
614
615       Object JavaDoc listenerHandback = new Object JavaDoc();
616
617       TestNotificationListener listener = new TestNotificationListener();
618       m_server.addNotificationListener(name, listener, null, listenerHandback);
619
620       rmmb.sendNotification("generic notification");
621       assertEquals("jmx.modelmbean.generic", listener.type);
622       assertEquals("generic notification", listener.message);
623       assertSame(listenerHandback, listener.handback);
624
625       rmmb.sendNotification(new Notification("my.type", rmmb, 1, "a message"));
626       assertEquals("my.type", listener.type);
627       assertEquals("a message", listener.message);
628       assertSame(listenerHandback, listener.handback);
629
630       m_server.setAttribute(name, new Attribute("MutableContent", "Hello World"));
631       assertEquals("jmx.attribute.change", listener.type);
632    }
633
634    public void testGetNotificationInfo() throws Exception JavaDoc
635    {
636       RequiredModelMBean rmmb = new RequiredModelMBean();
637       MBeanNotificationInfo[] notificationInfos = rmmb.getNotificationInfo();
638       assertEquals(2, notificationInfos.length);
639
640       ModelMBeanNotificationInfo notification[] = new ModelMBeanNotificationInfo[1];
641       notification[0] = new ModelMBeanNotificationInfo(new String JavaDoc[]{ModelMBeanTarget.class.getName() + ".notification"}, "name", "");
642
643       ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", null, null, null, notification);
644       rmmb.setModelMBeanInfo(info);
645       notificationInfos = rmmb.getNotificationInfo();
646       assertEquals(3, notificationInfos.length);
647    }
648
649    public static class StoreTesterRMMB extends RequiredModelMBean
650    {
651       private MutableBoolean m_stored;
652
653       public StoreTesterRMMB(MutableBoolean storeTester) throws MBeanException
654       {
655          m_stored = storeTester;
656       }
657
658       public void store() throws MBeanException, RuntimeOperationsException, InstanceNotFoundException
659       {
660          m_stored.set(true);
661          super.store();
662       }
663    }
664
665    public static class TestNotificationListener implements NotificationListener
666    {
667       String JavaDoc type = null;
668       String JavaDoc message = null;
669       Object JavaDoc handback = null;
670
671       public void handleNotification(Notification notification, Object JavaDoc handback)
672       {
673          type = notification.getType();
674          message = notification.getMessage();
675          this.handback = handback;
676       }
677    }
678 }
679
Popular Tags