KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > metadata > MBeanInfoTEST


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.metadata;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.util.Arrays JavaDoc;
29
30 import javax.management.MBeanAttributeInfo JavaDoc;
31 import javax.management.MBeanConstructorInfo JavaDoc;
32 import javax.management.MBeanInfo JavaDoc;
33 import javax.management.MBeanNotificationInfo JavaDoc;
34 import javax.management.MBeanOperationInfo JavaDoc;
35 import javax.management.MBeanParameterInfo JavaDoc;
36
37 import junit.framework.TestCase;
38
39 /**
40  * MBean Info tests.<p>
41  *
42  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
43  */

44 public class MBeanInfoTEST
45   extends TestCase
46 {
47    // Static --------------------------------------------------------------------
48

49    // Attributes ----------------------------------------------------------------
50

51    // Constructor ---------------------------------------------------------------
52

53    /**
54     * Construct the test
55     */

56    public MBeanInfoTEST(String JavaDoc s)
57    {
58       super(s);
59    }
60
61    // Tests ---------------------------------------------------------------------
62

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