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