KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > metadata > MBeanInfoTEST


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package test.compliance.metadata;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
14 import java.util.Arrays;
15
16 import javax.management.MBeanAttributeInfo;
17 import javax.management.MBeanConstructorInfo;
18 import javax.management.MBeanInfo;
19 import javax.management.MBeanNotificationInfo;
20 import javax.management.MBeanOperationInfo;
21 import javax.management.MBeanParameterInfo;
22
23 import junit.framework.TestCase;
24
25 /**
26  * MBean Info tests.<p>
27  *
28  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
29  */

30 public class MBeanInfoTEST
31   extends TestCase
32 {
33    // Static --------------------------------------------------------------------
34

35    // Attributes ----------------------------------------------------------------
36

37    // Constructor ---------------------------------------------------------------
38

39    /**
40     * Construct the test
41     */

42    public MBeanInfoTEST(String s)
43    {
44       super(s);
45    }
46
47    // Tests ---------------------------------------------------------------------
48

49    public void testMBeanInfo()
50       throws Exception
51    {
52       MBeanInfo info = new MBeanInfo(
53          "name", "description", null, null, null, null);
54       assertEquals("name", info.getClassName());
55       assertEquals("description", info.getDescription());
56       assertEquals(0, info.getAttributes().length);
57       assertEquals(0, info.getConstructors().length);
58       assertEquals(0, info.getNotifications().length);
59       assertEquals(0, info.getOperations().length);
60
61       info = new MBeanInfo(
62          "name", "description", new MBeanAttributeInfo[0], new MBeanConstructorInfo[0],
63          new MBeanOperationInfo[0], new MBeanNotificationInfo[0]);
64       assertEquals("name", info.getClassName());
65       assertEquals("description", info.getDescription());
66       assertEquals(0, info.getAttributes().length);
67       assertEquals(0, info.getConstructors().length);
68       assertEquals(0, info.getNotifications().length);
69       assertEquals(0, info.getOperations().length);
70
71       MBeanParameterInfo[] parms = new MBeanParameterInfo[]
72       {
73          new MBeanParameterInfo(
74             "name", "type", "description")
75       };
76
77       MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]
78       {
79          new MBeanAttributeInfo(
80          "name", "type", "description", true, true, false)
81       };
82       MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]
83       {
84          new MBeanConstructorInfo(
85          "name", "description", parms)
86       };
87       MBeanOperationInfo[] operations = new MBeanOperationInfo[]
88       {
89          new MBeanOperationInfo(
90          "name", "description", parms,
91          "type", MBeanOperationInfo.ACTION_INFO)
92       };
93       MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[]
94       {
95          new MBeanNotificationInfo(new String[] { "type1", "type" }, "name", "description")
96       };
97       info = new MBeanInfo(
98          "name", "description", attributes, constructors,
99          operations, notifications);
100       assertEquals("name", info.getClassName());
101       assertEquals("description", info.getDescription());
102       assertEquals(1, info.getAttributes().length);
103       assertEquals(1, info.getConstructors().length);
104       assertEquals(1, info.getNotifications().length);
105       assertEquals(1, info.getOperations().length);
106    }
107
108    public void testEquals()
109       throws Exception
110    {
111       MBeanParameterInfo[] parms = new MBeanParameterInfo[]
112       {
113          new MBeanParameterInfo(
114             "name", "type", "description")
115       };
116
117       MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]
118       {
119          new MBeanAttributeInfo(
120          "name", "type", "description", true, true, false)
121       };
122       MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]
123       {
124          new MBeanConstructorInfo(
125          "name", "description", parms)
126       };
127       MBeanOperationInfo[] operations = new MBeanOperationInfo[]
128       {
129          new MBeanOperationInfo(
130          "name", "description", parms,
131          "type", MBeanOperationInfo.ACTION_INFO)
132       };
133       MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[]
134       {
135          new MBeanNotificationInfo(new String[] { "type1", "type" }, "name", "description")
136       };
137       MBeanInfo info = new MBeanInfo(
138          "name", "description", attributes, constructors,
139          operations, notifications);
140
141       assertTrue("Null is not equal to any instance", info.equals(null) == false);
142       assertTrue("Instance is only equal another MBeanInfo instance", info.equals(new Object()) == false);
143       assertTrue("Instance should equal itself", info.equals(info));
144
145       MBeanInfo info2 = new MBeanInfo(
146          "name", "description", attributes, constructors,
147          operations, notifications);
148       assertTrue("Instances with same values should be equal", info.equals(info2));
149       assertTrue("Instances with same values should be equal", info2.equals(info));
150
151       info2 = new MBeanInfo(
152          "name2", "description", attributes, constructors,
153          operations, notifications);
154       assertTrue("Instances with different class names are not equal", info.equals(info2) == false);
155       assertTrue("Instances with different class names are not equal", info2.equals(info) == false);
156
157       info2 = new MBeanInfo(
158          "name", "description2", attributes, constructors,
159          operations, notifications);
160       assertTrue("Instances with different descriptions are not equal", info.equals(info2) == false);
161       assertTrue("Instances with different descriptions are not equal", info2.equals(info) == false);
162
163       MBeanAttributeInfo[] attributes2 = new MBeanAttributeInfo[]
164       {
165          new MBeanAttributeInfo(
166          "name2", "type", "description", true, true, false)
167       };
168
169       info2 = new MBeanInfo(
170          "name", "description", attributes2, constructors,
171          operations, notifications);
172       assertTrue("Instances with different attributes are not equal", info.equals(info2) == false);
173       assertTrue("Instances with different attributes are not equal", info2.equals(info) == false);
174
175       attributes2 = new MBeanAttributeInfo[]
176       {
177          new MBeanAttributeInfo(
178          "name2", "type", "description", true, true, false),
179          new MBeanAttributeInfo(
180          "name3", "type", "description", true, true, false)
181       };
182
183       info2 = new MBeanInfo(
184          "name", "description", attributes2, constructors,
185          operations, notifications);
186       assertTrue("Instances with different numbers of attributes are not equal", info.equals(info2) == false);
187       assertTrue("Instances with different numbers of attributes are not equal", info2.equals(info) == false);
188
189       info2 = new MBeanInfo(
190          "name", "description", null, constructors,
191          operations, notifications);
192       assertTrue("Instances with and without attributes are not equal", info.equals(info2) == false);
193       assertTrue("Instances with and without attributes are not equal", info2.equals(info) == false);
194
195       MBeanConstructorInfo[] constructors2 = new MBeanConstructorInfo[]
196       {
197          new MBeanConstructorInfo(
198          "name2", "description", parms)
199       };
200
201       info2 = new MBeanInfo(
202          "name", "description", attributes, constructors2,
203          operations, notifications);
204       assertTrue("Instances with different constructors are not equal", info.equals(info2) == false);
205       assertTrue("Instances with different constructors are not equal", info2.equals(info) == false);
206
207       constructors2 = new MBeanConstructorInfo[]
208       {
209          new MBeanConstructorInfo(
210          "name2", "description", parms),
211          new MBeanConstructorInfo(
212          "name3", "description", parms)
213       };
214
215       info2 = new MBeanInfo(
216          "name", "description", attributes, constructors2,
217          operations, notifications);
218       assertTrue("Instances with different numbers of constructors are not equal", info.equals(info2) == false);
219       assertTrue("Instances with different numbers of constructors are not equal", info2.equals(info) == false);
220
221       info2 = new MBeanInfo(
222          "name", "description", attributes, null,
223          operations, notifications);
224       assertTrue("Instances with and without constructors are not equal", info.equals(info2) == false);
225       assertTrue("Instances with and without constructors are not equal", info2.equals(info) == false);
226
227       MBeanOperationInfo[] operations2 = new MBeanOperationInfo[]
228       {
229          new MBeanOperationInfo(
230          "name2", "description", parms,
231          "type", MBeanOperationInfo.ACTION_INFO)
232       };
233
234       info2 = new MBeanInfo(
235          "name", "description", attributes, constructors,
236          operations2, notifications);
237       assertTrue("Instances with different operations are not equal", info.equals(info2) == false);
238       assertTrue("Instances with different operations are not equal", info2.equals(info) == false);
239
240       operations2 = new MBeanOperationInfo[]
241       {
242          new MBeanOperationInfo(
243          "name2", "description", parms,
244          "type", MBeanOperationInfo.ACTION_INFO),
245          new MBeanOperationInfo(
246          "name3", "description", parms,
247          "type", MBeanOperationInfo.ACTION_INFO)
248       };
249
250       info2 = new MBeanInfo(
251          "name", "description", attributes, constructors,
252          operations2, notifications);
253       assertTrue("Instances with different numbers of operations are not equal", info.equals(info2) == false);
254       assertTrue("Instances with different numbers of operations are not equal", info2.equals(info) == false);
255
256       info2 = new MBeanInfo(
257          "name", "description", attributes, constructors,
258          null, notifications);
259       assertTrue("Instances with and without operations are not equal", info.equals(info2) == false);
260       assertTrue("Instances with and without operations are not equal", info2.equals(info) == false);
261
262       MBeanNotificationInfo[] notifications2 = new MBeanNotificationInfo[]
263       {
264          new MBeanNotificationInfo(new String[] { "type", "type" }, "name2", "description")
265       };
266
267       info2 = new MBeanInfo(
268          "name", "description", attributes, constructors,
269          operations, notifications2);
270       assertTrue("Instances with different notifications are not equal", info.equals(info2) == false);
271       assertTrue("Instances with different notifications are not equal", info2.equals(info) == false);
272
273       notifications2 = new MBeanNotificationInfo[]
274       {
275          new MBeanNotificationInfo(new String[] { "type", "type" }, "name2", "description"),
276          new MBeanNotificationInfo(new String[] { "type", "type" }, "name3", "description")
277       };
278
279       info2 = new MBeanInfo(
280          "name", "description", attributes, constructors,
281          operations, notifications2);
282       assertTrue("Instances with different numbers of notifications are not equal", info.equals(info2) == false);
283       assertTrue("Instances with different numbers of notifications are not equal", info2.equals(info) == false);
284
285       info2 = new MBeanInfo(
286          "name", "description", attributes, constructors,
287          operations, null);
288       assertTrue("Instances with and without notifications are not equal", info.equals(info2) == false);
289       assertTrue("Instances with and without notifications are not equal", info2.equals(info) == false);
290    }
291
292    public void testHashCode()
293       throws Exception
294    {
295       MBeanParameterInfo[] parms = new MBeanParameterInfo[]
296       {
297          new MBeanParameterInfo(
298             "name", "type", "description")
299       };
300
301       MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]
302       {
303          new MBeanAttributeInfo(
304          "name", "type", "description", true, true, false)
305       };
306       MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]
307       {
308          new MBeanConstructorInfo(
309          "name", "description", parms)
310       };
311       MBeanOperationInfo[] operations = new MBeanOperationInfo[]
312       {
313          new MBeanOperationInfo(
314          "name", "description", parms,
315          "type", MBeanOperationInfo.ACTION_INFO)
316       };
317       MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[]
318       {
319          new MBeanNotificationInfo(
320          new String[] { "type1", "type" }, "name", "description")
321       };
322       MBeanInfo info1 = new MBeanInfo(
323          "name", "description", attributes, constructors,
324          operations, notifications);
325       MBeanInfo info2 = new MBeanInfo(
326          "name", "description", attributes, constructors,
327          operations, notifications);
328
329       assertTrue("Different instances with the same hashcode are equal", info1.hashCode() == info2.hashCode());
330    }
331
332    public void testSerialization()
333       throws Exception
334    {
335       MBeanParameterInfo[] parms = new MBeanParameterInfo[]
336       {
337          new MBeanParameterInfo(
338             "name", "type", "description")
339       };
340
341       MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]
342       {
343          new MBeanAttributeInfo(
344          "name", "type", "description", true, true, false)
345       };
346       MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]
347       {
348          new MBeanConstructorInfo(
349          "name", "description", parms)
350       };
351       MBeanOperationInfo[] operations = new MBeanOperationInfo[]
352       {
353          new MBeanOperationInfo(
354          "name", "description", parms,
355          "type", MBeanOperationInfo.ACTION_INFO)
356       };
357       MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[]
358       {
359          new MBeanNotificationInfo(
360          new String[] { "type1", "type" }, "name", "description")
361       };
362       MBeanInfo info = new MBeanInfo(
363          "name", "description", attributes, constructors,
364          operations, notifications);
365       // Serialize it
366
ByteArrayOutputStream baos = new ByteArrayOutputStream();
367       ObjectOutputStream oos = new ObjectOutputStream(baos);
368       oos.writeObject(info);
369     
370       // Deserialize it
371
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
372       ObjectInputStream ois = new ObjectInputStream(bais);
373       MBeanInfo result = (MBeanInfo) ois.readObject();
374
375       assertEquals(info.getClassName(), result.getClassName());
376       assertEquals(Arrays.asList(info.getAttributes()), Arrays.asList(result.getAttributes()));
377       assertEquals(Arrays.asList(info.getConstructors()), Arrays.asList(result.getConstructors()));
378       assertEquals(Arrays.asList(info.getOperations()), Arrays.asList(result.getOperations()));
379
380       // UGLY!
381
MBeanNotificationInfo origNotification = info.getNotifications()[0];
382       MBeanNotificationInfo resultNotification = result.getNotifications()[0];
383       assertEquals(origNotification.getName(), resultNotification.getName());
384       assertEquals(origNotification.getDescription(), resultNotification.getDescription());
385       assertEquals(Arrays.asList(origNotification.getNotifTypes()), Arrays.asList(resultNotification.getNotifTypes()));
386    }
387 }
388
Popular Tags