1   /*
2    * Copyright 1999-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.commons.modeler;
19  
20  
21  import java.io.FileInputStream;
22  import java.util.List;
23  
24  import javax.management.Descriptor;
25  import javax.management.MBeanConstructorInfo;
26  import javax.management.modelmbean.ModelMBeanAttributeInfo;
27  import javax.management.modelmbean.ModelMBeanConstructorInfo;
28  import javax.management.modelmbean.ModelMBeanInfo;
29  import javax.management.modelmbean.ModelMBeanNotificationInfo;
30  import javax.management.modelmbean.ModelMBeanOperationInfo;
31  
32  import junit.framework.Test;
33  import junit.framework.TestCase;
34  import junit.framework.TestSuite;
35  
36  
37  /***
38   * <p>Test Case for the Registry class.</p>
39   *
40   * @author Craig R. McClanahan
41   * @version $Revision: 155428 $ $Date: 2005-02-26 08:12:25 -0500 (Sat, 26 Feb 2005) $
42   */
43  
44  public class RegistryTestCase extends TestCase {
45  
46  
47      // ----------------------------------------------------- Instance Variables
48  
49  
50      /***
51       * The Registry we will be testing.
52       */
53      protected Registry registry = null;
54  
55  
56      // ----------------------------------------------------------- Constructors
57  
58  
59      /***
60       * Construct a new instance of this test case.
61       *
62       * @param name Name of the test case
63       */
64      public RegistryTestCase(String name) {
65  
66          super(name);
67  
68      }
69  
70  
71      // --------------------------------------------------- Overall Test Methods
72  
73  
74      /***
75       * Set up instance variables required by this test case.
76       */
77      public void setUp() throws Exception {
78  
79          registry = Registry.getRegistry();
80          String names[] = registry.findManagedBeans();
81          if (names.length == 0) {
82              FileInputStream stream = new FileInputStream
83                  ("src/test/org/apache/commons/modeler/mbeans-descriptors.xml");
84              Registry.loadRegistry(stream);
85              stream.close();
86          }
87  
88      }
89  
90  
91      /***
92       * Return the tests included in this test suite.
93       */
94      public static Test suite() {
95  
96          return (new TestSuite(RegistryTestCase.class));
97  
98      }
99  
100 
101     /***
102      * Tear down instance variables required by this test case.
103      */
104     public void tearDown() {
105 
106         registry = null;
107 
108     }
109 
110 
111     // ------------------------------------------------ Individual Test Methods
112 
113 
114     /***
115      * Test ModelMBeanAttributeInfo information.
116      */
117     public void testModelMBeanAttributeInfo() throws Exception {
118 
119         // Retrieve a ManagedBean
120         ManagedBean http = registry.findManagedBean("HttpConnector");
121         assertNotNull("Found HttpConnector managed bean");
122 
123         // Create the associated ModelMBeanInfo
124         ModelMBeanInfo info = http.createMBeanInfo();
125         assertNotNull("Found HttpConnector ModelMBeanInfo", info);
126 
127         // Retrieve the specified ModelMBeanAttributeInfo
128         ModelMBeanAttributeInfo mmainfo = info.getAttribute("acceptCount");
129         assertNotNull("Found HttpConnector acceptCount info", mmainfo);
130 
131         // Get the Descriptor
132         Descriptor desc = mmainfo.getDescriptor();
133         assertNotNull("Found HttpConnector acceptCount descriptor", desc);
134 
135         // Check the configured fields
136         checkDescriptor(desc, "field1", "HttpConnector.acceptCount/field1");
137         checkDescriptor(desc, "field2", "HttpConnector.acceptCount/field2");
138 
139     }
140 
141 
142     /***
143      * Test ModelMBeanConstructorInfo information.
144      */
145     public void testModelMBeanConstructorInfo() throws Exception {
146 
147         // Retrieve a ManagedBean
148         ManagedBean http = registry.findManagedBean("HttpConnector");
149         assertNotNull("Found HttpConnector managed bean");
150 
151         // Create the associated ModelMBeanInfo
152         ModelMBeanInfo info = http.createMBeanInfo();
153         assertNotNull("Found HttpConnector ModelMBeanInfo", info);
154 
155         // Retrieve the relevant MBeanConstructorInfo array
156         MBeanConstructorInfo mcinfo[] = info.getConstructors();
157         assertNotNull("Found HttpConnector MBeanConstructorInfo array", mcinfo);
158         assertEquals("Found HttpConnector MBeanConstructorInfo entry",
159                      1, mcinfo.length);
160 
161         // Cast first entry to ModelMBeanConstructorInfo
162         ModelMBeanConstructorInfo mmcinfo =
163             (ModelMBeanConstructorInfo) mcinfo[0];
164 
165         // Get the Descriptor
166         Descriptor desc = mmcinfo.getDescriptor();
167         assertNotNull("Found HttpConnector constructor descriptor", desc);
168 
169         // Check the configured fields
170         checkDescriptor(desc, "role", "constructor");
171         checkDescriptor(desc, "field1", "HttpConnector.constructor/field1");
172         checkDescriptor(desc, "field2", "HttpConnector.constructor/field2");
173 
174     }
175 
176 
177     /***
178      * Test descriptor entries.
179      */
180     public void testDescriptorEntries() {
181 
182         // Retrive the ManageBean that has descriptor info
183         ManagedBean http = registry.findManagedBean("HttpConnector");
184         assertNotNull("Found HttpConnector managed bean");
185 
186         // Check descriptor fields on the ManagedBean itself
187         List beanFields = http.getFields();
188         assertNotNull("Found HttpConnector fields");
189         checkField(beanFields, "field1", "HttpConnector/field1");
190         checkField(beanFields, "field2", "HttpConnector/field2");
191 
192         // Retrieve the AttributeInfo that has descriptors set
193         AttributeInfo attrs[] = http.getAttributes();
194         AttributeInfo attr = null;
195         for (int i = 0; i < attrs.length; i++) {
196             if ("acceptCount".equals(attrs[i].getName())) {
197                 attr = attrs[i];
198                 break;
199             }
200         }
201         assertNotNull("Found attribute");
202 
203         // Check descriptor fields on the AttributeInfo
204         List attrFields = attr.getFields();
205         assertNotNull("Found attribute fields");
206         checkField(attrFields, "field1", "HttpConnector.acceptCount/field1");
207         checkField(attrFields, "field2", "HttpConnector.acceptCount/field2");
208 
209         // Retrieve the ConstructorInfo that has descriptors set
210         ConstructorInfo constrs[] = http.getConstructors();
211         ConstructorInfo constr = null;
212         for (int i = 0; i < constrs.length; i++) {
213             if ("HttpConnector".equals(constrs[i].getName())) {
214                 constr = constrs[i];
215                 break;
216             }
217         }
218         assertNotNull("Found constructor");
219 
220         // Check descriptor fields on the ConstructorInfo
221         List constrFields = constr.getFields();
222         assertNotNull("Found constructor fields");
223         checkField(constrFields, "field1", "HttpConnector.constructor/field1");
224         checkField(constrFields, "field2", "HttpConnector.constructor/field2");
225 
226         // Retrieve the NotificationInfo that has descriptors set
227         NotificationInfo notifs[] = http.getNotifications();
228         NotificationInfo notif = null;
229         for (int i = 0; i < notifs.length; i++) {
230             if ("Problem".equals(notifs[i].getName())) {
231                 notif = notifs[i];
232                 break;
233             }
234         }
235         assertNotNull("Found notification");
236 
237         // Check descriptor fields on the NotificationInfo
238         List notifFields = notif.getFields();
239         assertNotNull("Found notification fields");
240         checkField(notifFields, "field1", "HttpConnector.problem/field1");
241         checkField(notifFields, "field2", "HttpConnector.problem/field2");
242 
243         // Retrieve the OperationInfo that has descriptors set
244         OperationInfo opers[] = http.getOperations();
245         OperationInfo oper = null;
246         for (int i = 0; i < opers.length; i++) {
247             if ("initialize".equals(opers[i].getName())) {
248                 oper = opers[i];
249                 break;
250             }
251         }
252         assertNotNull("Found operation");
253 
254         // Check descriptor fields on the OperationInfo
255         List operFields = oper.getFields();
256         assertNotNull("Found operation fields");
257         checkField(operFields, "field1", "HttpConnector.initialize/field1");
258         checkField(operFields, "field2", "HttpConnector.initialize/field2");
259 
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",
282                      info.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     /***
296      * Test ModelMBeanNotificationInfo information.
297      */
298     public void testModelMBeanNotificationInfo() throws Exception {
299 
300         // Retrieve a ManagedBean
301         ManagedBean http = registry.findManagedBean("HttpConnector");
302         assertNotNull("Found HttpConnector managed bean");
303 
304         // Create the associated ModelMBeanInfo
305         ModelMBeanInfo info = http.createMBeanInfo();
306         assertNotNull("Found HttpConnector ModelMBeanInfo", info);
307 
308         // Retrieve the specified ModelMBeanNotificationInfo
309         ModelMBeanNotificationInfo mmninfo = info.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     /***
324      * Test ModelMBeanOperationInfo information.
325      */
326     public void testModelMBeanOperationInfo() throws Exception {
327 
328         // Retrieve a ManagedBean
329         ManagedBean http = registry.findManagedBean("HttpConnector");
330         assertNotNull("Found HttpConnector managed bean");
331 
332         // Create the associated ModelMBeanInfo
333         ModelMBeanInfo info = http.createMBeanInfo();
334         assertNotNull("Found HttpConnector ModelMBeanInfo", info);
335 
336         // Retrieve the specified ModelMBeanOperationInfo
337         ModelMBeanOperationInfo mmoinfo = info.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", "HttpConnector.initialize/field1");
346         checkDescriptor(desc, "field2", "HttpConnector.initialize/field2");
347 
348     }
349 
350 
351     /***
352      * Test registry creation.
353      */
354     public void testRegistryCreation() {
355 
356         String names[] = null;
357 
358         System.out.println("Registered managed beans:");
359         names = registry.findManagedBeans();
360         for (int i = 0; i < names.length; i++)
361             System.out.println("  " + names[i]);
362         System.out.println("-------------------------");
363 
364         System.out.println("Registered managed beans for Containers:");
365         names = registry.findManagedBeans("org.apache.catalina.Container");
366         for (int i = 0; i < names.length; i++)
367             System.out.println("  " + names[i]);
368         System.out.println("-------------------------");
369 
370     }
371 
372 
373     // ------------------------------------------------------ Protected Methods
374 
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",
390                      value,
391                      desc.getFieldValue(name));
392 
393     }
394 
395 
396     // Check presence of an appropriate FieldInfo
397     protected void checkField(List fields, String name, Object value) {
398 
399         int n = fields.size();
400         for (int i = 0; i < n; i++) {
401             FieldInfo field = (FieldInfo) fields.get(i);
402             if (name.equals(field.getName())) {
403                 assertEquals("name=" + name + " value",
404                              value,
405                              field.getValue());
406                 return;
407             }
408         }
409         fail("Cannot find field name=" + name + " and value=" + value);
410 
411     }
412 
413 
414 }