KickJava   Java API By Example, From Geeks To Geeks.

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


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
11 import java.lang.reflect.Method;
12
13 import javax.management.MBeanOperationInfo;
14 import javax.management.MBeanParameterInfo;
15
16 import junit.framework.AssertionFailedError;
17 import junit.framework.TestCase;
18
19 /**
20  * Tests MBeanOperationInfo.
21  *
22  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
23  * @version $Revision: 1.5 $
24  */

25 public class MBeanOperationInfoTEST extends TestCase
26 {
27    public MBeanOperationInfoTEST(String s)
28    {
29       super(s);
30    }
31
32    /**
33     * Tests <tt>MBeanOperationInfo(String descr, Method m)</tt> constructor.
34     */

35    public void testConstructorWithMethod()
36    {
37       try
38       {
39          Class c = this.getClass();
40          Method m = c.getMethod("testConstructorWithMethod", new Class[0]);
41          
42          MBeanOperationInfo info = new MBeanOperationInfo("This is a description.", m);
43          
44          assertTrue(info.getDescription().equals("This is a description."));
45          assertTrue(info.getName().equals(m.getName()));
46          assertTrue(info.getReturnType().equals("void"));
47          assertTrue(info.getSignature().length == 0);
48          assertTrue(info.getImpact() == MBeanOperationInfo.UNKNOWN);
49       }
50       catch (AssertionFailedError e)
51       {
52          throw e;
53       }
54       catch (Throwable t)
55       {
56          t.printStackTrace();
57          fail("Unexpected error: " + t.toString());
58       }
59    }
60  
61    /**
62     * Tests <tt>MBeanOperationInfo(String name, String descr, MBeanParameterInfo[] sign, String returnType, int impact)</tt> constructor.
63     */

64    public void testConstructor()
65    {
66       try
67       {
68          MBeanOperationInfo info = new MBeanOperationInfo(
69                "MyOperation",
70                "This is a description.",
71                new MBeanParameterInfo[] {
72                         new MBeanParameterInfo("FooParam", "java.lang.Object", "description"),
73                         new MBeanParameterInfo("BarParam", "java.lang.String", "description")
74                },
75                "java.util.StringBuffer",
76                MBeanOperationInfo.INFO
77          );
78          
79          assertTrue(info.getDescription().equals("This is a description."));
80          assertTrue(info.getName().equals("MyOperation"));
81          assertTrue(info.getReturnType().equals("java.util.StringBuffer"));
82          assertTrue(info.getSignature().length == 2);
83          assertTrue(info.getImpact() == MBeanOperationInfo.INFO);
84          assertTrue(info.getSignature() [0].getName().equals("FooParam"));
85          assertTrue(info.getSignature() [1].getName().equals("BarParam"));
86          assertTrue(info.getSignature() [0].getDescription().equals("description"));
87          assertTrue(info.getSignature() [1].getDescription().equals("description"));
88          assertTrue(info.getSignature() [0].getType().equals("java.lang.Object"));
89          assertTrue(info.getSignature() [1].getType().equals("java.lang.String"));
90          
91       }
92       catch (AssertionFailedError e)
93       {
94          throw e;
95       }
96       catch (Throwable t)
97       {
98          t.printStackTrace();
99          fail("Unexpected error: " + t.toString());
100       }
101    }
102    
103    /**
104     * Tests the clone operation.
105     */

106    public void testClone()
107    {
108       try
109       {
110          MBeanOperationInfo info = new MBeanOperationInfo(
111                "MyOperation",
112                "This is a description.",
113                new MBeanParameterInfo[] {
114                         new MBeanParameterInfo("FooParam", "java.lang.Object", "description"),
115                         new MBeanParameterInfo("BarParam", "java.lang.String", "description")
116                },
117                "java.util.StringBuffer",
118                MBeanOperationInfo.ACTION_INFO
119          );
120          
121          MBeanOperationInfo clone = (MBeanOperationInfo)info.clone();
122          
123          assertTrue(clone.getDescription().equals("This is a description."));
124          assertTrue(clone.getName().equals("MyOperation"));
125          assertTrue(clone.getReturnType().equals("java.util.StringBuffer"));
126          assertTrue(clone.getSignature().length == 2);
127          assertTrue(clone.getImpact() == MBeanOperationInfo.ACTION_INFO);
128          assertTrue(clone.getSignature() [0].getName().equals("FooParam"));
129          assertTrue(clone.getSignature() [1].getName().equals("BarParam"));
130          assertTrue(clone.getSignature() [0].getDescription().equals("description"));
131          assertTrue(clone.getSignature() [1].getDescription().equals("description"));
132          assertTrue(clone.getSignature() [0].getType().equals("java.lang.Object"));
133          assertTrue(clone.getSignature() [1].getType().equals("java.lang.String"));
134          
135       }
136       catch (AssertionFailedError e)
137       {
138          throw e;
139       }
140       catch (Throwable t)
141       {
142          t.printStackTrace();
143          fail("Unexpected error: " + t.toString());
144       }
145    }
146
147    /**
148     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getName()</tt> accessor with empty name.
149     */

150    public void testGetNameEmpty()
151    {
152       try
153       {
154          MBeanOperationInfo info1 = new MBeanOperationInfo(
155                "",
156                "This is a description.",
157                new MBeanParameterInfo[] {
158                         new MBeanParameterInfo("FooParam", "java.lang.Object", "description"),
159                         new MBeanParameterInfo("BarParam", "java.lang.String", "description")
160                },
161                "java.util.StringBuffer",
162                MBeanOperationInfo.ACTION_INFO
163          );
164       }
165       catch (Exception e)
166       {
167          return;
168       }
169       fail("empty name is not a valid java identifier");
170    }
171
172    /**
173     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getName()</tt> accessor with <tt>null</tt> name.
174     */

175    public void testGetNameNull()
176    {
177       try
178       {
179          MBeanOperationInfo info1 = new MBeanOperationInfo(
180                null,
181                "This is a description.",
182                new MBeanParameterInfo[] {
183                         new MBeanParameterInfo("FooParam", "java.lang.Object", "description"),
184                         new MBeanParameterInfo("BarParam", "java.lang.String", "description")
185                },
186                "java.util.StringBuffer",
187                MBeanOperationInfo.ACTION_INFO
188          );
189       }
190       catch (Exception e)
191       {
192          return;
193       }
194       fail("null name is not a valid java identifier");
195    }
196
197    public void testGetNameInvalidType()
198    {
199       try
200       {
201          MBeanOperationInfo info1 = new MBeanOperationInfo(
202                "invalid type",
203                "This is a description.",
204                new MBeanParameterInfo[] {
205                         new MBeanParameterInfo("FooParam", "java.lang.Object", "description"),
206                         new MBeanParameterInfo("BarParam", "java.lang.String", "description")
207                },
208                "java.util.StringBuffer",
209                MBeanOperationInfo.ACTION_INFO
210          );
211       }
212       catch (Exception e)
213       {
214          return;
215       }
216       fail("'invalid type' is not a valid java identifier");
217    }
218
219    /**
220     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getDescription()</tt> accessor with <tt>null</tt> description.
221     */

222    public void testGetDescriptionNull()
223    {
224       try
225       {
226          MBeanOperationInfo info1 = new MBeanOperationInfo(
227                "SomeName",
228                null,
229                new MBeanParameterInfo[] {
230                         new MBeanParameterInfo("FooParam", "java.lang.Object", "description"),
231                         new MBeanParameterInfo("BarParam", "java.lang.String", "description")
232                },
233                "java.util.StringBuffer",
234                MBeanOperationInfo.ACTION_INFO
235          );
236          
237          assertTrue(info1.getDescription() == null);
238          
239       }
240       catch (AssertionFailedError e)
241       {
242          throw e;
243       }
244       catch (Throwable t)
245       {
246          t.printStackTrace();
247          fail("Unexpected error: " + t.toString());
248       }
249    }
250    
251    /**
252     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getImpact()</tt> accessor with invalid value.
253     */

254    public void testGetImpactInvalid()
255    {
256       try
257       {
258          MBeanOperationInfo info1 = new MBeanOperationInfo(
259                "SomeName",
260                "some description",
261                new MBeanParameterInfo[] {
262                         new MBeanParameterInfo("FooParam", "java.lang.Object", "description"),
263                         new MBeanParameterInfo("BarParam", "java.lang.String", "description")
264                },
265                "java.util.StringBuffer",
266                -22342
267          );
268          
269          // according to javadoc, getImpact() is only allowed to return a value that matches
270
// either ACTION, ACTION_INFO, INFO or UNKNOWN constant value.
271
if (info1.getImpact() != MBeanOperationInfo.ACTION)
272             if (info1.getImpact() != MBeanOperationInfo.INFO)
273                if (info1.getImpact() != MBeanOperationInfo.ACTION_INFO)
274                   if (info1.getImpact() != MBeanOperationInfo.UNKNOWN)
275                      
276                      // JPL: This fails in RI. The spec doesn't define how invalid impact types should be
277
// handled. This could be checked at construction time (early) or at getImpact()
278
// invocation time (late). Since behaviour is not specified, I've opted to check
279
// late and throw an JMRuntimeException in case there is an invalid impact value.
280
fail("FAILS IN RI: MBeanOperation.getImpact() is only allowed to return values that match either ACTION, ACTION_INFO, INFO or UNKNOWN constant values.");
281       
282          // should not reach here unless -22342 has somehow become a valid impact value (in which case this test should be modified)
283
fail("ERROR IN TEST: invalid impact value test does not work correctly.");
284       }
285       catch (AssertionFailedError e)
286       {
287          throw e;
288       }
289       catch (Exception e)
290       {
291          return;
292       }
293       fail("Invalid impact");
294    }
295    
296    /**
297     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getSignature()</tt> with <tt>null</tt> signature.
298     */

299    public void testGetSignatureNull()
300    {
301       try
302       {
303          MBeanOperationInfo info1 = new MBeanOperationInfo(
304                "SomeName",
305                "some description",
306                null,
307                "java.util.StringBuffer",
308                MBeanOperationInfo.ACTION
309          );
310          
311          assertTrue(info1.getSignature().length == 0);
312          
313       }
314       catch (AssertionFailedError e)
315       {
316          throw e;
317       }
318       catch (Throwable t)
319       {
320          t.printStackTrace();
321          fail("Unexpected error: " + t.toString());
322       }
323    }
324
325    /**
326     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getSignature()</tt> with empty signature array.
327     */

328    public void testGetSignatureEmpty()
329    {
330       try
331       {
332          MBeanOperationInfo info1 = new MBeanOperationInfo(
333                "SomeName",
334                "some description",
335                new MBeanParameterInfo[0],
336                "java.util.StringBuffer",
337                MBeanOperationInfo.ACTION
338          );
339          
340          assertTrue(info1.getSignature().length == 0);
341          
342       }
343       catch (AssertionFailedError e)
344       {
345          throw e;
346       }
347       catch (Throwable t)
348       {
349          t.printStackTrace();
350          fail("Unexpected error: " + t.toString());
351       }
352    }
353
354    /**
355     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getReturnType()</tt> with empty return type string.
356     */

357    public void testGetReturnTypeEmpty()
358    {
359       try
360       {
361          MBeanOperationInfo info1 = new MBeanOperationInfo(
362                "SomeName",
363                "some description",
364                new MBeanParameterInfo[0],
365                "",
366                MBeanOperationInfo.ACTION
367          );
368       }
369       catch (Exception e)
370       {
371          return;
372       }
373       fail("An empty return type is not a valid java identifier");
374    }
375
376
377    /**
378     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getReturnType()</tt> with <tt>null</tt> return type.
379     */

380    public void testGetReturnTypeNull()
381    {
382       try
383       {
384          MBeanOperationInfo info1 = new MBeanOperationInfo(
385                "SomeName",
386                "some description",
387                new MBeanParameterInfo[0],
388                "",
389                MBeanOperationInfo.ACTION
390          );
391       }
392       catch (Exception e)
393       {
394          return;
395       }
396       fail("A null return type is not a valid java identifier");
397    }
398
399    public void testGetReturnTypeInvalid()
400    {
401       try
402       {
403          MBeanOperationInfo info1 = new MBeanOperationInfo(
404                "SomeName",
405                "some description",
406                new MBeanParameterInfo[0],
407                "invalid type",
408                MBeanOperationInfo.ACTION
409          );
410       }
411       catch (Exception e)
412       {
413          return;
414       }
415       fail("'invalid type' return type is not a valid java identifier");
416    }
417 }
418
Popular Tags