Source Code Cross Referenced for RequiredModelMBeanTest.java in  » JMX » mx4j » test » javax » management » modelmbean » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » mx4j » test.javax.management.modelmbean 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) The MX4J Contributors.
003:         * All rights reserved.
004:         *
005:         * This software is distributed under the terms of the MX4J License version 1.0.
006:         * See the terms of the MX4J License in the documentation provided with this software.
007:         */
008:
009:        package test.javax.management.modelmbean;
010:
011:        import java.util.List;
012:        import javax.management.*;
013:        import javax.management.modelmbean.*;
014:
015:        import test.MX4JTestCase;
016:        import test.MutableBoolean;
017:        import test.MutableInteger;
018:        import test.javax.management.modelmbean.support.ModelMBeanTarget;
019:
020:        /**
021:         * @version $Revision: 1.14 $
022:         */
023:        public class RequiredModelMBeanTest extends MX4JTestCase {
024:            private MBeanServer m_server;
025:
026:            public RequiredModelMBeanTest(String s) {
027:                super (s);
028:            }
029:
030:            protected void setUp() throws Exception {
031:                super .setUp();
032:                m_server = MBeanServerFactory.createMBeanServer("ModelMBean");
033:            }
034:
035:            protected void tearDown() throws Exception {
036:                super .tearDown();
037:                MBeanServerFactory.releaseMBeanServer(m_server);
038:                m_server = null;
039:            }
040:
041:            public void testCopyConstructor() throws Exception {
042:                try {
043:                    new RequiredModelMBean(null);
044:                    fail("Expecting RuntimeOperationsException");
045:                } catch (RuntimeOperationsException x) {
046:                    assertTrue(true); //success
047:                }
048:            }
049:
050:            public void testRegistration() throws Exception {
051:                RequiredModelMBean rmmb = new RequiredModelMBean();
052:                ObjectName name = new ObjectName(":type=test");
053:                try {
054:                    m_server.registerMBean(rmmb, name);
055:                    m_server.unregisterMBean(name);
056:                } catch (NotCompliantMBeanException x) {
057:                    fail("Default RequireModelMBean cannot be registered");
058:                }
059:
060:                try {
061:                    m_server.createMBean(RequiredModelMBean.class.getName(),
062:                            name, null);
063:                    m_server.unregisterMBean(name);
064:                } catch (NotCompliantMBeanException x) {
065:                    fail("Default RequireModelMBean cannot be created");
066:                }
067:
068:                rmmb = (RequiredModelMBean) m_server.instantiate(
069:                        RequiredModelMBean.class.getName(), null);
070:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
071:                        Object.class.getName(), "Test", null, null, null, null);
072:                rmmb.setModelMBeanInfo(info);
073:                m_server.registerMBean(rmmb, name);
074:            }
075:
076:            public void testGetAttributeDefault() throws Exception {
077:                ObjectName name = new ObjectName(":type=test");
078:
079:                MutableInteger counter = new MutableInteger(0);
080:                ModelMBeanTarget bean = new ModelMBeanTarget(counter);
081:
082:                String attrName = "FixedContent";
083:
084:                String[] names = new String[] { "name", "descriptorType",
085:                        "value", "iterable", "displayName", "default",
086:                        "currencyTimeLimit" };
087:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
088:                // currencyTimeLimit is now -1
089:                Object[] values = new Object[] { attrName, "attribute", null,
090:                        "false", "", "DEFAULT", "-1" };
091:                DescriptorSupport attrDescr = new DescriptorSupport(names,
092:                        values);
093:                ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(
094:                        attrName, String.class.getName(), "", true, false,
095:                        false, attrDescr);
096:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
097:                        ModelMBeanTarget.class.getName(), "",
098:                        new ModelMBeanAttributeInfo[] { attrInfo }, null, null,
099:                        null);
100:
101:                RequiredModelMBean rmmb = new RequiredModelMBean();
102:                rmmb.setModelMBeanInfo(info);
103:                rmmb.setManagedResource(bean, "ObjectReference");
104:                m_server.registerMBean(rmmb, name);
105:
106:                // No get method, should always get the default value back
107:                int num = 5;
108:                for (int i = 0; i < num; ++i) {
109:                    String value = (String) m_server.getAttribute(name,
110:                            attrName);
111:                    assertEquals("Returned value is not the default", value,
112:                            "DEFAULT");
113:                }
114:
115:                assertEquals("Wrong staleness algorithm", 0, counter.get());
116:            }
117:
118:            public void testGetAttributeAlwaysStale() throws Exception {
119:                ObjectName name = new ObjectName(":type=test");
120:
121:                MutableInteger counter = new MutableInteger(0);
122:                ModelMBeanTarget bean = new ModelMBeanTarget(counter);
123:
124:                String attrName = "FixedContent";
125:
126:                String[] names = new String[] { "name", "descriptorType",
127:                        "value", "iterable", "displayName", "default",
128:                        "getMethod", "currencyTimeLimit" };
129:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
130:                // if currencyTimeLimit is -1 then the value is always stale
131:                // fix for bug #794313
132:                Object[] values = new Object[] { attrName, "attribute", null,
133:                        "false", "", "DEFAULT", "get" + attrName, "-1" };
134:                DescriptorSupport attrDescr = new DescriptorSupport(names,
135:                        values);
136:                ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(
137:                        attrName, String.class.getName(), "", true, false,
138:                        false, attrDescr);
139:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
140:                        ModelMBeanTarget.class.getName(), "",
141:                        new ModelMBeanAttributeInfo[] { attrInfo }, null, null,
142:                        null);
143:
144:                RequiredModelMBean rmmb = new RequiredModelMBean();
145:                rmmb.setModelMBeanInfo(info);
146:                rmmb.setManagedResource(bean, "ObjectReference");
147:                m_server.registerMBean(rmmb, name);
148:
149:                // We set the staleness to 0 (-> always stale) so test if the bean method is always called
150:                String fixed = bean.getFixedContent();
151:                counter.set(0);
152:                int num = 5;
153:                for (int i = 0; i < num; ++i) {
154:                    String value = (String) m_server.getAttribute(name,
155:                            attrName);
156:                    assertEquals("Method returned different value", value,
157:                            fixed);
158:                }
159:
160:                assertEquals("Wrong staleness algorithm: " + counter.get(),
161:                        counter.get(), num);
162:            }
163:
164:            public void testGetAttributeNeverStale() throws Exception {
165:                ObjectName name = new ObjectName(":type=test");
166:
167:                MutableInteger counter = new MutableInteger(0);
168:                ModelMBeanTarget bean = new ModelMBeanTarget(counter);
169:
170:                String attrName = "FixedContent";
171:
172:                String[] names = new String[] { "name", "descriptorType",
173:                        "value", "iterable", "displayName", "default",
174:                        "getMethod", "currencyTimeLimit", "value" };
175:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
176:                // if currencyTimeLimit is 0 then the value is never stale
177:                // fix for bug #794313
178:                Object[] values = new Object[] { attrName, "attribute", null,
179:                        "false", "", "DEFAULT", "get" + attrName, "0", "NEVER" };
180:                DescriptorSupport attrDescr = new DescriptorSupport(names,
181:                        values);
182:                ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(
183:                        attrName, String.class.getName(), "", true, false,
184:                        false, attrDescr);
185:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
186:                        ModelMBeanTarget.class.getName(), "",
187:                        new ModelMBeanAttributeInfo[] { attrInfo }, null, null,
188:                        null);
189:
190:                RequiredModelMBean rmmb = new RequiredModelMBean();
191:                rmmb.setModelMBeanInfo(info);
192:                rmmb.setManagedResource(bean, "ObjectReference");
193:                m_server.registerMBean(rmmb, name);
194:
195:                // We set the staleness to 0 (-> never stale) so test that the bean method is never called
196:                int num = 5;
197:                for (int i = 0; i < num; ++i) {
198:                    String value = (String) m_server.getAttribute(name,
199:                            attrName);
200:                    assertEquals("Method returned different value", value,
201:                            "NEVER");
202:                }
203:
204:                assertEquals("Wrong staleness algorithm", counter.get(), 0);
205:            }
206:
207:            public void testGetAttributeStale() throws Exception {
208:                ObjectName name = new ObjectName(":type=test");
209:
210:                MutableInteger counter = new MutableInteger(0);
211:                ModelMBeanTarget bean = new ModelMBeanTarget(counter);
212:
213:                String attrName = "MutableContent";
214:
215:                String[] names = new String[] { "name", "descriptorType",
216:                        "value", "iterable", "displayName", "default",
217:                        "getMethod", "currencyTimeLimit" };
218:                Object[] values = new Object[] { attrName, "attribute", null,
219:                        "false", "", "DEFAULT", "get" + attrName, "2" };
220:                DescriptorSupport attrDescr = new DescriptorSupport(names,
221:                        values);
222:                ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(
223:                        attrName, "java.lang.String", "", true, false, false,
224:                        attrDescr);
225:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
226:                        "test.javax.management.modelmbean.ModelMBeanTarget",
227:                        "", new ModelMBeanAttributeInfo[] { attrInfo }, null,
228:                        null, null);
229:
230:                RequiredModelMBean rmmb = new RequiredModelMBean();
231:                rmmb.setModelMBeanInfo(info);
232:                rmmb.setManagedResource(bean, "ObjectReference");
233:                m_server.registerMBean(rmmb, name);
234:
235:                // We set the staleness to 2 seconds
236:
237:                // First time
238:                bean.setMutableContent("First");
239:                String attrValue = (String) m_server.getAttribute(name,
240:                        attrName);
241:                assertEquals("getAttribute does not work", attrValue, "First");
242:
243:                // Now value should be cached, check it
244:                bean.setMutableContent("Second");
245:                attrValue = (String) m_server.getAttribute(name, attrName);
246:                assertEquals("Attribute value caching does not work",
247:                        attrValue, "First");
248:
249:                // Now wait 2 seconds
250:                Thread.sleep(2000);
251:                attrValue = (String) m_server.getAttribute(name, attrName);
252:                assertEquals("Attribute staleness algorithm does not work",
253:                        attrValue, "Second");
254:            }
255:
256:            public void testGetAttributes() throws Exception {
257:                String attrName1 = "FixedContent";
258:                String attrName2 = "MutableContent";
259:
260:                ObjectName name = new ObjectName(":type=test");
261:
262:                MutableInteger counter = new MutableInteger(0);
263:                ModelMBeanTarget bean = new ModelMBeanTarget(counter);
264:
265:                String[] names1 = new String[] { "name", "descriptorType",
266:                        "value", "iterable", "displayName", "getMethod",
267:                        "currencyTimeLimit" };
268:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
269:                // currencyTimeLimit is now -1
270:                Object[] values1 = new Object[] { attrName1, "attribute", null,
271:                        "false", "", "get" + attrName1, "-1" };
272:                DescriptorSupport attrDescr1 = new DescriptorSupport(names1,
273:                        values1);
274:
275:                String[] names2 = new String[] { "name", "descriptorType",
276:                        "value", "iterable", "displayName", "getMethod",
277:                        "currencyTimeLimit" };
278:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
279:                // currencyTimeLimit is now -1
280:                Object[] values2 = new Object[] { attrName2, "attribute", null,
281:                        "false", "", "get" + attrName2, "-1" };
282:                DescriptorSupport attrDescr2 = new DescriptorSupport(names2,
283:                        values2);
284:
285:                ModelMBeanAttributeInfo attrInfo1 = new ModelMBeanAttributeInfo(
286:                        attrName1, String.class.getName(), "", true, false,
287:                        false, attrDescr1);
288:                ModelMBeanAttributeInfo attrInfo2 = new ModelMBeanAttributeInfo(
289:                        attrName2, String.class.getName(), "", true, false,
290:                        false, attrDescr2);
291:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
292:                        ModelMBeanTarget.class.getName(), "",
293:                        new ModelMBeanAttributeInfo[] { attrInfo1, attrInfo2 },
294:                        null, null, null);
295:
296:                RequiredModelMBean rmmb = new RequiredModelMBean();
297:                rmmb.setModelMBeanInfo(info);
298:                rmmb.setManagedResource(bean, "ObjectReference");
299:                m_server.registerMBean(rmmb, name);
300:
301:                String[] attributes = new String[] { attrName1, attrName2 };
302:                AttributeList list = m_server.getAttributes(name, attributes);
303:                assertEquals("Wrong number of attributes", list.size(), 2);
304:
305:                // Check that they're really the right ones
306:                Attribute attr = (Attribute) list.get(0);
307:                assertEquals(attr.getName(), attrName1);
308:                attr = (Attribute) list.get(1);
309:                assertEquals(attr.getName(), attrName2);
310:
311:                // Test also a wrong attribute
312:                attributes = new String[] { attrName1, null, attrName2 };
313:                list = m_server.getAttributes(name, attributes);
314:                assertEquals(list.size(), 2);
315:
316:                // Check that they're really the right ones
317:                attr = (Attribute) list.get(0);
318:                assertEquals(attr.getName(), attrName1);
319:                attr = (Attribute) list.get(1);
320:                assertEquals(attr.getName(), attrName2);
321:
322:                // Test also a wrong attribute
323:                attributes = new String[] { "NonExisting", attrName1, attrName2 };
324:                list = m_server.getAttributes(name, attributes);
325:                assertEquals(list.size(), 2);
326:                // Check that they're really the right ones
327:                attr = (Attribute) list.get(0);
328:                assertEquals(attr.getName(), attrName1);
329:                attr = (Attribute) list.get(1);
330:                assertEquals(attr.getName(), attrName2);
331:
332:                // Test also a wrong attribute
333:                attributes = new String[] { "NonExisting", attrName2 };
334:                list = m_server.getAttributes(name, attributes);
335:                assertEquals(list.size(), 1);
336:                // Check that it is really the right one
337:                attr = (Attribute) list.get(0);
338:                assertEquals(attr.getName(), attrName2);
339:            }
340:
341:            public void testSetAttribute() throws Exception {
342:                String attrName1 = "MutableContent";
343:
344:                ObjectName name = new ObjectName(":type=test");
345:
346:                MutableInteger counter = new MutableInteger(0);
347:                ModelMBeanTarget bean = new ModelMBeanTarget(counter);
348:
349:                String[] names1 = new String[] { "name", "descriptorType",
350:                        "value", "iterable", "displayName", "setMethod",
351:                        "getMethod", "currencyTimeLimit" };
352:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
353:                // currencyTimeLimit is now -1
354:                Object[] values1 = new Object[] { attrName1, "attribute", null,
355:                        "false", "", "set" + attrName1, "get" + attrName1, "-1" };
356:                DescriptorSupport attrDescr1 = new DescriptorSupport(names1,
357:                        values1);
358:
359:                ModelMBeanAttributeInfo attrInfo1 = new ModelMBeanAttributeInfo(
360:                        attrName1, String.class.getName(), "", true, true,
361:                        false, attrDescr1);
362:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
363:                        ModelMBeanTarget.class.getName(), "",
364:                        new ModelMBeanAttributeInfo[] { attrInfo1 }, null,
365:                        null, null);
366:
367:                final MutableBoolean storeTester = new MutableBoolean(false);
368:                RequiredModelMBean rmmb = new StoreTesterRMMB(storeTester);
369:                rmmb.setModelMBeanInfo(info);
370:                rmmb.setManagedResource(bean, "ObjectReference");
371:                m_server.registerMBean(rmmb, name);
372:
373:                // Adding a attribute change notification listener
374:                final MutableInteger listenerCount = new MutableInteger(0);
375:                rmmb.addAttributeChangeNotificationListener(
376:                        new NotificationListener() {
377:                            public void handleNotification(
378:                                    Notification notification, Object handback) {
379:                                listenerCount.set(listenerCount.get() + 1);
380:                            }
381:                        }, attrName1, null);
382:
383:                String value = "SET_FIRST_TIME";
384:                Attribute attribute = new Attribute(attrName1, value);
385:                m_server.setAttribute(name, attribute);
386:
387:                // check that has really been set
388:                assertEquals(bean.getMutableContent(), value);
389:                // check through MBeanServer
390:                assertEquals(m_server.getAttribute(name, attrName1), value);
391:                // check that listener has been called
392:                assertEquals(listenerCount.get(), 1);
393:                // There is no persistence settings, check that store was not called
394:                assertFalse("Store should not have been called", storeTester
395:                        .get());
396:
397:                // Adding a attribute change notification listener with
398:                // null as attribute. test for bug #742389
399:                NotificationListener dummyListener = new NotificationListener() {
400:                    public void handleNotification(Notification notification,
401:                            Object handback) {
402:                    }
403:                };
404:
405:                rmmb.addAttributeChangeNotificationListener(dummyListener,
406:                        null, null);
407:                rmmb.removeAttributeChangeNotificationListener(dummyListener,
408:                        null);
409:
410:                // Change the persist policy - have to unregeister to call setModelMBeanInfo
411:                m_server.unregisterMBean(name);
412:                attrDescr1.setField("persistPolicy", "OnUpdate");
413:                info.setDescriptor(attrDescr1, "attribute");
414:                rmmb.setModelMBeanInfo(info);
415:                storeTester.set(false);
416:                m_server.registerMBean(rmmb, name);
417:
418:                value = "SET_SECOND_TIME";
419:                attribute = new Attribute(attrName1, value);
420:                m_server.setAttribute(name, attribute);
421:
422:                // check that listener has been called
423:                assertEquals(listenerCount.get(), 2);
424:                // There are persistence settings, check that store was called
425:                assertTrue("Store should have been called", storeTester.get());
426:
427:                // Now remove setMethod - again we have to unregister
428:                m_server.unregisterMBean(name);
429:                names1 = new String[] { "name", "descriptorType", "value",
430:                        "iterable", "displayName", "getMethod",
431:                        "currencyTimeLimit" };
432:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
433:                // currencyTimeLimit is now -1
434:                values1 = new Object[] { attrName1, "attribute", null, "false",
435:                        "", "get" + attrName1, "-1" };
436:                attrDescr1 = new DescriptorSupport(names1, values1);
437:                attrDescr1.setField("persistPolicy", "OnUpdate");
438:                info.setDescriptor(attrDescr1, "attribute");
439:                rmmb.setModelMBeanInfo(info);
440:                storeTester.set(false);
441:                m_server.registerMBean(rmmb, name);
442:
443:                value = "SET_THIRD_TIME";
444:                attribute = new Attribute(attrName1, value);
445:                m_server.setAttribute(name, attribute);
446:
447:                // check that listener has been called
448:                assertEquals(listenerCount.get(), 3);
449:                // There are persistence settings, check that store was called
450:                assertTrue("Store should have been called", storeTester.get());
451:                // Check the attribute value
452:                if (bean.getMutableContent().equals(value)) {
453:                    fail("No setMethod, bean should not have been modified");
454:                }
455:                if (info.getAttribute(attrName1).getDescriptor().getFieldValue(
456:                        "value") != null) {
457:                    fail("New value should not have been cached since currencyTimeLimit is negative");
458:                }
459:
460:                // Test attribute that takes array as parameters
461:                m_server.unregisterMBean(name);
462:                String attrName = "ArrayAttribute";
463:                String[] names = new String[] { "name", "descriptorType",
464:                        "value", "iterable", "displayName", "getMethod",
465:                        "setMethod", "currencyTimeLimit" };
466:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
467:                // currencyTimeLimit is now -1
468:                Object[] values = new Object[] { attrName, "attribute", null,
469:                        "true", "", "get" + attrName, "set" + attrName, "-1" };
470:                Descriptor attrDescr = new DescriptorSupport(names, values);
471:
472:                ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(
473:                        attrName, new String[0].getClass().getName(), "", true,
474:                        true, false, attrDescr);
475:                info = new ModelMBeanInfoSupport(ModelMBeanTarget.class
476:                        .getName(), "",
477:                        new ModelMBeanAttributeInfo[] { attrInfo }, null, null,
478:                        null);
479:                rmmb.setModelMBeanInfo(info);
480:                m_server.registerMBean(rmmb, name);
481:
482:                String[] v = new String[] { "one", "two" };
483:                attribute = new Attribute(attrName, v);
484:                m_server.setAttribute(name, attribute);
485:            }
486:
487:            public void testSetAttributes() throws Exception {
488:                String attrName1 = "MutableContent";
489:                String attrName2 = "MutableContent2";
490:
491:                ObjectName name = new ObjectName(":type=test");
492:
493:                MutableInteger counter = new MutableInteger(0);
494:                ModelMBeanTarget bean = new ModelMBeanTarget(counter);
495:
496:                String[] names1 = new String[] { "name", "descriptorType",
497:                        "value", "iterable", "displayName", "getMethod",
498:                        "setMethod", "currencyTimeLimit" };
499:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
500:                // currencyTimeLimit is now -1
501:                Object[] values1 = new Object[] { attrName1, "attribute", null,
502:                        "false", "", "get" + attrName1, "set" + attrName1, "-1" };
503:                DescriptorSupport attrDescr1 = new DescriptorSupport(names1,
504:                        values1);
505:
506:                String[] names2 = new String[] { "name", "descriptorType",
507:                        "value", "iterable", "displayName", "getMethod",
508:                        "setMethod", "currencyTimeLimit" };
509:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
510:                // currencyTimeLimit is now -1
511:                Object[] values2 = new Object[] { attrName2, "attribute", null,
512:                        "false", "", "get" + attrName2, "set" + attrName2, "-1" };
513:                DescriptorSupport attrDescr2 = new DescriptorSupport(names2,
514:                        values2);
515:
516:                ModelMBeanAttributeInfo attrInfo1 = new ModelMBeanAttributeInfo(
517:                        attrName1, String.class.getName(), "", true, true,
518:                        false, attrDescr1);
519:                ModelMBeanAttributeInfo attrInfo2 = new ModelMBeanAttributeInfo(
520:                        attrName2, "int", "", true, true, false, attrDescr2);
521:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
522:                        ModelMBeanTarget.class.getName(), "",
523:                        new ModelMBeanAttributeInfo[] { attrInfo1, attrInfo2 },
524:                        null, null, null);
525:
526:                RequiredModelMBean rmmb = new RequiredModelMBean();
527:                rmmb.setModelMBeanInfo(info);
528:                rmmb.setManagedResource(bean, "ObjectReference");
529:                m_server.registerMBean(rmmb, name);
530:
531:                Attribute attr1 = new Attribute(attrName1, "FIRST");
532:                Attribute attr2 = new Attribute(attrName2, new Integer(5));
533:                AttributeList list = new AttributeList();
534:                list.add(attr1);
535:                list.add(attr2);
536:                AttributeList result = m_server.setAttributes(name, list);
537:                assertEquals("Wrong number of attributes were set", result
538:                        .size(), 2);
539:                // Check that they're really the right ones
540:                Attribute attr = (Attribute) result.get(0);
541:                assertEquals(attr, attr1);
542:                attr = (Attribute) result.get(1);
543:                assertEquals(attr, attr2);
544:                // Check that they were really set
545:                assertEquals(bean.getMutableContent(), attr1.getValue());
546:                assertEquals(bean.getMutableContent2(), ((Integer) attr2
547:                        .getValue()).intValue());
548:
549:                // Test non-existing attribute
550:                attr = new Attribute("NonExisting", null);
551:                attr2 = new Attribute(attrName2, new Integer(7));
552:                list.clear();
553:                list.add(attr);
554:                list.add(attr2);
555:                result = m_server.setAttributes(name, list);
556:                assertEquals(result.size(), 1);
557:                // Check that they're really the right ones
558:                attr = (Attribute) result.get(0);
559:                assertEquals(attr, attr2);
560:                // Check that they were really set
561:                assertEquals(bean.getMutableContent2(), ((Integer) attr2
562:                        .getValue()).intValue());
563:
564:                attr = new Attribute("NonExisting", null);
565:                list.clear();
566:                list.add(attr);
567:                result = m_server.setAttributes(name, list);
568:                assertEquals(result.size(), 0);
569:            }
570:
571:            public void testInvoke() throws Exception {
572:                String operation = "operation1";
573:
574:                ObjectName name = new ObjectName(":type=test");
575:
576:                MutableInteger counter = new MutableInteger(0);
577:                ModelMBeanTarget bean = new ModelMBeanTarget(counter);
578:
579:                String[] names1 = new String[] { "name", "descriptorType",
580:                        "displayName", "role", "targetObject",
581:                        "targetObjectType", "currencyTimeLimit" };
582:                // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
583:                // currencyTimeLimit is now -1
584:                Object[] values1 = new Object[] { operation, "operation", "",
585:                        "operation", null, null, "-1" };
586:                DescriptorSupport operDescr = new DescriptorSupport(names1,
587:                        values1);
588:
589:                MBeanParameterInfo paramInfo1 = new MBeanParameterInfo("c",
590:                        "char", "");
591:                MBeanParameterInfo paramInfo2 = new MBeanParameterInfo("s",
592:                        "short", "");
593:                MBeanParameterInfo paramInfo3 = new MBeanParameterInfo("f",
594:                        new float[0].getClass().getName(), "");
595:                MBeanParameterInfo paramInfo4 = new MBeanParameterInfo("c",
596:                        new Object[0][0].getClass().getName(), "");
597:                ModelMBeanOperationInfo operInfo = new ModelMBeanOperationInfo(
598:                        operation, "", new MBeanParameterInfo[] { paramInfo1,
599:                                paramInfo2, paramInfo3, paramInfo4 },
600:                        "java.util.List", ModelMBeanOperationInfo.UNKNOWN,
601:                        operDescr);
602:
603:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
604:                        ModelMBeanTarget.class.getName(), "", null, null,
605:                        new ModelMBeanOperationInfo[] { operInfo }, null);
606:
607:                RequiredModelMBean rmmb = new RequiredModelMBean();
608:                rmmb.setModelMBeanInfo(info);
609:                rmmb.setManagedResource(bean, "ObjectReference");
610:                m_server.registerMBean(rmmb, name);
611:
612:                short s = 10;
613:                Object[] args = new Object[] { new Character('z'),
614:                        new Short(s), new float[] { 1.0F },
615:                        new Object[][] { { "Hello" }, { "World" } } };
616:                String[] params = new String[] { paramInfo1.getType(),
617:                        paramInfo2.getType(), paramInfo3.getType(),
618:                        paramInfo4.getType() };
619:                List list = (List) m_server.invoke(name, operation, args,
620:                        params);
621:
622:                // Test that was really called
623:                assertEquals(counter.get(), 1);
624:                // Right values ?
625:                for (int i = 0; i < list.size(); ++i) {
626:                    Object obj = list.get(i);
627:                    assertEquals("Returned value is different: " + obj,
628:                            args[i], obj);
629:                }
630:
631:                m_server.unregisterMBean(name);
632:                ModelMBeanTarget.TargetBean target = new ModelMBeanTarget.TargetBean();
633:                operDescr.setField("targetObject", target);
634:                operDescr.setField("targetObjectType", "ObjectReference");
635:
636:                info.setDescriptor(operDescr, "operation");
637:                rmmb.setModelMBeanInfo(info);
638:                m_server.registerMBean(rmmb, name);
639:
640:                list = (List) m_server.invoke(name, operation, args, params);
641:
642:                // Test that was not called
643:                assertEquals("Operation should not have been called", counter
644:                        .get(), 1);
645:                // Right values ?
646:                for (int i = 0; i < list.size(); ++i) {
647:                    Object obj = list.get(list.size() - 1 - i);
648:                    assertEquals("Returned value is different: " + obj,
649:                            args[i], obj);
650:                }
651:            }
652:
653:            public void testInvokeModelMBeanMethods() throws Exception {
654:                ObjectName name = new ObjectName(":type=test");
655:
656:                ModelMBean mmb = (ModelMBean) m_server.instantiate(
657:                        RequiredModelMBean.class.getName(), null);
658:
659:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
660:                        ModelMBeanTarget.class.getName(), "", null, null, null,
661:                        null);
662:
663:                mmb.setModelMBeanInfo(info);
664:
665:                m_server.registerMBean(mmb, name);
666:
667:                // Now try to invoke methods that are part of the ModelMBean interface
668:
669:                try {
670:                    m_server.setAttribute(name, new Attribute("ModelMBeanInfo",
671:                            info));
672:                    fail("Cannot invoke a ModelMBean method via MBeanServer");
673:                } catch (Exception ignored) {
674:                }
675:
676:                // Bug #940161 Required ModelMBean methods are not invoked
677:                m_server.invoke(name, "setManagedResource", new Object[] {
678:                        new ModelMBeanTarget(new MutableInteger(0)),
679:                        "ObjectReference" }, new String[] {
680:                        Object.class.getName(), String.class.getName() });
681:                m_server.invoke(name, "store", new Object[0], new String[0]);
682:                m_server.invoke(name, "sendNotification",
683:                        new Object[] { "generic" }, new String[] { String.class
684:                                .getName() });
685:
686:                // Now specify setManagedResource as an operation in the MMBI
687:                m_server.unregisterMBean(name);
688:                String operation = "setManagedResource";
689:                MBeanParameterInfo paramInfo1 = new MBeanParameterInfo(
690:                        "resource", Object.class.getName(), "");
691:                MBeanParameterInfo paramInfo2 = new MBeanParameterInfo("type",
692:                        String.class.getName(), "");
693:                ModelMBeanOperationInfo operInfo = new ModelMBeanOperationInfo(
694:                        operation, "", new MBeanParameterInfo[] { paramInfo1,
695:                                paramInfo2 }, null,
696:                        ModelMBeanOperationInfo.ACTION, null);
697:                info = new ModelMBeanInfoSupport(ModelMBeanTarget.class
698:                        .getName(), "", null, null,
699:                        new ModelMBeanOperationInfo[] { operInfo }, null);
700:                mmb.setModelMBeanInfo(info);
701:                m_server.registerMBean(mmb, name);
702:
703:                // Spec says I must be able to invoke it
704:                Object target = new ModelMBeanTarget(new MutableInteger(0));
705:                m_server.invoke(name, operation, new Object[] { target,
706:                        "ObjectReference" }, new String[] {
707:                        Object.class.getName(), String.class.getName() });
708:            }
709:
710:            public void testNotifications() throws Exception {
711:                ObjectName name = new ObjectName(":type=test");
712:
713:                ModelMBeanNotificationInfo notification[] = new ModelMBeanNotificationInfo[1];
714:                notification[0] = new ModelMBeanNotificationInfo(
715:                        new String[] { ModelMBeanTarget.class.getName()
716:                                + ".notification" }, "name", "");
717:
718:                ModelMBeanAttributeInfo attributeInfo = new ModelMBeanAttributeInfo(
719:                        "MutableContent", String.class.getName(), "", true,
720:                        true, false);
721:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
722:                        ModelMBeanTarget.class.getName(), "",
723:                        new ModelMBeanAttributeInfo[] { attributeInfo }, null,
724:                        null, notification);
725:
726:                RequiredModelMBean rmmb = new RequiredModelMBean();
727:                rmmb.setModelMBeanInfo(info);
728:                rmmb.setManagedResource(new ModelMBeanTarget(
729:                        new MutableInteger(0)), "objectReference");
730:                m_server.registerMBean(rmmb, name);
731:
732:                Object listenerHandback = new Object();
733:
734:                TestNotificationListener listener = new TestNotificationListener();
735:                m_server.addNotificationListener(name, listener, null,
736:                        listenerHandback);
737:
738:                rmmb.sendNotification("generic notification");
739:                assertEquals("jmx.modelmbean.generic", listener.type);
740:                assertEquals("generic notification", listener.message);
741:                assertSame(listenerHandback, listener.handback);
742:
743:                rmmb.sendNotification(new Notification("my.type", rmmb, 1,
744:                        "a message"));
745:                assertEquals("my.type", listener.type);
746:                assertEquals("a message", listener.message);
747:                assertSame(listenerHandback, listener.handback);
748:
749:                m_server.setAttribute(name, new Attribute("MutableContent",
750:                        "Hello World"));
751:                assertEquals("jmx.attribute.change", listener.type);
752:            }
753:
754:            public void testGetNotificationInfo() throws Exception {
755:                RequiredModelMBean rmmb = new RequiredModelMBean();
756:                MBeanNotificationInfo[] notificationInfos = rmmb
757:                        .getNotificationInfo();
758:                assertEquals(2, notificationInfos.length);
759:
760:                ModelMBeanNotificationInfo notification[] = new ModelMBeanNotificationInfo[1];
761:                notification[0] = new ModelMBeanNotificationInfo(
762:                        new String[] { ModelMBeanTarget.class.getName()
763:                                + ".notification" }, "name", "");
764:
765:                ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(
766:                        ModelMBeanTarget.class.getName(), "", null, null, null,
767:                        notification);
768:                rmmb.setModelMBeanInfo(info);
769:                notificationInfos = rmmb.getNotificationInfo();
770:                assertEquals(3, notificationInfos.length);
771:            }
772:
773:            public static class StoreTesterRMMB extends RequiredModelMBean {
774:                private MutableBoolean m_stored;
775:
776:                public StoreTesterRMMB(MutableBoolean storeTester)
777:                        throws MBeanException {
778:                    m_stored = storeTester;
779:                }
780:
781:                public void store() throws MBeanException,
782:                        RuntimeOperationsException, InstanceNotFoundException {
783:                    m_stored.set(true);
784:                    super .store();
785:                }
786:            }
787:
788:            public static class TestNotificationListener implements 
789:                    NotificationListener {
790:                String type = null;
791:                String message = null;
792:                Object handback = null;
793:
794:                public void handleNotification(Notification notification,
795:                        Object handback) {
796:                    type = notification.getType();
797:                    message = notification.getMessage();
798:                    this.handback = handback;
799:                }
800:            }
801:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.