Source Code Cross Referenced for MBeanServerTEST.java in  » EJB-Server-JBoss-4.2.1 » jmx » test » compliance » server » 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 » EJB Server JBoss 4.2.1 » jmx » test.compliance.server 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * JBoss, Home of Professional Open Source.
0003:         * Copyright 2006, Red Hat Middleware LLC, and individual contributors
0004:         * as indicated by the @author tags. See the copyright.txt file in the
0005:         * distribution for a full listing of individual contributors.
0006:         *
0007:         * This is free software; you can redistribute it and/or modify it
0008:         * under the terms of the GNU Lesser General Public License as
0009:         * published by the Free Software Foundation; either version 2.1 of
0010:         * the License, or (at your option) any later version.
0011:         *
0012:         * This software is distributed in the hope that it will be useful,
0013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015:         * Lesser General Public License for more details.
0016:         *
0017:         * You should have received a copy of the GNU Lesser General Public
0018:         * License along with this software; if not, write to the Free
0019:         * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020:         * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
0021:         */
0022:        package test.compliance.server;
0023:
0024:        import java.util.Arrays;
0025:        import java.util.List;
0026:
0027:        import javax.management.Attribute;
0028:        import javax.management.AttributeNotFoundException;
0029:        import javax.management.InstanceNotFoundException;
0030:        import javax.management.MBeanException;
0031:        import javax.management.MBeanRegistrationException;
0032:        import javax.management.MBeanServer;
0033:        import javax.management.MBeanServerFactory;
0034:        import javax.management.MBeanServerNotification;
0035:        import javax.management.Notification;
0036:        import javax.management.NotificationFilterSupport;
0037:        import javax.management.NotificationListener;
0038:        import javax.management.ObjectInstance;
0039:        import javax.management.ObjectName;
0040:        import javax.management.ReflectionException;
0041:        import javax.management.RuntimeErrorException;
0042:        import javax.management.RuntimeMBeanException;
0043:        import javax.management.RuntimeOperationsException;
0044:        import javax.management.loading.MLet;
0045:
0046:        import junit.framework.AssertionFailedError;
0047:        import junit.framework.TestCase;
0048:        import test.compliance.server.support.BabarError;
0049:        import test.compliance.server.support.Base;
0050:        import test.compliance.server.support.BaseMBean;
0051:        import test.compliance.server.support.Broadcaster;
0052:        import test.compliance.server.support.Derived;
0053:        import test.compliance.server.support.Dynamic;
0054:        import test.compliance.server.support.ExceptionOnTheRun;
0055:        import test.compliance.server.support.LockedTest;
0056:        import test.compliance.server.support.LockedTest2;
0057:        import test.compliance.server.support.LockedTest3;
0058:        import test.compliance.server.support.MBeanListener;
0059:        import test.compliance.server.support.MyScreamingException;
0060:        import test.compliance.server.support.Test;
0061:        import test.compliance.server.support.Test2;
0062:        import test.compliance.server.support.Test3;
0063:        import test.compliance.server.support.Test4;
0064:        import test.compliance.server.support.Unrelated;
0065:        import test.compliance.server.support.UnrelatedMBean;
0066:
0067:        /**
0068:         * Tests the MBean server impl. through the <tt>MBeanServer</tt> interface.
0069:         *
0070:         * @author  <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
0071:         * @version $Revision: 57200 $
0072:         *   
0073:         */
0074:        public class MBeanServerTEST extends TestCase {
0075:            public MBeanServerTEST(String s) {
0076:                super (s);
0077:            }
0078:
0079:            // MBeanServer invoke --------------------------------------------
0080:
0081:            /**
0082:             * Tests invoke with primitive boolean return type. <p>
0083:             */
0084:            public void testInvokeWithPrimitiveBooleanReturn() throws Exception {
0085:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0086:                ObjectName name = new ObjectName(":test=test");
0087:                server.registerMBean(new Test(), name);
0088:
0089:                Boolean bool = (Boolean) server.invoke(name,
0090:                        "opWithPrimBooleanReturn", null, null);
0091:
0092:                assertTrue(bool.booleanValue() == true);
0093:            }
0094:
0095:            /**
0096:             * Tests invoke with primitive long array return type. <p>
0097:             */
0098:            public void testInvokeWithPrimitiveLongArrayReturn()
0099:                    throws Exception {
0100:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0101:                ObjectName name = new ObjectName(":test=test");
0102:                server.registerMBean(new Test(), name);
0103:
0104:                long[] array = (long[]) server.invoke(name,
0105:                        "opWithPrimLongArrayReturn", null, null);
0106:
0107:                assertTrue(array[0] == 1);
0108:                assertTrue(array[1] == 2);
0109:                assertTrue(array[2] == 3);
0110:            }
0111:
0112:            /**
0113:             * Tests invoke with Long array return type. <p>
0114:             */
0115:            public void testInvokeWithLongArrayReturn() throws Exception {
0116:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0117:                ObjectName name = new ObjectName(":test=test");
0118:                server.registerMBean(new Test(), name);
0119:
0120:                Long[] array = (Long[]) server.invoke(name,
0121:                        "opWithLongArrayReturn", null, null);
0122:
0123:                assertTrue(array[0].longValue() == 1);
0124:                assertTrue(array[1].longValue() == 2);
0125:                assertTrue(array[2].longValue() == 3);
0126:            }
0127:
0128:            /**
0129:             * Tests invoke with primitive long return type. <p>
0130:             */
0131:            public void testInvokeWithPrimitiveLongReturn() throws Exception {
0132:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0133:                ObjectName name = new ObjectName(":test=test");
0134:                server.registerMBean(new Test(), name);
0135:
0136:                Long l = (Long) server.invoke(name, "opWithPrimLongReturn",
0137:                        null, null);
0138:
0139:                assertTrue(l.longValue() == 1234567890123l);
0140:            }
0141:
0142:            /**
0143:             * Tests invoke with primitive double return type. <p>
0144:             */
0145:            public void testInvokeWithPrimitiveDoubleReturn() throws Exception {
0146:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0147:                ObjectName name = new ObjectName(":test=test");
0148:                server.registerMBean(new Test(), name);
0149:
0150:                Double d = (Double) server.invoke(name,
0151:                        "opWithPrimDoubleReturn", null, null);
0152:
0153:                assertTrue(d.doubleValue() == 0.1234567890123d);
0154:            }
0155:
0156:            /**
0157:             * Tests invoke with long signature. <p>
0158:             */
0159:            public void testInvokeWithLongSignature() throws Exception {
0160:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0161:                ObjectName name = new ObjectName(":test=test");
0162:                server.registerMBean(new Test(), name);
0163:
0164:                server.invoke(name, "opWithLongSignature", new Object[] {
0165:                        new Integer(1), new Integer(2), new Integer(3),
0166:                        new Integer(4), new Integer(5), new Integer(6),
0167:                        new Integer(7), new Integer(8), new Integer(9),
0168:                        new Integer(10), new Integer(11), new Integer(12),
0169:                        new Integer(13), new Integer(14), new Integer(15),
0170:                        new Integer(16), new Integer(17), new Integer(18),
0171:                        new Integer(19), new Integer(20) }, new String[] {
0172:                        "int", "int", "int", "int", "int", "int", "int", "int",
0173:                        "int", "int", "int", "int", "int", "int", "int", "int",
0174:                        "int", "int", "int", "int" });
0175:            }
0176:
0177:            /**
0178:             * Tests invoke with mixed types in signature, especially types with double
0179:             * byte code length and arrays. <p>
0180:             */
0181:            public void testInvokeWithMixedSignature() throws Exception {
0182:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0183:                ObjectName name = new ObjectName(":test=test");
0184:                server.registerMBean(new Test(), name);
0185:
0186:                server
0187:                        .invoke(name, "opWithMixedSignature", new Object[] {
0188:                                new Integer(1), new Double(2.2D),
0189:                                new Long(333L), new Boolean(true),
0190:                                new Byte((byte) 0x02), new Short((short) 6),
0191:                                new long[] { 7L, 8L },
0192:                                new Long[] { new Long(1L), new Long(2L) },
0193:                                new Short((short) 9), new Byte((byte) 10),
0194:                                new Long(11L), new Double(1.2D),
0195:                                new Integer(13), new Integer(14),
0196:                                new Integer(15), new Integer(16),
0197:                                new Integer(17), new Integer(18),
0198:                                new Integer(19), new Integer(20) },
0199:                                new String[] { "int", "double", "long",
0200:                                        "boolean", "byte", "short", "[J",
0201:                                        "[Ljava.lang.Long;", "java.lang.Short",
0202:                                        "java.lang.Byte", "java.lang.Long",
0203:                                        "java.lang.Double", "int", "int",
0204:                                        "int", "int", "int", "int", "int",
0205:                                        "int" });
0206:            }
0207:
0208:            /**
0209:             * Attempts to invoke a method on an unregistered MBean; <tt>InstanceNotFoundException</tt> should occur.
0210:             */
0211:            public void testInvokeWithNonExistantMBean() throws Exception {
0212:                try {
0213:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0214:                    server.invoke(new ObjectName(":mbean=doesnotexist"),
0215:                            "noMethod", null, null);
0216:
0217:                    // should not reach here
0218:                    fail("InstanceNotFoundException was not thrown from an invoke operation on a non-existant MBean.");
0219:                } catch (InstanceNotFoundException e) {
0220:                    // should get here
0221:                }
0222:
0223:            }
0224:
0225:            /**
0226:             * Attempts to invoke a MBean operation that throws a business exception; <tt>MBeanException</tt> should be thrown.
0227:             */
0228:            public void testInvokeWithBusinessException() throws Exception {
0229:                try {
0230:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0231:                    ObjectName name = new ObjectName("test:test=test");
0232:                    server.registerMBean(new Test(), name);
0233:
0234:                    server.invoke(name, "operationWithException", null, null);
0235:
0236:                    // should not get here
0237:                    fail("MBeanException was not thrown.");
0238:                } catch (MBeanException e) {
0239:                    // this is expected
0240:                    assertTrue(e.getTargetException() instanceof  MyScreamingException);
0241:                }
0242:            }
0243:
0244:            // MBeanServer getAttribute --------------------------------------
0245:
0246:            public void testGetAttributeWithNonExistingAttribute()
0247:                    throws Exception {
0248:                try {
0249:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0250:                    Object foo = server
0251:                            .getAttribute(
0252:                                    new ObjectName(
0253:                                            "JMImplementation:type=MBeanServerDelegate"),
0254:                                    "Foo");
0255:
0256:                    // should not reach here
0257:                    fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
0258:                } catch (AttributeNotFoundException e) {
0259:                    // Expecting this.
0260:                }
0261:            }
0262:
0263:            public void testGetAttributeWithBusinessException()
0264:                    throws Exception {
0265:                try {
0266:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0267:                    ObjectName name = new ObjectName("test:test=test");
0268:                    server.registerMBean(new Test(), name);
0269:
0270:                    Object foo = server.getAttribute(name, "ThisWillScream");
0271:
0272:                    // should not reach here
0273:                    fail("Did not throw the screaming exception");
0274:                } catch (MBeanException e) {
0275:                    // this is expected
0276:                    assertTrue(e.getTargetException() instanceof  MyScreamingException);
0277:                }
0278:            }
0279:
0280:            public void testGetAttributeWithNonExistingMBean() throws Exception {
0281:                try {
0282:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0283:                    ObjectName name = new ObjectName("test:name=DoesNotExist");
0284:
0285:                    server.getAttribute(name, "Whatever");
0286:
0287:                    // should not reach here
0288:                    fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
0289:                } catch (InstanceNotFoundException e) {
0290:                    // this is expected
0291:                }
0292:            }
0293:
0294:            public void testGetAttributeWithUncheckedException()
0295:                    throws Exception {
0296:                try {
0297:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0298:                    ObjectName name = new ObjectName("test:test=test");
0299:                    server.registerMBean(new Test(), name);
0300:
0301:                    server.getAttribute(name, "ThrowUncheckedException");
0302:
0303:                    // should not reach here
0304:                    fail("RuntimeMBeanException was not thrown");
0305:                } catch (RuntimeMBeanException e) {
0306:                    // this is expected
0307:                    assertTrue(e.getTargetException() instanceof  ExceptionOnTheRun);
0308:                }
0309:            }
0310:
0311:            public void testGetAttributeWithError() throws Exception {
0312:                try {
0313:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0314:                    ObjectName name = new ObjectName("test:test=test");
0315:                    server.registerMBean(new Test(), name);
0316:
0317:                    server.getAttribute(name, "Error");
0318:
0319:                    // should not reach here
0320:                    fail("Error was not thrown");
0321:                } catch (RuntimeErrorException e) {
0322:                    // this is expected
0323:                    assertTrue(e.getTargetError() instanceof  BabarError);
0324:                }
0325:            }
0326:
0327:            // MBeanServer setAttribute --------------------------------------
0328:
0329:            public void testSetAttributeWithNonExistingAttribute()
0330:                    throws Exception {
0331:                try {
0332:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0333:                    server.setAttribute(new ObjectName(
0334:                            "JMImplementation:type=MBeanServerDelegate"),
0335:                            new Attribute("Foo", "value"));
0336:
0337:                    // should not reach here
0338:                    fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute.");
0339:                } catch (AttributeNotFoundException e) {
0340:                    // Expecting this.
0341:                }
0342:            }
0343:
0344:            public void testSetAttributeWithBusinessException()
0345:                    throws Exception {
0346:                try {
0347:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0348:                    ObjectName name = new ObjectName("test:test=test");
0349:                    server.registerMBean(new Test(), name);
0350:
0351:                    server.setAttribute(name, new Attribute("ThisWillScream",
0352:                            "value"));
0353:
0354:                    // should not reach here
0355:                    fail("Did not throw the screaming exception");
0356:                } catch (MBeanException e) {
0357:                    // this is expected
0358:                    assertTrue(e.getTargetException() instanceof  MyScreamingException);
0359:                }
0360:            }
0361:
0362:            public void testSetAttributeWithNonExistingMBean() throws Exception {
0363:                try {
0364:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0365:                    ObjectName name = new ObjectName("test:name=DoesNotExist");
0366:
0367:                    server.setAttribute(name, new Attribute("Whatever",
0368:                            "nothing"));
0369:
0370:                    // should not reach here
0371:                    fail("InstanceNotFoundException was not thrown on a nonexistant MBean.");
0372:                } catch (InstanceNotFoundException e) {
0373:                    // this is expected
0374:                }
0375:            }
0376:
0377:            public void testSetAttributeWithUncheckedException()
0378:                    throws Exception {
0379:                try {
0380:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0381:                    ObjectName name = new ObjectName("test:test=test");
0382:                    server.registerMBean(new Test(), name);
0383:
0384:                    server.setAttribute(name, new Attribute(
0385:                            "ThrowUncheckedException", "value"));
0386:
0387:                    // should not reach here
0388:                    fail("RuntimeMBeanException was not thrown");
0389:                } catch (RuntimeMBeanException e) {
0390:                    // this is expected
0391:                    assertTrue(e.getTargetException() instanceof  ExceptionOnTheRun);
0392:                }
0393:            }
0394:
0395:            public void testSetAttributeWithError() throws Exception {
0396:                try {
0397:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0398:                    ObjectName name = new ObjectName("test:test=test");
0399:                    server.registerMBean(new Test(), name);
0400:
0401:                    server.setAttribute(name, new Attribute("Error", "value"));
0402:
0403:                    // should not reach here
0404:                    fail("Error was not thrown");
0405:                } catch (RuntimeErrorException e) {
0406:                    // this is expected
0407:                    assertTrue(e.getTargetError() instanceof  BabarError);
0408:                }
0409:            }
0410:
0411:            // MBeanServer instantiate ---------------------------------------
0412:
0413:            /**
0414:             * Tests instantiate(String className). Class defined by system classloader.
0415:             */
0416:            public void testInstantiateWithDefaultConstructor()
0417:                    throws Exception {
0418:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0419:                Object o = server
0420:                        .instantiate("test.compliance.server.support.Test");
0421:
0422:                assertTrue(o instanceof  test.compliance.server.support.Test);
0423:            }
0424:
0425:            /**
0426:             * Tests instantiate(String className) with constructor that throws a checked application exception.
0427:             * Class defined by system classloader.
0428:             */
0429:            public void testInstantiateWithDefaultConstructorAndApplicationException()
0430:                    throws Exception {
0431:                try {
0432:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0433:                    Object o = server
0434:                            .instantiate("test.compliance.server.support.ConstructorTest");
0435:
0436:                    // shouldn't get here
0437:                    fail("Instantiate should have thrown an MBeanException.");
0438:                } catch (MBeanException e) {
0439:                    // this is expected
0440:                }
0441:            }
0442:
0443:            /**
0444:             * Tests instantiate(String className) with constructor that throws an unchecked application exception.
0445:             * Class defined by the system classloader.
0446:             */
0447:            public void testInstantiateWithDefaultConstructorAndRuntimeException()
0448:                    throws Exception {
0449:                try {
0450:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0451:                    Object o = server
0452:                            .instantiate("test.compliance.server.support.ConstructorTest2");
0453:
0454:                    // shouldn't get here
0455:                    fail("Instantiate should have thrown a RuntimeMBeanException.");
0456:                } catch (RuntimeMBeanException e) {
0457:                    // this is expected
0458:                }
0459:            }
0460:
0461:            /**
0462:             * Tests instantiate(String className) with constructor that throws an error.
0463:             * Class defined by the system classloader.
0464:             */
0465:            public void testInstantiateWithDefaultConstructorAndError()
0466:                    throws Exception {
0467:                try {
0468:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0469:                    Object o = server
0470:                            .instantiate("test.compliance.server.support.ConstructorTest3");
0471:
0472:                    // shouldn't get here
0473:                    fail("Instantiate should have thrown a RuntimeErrorException.");
0474:                } catch (RuntimeErrorException e) {
0475:                    // this is expected
0476:                }
0477:            }
0478:
0479:            /**
0480:             * Tests instantiate(String className) with constructor that fails with an unchecked exception in static init block.
0481:             * Class defined by the system classloader.
0482:             */
0483:            public void testInstantiateWithDefaultConstructorAndExceptionInInit()
0484:                    throws Exception {
0485:                try {
0486:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0487:
0488:                    // FAILS IN RI
0489:                    try {
0490:                        Object o = server
0491:                                .instantiate("test.compliance.server.support.ConstructorTest4");
0492:                    } catch (ExceptionInInitializerError e) {
0493:                        // RI lets this error through unwrapped. In general, the MBean server is responsible
0494:                        // of wrapping all errors and exceptions from MBeans and resource classes with either
0495:                        // RuntimeErrorException, RuntimeMBeanException or MBeanException. The javadoc is unclear in 
0496:                        // this case should a ReflectionException or MBeanException be thrown (neither one can wrap an
0497:                        // Error though). JBossMX throws an RuntimeMBeanException in case of an unchecked exception in
0498:                        // static initializer and a RuntimeErrorException in case of an error in static initializer.
0499:                        fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly.");
0500:                    }
0501:
0502:                    // shouldn't get here
0503:                    fail("Instantiate should have thrown a RuntimeMBeanException.");
0504:                } catch (RuntimeMBeanException e) {
0505:                    // this is expected
0506:
0507:                    assertTrue(e.getTargetException() instanceof  NullPointerException);
0508:                }
0509:            }
0510:
0511:            /**
0512:             * Tests instatiante(String className) with constructor that fails with an error in static init block.
0513:             * Class defined by the system classloader.
0514:             */
0515:            public void testInstantiateWithDefaultConstructorAndErrorInInit()
0516:                    throws Exception {
0517:                try {
0518:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0519:
0520:                    // FAILS IN RI
0521:                    try {
0522:                        Object o = server
0523:                                .instantiate("test.compliance.server.support.ConstructorTest5");
0524:                    } catch (BabarError e) {
0525:                        // RI lets this error through unwrapped. In general, the MBean server is responsible
0526:                        // of wrapping all errors and exceptions from MBeans and resource classes with either
0527:                        // RuntimeErrorException, RuntimeMBeanException or MBeanException. The javadoc is unclear in 
0528:                        // this case should a ReflectionException or MBeanException be thrown (neither one can wrap an
0529:                        // Error though). JBossMX throws an RuntimeMBeanException in case of an unchecked exception in
0530:                        // static initializer and a RuntimeErrorException in case of an error in static initializer.            
0531:                        fail("FAILS IN RI: MBeanServer fails to wrap an error or exception from a static initializer block correctly.");
0532:                    }
0533:
0534:                    // shouldn't get here
0535:                    fail("Instantiate should have thrown a RuntimeErrorException.");
0536:                } catch (RuntimeErrorException e) {
0537:                    // this is expected
0538:
0539:                    assertTrue(e.getTargetError() instanceof  test.compliance.server.support.BabarError);
0540:                }
0541:            }
0542:
0543:            /**
0544:             * Tests instantiate(String className) with unfound class.
0545:             */
0546:            public void testInstantiateWithDefaultConstructorAndUnknownClass()
0547:                    throws Exception {
0548:                try {
0549:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0550:                    Object o = server.instantiate("foo.Bar");
0551:
0552:                    // should not get here
0553:                    fail("Instantiate should have thrown a ReflectionException.");
0554:                } catch (ReflectionException e) {
0555:                    // this is expected
0556:                    assertTrue(e.getTargetException() instanceof  ClassNotFoundException);
0557:                }
0558:            }
0559:
0560:            /**
0561:             * Tests instantiate(String className) with class that doesn't have a default constructor.
0562:             */
0563:            public void testInstantiateWithMissingDefaultConstructor()
0564:                    throws Exception {
0565:                try {
0566:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0567:                    Object o = server
0568:                            .instantiate("test.compliance.server.support.ConstructorTest6");
0569:
0570:                    // should not get here
0571:                    fail("Instantiate should have thrown a ReflectionException.");
0572:                } catch (ReflectionException e) {
0573:                    // this is expected
0574:                }
0575:            }
0576:
0577:            /**
0578:             * Tests instantiate(String className) with protected (no access) no args constructor.
0579:             */
0580:            public void testInstantiateWithInaccessibleNoArgsConstructor()
0581:                    throws Exception {
0582:                try {
0583:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0584:                    Object o = server
0585:                            .instantiate("test.compliance.server.support.ConstructorTest7");
0586:
0587:                    // should not get here
0588:                    fail("Instantiate should have thrown a ReflectionException.");
0589:                } catch (ReflectionException e) {
0590:                    // this is expected
0591:                }
0592:            }
0593:
0594:            /**
0595:             * Tests instantiate(String className) with null class name. According to
0596:             * javadoc, should throw RuntimeOperationsException wrapping IllegalArgException.
0597:             */
0598:            public void testInstantiateWithNullClassName() throws Exception {
0599:                try {
0600:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0601:                    Object o = server.instantiate(null);
0602:
0603:                    // should not reach here
0604:                    fail("incorrect exception behavior");
0605:                } catch (RuntimeOperationsException e) {
0606:                    // expected
0607:
0608:                    // check that it wraps IAE
0609:                    assertTrue(e.getTargetException() instanceof  IllegalArgumentException);
0610:                }
0611:            }
0612:
0613:            /**
0614:             * Tests instantiate(String className) with empty class name string. should
0615:             * throw ReflectionException wrapping CNFE.
0616:             */
0617:            public void testInstantiateWithEmptyClassName() throws Exception {
0618:                try {
0619:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0620:                    Object o = server.instantiate("");
0621:
0622:                    // should not reach here
0623:                    fail("incorrect exception/classloading behavior");
0624:                } catch (ReflectionException e) {
0625:                    // expected
0626:
0627:                    // check that it wraps CNFE
0628:                    assertTrue(e.getTargetException() instanceof  ClassNotFoundException);
0629:                }
0630:            }
0631:
0632:            /**
0633:             * Tests instantiate(String className, ObjectName loader) with null class name. According to
0634:             * javadoc, should throw RuntimeOperationsException wrapping IllegalArgException.
0635:             */
0636:            public void testInstantiateWithNullClassName2() throws Exception {
0637:                try {
0638:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0639:                    Object o = server.instantiate(null, null);
0640:
0641:                    // should not reach here
0642:                    fail("incorrect exception behavior");
0643:                } catch (RuntimeOperationsException e) {
0644:                    // expected
0645:
0646:                    // check that it wraps IAE
0647:                    assertTrue(e.getTargetException() instanceof  IllegalArgumentException);
0648:                }
0649:            }
0650:
0651:            /**
0652:             * Tests instantiate(String className, ObjectName loader) with empty class name string. should
0653:             * throw ReflectionException wrapping CNFE.
0654:             */
0655:            public void testInstantiateWithEmptyClassName2() throws Exception {
0656:                try {
0657:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0658:                    Object o = server.instantiate("", null);
0659:
0660:                    // should not reach here
0661:                    fail("incorrect exception/classloading behavior");
0662:                } catch (ReflectionException e) {
0663:                    // expected
0664:
0665:                    // check that it wraps CNFE
0666:                    assertTrue(e.getTargetException() instanceof  ClassNotFoundException);
0667:                }
0668:            }
0669:
0670:            /**
0671:             * Tests instantiate(String className, Object[] args, String[] sign) with null
0672:             * class name. According to javadoc, should throw RuntimeOperationsException
0673:             * wrapping IllegalArgException.
0674:             */
0675:            public void testInstantiateWithNullClassName3() throws Exception {
0676:                try {
0677:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0678:                    Object o = server.instantiate(null, null, null);
0679:
0680:                    // should not reach here
0681:                    fail("incorrect exception behavior");
0682:                } catch (RuntimeOperationsException e) {
0683:                    // expected
0684:
0685:                    // check that it wraps IAE
0686:                    assertTrue(e.getTargetException() instanceof  IllegalArgumentException);
0687:                }
0688:            }
0689:
0690:            /**
0691:             * Tests instantiate(String className, Object[] args, String[] sign) with
0692:             * empty class name string. should throw ReflectionException wrapping CNFE.
0693:             */
0694:            public void testInstantiateWithEmptyClassName3() throws Exception {
0695:                try {
0696:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0697:                    Object o = server.instantiate("", null, null);
0698:
0699:                    // should not reach here
0700:                    fail("incorrect exception/classloading behavior");
0701:                } catch (ReflectionException e) {
0702:                    // expected
0703:
0704:                    // check that it wraps CNFE
0705:                    assertTrue(e.getTargetException() instanceof  ClassNotFoundException);
0706:                }
0707:            }
0708:
0709:            /**
0710:             * Tests instantiate(String className, ObjectName loader, Object[] args, String[] sign)
0711:             * with null class name. According to javadoc, should throw RuntimeOperationsException
0712:             * wrapping IllegalArgException.
0713:             */
0714:            public void testInstantiateWithNullClassName4() throws Exception {
0715:                try {
0716:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0717:                    Object o = server.instantiate(null, null, null, null);
0718:
0719:                    // should not reach here
0720:                    fail("incorrect exception behavior");
0721:                } catch (RuntimeOperationsException e) {
0722:                    // expected
0723:
0724:                    // check that it wraps IAE
0725:                    assertTrue(e.getTargetException() instanceof  IllegalArgumentException);
0726:                }
0727:            }
0728:
0729:            /**
0730:             * Tests instantiate(String className, ObjectName loader, Object[] args, String[] sign)
0731:             * with empty class name string. should throw ReflectionException wrapping CNFE.
0732:             */
0733:            public void testInstantiateWithEmptyClassName4() throws Exception {
0734:                try {
0735:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0736:                    Object o = server.instantiate("", null, null, null);
0737:
0738:                    // should not reach here
0739:                    fail("incorrect exception/classloading behavior");
0740:                } catch (ReflectionException e) {
0741:                    // expected
0742:
0743:                    // check that it wraps CNFE
0744:                    assertTrue(e.getTargetException() instanceof  ClassNotFoundException);
0745:                }
0746:            }
0747:
0748:            /**
0749:             * Tests instantiate(String className) classloading behaviour. According to
0750:             * javadoc, DLR should be used to instantiate the class
0751:             */
0752:            public void testInstantiateWithDefaultLoaderRepository()
0753:                    throws Exception {
0754:                // NOTE: 
0755:                // the urls used here are relative to the location of the build.xml
0756:
0757:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0758:                MLet mlet = new MLet();
0759:                ObjectName name = new ObjectName(":test=test");
0760:
0761:                // mlet cl to DLR
0762:                try {
0763:                    mlet
0764:                            .addURL("file:./output/etc/test/compliance/server/Test.jar");
0765:                    server.registerMBean(mlet, name);
0766:
0767:                    Object o = server
0768:                            .instantiate("test.compliance.server.support.AClass");
0769:                } finally {
0770:                    try {
0771:                        server.unregisterMBean(name);
0772:                    } catch (Exception ignored) {
0773:                    }
0774:                }
0775:
0776:                //assertTrue(o.getClass().getClassLoader().equals(mlet));
0777:            }
0778:
0779:            /**
0780:             * Tests instantiate(String className, ObjectName loader) classloading behaviour. According to
0781:             * javadoc, DLR should be used to instantiate the class. This should fail as
0782:             * the MLet MBean is never added to the agent and therefore not in the DLR.
0783:             */
0784:            public void testInstantiateWithDefaultLoaderRepository2()
0785:                    throws Exception {
0786:                try {
0787:                    // NOTE: 
0788:                    // the urls used here are relative to the location of the build.xml
0789:
0790:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0791:                    MLet mlet = new MLet();
0792:
0793:                    // mlet cl to DLR
0794:                    mlet
0795:                            .addURL("file:./output/etc/test/compliance/server/Test.jar");
0796:                    //server.registerMBean(mlet, new ObjectName(":test=test"));
0797:
0798:                    Object o = server
0799:                            .instantiate("test.compliance.server.support.AClass");
0800:
0801:                    //
0802:                    // FIXME: this test won't work until we have means to reset the JVM wide
0803:                    //        loader repository
0804:                    //
0805:
0806:                    // should not reach here
0807:                    //fail("incorrect classloading behavior");
0808:                    //assertTrue(o.getClass().getClassLoader().equals(mlet));
0809:                } catch (ReflectionException e) {
0810:                    // expected
0811:
0812:                    // check that it wraps CNFE
0813:                    assertTrue(e.getTargetException() instanceof  ClassNotFoundException);
0814:                }
0815:            }
0816:
0817:            // MBeanServer registerMBean ------------------------------------
0818:
0819:            /**
0820:             * Tests registering with null object name.
0821:             */
0822:            public void testRegisterNullObjectName() throws Exception {
0823:                boolean caught = false;
0824:                try {
0825:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0826:                    server.registerMBean(new Test(), null);
0827:                } catch (RuntimeOperationsException e) {
0828:                    if (e.getTargetException() instanceof  IllegalArgumentException)
0829:                        caught = true;
0830:                    else
0831:                        fail("Wrong wrapped exception "
0832:                                + e.getTargetException());
0833:                }
0834:                if (caught == false)
0835:                    fail("Allowed to register with a null object name");
0836:            }
0837:
0838:            /**
0839:             * Tests registering with a pattern object name.
0840:             */
0841:            public void testRegisterPatternObjectName() throws Exception {
0842:                boolean caught = false;
0843:                try {
0844:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0845:                    server.registerMBean(new Test(), new ObjectName(
0846:                            "Domai?:type=test"));
0847:                } catch (RuntimeOperationsException e) {
0848:                    if (e.getTargetException() instanceof  IllegalArgumentException)
0849:                        caught = true;
0850:                    else
0851:                        fail("Wrong wrapped exception "
0852:                                + e.getTargetException());
0853:                }
0854:                if (caught == false)
0855:                    fail("Allowed to register with a pattern object name");
0856:            }
0857:
0858:            /**
0859:             * Tests registering into JMImplementation
0860:             */
0861:            public void testRegisterJMImplementationObjectName()
0862:                    throws Exception {
0863:                boolean caught = false;
0864:                try {
0865:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0866:                    server.registerMBean(new Test(), new ObjectName(
0867:                            "JMImplementation:type=test"));
0868:                } catch (RuntimeOperationsException e) {
0869:                    if (e.getTargetException() instanceof  IllegalArgumentException)
0870:                        caught = true;
0871:                    else
0872:                        fail("Wrong wrapped exception "
0873:                                + e.getTargetException());
0874:                }
0875:                if (caught == false)
0876:                    fail("Allowed to register into JMImplementation");
0877:            }
0878:
0879:            /**
0880:             * Tests registering into JMImplementation using default domain
0881:             */
0882:            public void testRegisterJMImplementationDefaultDomainObjectName()
0883:                    throws Exception {
0884:                boolean caught = false;
0885:                try {
0886:                    MBeanServer server = MBeanServerFactory
0887:                            .newMBeanServer("JMImplementation");
0888:                    server.registerMBean(new Test(), new ObjectName(
0889:                            ":type=test"));
0890:                } catch (RuntimeOperationsException e) {
0891:                    if (e.getTargetException() instanceof  IllegalArgumentException)
0892:                        caught = true;
0893:                    else
0894:                        fail("Wrong wrapped exception "
0895:                                + e.getTargetException());
0896:                }
0897:                if (caught == false)
0898:                    fail("Allowed to register into JMImplementation");
0899:            }
0900:
0901:            /**
0902:             * Tests register for an MBean that throws unchecked exception from preRegister()
0903:             */
0904:            public void testRegisterMBeanOnExceptionFromPreRegister()
0905:                    throws Exception {
0906:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0907:                ObjectName name = new ObjectName("test:foo=bar");
0908:
0909:                try {
0910:                    server.registerMBean(new Test2(), name);
0911:
0912:                    // should not reach here
0913:                    fail("Test2 registered despite of throwing an exception from the preRegister() method.");
0914:                } catch (MBeanRegistrationException e) {
0915:                    // expected
0916:                    assertTrue(!server.isRegistered(name));
0917:                    assertTrue(e.getTargetException() instanceof  java.lang.RuntimeException);
0918:                } catch (RuntimeMBeanException e) {
0919:                    fail("FAILS IN RI: RuntimeMBeanException instead of MBeanRegistrationException?");
0920:                }
0921:            }
0922:
0923:            /**
0924:             * Tests register for an MBean that throws checked exception from preRegister()
0925:             */
0926:            public void testRegisterMBeanOnExceptionFromPreRegister2()
0927:                    throws Exception {
0928:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0929:                ObjectName name = new ObjectName("test:foo=bar");
0930:
0931:                try {
0932:                    server.registerMBean(new Test3(), name);
0933:
0934:                    // should not reach here
0935:                    fail("Test3 registered despite of throwin an exception from the preRegister() method");
0936:                } catch (MBeanRegistrationException e) {
0937:                    // expected
0938:                    assertTrue(!server.isRegistered(name));
0939:                    assertTrue(e.getTargetException() instanceof  MyScreamingException);
0940:                }
0941:            }
0942:
0943:            /**
0944:             * Tests register for an MBean that throws an MBeanRegistrationException from
0945:             * preRegister() method.
0946:             */
0947:            public void testRegisterMBeanOnExceptionFromPreRegister3()
0948:                    throws Exception {
0949:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0950:                ObjectName name = new ObjectName("test:foo=bar");
0951:
0952:                try {
0953:                    server.registerMBean(new Test4(), name);
0954:
0955:                    // should not reach here
0956:                    fail("Test4 registered despite of throwing an exception from the preRegister() method.");
0957:                } catch (MBeanRegistrationException e) {
0958:                    // expected
0959:                    assertTrue(!server.isRegistered(name));
0960:                    assertTrue(e.getTargetException() instanceof  MyScreamingException);
0961:                }
0962:            }
0963:
0964:            // MBeanServer unregisterMBean ----------------------------------
0965:
0966:            /**
0967:             * Tests unregister the delegate.
0968:             */
0969:            public void testUnregisterDelegate() throws Exception {
0970:                boolean caught = false;
0971:                try {
0972:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
0973:                    server.unregisterMBean(new ObjectName(
0974:                            "JMImplementation:type=MBeanServerDelegate"));
0975:                }
0976:                // REVIEW: This exception type isn't specified, but it is logical
0977:                //         and agrees with the RI.
0978:                // JPL: agreed
0979:                catch (RuntimeOperationsException e) {
0980:                    caught = true;
0981:                }
0982:                if (caught == false)
0983:                    fail("Allowed to unregister the delegate");
0984:            }
0985:
0986:            /**
0987:             * Tests basic register/unregister
0988:             */
0989:            public void testBasicUnregister() throws Exception {
0990:                MBeanServer server = MBeanServerFactory.newMBeanServer();
0991:                ObjectName name = new ObjectName("test:foo=bar");
0992:
0993:                server.registerMBean(new Test(), name);
0994:                server.unregisterMBean(name);
0995:            }
0996:
0997:            /**
0998:             * Tests unregister with default domain name
0999:             */
1000:            public void testUnregisterWithDefaultDomainName() throws Exception {
1001:                try {
1002:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
1003:                    ObjectName name = new ObjectName(":foo=bar");
1004:
1005:                    server.registerMBean(new Test(), name);
1006:                    server.unregisterMBean(name);
1007:
1008:                } catch (InstanceNotFoundException e) {
1009:                    // FAILS IN RI: RI throws InstanceNotFoundException if you try to
1010:                    // unregister with implicit default domain name
1011:                    fail("FAILS IN RI: RI throws InstanceNotFoundException when an existing MBean is unregistered with an implicit default domain name.");
1012:                }
1013:            }
1014:
1015:            /**
1016:             * Tests unregister with default domain name gotten from ObjectInstance at registration time.
1017:             */
1018:            public void testUnregisterWithObjectNameFromRegistration()
1019:                    throws Exception {
1020:                try {
1021:                    MBeanServer server = MBeanServerFactory.newMBeanServer();
1022:                    ObjectName name = new ObjectName(":foo=bar");
1023:
1024:                    ObjectInstance oi = server.registerMBean(new Test(), name);
1025:                    name = oi.getObjectName();
1026:
1027:                    server.unregisterMBean(name);
1028:
1029:                } catch (InstanceNotFoundException e) {
1030:                    // FAILS IN RI: RI throws InstanceNotFoundExceptin if you try yo
1031:                    // unregister with implicit default domain name
1032:                    fail("FAILS IN RI: RI throws InstanceNotFoundException when an existing MBean is unregistered with an implicit default domain name retrieved from the ObjectInstance returned at registration time.");
1033:                }
1034:            }
1035:
1036:            /**
1037:             * Tests unregister for an MBean that prevents unregistration by throwing an
1038:             * unchecked exception from its preDeregister() method.
1039:             */
1040:            public void testUnregisterMBeanOnExceptionFromPreDeregister()
1041:                    throws Exception {
1042:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1043:                ObjectName name = new ObjectName("test:foo=bar");
1044:
1045:                server.registerMBean(new LockedTest(), name);
1046:
1047:                try {
1048:                    server.unregisterMBean(name);
1049:
1050:                    // should not reach here
1051:                    fail("LockedTest unregistered despite of throwing an exception from the preDeregister() method.");
1052:                } catch (MBeanRegistrationException e) {
1053:                    // expected, LockedTest should prevent unregistration
1054:                    assertTrue(server.isRegistered(name));
1055:                    assertTrue(e.getTargetException() instanceof  java.lang.RuntimeException);
1056:                } catch (RuntimeMBeanException e) {
1057:                    // FAILS IN RI: according to spec (v1.0, p. 117) any exception thrown from the
1058:                    // preDeregister() method is wrapped in MBeanRegistrationException by the agent.
1059:                    fail("FAILS IN RI: spec v1.0: any exception thrown from MBean's preDeregister() method should be wrapped in an MBeanRegistrationException by the agent.");
1060:                }
1061:            }
1062:
1063:            /**
1064:             * Tests unregister for an MBean that prevents unregistration by throwing a
1065:             * checked exception from its preDeregister() method.
1066:             */
1067:            public void testUnregisterMBeanOnExceptionFromPreDeregister2()
1068:                    throws Exception {
1069:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1070:                ObjectName name = new ObjectName("test:foo=bar");
1071:
1072:                server.registerMBean(new LockedTest2(), name);
1073:
1074:                try {
1075:
1076:                    server.unregisterMBean(name);
1077:
1078:                    // should not reach here
1079:                    fail("LockedTest2 unregistered despite of throwin an exception from the preDeregister() method");
1080:                } catch (MBeanRegistrationException e) {
1081:                    // expected
1082:                    assertTrue(server.isRegistered(name));
1083:                    assertTrue(e.getTargetException() instanceof  MyScreamingException);
1084:                }
1085:            }
1086:
1087:            /**
1088:             * Tests unregister for an MBean that prevents unregistration by throwing a
1089:             * MBeanRegistrationException from its preDeregister() method. This should
1090:             * be rethrown by the agent as-is, and not wrapped into another MBeanRegistrationException.
1091:             */
1092:            public void testUnregisterMBeanOnExceptionFromPreDeregister3()
1093:                    throws Exception {
1094:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1095:                ObjectName name = new ObjectName("test:foo=bar");
1096:
1097:                server.registerMBean(new LockedTest3(), name);
1098:
1099:                try {
1100:                    server.unregisterMBean(name);
1101:
1102:                    // should not reach here
1103:                    fail("LockedTest3 unregistered despite of throwing an exception from the preDeregister() method.");
1104:                } catch (MBeanRegistrationException e) {
1105:                    // expected
1106:                    assertTrue(server.isRegistered(name));
1107:                    assertTrue(e.getTargetException() instanceof  MyScreamingException);
1108:                }
1109:            }
1110:
1111:            // MBeanServer NotificationListener Plain -----------------------
1112:
1113:            /**
1114:             * Tests basic listener registration to server delegate
1115:             */
1116:            public synchronized void testAddNotificationListenerToDelegate()
1117:                    throws Exception {
1118:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1119:
1120:                class MyNotificationListener implements  NotificationListener {
1121:
1122:                    int notificationCount = 0;
1123:
1124:                    public void handleNotification(Notification notification,
1125:                            Object handback) {
1126:                        try {
1127:                            notificationCount++;
1128:
1129:                            assertTrue(handback instanceof  String);
1130:                            assertTrue(handback.equals("MyHandback"));
1131:                            assertTrue(notification
1132:                                    .getSource()
1133:                                    .equals(
1134:                                            new ObjectName(
1135:                                                    "JMImplementation:type=MBeanServerDelegate")));
1136:                        } catch (Exception e) {
1137:                            fail("Unexpected error: " + e.toString());
1138:                        }
1139:                    }
1140:                }
1141:
1142:                MyNotificationListener listener = new MyNotificationListener();
1143:
1144:                NotificationFilterSupport filter = new NotificationFilterSupport();
1145:                filter
1146:                        .enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
1147:
1148:                server.addNotificationListener(new ObjectName(
1149:                        "JMImplementation:type=MBeanServerDelegate"), listener,
1150:                        filter, "MyHandback");
1151:
1152:                // force notification
1153:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1154:
1155:                assertTrue(listener.notificationCount == 1);
1156:            }
1157:
1158:            /**
1159:             * Tests multiple listeners with different handbacks
1160:             */
1161:            public synchronized void testAddMultipleListeners()
1162:                    throws Exception {
1163:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1164:
1165:                class MyNotificationListener implements  NotificationListener {
1166:                    Object handback;
1167:                    int result = 0;
1168:
1169:                    public MyNotificationListener(Object handback) {
1170:                        this .handback = handback;
1171:                    }
1172:
1173:                    public void handleNotification(Notification notification,
1174:                            Object handback) {
1175:                        result++;
1176:                        assertEquals(this .handback, handback);
1177:                        result++;
1178:                    }
1179:                }
1180:
1181:                MyNotificationListener listener1 = new MyNotificationListener(
1182:                        "handback1");
1183:                MyNotificationListener listener2 = new MyNotificationListener(
1184:                        "handback2");
1185:
1186:                server.addNotificationListener(new ObjectName(
1187:                        "JMImplementation:type=MBeanServerDelegate"),
1188:                        listener1, null, "handback1");
1189:                server.addNotificationListener(new ObjectName(
1190:                        "JMImplementation:type=MBeanServerDelegate"),
1191:                        listener2, null, "handback2");
1192:
1193:                // force notification
1194:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1195:
1196:                assertTrue(listener1.result == 2);
1197:                assertTrue(listener2.result == 2);
1198:            }
1199:
1200:            /**
1201:             * Tests one listener multiple handbacks
1202:             */
1203:            public synchronized void testAddListenerMultipleHandbacks()
1204:                    throws Exception {
1205:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1206:
1207:                class MyNotificationListener implements  NotificationListener {
1208:                    boolean result1 = false;
1209:                    boolean result2 = false;
1210:
1211:                    public void handleNotification(Notification notification,
1212:                            Object handback) {
1213:                        if (handback.equals("handback1"))
1214:                            result1 = true;
1215:                        else if (handback.equals("handback2"))
1216:                            result2 = true;
1217:                        else
1218:                            fail("Unexpected handback: " + handback);
1219:                    }
1220:                }
1221:
1222:                MyNotificationListener listener = new MyNotificationListener();
1223:
1224:                server.addNotificationListener(new ObjectName(
1225:                        "JMImplementation:type=MBeanServerDelegate"), listener,
1226:                        null, "handback1");
1227:                server.addNotificationListener(new ObjectName(
1228:                        "JMImplementation:type=MBeanServerDelegate"), listener,
1229:                        null, "handback2");
1230:
1231:                // force notification
1232:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1233:
1234:                assertTrue(listener.result1);
1235:                assertTrue(listener.result2);
1236:            }
1237:
1238:            /**
1239:             * Tests removing a notification listener including multiple handbacks
1240:             */
1241:            public synchronized void testRemoveListener() throws Exception {
1242:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1243:
1244:                class MyNotificationListener implements  NotificationListener {
1245:                    Object handback;
1246:                    int result = 0;
1247:
1248:                    public MyNotificationListener(Object handback) {
1249:                        this .handback = handback;
1250:                    }
1251:
1252:                    public void handleNotification(Notification notification,
1253:                            Object handback) {
1254:                        result++;
1255:                        assertEquals(this .handback, handback);
1256:                        result++;
1257:                    }
1258:                }
1259:
1260:                class MyOtherNotificationListener implements 
1261:                        NotificationListener {
1262:                    boolean result1 = false;
1263:                    boolean result2 = false;
1264:
1265:                    public void handleNotification(Notification notification,
1266:                            Object handback) {
1267:                        if (handback.equals("handback1"))
1268:                            result1 = true;
1269:                        else if (handback.equals("handback2"))
1270:                            result2 = true;
1271:                        else
1272:                            fail("Unexpected handback: " + handback);
1273:                    }
1274:                }
1275:
1276:                MyNotificationListener listener1 = new MyNotificationListener(
1277:                        "handback1");
1278:                MyOtherNotificationListener listener2 = new MyOtherNotificationListener();
1279:
1280:                server.addNotificationListener(new ObjectName(
1281:                        "JMImplementation:type=MBeanServerDelegate"),
1282:                        listener1, null, "handback1");
1283:                server.addNotificationListener(new ObjectName(
1284:                        "JMImplementation:type=MBeanServerDelegate"),
1285:                        listener2, null, "handback2");
1286:                server.addNotificationListener(new ObjectName(
1287:                        "JMImplementation:type=MBeanServerDelegate"),
1288:                        listener2, null, "handback3");
1289:                server
1290:                        .removeNotificationListener(new ObjectName(
1291:                                "JMImplementation:type=MBeanServerDelegate"),
1292:                                listener2);
1293:
1294:                // force notification
1295:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1296:
1297:                assertTrue(listener1.result == 2);
1298:                assertTrue(listener2.result1 == false);
1299:                assertTrue(listener2.result2 == false);
1300:            }
1301:
1302:            /**
1303:             * Tests removing a notification listener triplet
1304:             */
1305:            public synchronized void testRemoveTriplet() throws Exception {
1306:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1307:
1308:                class MyNotificationListener implements  NotificationListener {
1309:                    Object handback;
1310:                    int result = 0;
1311:
1312:                    public MyNotificationListener(Object handback) {
1313:                        this .handback = handback;
1314:                    }
1315:
1316:                    public void handleNotification(Notification notification,
1317:                            Object handback) {
1318:                        result++;
1319:                        assertEquals(this .handback, handback);
1320:                        result++;
1321:                    }
1322:                }
1323:
1324:                MyNotificationListener listener1 = new MyNotificationListener(
1325:                        "handback1");
1326:                MyNotificationListener listener2 = new MyNotificationListener(
1327:                        "handback2");
1328:
1329:                server.addNotificationListener(new ObjectName(
1330:                        "JMImplementation:type=MBeanServerDelegate"),
1331:                        listener1, null, "handback1");
1332:                server.addNotificationListener(new ObjectName(
1333:                        "JMImplementation:type=MBeanServerDelegate"),
1334:                        listener2, null, "handback2");
1335:                server.addNotificationListener(new ObjectName(
1336:                        "JMImplementation:type=MBeanServerDelegate"),
1337:                        listener2, null, "handback3");
1338:                server.removeNotificationListener(new ObjectName(
1339:                        "JMImplementation:type=MBeanServerDelegate"),
1340:                        listener2, null, "handback3");
1341:
1342:                // force notification
1343:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1344:
1345:                assertTrue(listener1.result == 2);
1346:                assertTrue(listener1.result == 2);
1347:            }
1348:
1349:            /**
1350:             * Tests removing a broadcaster
1351:             */
1352:            public synchronized void testRemoveBroadcaster() throws Exception {
1353:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1354:
1355:                class MyNotificationListener implements  NotificationListener {
1356:                    long result = 0;
1357:
1358:                    public MyNotificationListener() {
1359:                    }
1360:
1361:                    public void handleNotification(Notification notification,
1362:                            Object handback) {
1363:                        result = notification.getSequenceNumber();
1364:                    }
1365:                }
1366:
1367:                // Register the broadcaster
1368:                ObjectName broadcasterName = new ObjectName(
1369:                        "test:type=broadcaster");
1370:                Broadcaster broadcaster = new Broadcaster();
1371:                server.registerMBean(broadcaster, broadcasterName);
1372:
1373:                // Add the listener
1374:                MyNotificationListener listener = new MyNotificationListener();
1375:                server.addNotificationListener(broadcasterName, listener, null,
1376:                        null);
1377:
1378:                // Test we get a notification
1379:                broadcaster.doSomething();
1380:                assertEquals(1, listener.result);
1381:
1382:                // Remove the broadcaster
1383:                server.unregisterMBean(broadcasterName);
1384:
1385:                // This notification shouldn't work
1386:                broadcaster.doSomething();
1387:                try {
1388:                    assertEquals(1, listener.result);
1389:                } catch (AssertionFailedError e) {
1390:                    fail("FAILS IN RI: Removing a notification broadcaster does not "
1391:                            + "remove the listeners registered against the object name.");
1392:                }
1393:            }
1394:
1395:            /**
1396:             * Tests adding the listener to different broadcasters
1397:             */
1398:            public synchronized void testAddListenerToTwoBroadcasters()
1399:                    throws Exception {
1400:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1401:
1402:                class MyNotificationListener implements  NotificationListener {
1403:                    long result = 0;
1404:
1405:                    public MyNotificationListener() {
1406:                    }
1407:
1408:                    public void handleNotification(Notification notification,
1409:                            Object handback) {
1410:                        result++;
1411:                    }
1412:                }
1413:
1414:                // Register the broadcaster
1415:                ObjectName broadcasterName = new ObjectName(
1416:                        "test:type=broadcaster");
1417:                Broadcaster broadcaster = new Broadcaster();
1418:                server.registerMBean(broadcaster, broadcasterName);
1419:
1420:                // Add the listener to the broadcaster
1421:                MyNotificationListener listener = new MyNotificationListener();
1422:                server.addNotificationListener(broadcasterName, listener, null,
1423:                        null);
1424:
1425:                // Add the listener to the delegate
1426:                server.addNotificationListener(new ObjectName(
1427:                        "JMImplementation:type=MBeanServerDelegate"), listener,
1428:                        null, null);
1429:
1430:                // Test we get a notification from the broadcaster
1431:                broadcaster.doSomething();
1432:                assertEquals(1, listener.result);
1433:
1434:                // Test we get a notification from the delegate
1435:                server
1436:                        .registerMBean(new Test(), new ObjectName(
1437:                                "Test:foo=bar"));
1438:                assertEquals(2, listener.result);
1439:
1440:                // Remove the broadcaster
1441:                server.unregisterMBean(broadcasterName);
1442:                assertEquals(3, listener.result);
1443:
1444:                // Make sure we are still listening to the delegate
1445:                server.unregisterMBean(new ObjectName("Test:foo=bar"));
1446:                assertEquals(4, listener.result);
1447:            }
1448:
1449:            /**
1450:             * Tests adding the listener to different broadcasters but remove one
1451:             */
1452:            public synchronized void testAddListenerToTwoBroadcastersRemoveOne()
1453:                    throws Exception {
1454:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1455:
1456:                class MyNotificationListener implements  NotificationListener {
1457:                    long result = 0;
1458:
1459:                    public MyNotificationListener() {
1460:                    }
1461:
1462:                    public void handleNotification(Notification notification,
1463:                            Object handback) {
1464:                        result++;
1465:                    }
1466:                }
1467:
1468:                // Register the broadcaster
1469:                ObjectName broadcasterName = new ObjectName(
1470:                        "test:type=broadcaster");
1471:                Broadcaster broadcaster = new Broadcaster();
1472:                server.registerMBean(broadcaster, broadcasterName);
1473:
1474:                // Add the listener to the broadcaster
1475:                MyNotificationListener listener = new MyNotificationListener();
1476:                server.addNotificationListener(broadcasterName, listener, null,
1477:                        null);
1478:
1479:                // Add the listener to the delegate
1480:                server.addNotificationListener(new ObjectName(
1481:                        "JMImplementation:type=MBeanServerDelegate"), listener,
1482:                        null, null);
1483:
1484:                // Remove ourselves from the broadcaster
1485:                server.removeNotificationListener(broadcasterName, listener);
1486:
1487:                // Test we get a notification from the broadcaster
1488:                broadcaster.doSomething();
1489:                assertEquals(0, listener.result);
1490:
1491:                // Test we get a notification from the delegate
1492:                server
1493:                        .registerMBean(new Test(), new ObjectName(
1494:                                "Test:foo=bar"));
1495:                assertEquals(1, listener.result);
1496:            }
1497:
1498:            // MBeanServer NotificationListener Object Name -----------------
1499:
1500:            /**
1501:             * Tests basic listener registration to server delegate
1502:             */
1503:            public synchronized void testaddMBeanToDelegate() throws Exception {
1504:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1505:
1506:                MBeanListener listener = new MBeanListener();
1507:                ObjectName listenerName = new ObjectName("test:type=listener");
1508:                server.registerMBean(listener, listenerName);
1509:
1510:                NotificationFilterSupport filter = new NotificationFilterSupport();
1511:                filter
1512:                        .enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
1513:
1514:                ObjectName delegateName = new ObjectName(
1515:                        "JMImplementation:type=MBeanServerDelegate");
1516:                server.addNotificationListener(delegateName, listenerName,
1517:                        filter, "MyHandback");
1518:
1519:                // force notification
1520:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1521:
1522:                assertTrue(listener.count == 1);
1523:                assertTrue(listener.source.equals(delegateName));
1524:                assertTrue(listener.handback.equals("MyHandback"));
1525:            }
1526:
1527:            /**
1528:             * Tests multiple listeners with different handbacks
1529:             */
1530:            public synchronized void testAddMBeanMultipleListeners()
1531:                    throws Exception {
1532:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1533:
1534:                MBeanListener listener1 = new MBeanListener();
1535:                ObjectName listenerName1 = new ObjectName("test:type=listener1");
1536:                server.registerMBean(listener1, listenerName1);
1537:                MBeanListener listener2 = new MBeanListener();
1538:                ObjectName listenerName2 = new ObjectName("test:type=listener2");
1539:                server.registerMBean(listener2, listenerName2);
1540:
1541:                ObjectName delegateName = new ObjectName(
1542:                        "JMImplementation:type=MBeanServerDelegate");
1543:                server.addNotificationListener(delegateName, listenerName1,
1544:                        null, "handback1");
1545:                server.addNotificationListener(delegateName, listenerName2,
1546:                        null, "handback2");
1547:
1548:                // force notification
1549:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1550:
1551:                assertEquals(1, listener1.count);
1552:                assertEquals(listener1.source, delegateName);
1553:                assertEquals(listener1.handback, "handback1");
1554:                assertEquals(1, listener2.count);
1555:                assertEquals(listener2.source, delegateName);
1556:                assertEquals(listener2.handback, "handback2");
1557:            }
1558:
1559:            /**
1560:             * Tests one listener multiple handbacks
1561:             */
1562:            public synchronized void testAddMBeanListenerMultipleHandbacks()
1563:                    throws Exception {
1564:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1565:
1566:                MBeanListener listener = new MBeanListener("handback1",
1567:                        "handback2");
1568:                ObjectName listenerName = new ObjectName("test:type=listener");
1569:                server.registerMBean(listener, listenerName);
1570:
1571:                ObjectName delegateName = new ObjectName(
1572:                        "JMImplementation:type=MBeanServerDelegate");
1573:                server.addNotificationListener(delegateName, listenerName,
1574:                        null, "handback1");
1575:                server.addNotificationListener(delegateName, listenerName,
1576:                        null, "handback2");
1577:
1578:                // force notification
1579:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1580:
1581:                assertTrue(listener.count1 == 1);
1582:                assertEquals(listener.source1, delegateName);
1583:                assertEquals(listener.handback1, "handback1");
1584:                assertTrue(listener.count2 == 1);
1585:                assertEquals(listener.source2, delegateName);
1586:                assertEquals(listener.handback2, "handback2");
1587:            }
1588:
1589:            /**
1590:             * Tests removing a notification listener including multiple handbacks
1591:             */
1592:            public synchronized void testMBeanRemoveListener() throws Exception {
1593:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1594:
1595:                MBeanListener listener1 = new MBeanListener();
1596:                ObjectName listenerName1 = new ObjectName("test:type=listener1");
1597:                server.registerMBean(listener1, listenerName1);
1598:                MBeanListener listener2 = new MBeanListener("handback2",
1599:                        "handback3");
1600:                ObjectName listenerName2 = new ObjectName("test:type=listener2");
1601:                server.registerMBean(listener2, listenerName2);
1602:
1603:                ObjectName delegateName = new ObjectName(
1604:                        "JMImplementation:type=MBeanServerDelegate");
1605:                server.addNotificationListener(delegateName, listenerName1,
1606:                        null, "handback1");
1607:                server.addNotificationListener(delegateName, listenerName2,
1608:                        null, "handback2");
1609:                server.addNotificationListener(delegateName, listenerName2,
1610:                        null, "handback3");
1611:                server.removeNotificationListener(delegateName, listenerName2);
1612:
1613:                // force notification
1614:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1615:                assertTrue("Listener1 should get a notification",
1616:                        listener1.count == 1);
1617:                assertTrue("Source should be the delegate", listener1.source
1618:                        .equals(delegateName));
1619:                assertTrue("Listener1 should get handback1", listener1.handback
1620:                        .equals("handback1"));
1621:                assertTrue("Listener2 should have no notiifcation",
1622:                        listener2.count == 0);
1623:                assertTrue(
1624:                        "Listener2 should have no notiifcation for handback2",
1625:                        listener2.count1 == 0);
1626:                assertTrue(
1627:                        "Listener2 should have no notiifcation for handback3",
1628:                        listener2.count2 == 0);
1629:            }
1630:
1631:            /**
1632:             * Tests removing a notification listener triple
1633:             */
1634:            public synchronized void testMBeanRemoveTriplet() throws Exception {
1635:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1636:
1637:                MBeanListener listener1 = new MBeanListener();
1638:                ObjectName listenerName1 = new ObjectName("test:type=listener1");
1639:                server.registerMBean(listener1, listenerName1);
1640:                MBeanListener listener2 = new MBeanListener("handback2",
1641:                        "handback3");
1642:                ObjectName listenerName2 = new ObjectName("test:type=listener2");
1643:                server.registerMBean(listener2, listenerName2);
1644:
1645:                ObjectName delegateName = new ObjectName(
1646:                        "JMImplementation:type=MBeanServerDelegate");
1647:                server.addNotificationListener(delegateName, listenerName1,
1648:                        null, "handback1");
1649:                server.addNotificationListener(delegateName, listenerName2,
1650:                        null, "handback2");
1651:                server.addNotificationListener(delegateName, listenerName2,
1652:                        null, "handback3");
1653:                server.removeNotificationListener(delegateName, listenerName2,
1654:                        null, "handback3");
1655:
1656:                // force notification
1657:                server.registerMBean(new Test(), new ObjectName(":foo=bar"));
1658:                assertTrue("Listener1 should get a notification",
1659:                        listener1.count == 1);
1660:                assertTrue("Source should be the delegate", listener1.source
1661:                        .equals(delegateName));
1662:                assertTrue("Listener1 should get handback1", listener1.handback
1663:                        .equals("handback1"));
1664:                assertTrue("Listener2 should get a notification",
1665:                        listener2.count1 == 1);
1666:                assertTrue("Source should be the delegate", listener2.source1
1667:                        .equals(delegateName));
1668:                assertTrue("Listener2 should get handback2",
1669:                        listener2.handback1.equals("handback2"));
1670:                assertTrue(
1671:                        "Listener2 should have no notiifcation for handback3",
1672:                        listener2.count2 == 0);
1673:            }
1674:
1675:            /**
1676:             * Tests MBeanRedeploy notification
1677:             */
1678:            public synchronized void testMBeanRedeployNotification()
1679:                    throws Exception {
1680:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1681:
1682:                MBeanListener listener = new MBeanListener();
1683:                ObjectName listenerName = new ObjectName("test:type=listener");
1684:                server.registerMBean(listener, listenerName);
1685:
1686:                ObjectName broadcasterName = new ObjectName(
1687:                        "test:type=Broadcaster");
1688:                server.registerMBean(new Broadcaster(), broadcasterName);
1689:
1690:                server.addNotificationListener(broadcasterName, listenerName,
1691:                        null, "handback1");
1692:                server.removeNotificationListener(broadcasterName,
1693:                        listenerName, null, "handback1");
1694:                server.unregisterMBean(broadcasterName);
1695:
1696:                Broadcaster broadcaster = new Broadcaster();
1697:                server.registerMBean(broadcaster, broadcasterName);
1698:                server.addNotificationListener(broadcasterName, listenerName,
1699:                        null, "handback2");
1700:
1701:                // force notification
1702:                broadcaster.doSomething();
1703:                assertTrue("Listener should get a notification",
1704:                        listener.count == 1);
1705:                assertTrue("Source should be the broadcaster", listener.source
1706:                        .equals(broadcasterName));
1707:                assertTrue("Listener should get handback2", listener.handback
1708:                        .equals("handback2"));
1709:            }
1710:
1711:            /**
1712:             * Tests removing a broadcaster
1713:             */
1714:            public synchronized void testMBeanRemoveBroadcaster()
1715:                    throws Exception {
1716:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1717:
1718:                MBeanListener listener1 = new MBeanListener();
1719:                ObjectName listenerName1 = new ObjectName("test:type=listener1");
1720:                server.registerMBean(listener1, listenerName1);
1721:
1722:                // Register the broadcaster
1723:                ObjectName broadcasterName = new ObjectName(
1724:                        "test:type=broadcaster");
1725:                Broadcaster broadcaster = new Broadcaster();
1726:                server.registerMBean(broadcaster, broadcasterName);
1727:
1728:                // Add the listener
1729:                server.addNotificationListener(broadcasterName, listenerName1,
1730:                        null, null);
1731:
1732:                // Test we get a notification
1733:                broadcaster.doSomething();
1734:                assertEquals(1, listener1.count);
1735:                assertEquals(broadcasterName, listener1.source);
1736:
1737:                // Remove the broadcaster
1738:                server.unregisterMBean(broadcasterName);
1739:
1740:                // This notification shouldn't work
1741:                broadcaster.doSomething();
1742:                try {
1743:                    assertEquals(1, listener1.count);
1744:                } catch (AssertionFailedError e) {
1745:                    fail("FAILS IN RI: Removing a notification broadcaster does not "
1746:                            + "remove the listeners registered against the object name.");
1747:                }
1748:            }
1749:
1750:            /**
1751:             * Tests adding the listener to different broadcasters
1752:             */
1753:            public synchronized void testAddMBeanListenerToTwoBroadcasters()
1754:                    throws Exception {
1755:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1756:
1757:                MBeanListener listener1 = new MBeanListener();
1758:                ObjectName listenerName1 = new ObjectName("test:type=listener1");
1759:                server.registerMBean(listener1, listenerName1);
1760:
1761:                // Register the broadcaster
1762:                ObjectName broadcasterName = new ObjectName(
1763:                        "test:type=broadcaster");
1764:                Broadcaster broadcaster = new Broadcaster();
1765:                server.registerMBean(broadcaster, broadcasterName);
1766:
1767:                // Add the listener to the broadcaster
1768:                server.addNotificationListener(broadcasterName, listenerName1,
1769:                        null, null);
1770:
1771:                // Add the listener to the delegate
1772:                ObjectName delegateName = new ObjectName(
1773:                        "JMImplementation:type=MBeanServerDelegate");
1774:                server.addNotificationListener(delegateName, listenerName1,
1775:                        null, null);
1776:
1777:                // Test we get a notification from the broadcaster
1778:                broadcaster.doSomething();
1779:                assertEquals(1, listener1.count);
1780:                assertEquals(broadcasterName, listener1.source);
1781:
1782:                try {
1783:                    // Test we get a notification from the delegate
1784:                    server.registerMBean(new Test(), new ObjectName(
1785:                            "Test:foo=bar"));
1786:                    assertEquals(2, listener1.count);
1787:                    assertEquals(delegateName, listener1.source);
1788:
1789:                    // Remove the broadcaster
1790:                    server.unregisterMBean(broadcasterName);
1791:                    assertEquals(3, listener1.count);
1792:                    assertEquals(delegateName, listener1.source);
1793:
1794:                    // Make sure we are still listening to the delegate
1795:                    server.unregisterMBean(new ObjectName("Test:foo=bar"));
1796:                    assertEquals(4, listener1.count);
1797:                    assertEquals(delegateName, listener1.source);
1798:                } catch (AssertionFailedError e) {
1799:                    fail("FAILS IN RI: Listener registered with ObjectName in MBeanServer "
1800:                            + "reports the wrong source for multiple broadcaster.");
1801:                }
1802:            }
1803:
1804:            /**
1805:             * Tests adding the listener to different broadcasters but remove one
1806:             */
1807:            public synchronized void testAddMBeanListenerToTwoBroadcastersRemoveOne()
1808:                    throws Exception {
1809:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1810:
1811:                MBeanListener listener1 = new MBeanListener();
1812:                ObjectName listenerName1 = new ObjectName("test:type=listener1");
1813:                server.registerMBean(listener1, listenerName1);
1814:
1815:                // Register the broadcaster
1816:                ObjectName broadcasterName = new ObjectName(
1817:                        "test:type=broadcaster");
1818:                Broadcaster broadcaster = new Broadcaster();
1819:                server.registerMBean(broadcaster, broadcasterName);
1820:
1821:                // Add the listener to the broadcaster
1822:                server.addNotificationListener(broadcasterName, listenerName1,
1823:                        null, null);
1824:
1825:                // Add the listener to the delegate
1826:                ObjectName delegateName = new ObjectName(
1827:                        "JMImplementation:type=MBeanServerDelegate");
1828:                server.addNotificationListener(delegateName, listenerName1,
1829:                        null, null);
1830:
1831:                // Remove ourselves from the broadcaster
1832:                server.removeNotificationListener(broadcasterName, listener1);
1833:
1834:                // Test we get a notification from the broadcaster
1835:                broadcaster.doSomething();
1836:                assertEquals(0, listener1.count);
1837:
1838:                // Test we get a notification from the delegate
1839:                server
1840:                        .registerMBean(new Test(), new ObjectName(
1841:                                "Test:foo=bar"));
1842:                assertEquals(1, listener1.count);
1843:                try {
1844:                    assertEquals(delegateName, listener1.source);
1845:                } catch (AssertionFailedError e) {
1846:                    fail("FAILS IN RI: Listener registered with ObjectName in MBeanServer "
1847:                            + "reports the wrong source for multiple broadcaster, "
1848:                            + "even when the broadcaster it reports has been removed.");
1849:                }
1850:            }
1851:
1852:            public void testGetDomains() throws Exception {
1853:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1854:
1855:                assertTrue("Only one domain at the start",
1856:                        server.getDomains().length == 1);
1857:                assertEquals(server.getDomains()[0], "JMImplementation");
1858:
1859:                server.registerMBean(new Test(), new ObjectName(
1860:                        "Domain1:test=test1"));
1861:                server.registerMBean(new Test(), new ObjectName(
1862:                        "Domain1:test=test2"));
1863:                server.registerMBean(new Test(), new ObjectName(
1864:                        "Domain2:test=test1"));
1865:                server.registerMBean(new Test(), new ObjectName(
1866:                        "Domain3:test=test1"));
1867:
1868:                assertTrue("Now four domains", server.getDomains().length == 4);
1869:                List domains = Arrays.asList(server.getDomains());
1870:                assertTrue("server has JMImplementation", domains
1871:                        .contains("JMImplementation"));
1872:                assertTrue("server has Domain1", domains.contains("Domain1"));
1873:                assertTrue("server has Domain2", domains.contains("Domain2"));
1874:                assertTrue("server has Domain3", domains.contains("Domain3"));
1875:
1876:                server.unregisterMBean(new ObjectName("Domain3:test=test1"));
1877:
1878:                assertTrue("Now three domains", server.getDomains().length == 3);
1879:                domains = Arrays.asList(server.getDomains());
1880:                assertTrue("server has JMImplementation", domains
1881:                        .contains("JMImplementation"));
1882:                assertTrue("server has Domain1", domains.contains("Domain1"));
1883:                assertTrue("server has Domain2", domains.contains("Domain2"));
1884:                assertTrue("server no longer has Domain3", domains
1885:                        .contains("Domain3") == false);
1886:
1887:                server.unregisterMBean(new ObjectName("Domain1:test=test1"));
1888:
1889:                assertTrue("Still three domains",
1890:                        server.getDomains().length == 3);
1891:                domains = Arrays.asList(server.getDomains());
1892:                assertTrue("server has JMImplementation", domains
1893:                        .contains("JMImplementation"));
1894:                assertTrue("server still has Domain1", domains
1895:                        .contains("Domain1"));
1896:                assertTrue("server has Domain2", domains.contains("Domain2"));
1897:                assertTrue("server no longer has Domain3", domains
1898:                        .contains("Domain3") == false);
1899:            }
1900:
1901:            public void testIsInstanceOf() throws Exception {
1902:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1903:                ObjectName baseName = new ObjectName(
1904:                        "MBeanServerTEST:type=testIsInstanceOf,name=Base");
1905:                ObjectName derivedName = new ObjectName(
1906:                        "MBeanServerTEST:type=testIsInstanceOf,name=Derived");
1907:                ObjectName unrelatedName = new ObjectName(
1908:                        "MBeanServerTEST:type=testIsInstanceOf,name=Unrelated");
1909:
1910:                server.registerMBean(new Base(), baseName);
1911:                server.registerMBean(new Derived(), derivedName);
1912:                server.registerMBean(new Unrelated(), unrelatedName);
1913:
1914:                assertTrue("Base is an instance Object", server.isInstanceOf(
1915:                        baseName, Object.class.getName()));
1916:                assertTrue("Base is an instance BaseMBean", server
1917:                        .isInstanceOf(baseName, BaseMBean.class.getName()));
1918:                assertTrue("Base is an instance Base", server.isInstanceOf(
1919:                        baseName, Base.class.getName()));
1920:                assertTrue("Derived is an instance Object", server
1921:                        .isInstanceOf(derivedName, Object.class.getName()));
1922:                assertTrue("Derived is an instance BaseMBean", server
1923:                        .isInstanceOf(derivedName, BaseMBean.class.getName()));
1924:                assertTrue("Derived is an instance Base", server.isInstanceOf(
1925:                        derivedName, Base.class.getName()));
1926:                assertTrue("Derived is an instance Derived", server
1927:                        .isInstanceOf(derivedName, Derived.class.getName()));
1928:                assertTrue("Unrelated is an instance Object", server
1929:                        .isInstanceOf(unrelatedName, Object.class.getName()));
1930:                assertTrue("Unrelated is an instance UnrelatedMBean", server
1931:                        .isInstanceOf(unrelatedName, UnrelatedMBean.class
1932:                                .getName()));
1933:                assertTrue("Unrelated is an instance Unrelated", server
1934:                        .isInstanceOf(unrelatedName, Unrelated.class.getName()));
1935:
1936:                assertTrue(
1937:                        "Base is an not instance Derived",
1938:                        server.isInstanceOf(baseName, Derived.class.getName()) == false);
1939:                assertTrue("Base is an not instance UnrelatedMBean",
1940:                        server.isInstanceOf(baseName, UnrelatedMBean.class
1941:                                .getName()) == false);
1942:                assertTrue("Base is an not instance Unrelated",
1943:                        server
1944:                                .isInstanceOf(baseName, Unrelated.class
1945:                                        .getName()) == false);
1946:                assertTrue("Derived is an not instance UnrelatedMBean", server
1947:                        .isInstanceOf(derivedName, UnrelatedMBean.class
1948:                                .getName()) == false);
1949:                assertTrue("Dervied is an not instance Unrelated",
1950:                        server.isInstanceOf(derivedName, Unrelated.class
1951:                                .getName()) == false);
1952:                assertTrue("Unrelated is an not instance BaseMBean",
1953:                        server.isInstanceOf(unrelatedName, BaseMBean.class
1954:                                .getName()) == false);
1955:                assertTrue("Unrelated is an not instance Base",
1956:                        server
1957:                                .isInstanceOf(unrelatedName, Base.class
1958:                                        .getName()) == false);
1959:                assertTrue("Unrelated is an not instance Derived",
1960:                        server.isInstanceOf(unrelatedName, Derived.class
1961:                                .getName()) == false);
1962:            }
1963:
1964:            public void testIsInstanceOfErrors() throws Exception {
1965:                MBeanServer server = MBeanServerFactory.newMBeanServer();
1966:                ObjectName baseName = new ObjectName(
1967:                        "MBeanServerTEST:type=testIsInstanceOf,name=Base");
1968:                ObjectName dynamicName = new ObjectName(
1969:                        "MBeanServerTEST:type=testIsInstanceOf,name=Dynamic");
1970:                ObjectName doesNotExistName = new ObjectName(
1971:                        "MBeanServerTEST:type=testIsInstanceOf,name=DoesNotExist");
1972:
1973:                server.registerMBean(new Base(), baseName);
1974:                server.registerMBean(new Dynamic(), dynamicName);
1975:
1976:                assertTrue(
1977:                        "Base is not an instance of a class that does not exist",
1978:                        server.isInstanceOf(baseName, "does.not.exist") == false);
1979:
1980:                assertTrue(
1981:                        "FAILS IN RI: Not an instance if the getMBeanInfo reports a class name that does not exist",
1982:                        server
1983:                                .isInstanceOf(dynamicName, Object.class
1984:                                        .getName()) == false);
1985:
1986:                boolean caught = false;
1987:                try {
1988:                    server.isInstanceOf(doesNotExistName, Object.class
1989:                            .getName());
1990:                } catch (InstanceNotFoundException e) {
1991:                    caught = true;
1992:                }
1993:                assertTrue(
1994:                        "Should get an instance not found for a non-existent mbean",
1995:                        caught);
1996:            }
1997:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.