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

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


001:        /*
002:         * Copyright (C) The MX4J Contributors.
003:         * All rights reserved.
004:         *
005:         * This software is distributed under the terms of the MX4J License version 1.0.
006:         * See the terms of the MX4J License in the documentation provided with this software.
007:         */
008:
009:        package test.javax.management;
010:
011:        import javax.management.*;
012:        import javax.management.loading.MLet;
013:
014:        import test.MX4JTestCase;
015:        import test.MutableBoolean;
016:        import test.MutableObject;
017:        import test.javax.management.support.RegistrationSupport;
018:
019:        /**
020:         * @version $Revision: 1.13 $
021:         */
022:        public class MBeanRegistrationTest extends MX4JTestCase {
023:            public static interface BarMBean {
024:                int getBeer();
025:
026:                void getBEER();
027:
028:                int getBeer(String name);
029:
030:                String[] get();
031:            }
032:
033:            public static class Bar implements  BarMBean {
034:                public Bar() {
035:                }
036:
037:                public String[] get() {
038:                    return new String[0];
039:                }
040:
041:                public int getBeer() {
042:                    return 0;
043:                }
044:
045:                public void getBEER() {
046:                    throw new java.lang.Error("No BEER here");
047:                }
048:
049:                public int getBeer(String name) {
050:                    return 0;
051:                }
052:
053:            }
054:
055:            public MBeanRegistrationTest(String s) {
056:                super (s);
057:            }
058:
059:            public void testNullObjectName() throws Exception {
060:                MBeanServer server = newMBeanServer();
061:                int count = server.getMBeanCount().intValue();
062:                Object nullObjectName = new RegistrationSupport.NullObjectName();
063:                try {
064:                    server.registerMBean(nullObjectName, null);
065:                    fail("MBean cannot be registered");
066:                } catch (RuntimeOperationsException ignored) {
067:                }
068:                // Check that was not registered
069:                if (server.getMBeanCount().intValue() != count) {
070:                    fail("MBean with null ObjectName was registered");
071:                }
072:            }
073:
074:            public void testPreRegisterException() throws Exception {
075:                MBeanServer server = newMBeanServer();
076:                int count = server.getMBeanCount().intValue();
077:                Object preRegisterException = new RegistrationSupport.PreRegisterException();
078:                try {
079:                    server.registerMBean(preRegisterException, null);
080:                    fail("MBean cannot be registered");
081:                } catch (MBeanRegistrationException ignored) {
082:                }
083:                // Check that was not registered
084:                if (server.getMBeanCount().intValue() != count) {
085:                    fail("MBean threw exception in preRegister, but was registered");
086:                }
087:            }
088:
089:            public void testPostRegisterException() throws Exception {
090:                MBeanServer server = newMBeanServer();
091:                int count = server.getMBeanCount().intValue();
092:                Object postRegisterException = new RegistrationSupport.PostRegisterException();
093:                ObjectName name = new ObjectName(":test=postRegister");
094:                try {
095:                    server.registerMBean(postRegisterException, name);
096:                    fail("MBean must throw an exception");
097:                } catch (RuntimeMBeanException ignored) {
098:                }
099:                // Check that was registered
100:                if (server.getMBeanCount().intValue() != count + 1) {
101:                    fail("MBean threw exception in postRegister, but was NOT registered");
102:                }
103:            }
104:
105:            public void testPreDeregisterException() throws Exception {
106:                MBeanServer server = newMBeanServer();
107:                int count = server.getMBeanCount().intValue();
108:                Object preDeregisterException = new RegistrationSupport.PreDeregisterException();
109:                ObjectName name = new ObjectName("simon:mbean=test");
110:                server.registerMBean(preDeregisterException, name);
111:                if (server.getMBeanCount().intValue() != count + 1) {
112:                    fail("MBean was not registered");
113:                }
114:                try {
115:                    server.unregisterMBean(name);
116:                    fail("MBean cannot be unregistered");
117:                } catch (MBeanRegistrationException ignored) {
118:                }
119:                if (server.getMBeanCount().intValue() != count + 1) {
120:                    fail("MBean was unregistered");
121:                }
122:            }
123:
124:            public void testPostDeregisterException() throws Exception {
125:                MBeanServer server = newMBeanServer();
126:                int count = server.getMBeanCount().intValue();
127:                Object postDeregisterException = new RegistrationSupport.PostDeregisterException();
128:                ObjectName name = new ObjectName("simon:mbean=test");
129:                server.registerMBean(postDeregisterException, name);
130:                if (server.getMBeanCount().intValue() != count + 1) {
131:                    fail("MBean was not registered");
132:                }
133:                try {
134:                    server.unregisterMBean(name);
135:                    fail("MBean must throw an exception");
136:                } catch (RuntimeMBeanException ignored) {
137:                }
138:                if (server.getMBeanCount().intValue() != count) {
139:                    fail("MBean was NOT unregistered");
140:                }
141:            }
142:
143:            public void testRegistration() throws Exception {
144:                MBeanServer server = newMBeanServer();
145:                int count = server.getMBeanCount().intValue();
146:                final MutableBoolean bool1 = new MutableBoolean(false);
147:                final MutableBoolean bool2 = new MutableBoolean(false);
148:                Object empty = new RegistrationSupport.Empty(bool1, bool2);
149:                final ObjectName name = new ObjectName("simon:mbean=empty");
150:                server.registerMBean(empty, name);
151:                // Check registration
152:                if (!bool1.get()) {
153:                    fail("postRegister called with wrong argument value for successful registration");
154:                }
155:                if (server.getMBeanCount().intValue() != count + 1) {
156:                    fail("MBean was not registered");
157:                }
158:            }
159:
160:            public void testDuplicateRegistration() throws Exception {
161:                MBeanServer server = newMBeanServer();
162:                int count = server.getMBeanCount().intValue();
163:                final MutableBoolean bool1 = new MutableBoolean(false);
164:                final MutableBoolean bool2 = new MutableBoolean(false);
165:                Object empty = new RegistrationSupport.Empty(bool1, bool2);
166:                final ObjectName name = new ObjectName("simon:mbean=empty");
167:                server.registerMBean(empty, name);
168:                if (server.getMBeanCount().intValue() != count + 1) {
169:                    fail("MBean was not registered");
170:                }
171:
172:                Object duplicate = new RegistrationSupport.EmptyDuplicate(name,
173:                        bool1);
174:                try {
175:                    server.registerMBean(duplicate, null);
176:                    fail("MBean with same name cannot be registered");
177:                } catch (InstanceAlreadyExistsException ignored) {
178:                }
179:                // Check that postRegister was called correctly
180:                if (bool1.get()) {
181:                    fail("postRegister called with wrong argument value for unsuccessful registration");
182:                }
183:                if (server.getMBeanCount().intValue() != count + 1) {
184:                    fail("MBean was registered, and it shouldn't");
185:                }
186:            }
187:
188:            public void testDeregistration() throws Exception {
189:                MBeanServer server = newMBeanServer();
190:                int count = server.getMBeanCount().intValue();
191:                final MutableBoolean bool1 = new MutableBoolean(false);
192:                final MutableBoolean bool2 = new MutableBoolean(false);
193:                Object empty = new RegistrationSupport.Empty(bool1, bool2);
194:                final ObjectName name = new ObjectName("simon:mbean=empty");
195:                server.registerMBean(empty, name);
196:                if (server.getMBeanCount().intValue() != count + 1) {
197:                    fail("MBean was not registered");
198:                }
199:
200:                bool1.set(true);
201:                bool2.set(true);
202:                server.unregisterMBean(name);
203:                if (server.getMBeanCount().intValue() != count) {
204:                    fail("MBean was not unregistered");
205:                }
206:                if (bool1.get() || bool2.get()) {
207:                    fail("preDeregister or postDeregister are not called");
208:                }
209:            }
210:
211:            public void testDuplicateDeregistration() throws Exception {
212:                MBeanServer server = newMBeanServer();
213:                int count = server.getMBeanCount().intValue();
214:                final MutableBoolean bool1 = new MutableBoolean(false);
215:                final MutableBoolean bool2 = new MutableBoolean(false);
216:                Object empty = new RegistrationSupport.Empty(bool1, bool2);
217:                final ObjectName name = new ObjectName("simon:mbean=empty");
218:                server.registerMBean(empty, name);
219:                if (server.getMBeanCount().intValue() != count + 1) {
220:                    fail("MBean was not registered");
221:                }
222:
223:                bool1.set(true);
224:                bool2.set(true);
225:                server.unregisterMBean(name);
226:                if (server.getMBeanCount().intValue() != count) {
227:                    fail("MBean was not unregistered");
228:                }
229:                if (bool1.get() || bool2.get()) {
230:                    fail("preDeregister or postDeregister are not called");
231:                }
232:
233:                // Try again
234:                try {
235:                    server.unregisterMBean(name);
236:                    fail("Already unregistered MBean can be unregistered");
237:                } catch (InstanceNotFoundException ignored) {
238:                }
239:            }
240:
241:            public void testNotificationDuringRegistrationForStdMBean()
242:                    throws Exception {
243:                final MBeanServer server = newMBeanServer();
244:                Object mbean = new RegistrationSupport.Std();
245:                final ObjectName name = new ObjectName(":mbean=std");
246:                server.addNotificationListener(new ObjectName(
247:                        "JMImplementation:type=MBeanServerDelegate"),
248:                        new NotificationListener() {
249:                            public void handleNotification(
250:                                    Notification notification, Object handback) {
251:                                invokeOperationsDuringRegistration(server,
252:                                        name, notification);
253:                            }
254:                        }, null, null);
255:
256:                server.registerMBean(mbean, name);
257:            }
258:
259:            public void testNotificationDuringRegistrationForDynMBean()
260:                    throws Exception {
261:                final MBeanServer server = newMBeanServer();
262:                Object mbean = new RegistrationSupport.Dyn();
263:                final ObjectName name = new ObjectName(":mbean=dyn");
264:                server.addNotificationListener(new ObjectName(
265:                        "JMImplementation:type=MBeanServerDelegate"),
266:                        new NotificationListener() {
267:                            public void handleNotification(
268:                                    Notification notification, Object handback) {
269:                                invokeOperationsDuringRegistration(server,
270:                                        name, notification);
271:                            }
272:                        }, null, null);
273:
274:                server.registerMBean(mbean, name);
275:            }
276:
277:            private void invokeOperationsDuringRegistration(MBeanServer server,
278:                    ObjectName name, Notification notification) {
279:                if (notification != null) {
280:                    MBeanServerNotification notif = (MBeanServerNotification) notification;
281:                    ObjectName registered = notif.getMBeanName();
282:                    if (!registered.equals(name))
283:                        fail("Notification for the wrong MBean: " + registered
284:                                + ", should be " + name);
285:                    if (!MBeanServerNotification.REGISTRATION_NOTIFICATION
286:                            .equals(notif.getType()))
287:                        fail("Expecting a registration notification");
288:                }
289:
290:                try {
291:                    MBeanInfo info = server.getMBeanInfo(name);
292:                    if (info.getClassName() == null)
293:                        fail("MBeanInfo not initialized correctly");
294:                    if (info.getOperations().length == 0)
295:                        fail("MBeanInfo not initialized correctly");
296:
297:                    ObjectInstance instance = server.getObjectInstance(name);
298:                    if (instance == null)
299:                        fail("ObjectInstance should be already initialized");
300:
301:                    boolean isRegistered = server.isRegistered(name);
302:                    if (!isRegistered)
303:                        fail("MBean is registered");
304:
305:                    // Must be able to invoke it with no exceptions
306:                    server.invoke(name, RegistrationSupport.StdMBean.class
307:                            .getMethods()[0].getName(), null, null);
308:                } catch (Exception x) {
309:                    fail("MBean metadata structures are not yet ready, but they should be: "
310:                            + x);
311:                }
312:            }
313:
314:            public void testInvokeMBeanServerOperationsInCallbacks()
315:                    throws Exception {
316:                MBeanServer server = newMBeanServer();
317:                Object mbean = new InvokeDuringCallbacks();
318:                ObjectName name = ObjectName.getInstance(":name=invoke");
319:                server.registerMBean(mbean, name);
320:                server.unregisterMBean(name);
321:            }
322:
323:            public interface InvokeDuringCallbacksMBean {
324:                public void method();
325:            }
326:
327:            public class InvokeDuringCallbacks implements 
328:                    InvokeDuringCallbacksMBean, MBeanRegistration {
329:                private MBeanServer server;
330:                private ObjectName name;
331:
332:                public ObjectName preRegister(MBeanServer server,
333:                        ObjectName name) throws Exception {
334:                    this .server = server;
335:                    this .name = name;
336:                    return name;
337:                }
338:
339:                public void postRegister(Boolean registrationDone) {
340:                    invokeOperationsDuringRegistration(server, name, null);
341:                }
342:
343:                public void preDeregister() throws Exception {
344:                    invokeOperationsDuringRegistration(server, name, null);
345:                }
346:
347:                public void postDeregister() {
348:                }
349:
350:                public void method() {
351:                }
352:            }
353:
354:            public void testDistinguishAttributesOperations() throws Exception {
355:                MBeanServer server = newMBeanServer();
356:                ObjectName objname = new ObjectName(
357:                        "tests:id=distinguishAttributesOperations");
358:                Bar b = new Bar();
359:                server.registerMBean(b, objname);
360:                MBeanInfo info = server.getMBeanInfo(objname);
361:                assertTrue("Expecting one attribute",
362:                        info.getAttributes().length == 1);
363:                try {
364:                    assertTrue("No 'Beer' attribute", ((Integer) server
365:                            .getAttribute(objname, "Beer")).intValue() == 0);
366:                    String[] getresult = (String[]) server.invoke(objname,
367:                            "get", new Object[0], new String[0]);
368:                    assertTrue("Expecting zero length result",
369:                            getresult.length == 0);
370:                    server.getAttribute(objname, "BEER");
371:                    fail("Expecting AttributeNotFoundException");
372:                } catch (AttributeNotFoundException x) {
373:                    assertTrue(true);
374:                }
375:                assertTrue("Expecting three operations",
376:                        info.getOperations().length == 3);
377:            }
378:
379:            public void testListenerRegistrationUnregistrationDuringCallbacks()
380:                    throws Exception {
381:                MBeanServer server = newMBeanServer();
382:                MutableObject holder = new MutableObject(null);
383:                Object mbean = new RegistrationSupport.ListenerRegistrar(holder);
384:                ObjectName name = ObjectName
385:                        .getInstance("test:type=notifications");
386:                server.registerMBean(mbean, name);
387:
388:                // Register a new MBean, the holder must be notified
389:                ObjectName mlet = ObjectName.getInstance("test:type=mlet");
390:                server.createMBean(MLet.class.getName(), mlet, null);
391:
392:                Notification notification = (Notification) holder.get();
393:                assertNotNull(notification);
394:                assertEquals(notification.getType(),
395:                        MBeanServerNotification.REGISTRATION_NOTIFICATION);
396:                holder.set(null);
397:
398:                server.unregisterMBean(mlet);
399:
400:                notification = (Notification) holder.get();
401:                assertNotNull(notification);
402:                assertEquals(notification.getType(),
403:                        MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
404:                holder.set(null);
405:
406:                // Unregisters also the listeners (in postDeregister)
407:                server.unregisterMBean(name);
408:                notification = (Notification) holder.get();
409:                assertNotNull(notification);
410:                assertEquals(notification.getType(),
411:                        MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
412:                holder.set(null);
413:
414:                server.createMBean(MLet.class.getName(), mlet, null);
415:                notification = (Notification) holder.get();
416:                assertNull(notification);
417:
418:                server.unregisterMBean(mlet);
419:                notification = (Notification) holder.get();
420:                assertNull(notification);
421:            }
422:
423:            public void testAbstractClass() throws Exception {
424:                MBeanServer server = newMBeanServer();
425:                try {
426:                    server.createMBean(Foo.class.getName(), null);
427:                    fail();
428:                } catch (NotCompliantMBeanException e) {
429:                    // ok
430:                } catch (Exception e) {
431:                    e.printStackTrace();
432:                    fail();
433:                }
434:            }
435:
436:            public static interface FooMBean {
437:                void something();
438:            }
439:
440:            public static abstract class Foo implements  FooMBean {
441:                public void something() {
442:                }
443:            }
444:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.