1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
40
41
42
43
44
45 public class RegistryTestCase extends TestCase {
46
47
48
49
50
51
52
53
54 protected Registry registry = null;
55
56
57
58
59
60
61
62
63
64
65 public RegistryTestCase(String name) {
66
67 super(name);
68
69 }
70
71
72
73
74
75
76
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
94
95 public static Test suite() {
96
97 return (new TestSuite(RegistryTestCase.class));
98
99 }
100
101
102
103
104
105 public void tearDown() {
106
107 registry = null;
108
109 }
110
111
112
113
114
115
116
117
118 public void testModelMBeanAttributeInfo() throws Exception {
119
120
121 ManagedBean http = registry.findManagedBean("HttpConnector");
122 assertNotNull("Found HttpConnector managed bean");
123
124
125 ModelMBeanInfo info = http.createMBeanInfo();
126 assertNotNull("Found HttpConnector ModelMBeanInfo", info);
127
128
129 ModelMBeanAttributeInfo mmainfo = info.getAttribute("acceptCount");
130 assertNotNull("Found HttpConnector acceptCount info", mmainfo);
131
132
133 Descriptor desc = mmainfo.getDescriptor();
134 assertNotNull("Found HttpConnector acceptCount descriptor", desc);
135
136
137 checkDescriptor(desc, "field1", "HttpConnector.acceptCount/field1");
138 checkDescriptor(desc, "field2", "HttpConnector.acceptCount/field2");
139
140 }
141
142
143
144
145
146 public void testModelMBeanConstructorInfo() throws Exception {
147
148
149 ManagedBean http = registry.findManagedBean("HttpConnector");
150 assertNotNull("Found HttpConnector managed bean");
151
152
153 ModelMBeanInfo info = http.createMBeanInfo();
154 assertNotNull("Found HttpConnector ModelMBeanInfo", info);
155
156
157 MBeanConstructorInfo mcinfo[] = info.getConstructors();
158 assertNotNull("Found HttpConnector MBeanConstructorInfo array", mcinfo);
159 assertEquals("Found HttpConnector MBeanConstructorInfo entry",
160 1, mcinfo.length);
161
162
163 ModelMBeanConstructorInfo mmcinfo =
164 (ModelMBeanConstructorInfo) mcinfo[0];
165
166
167 Descriptor desc = mmcinfo.getDescriptor();
168 assertNotNull("Found HttpConnector constructor descriptor", desc);
169
170
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
180
181 public void testDescriptorEntries() {
182
183
184 ManagedBean http = registry.findManagedBean("HttpConnector");
185 assertNotNull("Found HttpConnector managed bean");
186
187
188 List beanFields = http.getFields();
189 assertNotNull("Found HttpConnector fields");
190 checkField(beanFields, "field1", "HttpConnector/field1");
191 checkField(beanFields, "field2", "HttpConnector/field2");
192
193
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
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
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
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
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
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
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
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
266
267 public void testModelMBeanInfo() throws Exception {
268
269
270 ManagedBean http = registry.findManagedBean("HttpConnector");
271 assertNotNull("Found HttpConnector managed bean");
272
273
274 ModelMBeanInfo info = http.createMBeanInfo();
275 assertNotNull("Found HttpConnector ModelMBeanInfo", info);
276
277
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
286 Descriptor desc = info.getMBeanDescriptor();
287 assertNotNull("Found HttpConnector MBeanDescriptor", desc);
288
289
290 checkDescriptor(desc, "field1", "HttpConnector/field1");
291 checkDescriptor(desc, "field2", "HttpConnector/field2");
292
293 }
294
295
296
297
298
299 public void testModelMBeanNotificationInfo() throws Exception {
300
301
302 ManagedBean http = registry.findManagedBean("HttpConnector");
303 assertNotNull("Found HttpConnector managed bean");
304
305
306 ModelMBeanInfo info = http.createMBeanInfo();
307 assertNotNull("Found HttpConnector ModelMBeanInfo", info);
308
309
310 ModelMBeanNotificationInfo mmninfo = info.getNotification("Problem");
311 assertNotNull("Found HttpConnector problem info", mmninfo);
312
313
314 Descriptor desc = mmninfo.getDescriptor();
315 assertNotNull("Found HttpConnector problem descriptor", desc);
316
317
318 checkDescriptor(desc, "field1", "HttpConnector.problem/field1");
319 checkDescriptor(desc, "field2", "HttpConnector.problem/field2");
320
321 }
322
323
324
325
326
327 public void testModelMBeanOperationInfo() throws Exception {
328
329
330 ManagedBean http = registry.findManagedBean("HttpConnector");
331 assertNotNull("Found HttpConnector managed bean");
332
333
334 ModelMBeanInfo info = http.createMBeanInfo();
335 assertNotNull("Found HttpConnector ModelMBeanInfo", info);
336
337
338 ModelMBeanOperationInfo mmoinfo = info.getOperation("initialize");
339 assertNotNull("Found HttpConnector initialize info", mmoinfo);
340
341
342 Descriptor desc = mmoinfo.getDescriptor();
343 assertNotNull("Found HttpConnector initialize descriptor", desc);
344
345
346 checkDescriptor(desc, "field1", "HttpConnector.initialize/field1");
347 checkDescriptor(desc, "field2", "HttpConnector.initialize/field2");
348
349 }
350
351
352
353
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
375
376
377
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
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 }