KickJava   Java API By Example, From Geeks To Geeks.

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


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.IntrospectionException JavaDoc;
28 import javax.management.MBeanAttributeInfo JavaDoc;
29
30 import org.jboss.test.jmx.compliance.metadata.support.Trivial;
31
32 import junit.framework.AssertionFailedError;
33 import junit.framework.TestCase;
34
35 /**
36  * Tests MBeanAttributeInfo.
37  *
38  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
39  * @version $Revision: 37459 $
40  */

41 public class MBeanAttributeInfoTEST extends TestCase
42 {
43    public MBeanAttributeInfoTEST(String JavaDoc s)
44    {
45       super(s);
46    }
47
48    /**
49     * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> constructor.
50     */

51    public void testConstructorWithAccessorMethods()
52    {
53       try
54       {
55          Class JavaDoc c = Trivial.class;
56          Method JavaDoc getter = c.getMethod("getSomething", new Class JavaDoc[0]);
57          Method JavaDoc setter = c.getMethod("setSomething", new Class JavaDoc[] { String JavaDoc.class });
58          
59          MBeanAttributeInfo JavaDoc info = new MBeanAttributeInfo JavaDoc("Something", "a description", getter, setter);
60          
61          assertTrue(info.getDescription().equals("a description"));
62          assertTrue(info.getName().equals("Something"));
63          assertTrue(info.getType().equals("java.lang.String"));
64          assertTrue(info.isReadable() == true);
65          assertTrue(info.isWritable() == true);
66          assertTrue(info.isIs() == false);
67       }
68       catch (AssertionFailedError e)
69       {
70          throw e;
71       }
72       catch (Throwable JavaDoc t)
73       {
74          t.printStackTrace();
75          fail("Unexpected error: " + t.toString());
76       }
77    }
78  
79    /**
80     * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> with misplaced accessor methods.
81     */

82    public void testConstructorWithMisplacedAccessorMethods()
83    {
84       try
85       {
86          Class JavaDoc c = Trivial.class;
87          Method JavaDoc getter = c.getMethod("getSomething", new Class JavaDoc[0]);
88          Method JavaDoc setter = c.getMethod("setSomething", new Class JavaDoc[] { String JavaDoc.class });
89          
90          new MBeanAttributeInfo JavaDoc("Something", "a description", setter, getter);
91          
92          // shouldn't reach here
93
fail("Introspection exception should have been thrown.");
94       }
95       catch (IntrospectionException JavaDoc e)
96       {
97          // this is expected
98
}
99       catch (AssertionFailedError e)
100       {
101          throw e;
102       }
103       catch (Throwable JavaDoc t)
104       {
105          t.printStackTrace();
106          fail("Unexpected error: " + t.toString());
107       }
108    }
109    
110    /**
111     * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> with invalid getter method.
112     */

113    public void testConstructorWithInvalidGetterMethod()
114    {
115       try
116       {
117          Class JavaDoc c = Trivial.class;
118          Method JavaDoc getter = c.getMethod("getSomethingInvalid", new Class JavaDoc[] { Object JavaDoc.class });
119          Method JavaDoc setter = c.getMethod("setSomethingInvalid", new Class JavaDoc[] { String JavaDoc.class });
120          
121          new MBeanAttributeInfo JavaDoc("Something", "a description", getter, setter);
122          
123          // shouldn't reach here
124
fail("Introspection exception should have been thrown.");
125       }
126       catch (IntrospectionException JavaDoc e)
127       {
128          // this is expected
129
}
130       catch (AssertionFailedError e)
131       {
132          throw e;
133       }
134       catch (Throwable JavaDoc t)
135       {
136          t.printStackTrace();
137          fail("Unexpected error: " + t.toString());
138       }
139    }
140    
141    /**
142     * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> with invalid getter method (void return type).
143     */

144    public void testConstructorWithInvalidGetterMethod2()
145    {
146       try
147       {
148          Class JavaDoc c = Trivial.class;
149          Method JavaDoc getter = c.getMethod("getSomethingInvalid2", new Class JavaDoc[] { } );
150          Method JavaDoc setter = c.getMethod("setSomethingInvalid2", new Class JavaDoc[] { String JavaDoc.class });
151          
152          new MBeanAttributeInfo JavaDoc("Something", "a description", getter, setter);
153          
154          // shouldn't reach here
155
fail("Introspection exception should have been thrown.");
156       }
157       catch (IntrospectionException JavaDoc e)
158       {
159          // this is expected
160
}
161       catch (AssertionFailedError e)
162       {
163          throw e;
164       }
165       catch (Throwable JavaDoc t)
166       {
167          t.printStackTrace();
168          fail("Unexpected error: " + t.toString());
169       }
170    }
171
172    public void testConstructorWithNonBooleanIsIs()
173       throws Exception JavaDoc
174    {
175       try
176       {
177          new MBeanAttributeInfo JavaDoc("name", "type", "description", true, true, true);
178       }
179       catch (Exception JavaDoc e)
180       {
181          return;
182       }
183       fail("isIs is only allowed for boolean types");
184    }
185
186    public void testConstructorWithPrimitiveBooleanIsIs()
187       throws Exception JavaDoc
188    {
189       new MBeanAttributeInfo JavaDoc("name", Boolean.TYPE.getName(), "description", true, true, true);
190    }
191
192    public void testConstructorWithObjectBooleanIsIs()
193       throws Exception JavaDoc
194    {
195       new MBeanAttributeInfo JavaDoc("name", Boolean JavaDoc.class.getName(), "description", true, true, true);
196    }
197
198    public void testHashCode()
199       throws Exception JavaDoc
200    {
201       MBeanAttributeInfo JavaDoc info1 = new MBeanAttributeInfo JavaDoc("name", "type", "description", true, true, false);
202       MBeanAttributeInfo JavaDoc info2 = new MBeanAttributeInfo JavaDoc("name", "type", "description", true, true, false);
203
204       assertTrue("Different instances with the same hashcode are equal", info1.hashCode() == info2.hashCode());
205    }
206
207    public void testEquals()
208       throws Exception JavaDoc
209    {
210       MBeanAttributeInfo JavaDoc info = new MBeanAttributeInfo JavaDoc("name", "type", "description", true, true, false);
211
212       assertTrue("Null should not be equal", info.equals(null) == false);
213       assertTrue("Only MBeanAttributeInfo should be equal", info.equals(new Object JavaDoc()) == false);
214
215       MBeanAttributeInfo JavaDoc info2 = new MBeanAttributeInfo JavaDoc("name", "type", "description", true, true, false);
216
217       assertTrue("Different instances of the same data are equal", info.equals(info2));
218       assertTrue("Different instances of the same data are equal", info2.equals(info));
219
220       info2 = new MBeanAttributeInfo JavaDoc("name2", "type", "description", true, true, false);
221
222       assertTrue("Different instances with different names are not equal", info.equals(info2) == false);
223       assertTrue("Different instances with different names are not equal", info2.equals(info) == false);
224
225       info2 = new MBeanAttributeInfo JavaDoc("name", "type2", "description", true, true, false);
226
227       assertTrue("Different instances with different types are not equal", info.equals(info2) == false);
228       assertTrue("Different instances with different types are not equal", info2.equals(info) == false);
229
230       info2 = new MBeanAttributeInfo JavaDoc("name", "type", "description2", true, true, false);
231
232       assertTrue("Different instances with different descriptions are not equal", info.equals(info2) == false);
233       assertTrue("Different instances with different descritpions are not equal", info2.equals(info) == false);
234
235       info2 = new MBeanAttributeInfo JavaDoc("name", "type", "description", false, true, false);
236
237       assertTrue("Different instances with different readables are not equal", info.equals(info2) == false);
238       assertTrue("Different instances with different readables are not equal", info2.equals(info) == false);
239
240       info2 = new MBeanAttributeInfo JavaDoc("name", "type", "description", true, false, false);
241
242       assertTrue("Different instances with different writables are not equal", info.equals(info2) == false);
243       assertTrue("Different instances with different writables are not equal", info2.equals(info) == false);
244
245       info2 = new MBeanAttributeInfo JavaDoc("name", Boolean.TYPE.getName(), "description", true, true, true);
246
247       assertTrue("Different instances with different isIs are not equal", info.equals(info2) == false);
248       assertTrue("Different instances with different isIs are not equal", info2.equals(info) == false);
249    }
250 }
251
Popular Tags