Source Code Cross Referenced for RegistryTestCase.java in  » Library » Apache-commons-modeler-2.0.1-src » org » apache » commons » modeler » 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 » Library » Apache commons modeler 2.0.1 src » org.apache.commons.modeler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.modeler;
019:
020:        import java.io.FileInputStream;
021:        import java.util.List;
022:
023:        import javax.management.Descriptor;
024:        import javax.management.MBeanConstructorInfo;
025:        import javax.management.modelmbean.ModelMBeanAttributeInfo;
026:        import javax.management.modelmbean.ModelMBeanConstructorInfo;
027:        import javax.management.modelmbean.ModelMBeanInfo;
028:        import javax.management.modelmbean.ModelMBeanNotificationInfo;
029:        import javax.management.modelmbean.ModelMBeanOperationInfo;
030:
031:        import junit.framework.Test;
032:        import junit.framework.TestCase;
033:        import junit.framework.TestSuite;
034:
035:        /**
036:         * <p>Test Case for the Registry class.</p>
037:         *
038:         * @author Craig R. McClanahan
039:         * @version $Revision: 480402 $ $Date: 2006-11-29 04:43:23 +0000 (Wed, 29 Nov 2006) $
040:         */
041:
042:        public class RegistryTestCase extends TestCase {
043:
044:            // ----------------------------------------------------- Instance Variables
045:
046:            /**
047:             * The Registry we will be testing.
048:             */
049:            protected Registry registry = null;
050:
051:            // ----------------------------------------------------------- Constructors
052:
053:            /**
054:             * Construct a new instance of this test case.
055:             *
056:             * @param name Name of the test case
057:             */
058:            public RegistryTestCase(String name) {
059:
060:                super (name);
061:
062:            }
063:
064:            // --------------------------------------------------- Overall Test Methods
065:
066:            /**
067:             * Set up instance variables required by this test case.
068:             */
069:            public void setUp() throws Exception {
070:
071:                registry = Registry.getRegistry();
072:                String names[] = registry.findManagedBeans();
073:                if (names.length == 0) {
074:                    FileInputStream stream = new FileInputStream(
075:                            "src/test/org/apache/commons/modeler/mbeans-descriptors.xml");
076:                    Registry.loadRegistry(stream);
077:                    stream.close();
078:                }
079:
080:            }
081:
082:            /**
083:             * Return the tests included in this test suite.
084:             */
085:            public static Test suite() {
086:
087:                return (new TestSuite(RegistryTestCase.class));
088:
089:            }
090:
091:            /**
092:             * Tear down instance variables required by this test case.
093:             */
094:            public void tearDown() {
095:
096:                registry = null;
097:
098:            }
099:
100:            // ------------------------------------------------ Individual Test Methods
101:
102:            /**
103:             * Test ModelMBeanAttributeInfo information.
104:             */
105:            public void testModelMBeanAttributeInfo() throws Exception {
106:
107:                // Retrieve a ManagedBean
108:                ManagedBean http = registry.findManagedBean("HttpConnector");
109:                assertNotNull("Found HttpConnector managed bean");
110:
111:                // Create the associated ModelMBeanInfo
112:                ModelMBeanInfo info = http.createMBeanInfo();
113:                assertNotNull("Found HttpConnector ModelMBeanInfo", info);
114:
115:                // Retrieve the specified ModelMBeanAttributeInfo
116:                ModelMBeanAttributeInfo mmainfo = info
117:                        .getAttribute("acceptCount");
118:                assertNotNull("Found HttpConnector acceptCount info", mmainfo);
119:
120:                // Get the Descriptor
121:                Descriptor desc = mmainfo.getDescriptor();
122:                assertNotNull("Found HttpConnector acceptCount descriptor",
123:                        desc);
124:
125:                // Check the configured fields
126:                checkDescriptor(desc, "field1",
127:                        "HttpConnector.acceptCount/field1");
128:                checkDescriptor(desc, "field2",
129:                        "HttpConnector.acceptCount/field2");
130:
131:            }
132:
133:            /**
134:             * Test ModelMBeanConstructorInfo information.
135:             */
136:            public void testModelMBeanConstructorInfo() throws Exception {
137:
138:                // Retrieve a ManagedBean
139:                ManagedBean http = registry.findManagedBean("HttpConnector");
140:                assertNotNull("Found HttpConnector managed bean");
141:
142:                // Create the associated ModelMBeanInfo
143:                ModelMBeanInfo info = http.createMBeanInfo();
144:                assertNotNull("Found HttpConnector ModelMBeanInfo", info);
145:
146:                // Retrieve the relevant MBeanConstructorInfo array
147:                MBeanConstructorInfo mcinfo[] = info.getConstructors();
148:                assertNotNull("Found HttpConnector MBeanConstructorInfo array",
149:                        mcinfo);
150:                assertEquals("Found HttpConnector MBeanConstructorInfo entry",
151:                        1, mcinfo.length);
152:
153:                // Cast first entry to ModelMBeanConstructorInfo
154:                ModelMBeanConstructorInfo mmcinfo = (ModelMBeanConstructorInfo) mcinfo[0];
155:
156:                // Get the Descriptor
157:                Descriptor desc = mmcinfo.getDescriptor();
158:                assertNotNull("Found HttpConnector constructor descriptor",
159:                        desc);
160:
161:                // Check the configured fields
162:                checkDescriptor(desc, "role", "constructor");
163:                checkDescriptor(desc, "field1",
164:                        "HttpConnector.constructor/field1");
165:                checkDescriptor(desc, "field2",
166:                        "HttpConnector.constructor/field2");
167:
168:            }
169:
170:            /**
171:             * Test descriptor entries.
172:             */
173:            public void testDescriptorEntries() {
174:
175:                // Retrive the ManageBean that has descriptor info
176:                ManagedBean http = registry.findManagedBean("HttpConnector");
177:                assertNotNull("Found HttpConnector managed bean");
178:
179:                // Check descriptor fields on the ManagedBean itself
180:                List beanFields = http.getFields();
181:                assertNotNull("Found HttpConnector fields");
182:                checkField(beanFields, "field1", "HttpConnector/field1");
183:                checkField(beanFields, "field2", "HttpConnector/field2");
184:
185:                // Retrieve the AttributeInfo that has descriptors set
186:                AttributeInfo attrs[] = http.getAttributes();
187:                AttributeInfo attr = null;
188:                for (int i = 0; i < attrs.length; i++) {
189:                    if ("acceptCount".equals(attrs[i].getName())) {
190:                        attr = attrs[i];
191:                        break;
192:                    }
193:                }
194:                assertNotNull("Found attribute");
195:
196:                // Check descriptor fields on the AttributeInfo
197:                List attrFields = attr.getFields();
198:                assertNotNull("Found attribute fields");
199:                checkField(attrFields, "field1",
200:                        "HttpConnector.acceptCount/field1");
201:                checkField(attrFields, "field2",
202:                        "HttpConnector.acceptCount/field2");
203:
204:                // Retrieve the ConstructorInfo that has descriptors set
205:                ConstructorInfo constrs[] = http.getConstructors();
206:                ConstructorInfo constr = null;
207:                for (int i = 0; i < constrs.length; i++) {
208:                    if ("HttpConnector".equals(constrs[i].getName())) {
209:                        constr = constrs[i];
210:                        break;
211:                    }
212:                }
213:                assertNotNull("Found constructor");
214:
215:                // Check descriptor fields on the ConstructorInfo
216:                List constrFields = constr.getFields();
217:                assertNotNull("Found constructor fields");
218:                checkField(constrFields, "field1",
219:                        "HttpConnector.constructor/field1");
220:                checkField(constrFields, "field2",
221:                        "HttpConnector.constructor/field2");
222:
223:                // Retrieve the NotificationInfo that has descriptors set
224:                NotificationInfo notifs[] = http.getNotifications();
225:                NotificationInfo notif = null;
226:                for (int i = 0; i < notifs.length; i++) {
227:                    if ("Problem".equals(notifs[i].getName())) {
228:                        notif = notifs[i];
229:                        break;
230:                    }
231:                }
232:                assertNotNull("Found notification");
233:
234:                // Check descriptor fields on the NotificationInfo
235:                List notifFields = notif.getFields();
236:                assertNotNull("Found notification fields");
237:                checkField(notifFields, "field1",
238:                        "HttpConnector.problem/field1");
239:                checkField(notifFields, "field2",
240:                        "HttpConnector.problem/field2");
241:
242:                // Retrieve the OperationInfo that has descriptors set
243:                OperationInfo opers[] = http.getOperations();
244:                OperationInfo oper = null;
245:                for (int i = 0; i < opers.length; i++) {
246:                    if ("initialize".equals(opers[i].getName())) {
247:                        oper = opers[i];
248:                        break;
249:                    }
250:                }
251:                assertNotNull("Found operation");
252:
253:                // Check descriptor fields on the OperationInfo
254:                List operFields = oper.getFields();
255:                assertNotNull("Found operation fields");
256:                checkField(operFields, "field1",
257:                        "HttpConnector.initialize/field1");
258:                checkField(operFields, "field2",
259:                        "HttpConnector.initialize/field2");
260:
261:            }
262:
263:            /**
264:             * Test ModelMBeanInfo information.
265:             */
266:            public void testModelMBeanInfo() throws Exception {
267:
268:                // Retrive a ManagedBean
269:                ManagedBean http = registry.findManagedBean("HttpConnector");
270:                assertNotNull("Found HttpConnector managed bean");
271:
272:                // Create the associated ModelMBeanInfo
273:                ModelMBeanInfo info = http.createMBeanInfo();
274:                assertNotNull("Found HttpConnector ModelMBeanInfo", info);
275:
276:                // Check the basic properties
277:                assertEquals("Correct className",
278:                        "org.apache.catalina.mbeans.HttpConnectorModelMBean",
279:                        info.getClassName());
280:                assertEquals("Correct description",
281:                        "HTTP/1.1 Connector for Tomcat Standalone", info
282:                                .getDescription());
283:
284:                // Get the Descriptor
285:                Descriptor desc = info.getMBeanDescriptor();
286:                assertNotNull("Found HttpConnector MBeanDescriptor", desc);
287:
288:                // Check the configured fields
289:                checkDescriptor(desc, "field1", "HttpConnector/field1");
290:                checkDescriptor(desc, "field2", "HttpConnector/field2");
291:
292:            }
293:
294:            /**
295:             * Test ModelMBeanNotificationInfo information.
296:             */
297:            public void testModelMBeanNotificationInfo() throws Exception {
298:
299:                // Retrieve a ManagedBean
300:                ManagedBean http = registry.findManagedBean("HttpConnector");
301:                assertNotNull("Found HttpConnector managed bean");
302:
303:                // Create the associated ModelMBeanInfo
304:                ModelMBeanInfo info = http.createMBeanInfo();
305:                assertNotNull("Found HttpConnector ModelMBeanInfo", info);
306:
307:                // Retrieve the specified ModelMBeanNotificationInfo
308:                ModelMBeanNotificationInfo mmninfo = info
309:                        .getNotification("Problem");
310:                assertNotNull("Found HttpConnector problem info", mmninfo);
311:
312:                // Get the Descriptor
313:                Descriptor desc = mmninfo.getDescriptor();
314:                assertNotNull("Found HttpConnector problem descriptor", desc);
315:
316:                // Check the configured fields
317:                checkDescriptor(desc, "field1", "HttpConnector.problem/field1");
318:                checkDescriptor(desc, "field2", "HttpConnector.problem/field2");
319:
320:            }
321:
322:            /**
323:             * Test ModelMBeanOperationInfo information.
324:             */
325:            public void testModelMBeanOperationInfo() throws Exception {
326:
327:                // Retrieve a ManagedBean
328:                ManagedBean http = registry.findManagedBean("HttpConnector");
329:                assertNotNull("Found HttpConnector managed bean");
330:
331:                // Create the associated ModelMBeanInfo
332:                ModelMBeanInfo info = http.createMBeanInfo();
333:                assertNotNull("Found HttpConnector ModelMBeanInfo", info);
334:
335:                // Retrieve the specified ModelMBeanOperationInfo
336:                ModelMBeanOperationInfo mmoinfo = info
337:                        .getOperation("initialize");
338:                assertNotNull("Found HttpConnector initialize info", mmoinfo);
339:
340:                // Get the Descriptor
341:                Descriptor desc = mmoinfo.getDescriptor();
342:                assertNotNull("Found HttpConnector initialize descriptor", desc);
343:
344:                // Check the configured fields
345:                checkDescriptor(desc, "field1",
346:                        "HttpConnector.initialize/field1");
347:                checkDescriptor(desc, "field2",
348:                        "HttpConnector.initialize/field2");
349:
350:            }
351:
352:            /**
353:             * Test registry creation.
354:             */
355:            public void testRegistryCreation() {
356:
357:                String names[] = null;
358:
359:                System.out.println("Registered managed beans:");
360:                names = registry.findManagedBeans();
361:                for (int i = 0; i < names.length; i++)
362:                    System.out.println("  " + names[i]);
363:                System.out.println("-------------------------");
364:
365:                System.out.println("Registered managed beans for Containers:");
366:                names = registry
367:                        .findManagedBeans("org.apache.catalina.Container");
368:                for (int i = 0; i < names.length; i++)
369:                    System.out.println("  " + names[i]);
370:                System.out.println("-------------------------");
371:
372:            }
373:
374:            // ------------------------------------------------------ Protected Methods
375:
376:            // Check presence of an appropriate name/value pair in the descriptor
377:            protected void checkDescriptor(Descriptor desc, String name,
378:                    Object value) {
379:
380:                String names[] = desc.getFieldNames();
381:                boolean found = false;
382:                for (int i = 0; i < names.length; i++) {
383:                    if (name.equals(names[i])) {
384:                        found = true;
385:                        break;
386:                    }
387:                }
388:                assertTrue("Found name " + name, found);
389:                assertEquals("Correct name " + name + " value", value, desc
390:                        .getFieldValue(name));
391:
392:            }
393:
394:            // Check presence of an appropriate FieldInfo
395:            protected void checkField(List fields, String name, Object value) {
396:
397:                int n = fields.size();
398:                for (int i = 0; i < n; i++) {
399:                    FieldInfo field = (FieldInfo) fields.get(i);
400:                    if (name.equals(field.getName())) {
401:                        assertEquals("name=" + name + " value", value, field
402:                                .getValue());
403:                        return;
404:                    }
405:                }
406:                fail("Cannot find field name=" + name + " and value=" + value);
407:
408:            }
409:
410:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.