KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
12 import junit.framework.AssertionFailedError;
13
14 import java.lang.reflect.Method;
15
16 import javax.management.MBeanAttributeInfo;
17 import javax.management.IntrospectionException;
18
19 import test.compliance.metadata.support.Trivial;
20
21 /**
22  * Tests MBeanAttributeInfo.
23  *
24  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
25  * @version $Revision: 1.4 $
26  */

27 public class MBeanAttributeInfoTEST extends TestCase
28 {
29    public MBeanAttributeInfoTEST(String s)
30    {
31       super(s);
32    }
33
34    /**
35     * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> constructor.
36     */

37    public void testConstructorWithAccessorMethods()
38    {
39       try
40       {
41          Class c = Trivial.class;
42          Method getter = c.getMethod("getSomething", new Class[0]);
43          Method setter = c.getMethod("setSomething", new Class[] { String.class });
44          
45          MBeanAttributeInfo info = new MBeanAttributeInfo("Something", "a description", getter, setter);
46          
47          assertTrue(info.getDescription().equals("a description"));
48          assertTrue(info.getName().equals("Something"));
49          assertTrue(info.getType().equals("java.lang.String"));
50          assertTrue(info.isReadable() == true);
51          assertTrue(info.isWritable() == true);
52          assertTrue(info.isIs() == false);
53       }
54       catch (AssertionFailedError e)
55       {
56          throw e;
57       }
58       catch (Throwable t)
59       {
60          t.printStackTrace();
61          fail("Unexpected error: " + t.toString());
62       }
63    }
64  
65    /**
66     * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> with misplaced accessor methods.
67     */

68    public void testConstructorWithMisplacedAccessorMethods()
69    {
70       try
71       {
72          Class c = Trivial.class;
73          Method getter = c.getMethod("getSomething", new Class[0]);
74          Method setter = c.getMethod("setSomething", new Class[] { String.class });
75          
76          MBeanAttributeInfo info = new MBeanAttributeInfo("Something", "a description", setter, getter);
77          
78          // shouldn't reach here
79
fail("Introspection exception should have been thrown.");
80       }
81       catch (IntrospectionException e)
82       {
83          // this is expected
84
}
85       catch (AssertionFailedError e)
86       {
87          throw e;
88       }
89       catch (Throwable t)
90       {
91          t.printStackTrace();
92          fail("Unexpected error: " + t.toString());
93       }
94    }
95    
96    /**
97     * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> with invalid getter method.
98     */

99    public void testConstructorWithInvalidGetterMethod()
100    {
101       try
102       {
103          Class c = Trivial.class;
104          Method getter = c.getMethod("getSomethingInvalid", new Class[] { Object.class });
105          Method setter = c.getMethod("setSomethingInvalid", new Class[] { String.class });
106          
107          MBeanAttributeInfo info = new MBeanAttributeInfo("Something", "a description", getter, setter);
108          
109          // shouldn't reach here
110
fail("Introspection exception should have been thrown.");
111       }
112       catch (IntrospectionException e)
113       {
114          // this is expected
115
}
116       catch (AssertionFailedError e)
117       {
118          throw e;
119       }
120       catch (Throwable t)
121       {
122          t.printStackTrace();
123          fail("Unexpected error: " + t.toString());
124       }
125    }
126    
127    /**
128     * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> with invalid getter method (void return type).
129     */

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