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: }
|