KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import java.lang.reflect.Method JavaDoc;
26
27 import javax.management.MBeanOperationInfo JavaDoc;
28 import javax.management.MBeanParameterInfo JavaDoc;
29
30 import junit.framework.AssertionFailedError;
31 import junit.framework.TestCase;
32
33 /**
34  * Tests MBeanOperationInfo.
35  *
36  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
37  * @version $Revision: 37459 $
38  */

39 public class MBeanOperationInfoTEST extends TestCase
40 {
41    public MBeanOperationInfoTEST(String JavaDoc s)
42    {
43       super(s);
44    }
45
46    /**
47     * Tests <tt>MBeanOperationInfo(String descr, Method m)</tt> constructor.
48     */

49    public void testConstructorWithMethod()
50    {
51       try
52       {
53          Class JavaDoc c = this.getClass();
54          Method JavaDoc m = c.getMethod("testConstructorWithMethod", new Class JavaDoc[0]);
55          
56          MBeanOperationInfo JavaDoc info = new MBeanOperationInfo JavaDoc("This is a description.", m);
57          
58          assertTrue(info.getDescription().equals("This is a description."));
59          assertTrue(info.getName().equals(m.getName()));
60          assertTrue(info.getReturnType().equals("void"));
61          assertTrue(info.getSignature().length == 0);
62          assertTrue(info.getImpact() == MBeanOperationInfo.UNKNOWN);
63       }
64       catch (AssertionFailedError e)
65       {
66          throw e;
67       }
68       catch (Throwable JavaDoc t)
69       {
70          t.printStackTrace();
71          fail("Unexpected error: " + t.toString());
72       }
73    }
74  
75    /**
76     * Tests <tt>MBeanOperationInfo(String name, String descr, MBeanParameterInfo[] sign, String returnType, int impact)</tt> constructor.
77     */

78    public void testConstructor()
79    {
80       try
81       {
82          MBeanOperationInfo JavaDoc info = new MBeanOperationInfo JavaDoc(
83                "MyOperation",
84                "This is a description.",
85                new MBeanParameterInfo JavaDoc[] {
86                         new MBeanParameterInfo JavaDoc("FooParam", "java.lang.Object", "description"),
87                         new MBeanParameterInfo JavaDoc("BarParam", "java.lang.String", "description")
88                },
89                "java.util.StringBuffer",
90                MBeanOperationInfo.INFO
91          );
92          
93          assertTrue(info.getDescription().equals("This is a description."));
94          assertTrue(info.getName().equals("MyOperation"));
95          assertTrue(info.getReturnType().equals("java.util.StringBuffer"));
96          assertTrue(info.getSignature().length == 2);
97          assertTrue(info.getImpact() == MBeanOperationInfo.INFO);
98          assertTrue(info.getSignature() [0].getName().equals("FooParam"));
99          assertTrue(info.getSignature() [1].getName().equals("BarParam"));
100          assertTrue(info.getSignature() [0].getDescription().equals("description"));
101          assertTrue(info.getSignature() [1].getDescription().equals("description"));
102          assertTrue(info.getSignature() [0].getType().equals("java.lang.Object"));
103          assertTrue(info.getSignature() [1].getType().equals("java.lang.String"));
104          
105       }
106       catch (AssertionFailedError e)
107       {
108          throw e;
109       }
110       catch (Throwable JavaDoc t)
111       {
112          t.printStackTrace();
113          fail("Unexpected error: " + t.toString());
114       }
115    }
116    
117    /**
118     * Tests the clone operation.
119     */

120    public void testClone()
121    {
122       try
123       {
124          MBeanOperationInfo JavaDoc info = new MBeanOperationInfo JavaDoc(
125                "MyOperation",
126                "This is a description.",
127                new MBeanParameterInfo JavaDoc[] {
128                         new MBeanParameterInfo JavaDoc("FooParam", "java.lang.Object", "description"),
129                         new MBeanParameterInfo JavaDoc("BarParam", "java.lang.String", "description")
130                },
131                "java.util.StringBuffer",
132                MBeanOperationInfo.ACTION_INFO
133          );
134          
135          MBeanOperationInfo JavaDoc clone = (MBeanOperationInfo JavaDoc)info.clone();
136          
137          assertTrue(clone.getDescription().equals("This is a description."));
138          assertTrue(clone.getName().equals("MyOperation"));
139          assertTrue(clone.getReturnType().equals("java.util.StringBuffer"));
140          assertTrue(clone.getSignature().length == 2);
141          assertTrue(clone.getImpact() == MBeanOperationInfo.ACTION_INFO);
142          assertTrue(clone.getSignature() [0].getName().equals("FooParam"));
143          assertTrue(clone.getSignature() [1].getName().equals("BarParam"));
144          assertTrue(clone.getSignature() [0].getDescription().equals("description"));
145          assertTrue(clone.getSignature() [1].getDescription().equals("description"));
146          assertTrue(clone.getSignature() [0].getType().equals("java.lang.Object"));
147          assertTrue(clone.getSignature() [1].getType().equals("java.lang.String"));
148          
149       }
150       catch (AssertionFailedError e)
151       {
152          throw e;
153       }
154       catch (Throwable JavaDoc t)
155       {
156          t.printStackTrace();
157          fail("Unexpected error: " + t.toString());
158       }
159    }
160
161    /**
162     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getDescription()</tt> accessor with <tt>null</tt> description.
163     */

164    public void testGetDescriptionNull()
165    {
166       try
167       {
168          MBeanOperationInfo JavaDoc info1 = new MBeanOperationInfo JavaDoc(
169                "SomeName",
170                null,
171                new MBeanParameterInfo JavaDoc[] {
172                         new MBeanParameterInfo JavaDoc("FooParam", "java.lang.Object", "description"),
173                         new MBeanParameterInfo JavaDoc("BarParam", "java.lang.String", "description")
174                },
175                "java.util.StringBuffer",
176                MBeanOperationInfo.ACTION_INFO
177          );
178          
179          assertTrue(info1.getDescription() == null);
180          
181       }
182       catch (AssertionFailedError e)
183       {
184          throw e;
185       }
186       catch (Throwable JavaDoc t)
187       {
188          t.printStackTrace();
189          fail("Unexpected error: " + t.toString());
190       }
191    }
192    
193    /**
194     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getImpact()</tt> accessor with invalid value.
195     */

196    public void testGetImpactInvalid()
197    {
198       try
199       {
200          MBeanOperationInfo JavaDoc info1 = new MBeanOperationInfo JavaDoc(
201                "SomeName",
202                "some description",
203                new MBeanParameterInfo JavaDoc[] {
204                         new MBeanParameterInfo JavaDoc("FooParam", "java.lang.Object", "description"),
205                         new MBeanParameterInfo JavaDoc("BarParam", "java.lang.String", "description")
206                },
207                "java.util.StringBuffer",
208                -22342
209          );
210          
211          // according to javadoc, getImpact() is only allowed to return a value that matches
212
// either ACTION, ACTION_INFO, INFO or UNKNOWN constant value.
213
if (info1.getImpact() != MBeanOperationInfo.ACTION)
214             if (info1.getImpact() != MBeanOperationInfo.INFO)
215                if (info1.getImpact() != MBeanOperationInfo.ACTION_INFO)
216                   if (info1.getImpact() != MBeanOperationInfo.UNKNOWN)
217                      
218                      // JPL: This fails in RI. The spec doesn't define how invalid impact types should be
219
// handled. This could be checked at construction time (early) or at getImpact()
220
// invocation time (late). Since behaviour is not specified, I've opted to check
221
// late and throw an JMRuntimeException in case there is an invalid impact value.
222
fail("FAILS IN RI: MBeanOperation.getImpact() is only allowed to return values that match either ACTION, ACTION_INFO, INFO or UNKNOWN constant values.");
223       
224          // should not reach here unless -22342 has somehow become a valid impact value (in which case this test should be modified)
225
fail("ERROR IN TEST: invalid impact value test does not work correctly.");
226       }
227       catch (AssertionFailedError e)
228       {
229          throw e;
230       }
231       catch (Exception JavaDoc e)
232       {
233          return;
234       }
235       fail("Invalid impact");
236    }
237    
238    /**
239     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getSignature()</tt> with <tt>null</tt> signature.
240     */

241    public void testGetSignatureNull()
242    {
243       try
244       {
245          MBeanOperationInfo JavaDoc info1 = new MBeanOperationInfo JavaDoc(
246                "SomeName",
247                "some description",
248                null,
249                "java.util.StringBuffer",
250                MBeanOperationInfo.ACTION
251          );
252          
253          assertTrue(info1.getSignature().length == 0);
254          
255       }
256       catch (AssertionFailedError e)
257       {
258          throw e;
259       }
260       catch (Throwable JavaDoc t)
261       {
262          t.printStackTrace();
263          fail("Unexpected error: " + t.toString());
264       }
265    }
266
267    /**
268     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getSignature()</tt> with empty signature array.
269     */

270    public void testGetSignatureEmpty()
271    {
272       try
273       {
274          MBeanOperationInfo JavaDoc info1 = new MBeanOperationInfo JavaDoc(
275                "SomeName",
276                "some description",
277                new MBeanParameterInfo JavaDoc[0],
278                "java.util.StringBuffer",
279                MBeanOperationInfo.ACTION
280          );
281          
282          assertTrue(info1.getSignature().length == 0);
283          
284       }
285       catch (AssertionFailedError e)
286       {
287          throw e;
288       }
289       catch (Throwable JavaDoc t)
290       {
291          t.printStackTrace();
292          fail("Unexpected error: " + t.toString());
293       }
294    }
295
296    /**
297     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getReturnType()</tt> with empty return type string.
298     */

299    public void testGetReturnTypeEmpty()
300    {
301       try
302       {
303          new MBeanOperationInfo JavaDoc(
304                "SomeName",
305                "some description",
306                new MBeanParameterInfo JavaDoc[0],
307                "",
308                MBeanOperationInfo.ACTION
309          );
310       }
311       catch (Exception JavaDoc e)
312       {
313          return;
314       }
315       fail("An empty return type is not a valid java identifier");
316    }
317
318
319    /**
320     * Tests <tt>MBeanOperationInfo</tt> creation and <tt>getReturnType()</tt> with <tt>null</tt> return type.
321     */

322    public void testGetReturnTypeNull()
323    {
324       try
325       {
326          new MBeanOperationInfo JavaDoc(
327                "SomeName",
328                "some description",
329                new MBeanParameterInfo JavaDoc[0],
330                "",
331                MBeanOperationInfo.ACTION
332          );
333       }
334       catch (Exception JavaDoc e)
335       {
336          return;
337       }
338       fail("A null return type is not a valid java identifier");
339    }
340
341    public void testGetReturnTypeInvalid()
342    {
343       try
344       {
345          new MBeanOperationInfo JavaDoc(
346                "SomeName",
347                "some description",
348                new MBeanParameterInfo JavaDoc[0],
349                "invalid type",
350                MBeanOperationInfo.ACTION
351          );
352       }
353       catch (Exception JavaDoc e)
354       {
355          return;
356       }
357       fail("'invalid type' return type is not a valid java identifier");
358    }
359 }
360
Popular Tags