KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > openmbean > OpenMBeanOperationInfoSupportTestCase


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.openmbean;
9
10 import junit.framework.TestCase;
11
12 import java.io.ByteArrayInputStream;
13 import java.io.ByteArrayOutputStream;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutputStream;
16 import java.util.Arrays;
17 import java.util.Set;
18
19 import javax.management.openmbean.OpenMBeanParameterInfo;
20 import javax.management.openmbean.OpenMBeanParameterInfoSupport;
21 import javax.management.openmbean.OpenMBeanOperationInfoSupport;
22 import javax.management.openmbean.OpenType;
23 import javax.management.openmbean.SimpleType;
24
25 /**
26  * Open MBean Operation Info tests.<p>
27  *
28  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
29  */

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

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

37    // Operation ---------------------------------------------------------------
38

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

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

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