KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > openmbean > OpenMBeanOperationInfoSupportTestCase


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.openmbean;
23
24 import junit.framework.TestCase;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.management.MBeanOperationInfo JavaDoc;
34 import javax.management.openmbean.OpenMBeanParameterInfo JavaDoc;
35 import javax.management.openmbean.OpenMBeanParameterInfoSupport JavaDoc;
36 import javax.management.openmbean.OpenMBeanOperationInfoSupport JavaDoc;
37 import javax.management.openmbean.OpenType JavaDoc;
38 import javax.management.openmbean.SimpleType JavaDoc;
39
40 /**
41  * Open MBean Operation Info tests.<p>
42  *
43  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
44  */

45 public class OpenMBeanOperationInfoSupportTestCase
46   extends TestCase
47 {
48    // Static --------------------------------------------------------------------
49

50    // Attributes ----------------------------------------------------------------
51

52    // Operation ---------------------------------------------------------------
53

54    /**
55     * Construct the test
56     */

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

64    public void testOpenMBeanOperationInfoSupport()
65       throws Exception JavaDoc
66    {
67       OpenMBeanOperationInfoSupport JavaDoc info = new OpenMBeanOperationInfoSupport JavaDoc(
68          "name", "description", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
69       assertEquals("name", info.getName());
70       assertEquals("description", info.getDescription());
71       assertEquals(0, info.getSignature().length);
72       assertEquals("java.lang.String", info.getReturnType());
73       assertEquals(SimpleType.STRING, info.getReturnOpenType());
74       assertEquals(MBeanOperationInfo.ACTION_INFO, info.getImpact());
75
76       info = new OpenMBeanOperationInfoSupport JavaDoc(
77          "name", "description", new OpenMBeanParameterInfoSupport JavaDoc[0],
78          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
79       assertEquals("name", info.getName());
80       assertEquals("description", info.getDescription());
81       assertEquals(0, info.getSignature().length);
82       assertEquals("java.lang.String", info.getReturnType());
83       assertEquals(SimpleType.STRING, info.getReturnOpenType());
84       assertEquals(MBeanOperationInfo.ACTION_INFO, info.getImpact());
85
86       OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
87       {
88          new OpenMBeanParameterInfoSupport JavaDoc(
89             "name", "description", SimpleType.STRING)
90       };
91       info = new OpenMBeanOperationInfoSupport JavaDoc(
92          "name", "description", parms,
93          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
94       assertEquals("name", info.getName());
95       assertEquals("description", info.getDescription());
96       assertEquals(1, info.getSignature().length);
97       assertEquals("java.lang.String", info.getReturnType());
98       assertEquals(SimpleType.STRING, info.getReturnOpenType());
99       assertEquals(MBeanOperationInfo.ACTION_INFO, info.getImpact());
100    }
101
102    public void testReturnOpenType()
103       throws Exception JavaDoc
104    {
105       OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
106       {
107          new OpenMBeanParameterInfoSupport JavaDoc(
108             "name", "description", SimpleType.STRING)
109       };
110       OpenMBeanOperationInfoSupport JavaDoc info = new OpenMBeanOperationInfoSupport JavaDoc(
111          "name", "description", parms,
112          SimpleType.BOOLEAN, MBeanOperationInfo.ACTION_INFO);
113
114       assertEquals(SimpleType.BOOLEAN, info.getReturnOpenType());
115    }
116
117    public void testEquals()
118       throws Exception JavaDoc
119    {
120       OpenMBeanOperationInfoSupport JavaDoc info = new OpenMBeanOperationInfoSupport JavaDoc(
121          "name", "description", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
122
123       assertTrue("Null should not be equal", info.equals(null) == false);
124       assertTrue("Only OpenMBeanOperationInfo should be equal", info.equals(new Object JavaDoc()) == false);
125
126       OpenMBeanOperationInfoSupport JavaDoc info2 = new OpenMBeanOperationInfoSupport JavaDoc(
127          "name", "description", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
128
129       assertTrue("Different instances of the same data are equal", info.equals(info2));
130       assertTrue("Different instances of the same data are equal", info2.equals(info));
131
132       info2 = new OpenMBeanOperationInfoSupport JavaDoc(
133          "name", "description2", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
134
135       assertTrue("Different instances with different descriptions are equal", info.equals(info2));
136       assertTrue("Different instances with different descritpions are equal", info2.equals(info));
137
138       info2 = new OpenMBeanOperationInfoSupport JavaDoc(
139          "name2", "description", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
140
141       assertTrue("Instances with different names are not equal", info.equals(info2) == false);
142       assertTrue("Instances with different names are not equal", info2.equals(info) == false);
143
144       OpenMBeanParameterInfoSupport JavaDoc param1 = new OpenMBeanParameterInfoSupport JavaDoc(
145          "name", "description", SimpleType.STRING);
146       OpenMBeanParameterInfoSupport JavaDoc param2 = new OpenMBeanParameterInfoSupport JavaDoc(
147          "name2", "description", SimpleType.STRING);
148
149       info = new OpenMBeanOperationInfoSupport JavaDoc(
150          "name", "description", new OpenMBeanParameterInfoSupport JavaDoc[] { param1, param2 },
151          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
152       info2 = new OpenMBeanOperationInfoSupport JavaDoc(
153          "name", "description", new OpenMBeanParameterInfoSupport JavaDoc[] { param1, param2 },
154          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
155
156       assertTrue("Different instances with the same parameters are equal", info.equals(info2));
157       assertTrue("Different instances with the same parameters are equal", info2.equals(info));
158
159       info = new OpenMBeanOperationInfoSupport JavaDoc(
160          "name", "description", new OpenMBeanParameterInfoSupport JavaDoc[] { param1, param2 },
161          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
162       info2 = new OpenMBeanOperationInfoSupport JavaDoc(
163          "name", "description", new OpenMBeanParameterInfoSupport JavaDoc[] { param2, param1 },
164          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
165
166       assertTrue("Different instances with the same signature but different parameters are not equal",
167          info.equals(info2) == false);
168       assertTrue("Different instances with the same signature but different parameters are not equal",
169          info2.equals(info) == false);
170
171       param2 = new OpenMBeanParameterInfoSupport JavaDoc(
172          "name2", "description", SimpleType.INTEGER);
173       info2 = new OpenMBeanOperationInfoSupport JavaDoc(
174          "name", "description", new OpenMBeanParameterInfoSupport JavaDoc[] { param1, param2 },
175          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
176
177       assertTrue("Different instances with different signatures are not equal",
178          info.equals(info2) == false);
179       assertTrue("Different instances with different signatures are not equal",
180          info2.equals(info) == false);
181
182       info2 = new OpenMBeanOperationInfoSupport JavaDoc(
183          "name", "description", new OpenMBeanParameterInfoSupport JavaDoc[] { param1 },
184          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
185
186       assertTrue("Different instances with different numbers of paramters are not equal",
187          info.equals(info2) == false);
188       assertTrue("Different instances with different numbers of parameters are not equal",
189          info2.equals(info) == false);
190
191       info2 = new OpenMBeanOperationInfoSupport JavaDoc(
192          "name", "description", new OpenMBeanParameterInfoSupport JavaDoc[] { param1, param2 },
193          SimpleType.INTEGER, MBeanOperationInfo.ACTION_INFO);
194
195       assertTrue("Different instances with different return types are not equal",
196          info.equals(info2) == false);
197       assertTrue("Different instances with different return types are not equal",
198          info2.equals(info) == false);
199
200       info2 = new OpenMBeanOperationInfoSupport JavaDoc(
201          "name", "description", new OpenMBeanParameterInfoSupport JavaDoc[] { param1, param2 },
202          SimpleType.STRING, MBeanOperationInfo.ACTION);
203
204       assertTrue("Different instances with different impacts are not equal",
205          info.equals(info2) == false);
206       assertTrue("Different instances with different impacts are not equal",
207          info2.equals(info) == false);
208    }
209
210    public void testHashCode()
211       throws Exception JavaDoc
212    {
213       OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
214       {
215          new OpenMBeanParameterInfoSupport JavaDoc(
216             "name", "description", SimpleType.STRING)
217       };
218       OpenMBeanOperationInfoSupport JavaDoc info = new OpenMBeanOperationInfoSupport JavaDoc(
219          "name", "description", parms,
220          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
221
222       int myHash = "name".hashCode() + Arrays.asList(parms).hashCode()
223                    + SimpleType.STRING.hashCode() + MBeanOperationInfo.ACTION_INFO;
224       assertEquals(myHash, info.hashCode());
225    }
226
227    public void testToString()
228       throws Exception JavaDoc
229    {
230       OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
231       {
232          new OpenMBeanParameterInfoSupport JavaDoc(
233             "name", "description", SimpleType.STRING)
234       };
235       OpenMBeanOperationInfoSupport JavaDoc info = new OpenMBeanOperationInfoSupport JavaDoc(
236          "NAME", "DESCRIPTION", parms,
237          SimpleType.INTEGER, MBeanOperationInfo.ACTION_INFO);
238
239       String JavaDoc toString = info.toString();
240
241       assertTrue("info.toString() should contain NAME",
242          toString.indexOf("NAME") != -1);
243       assertTrue("info.toString() should contain the parameters",
244          toString.indexOf(Arrays.asList(parms).toString()) != -1);
245       assertTrue("info.toString() should contain the simple type",
246          toString.indexOf(SimpleType.INTEGER.toString()) != -1);
247    }
248
249    public void testSerialization()
250       throws Exception JavaDoc
251    {
252       OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
253       {
254          new OpenMBeanParameterInfoSupport JavaDoc(
255             "name", "description", SimpleType.STRING)
256       };
257       OpenMBeanOperationInfoSupport JavaDoc info = new OpenMBeanOperationInfoSupport JavaDoc(
258          "name", "description", parms,
259          SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
260
261       // Serialize it
262
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
263       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
264       oos.writeObject(info);
265     
266       // Deserialize it
267
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
268       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
269       Object JavaDoc result = ois.readObject();
270
271       assertEquals(info, result);
272    }
273
274    public void testErrors()
275       throws Exception JavaDoc
276    {
277       boolean caught = false;
278       try
279       {
280          OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
281          {
282             new OpenMBeanParameterInfoSupport JavaDoc(
283                "name", "description", SimpleType.STRING)
284          };
285          new OpenMBeanOperationInfoSupport JavaDoc(
286             null, "description", parms,
287             SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
288       }
289       catch (IllegalArgumentException JavaDoc e)
290       {
291          caught = true;
292       }
293       if (caught == false)
294          fail("Expected IllegalArgumentException for null name");
295
296       caught = false;
297       try
298       {
299          OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
300          {
301             new OpenMBeanParameterInfoSupport JavaDoc(
302                "name", "description", SimpleType.STRING)
303          };
304          new OpenMBeanOperationInfoSupport JavaDoc(
305             "", "description", parms,
306             SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
307       }
308       catch (IllegalArgumentException JavaDoc e)
309       {
310          caught = true;
311       }
312       if (caught == false)
313          fail("Expected IllegalArgumentException for empty name");
314
315       caught = false;
316       try
317       {
318          OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
319          {
320             new OpenMBeanParameterInfoSupport JavaDoc(
321                "name", "description", SimpleType.STRING)
322          };
323          new OpenMBeanOperationInfoSupport JavaDoc(
324             "name", null, parms,
325             SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
326       }
327       catch (IllegalArgumentException JavaDoc e)
328       {
329          caught = true;
330       }
331       if (caught == false)
332          fail("Expected IllegalArgumentException for null description");
333
334       caught = false;
335       try
336       {
337          OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
338          {
339             new OpenMBeanParameterInfoSupport JavaDoc(
340                "name", "description", SimpleType.STRING)
341          };
342          new OpenMBeanOperationInfoSupport JavaDoc(
343             "name", "", parms,
344             SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
345       }
346       catch (IllegalArgumentException JavaDoc e)
347       {
348          caught = true;
349       }
350       if (caught == false)
351          fail("Expected IllegalArgumentException for empty description");
352
353       caught = false;
354       try
355       {
356          new OpenMBeanOperationInfoSupport JavaDoc(
357             "name", "description", new MyOpenMBeanParameterInfo[] { new MyOpenMBeanParameterInfo() },
358             SimpleType.STRING, MBeanOperationInfo.ACTION_INFO);
359       }
360       catch (ArrayStoreException JavaDoc e)
361       {
362          caught = true;
363       }
364       if (caught == false)
365          fail("Expected ArrayStoreException for non MBeanParameterInfo array");
366
367       caught = false;
368       try
369       {
370          OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
371          {
372             new OpenMBeanParameterInfoSupport JavaDoc(
373                "name", "description", SimpleType.STRING)
374          };
375          new OpenMBeanOperationInfoSupport JavaDoc(
376             "name", "description", parms,
377             null, MBeanOperationInfo.ACTION_INFO);
378       }
379       catch (IllegalArgumentException JavaDoc e)
380       {
381          caught = true;
382       }
383       if (caught == false)
384          fail("Expected IllegalArgumentException for null return type");
385
386       caught = false;
387       try
388       {
389          OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
390          {
391             new OpenMBeanParameterInfoSupport JavaDoc(
392                "name", "description", SimpleType.STRING)
393          };
394          new OpenMBeanOperationInfoSupport JavaDoc(
395             "name", "description", parms,
396             SimpleType.VOID, MBeanOperationInfo.ACTION_INFO);
397       }
398       catch (IllegalArgumentException JavaDoc e)
399       {
400          caught = true;
401       }
402       if (caught == true)
403          fail("Didn't expect IllegalArgumentException for VOID return type");
404
405       caught = false;
406       try
407       {
408          OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
409          {
410             new OpenMBeanParameterInfoSupport JavaDoc(
411                "name", "description", SimpleType.STRING)
412          };
413          new OpenMBeanOperationInfoSupport JavaDoc(
414             "name", "description", parms,
415             SimpleType.STRING, 1234567);
416       }
417       catch (IllegalArgumentException JavaDoc e)
418       {
419          caught = true;
420       }
421       if (caught == false)
422          fail("Expected IllegalArgumentExecption for invalid action");
423
424       caught = false;
425       try
426       {
427          OpenMBeanParameterInfoSupport JavaDoc[] parms = new OpenMBeanParameterInfoSupport JavaDoc[]
428          {
429             new OpenMBeanParameterInfoSupport JavaDoc(
430                "name", "description", SimpleType.STRING)
431          };
432          new OpenMBeanOperationInfoSupport JavaDoc(
433             "name", "description", parms,
434             SimpleType.STRING, MBeanOperationInfo.UNKNOWN);
435       }
436       catch (IllegalArgumentException JavaDoc e)
437       {
438          caught = true;
439       }
440       if (caught == false)
441          fail("Expected IllegalArgumentExecption for UNKNOWN action");
442    }
443
444    public static class MyOpenMBeanParameterInfo
445       implements OpenMBeanParameterInfo JavaDoc
446    {
447       public boolean equals(Object JavaDoc o) { return false; }
448       public Object JavaDoc getDefaultValue() { return null; }
449       public String JavaDoc getDescription() { return null; }
450       public Set JavaDoc getLegalValues() { return null; }
451       public Comparable JavaDoc getMaxValue() { return null; }
452       public Comparable JavaDoc getMinValue() { return null; }
453       public String JavaDoc getName() { return null; }
454       public OpenType JavaDoc getOpenType() { return null; }
455       public boolean hasDefaultValue() { return false; }
456       public boolean hasLegalValues() { return false; }
457       public int hashCode() { return 0; }
458       public boolean hasMaxValue() { return false; }
459       public boolean hasMinValue() { return false; }
460       public boolean isValue(Object JavaDoc o) { return false; }
461       public String JavaDoc toString() { return null; }
462    }
463 }
464
Popular Tags