KickJava   Java API By Example, From Geeks To Geeks.

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


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.OpenMBeanConstructorInfoSupport;
22 import javax.management.openmbean.OpenType;
23 import javax.management.openmbean.SimpleType;
24
25 /**
26  * Open MBean Constructor Info tests.<p>
27  *
28  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
29  */

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

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

37    // Constructor ---------------------------------------------------------------
38

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

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

49    public void testOpenMBeanConstructorInfoSupport()
50       throws Exception
51    {
52       OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
53          "name", "description", null);
54       assertEquals("name", info.getName());
55       assertEquals("description", info.getDescription());
56       assertEquals(0, info.getSignature().length);
57
58       info = new OpenMBeanConstructorInfoSupport(
59          "name", "description", new OpenMBeanParameterInfoSupport[0]);
60       assertEquals("name", info.getName());
61       assertEquals("description", info.getDescription());
62       assertEquals(0, info.getSignature().length);
63
64       OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[]
65       {
66          new OpenMBeanParameterInfoSupport(
67             "name", "description", SimpleType.STRING)
68       };
69       info = new OpenMBeanConstructorInfoSupport(
70          "name", "description", parms);
71       assertEquals("name", info.getName());
72       assertEquals("description", info.getDescription());
73       assertEquals(1, info.getSignature().length);
74    }
75
76    public void testEquals()
77       throws Exception
78    {
79       OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
80          "name", "description", null);
81
82       assertTrue("Null should not be equal", info.equals(null) == false);
83       assertTrue("Only OpenMBeanConstructorInfo should be equal", info.equals(new Object()) == false);
84
85       OpenMBeanConstructorInfoSupport info2 = new OpenMBeanConstructorInfoSupport(
86          "name", "description", null);
87
88       assertTrue("Different instances of the same data are equal", info.equals(info2));
89       assertTrue("Different instances of the same data are equal", info2.equals(info));
90
91       info2 = new OpenMBeanConstructorInfoSupport(
92          "name", "description2", null);
93
94       assertTrue("Different instances with different descriptions are equal", info.equals(info2));
95       assertTrue("Different instances with different descritpions are equal", info2.equals(info));
96
97       info2 = new OpenMBeanConstructorInfoSupport(
98          "name2", "description", null);
99
100       assertTrue("Instances with different names are not equal", info.equals(info2) == false);
101       assertTrue("Instances with different names are not equal", info2.equals(info) == false);
102
103       OpenMBeanParameterInfoSupport param1 = new OpenMBeanParameterInfoSupport(
104          "name", "description", SimpleType.STRING);
105       OpenMBeanParameterInfoSupport param2 = new OpenMBeanParameterInfoSupport(
106          "name2", "description", SimpleType.STRING);
107
108       info = new OpenMBeanConstructorInfoSupport(
109          "name", "description", new OpenMBeanParameterInfoSupport[] { param1, param2 });
110       info2 = new OpenMBeanConstructorInfoSupport(
111          "name", "description", new OpenMBeanParameterInfoSupport[] { param1, param2 });
112
113       assertTrue("Different instances with the same parameters are equal", info.equals(info2));
114       assertTrue("Different instances with the same parameters are equal", info2.equals(info));
115
116       info = new OpenMBeanConstructorInfoSupport(
117          "name", "description", new OpenMBeanParameterInfoSupport[] { param1, param2 });
118       info2 = new OpenMBeanConstructorInfoSupport(
119          "name", "description", new OpenMBeanParameterInfoSupport[] { param2, param1 });
120
121       assertTrue("Different instances with the same signature but different parameters are not equal",
122          info.equals(info2) == false);
123       assertTrue("Different instances with the same signature but different parameters are not equal",
124          info2.equals(info) == false);
125
126       param2 = new OpenMBeanParameterInfoSupport(
127          "name2", "description", SimpleType.INTEGER);
128       info2 = new OpenMBeanConstructorInfoSupport(
129          "name", "description", new OpenMBeanParameterInfoSupport[] { param1, param2 });
130
131       assertTrue("Different instances with different signatures are not equal",
132          info.equals(info2) == false);
133       assertTrue("Different instances with different signatures are not equal",
134          info2.equals(info) == false);
135
136       info2 = new OpenMBeanConstructorInfoSupport(
137          "name", "description", new OpenMBeanParameterInfoSupport[] { param1 });
138
139       assertTrue("Different instances with different numbers of paramters are not equal",
140          info.equals(info2) == false);
141       assertTrue("Different instances with different numbers of parameters are not equal",
142          info2.equals(info) == false);
143    }
144
145    public void testHashCode()
146       throws Exception
147    {
148       OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[]
149       {
150          new OpenMBeanParameterInfoSupport(
151             "name", "description", SimpleType.STRING)
152       };
153       OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
154          "name", "description", parms);
155
156       int myHash = "name".hashCode() + Arrays.asList(parms).hashCode();
157       assertEquals(myHash, info.hashCode());
158    }
159
160    public void testToString()
161       throws Exception
162    {
163       OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[]
164       {
165          new OpenMBeanParameterInfoSupport(
166             "name", "description", SimpleType.STRING)
167       };
168       OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
169          "NAME", "DESCRIPTION", parms);
170
171       String toString = info.toString();
172
173       assertTrue("info.toString() should contain NAME",
174          toString.indexOf("NAME") != -1);
175       assertTrue("info.toString() should contain the parameters",
176          toString.indexOf(Arrays.asList(parms).toString()) != -1);
177    }
178
179    public void testSerialization()
180       throws Exception
181    {
182       OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[]
183       {
184          new OpenMBeanParameterInfoSupport(
185             "name", "description", SimpleType.STRING)
186       };
187       OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
188          "name", "description", parms);
189
190       // Serialize it
191
ByteArrayOutputStream baos = new ByteArrayOutputStream();
192       ObjectOutputStream oos = new ObjectOutputStream(baos);
193       oos.writeObject(info);
194     
195       // Deserialize it
196
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
197       ObjectInputStream ois = new ObjectInputStream(bais);
198       Object result = ois.readObject();
199
200       assertEquals(info, result);
201    }
202
203    public void testErrors()
204       throws Exception
205    {
206       boolean caught = false;
207       try
208       {
209          OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[]
210          {
211             new OpenMBeanParameterInfoSupport(
212                "name", "description", SimpleType.STRING)
213          };
214          OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
215             null, "description", parms);
216       }
217       catch (IllegalArgumentException e)
218       {
219          caught = true;
220       }
221       if (caught == false)
222          fail("Expected IllegalArgumentException for null name");
223
224       caught = false;
225       try
226       {
227          OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[]
228          {
229             new OpenMBeanParameterInfoSupport(
230                "name", "description", SimpleType.STRING)
231          };
232          OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
233             "", "description", parms);
234       }
235       catch (IllegalArgumentException e)
236       {
237          caught = true;
238       }
239       if (caught == false)
240          fail("Expected IllegalArgumentException for empty name");
241
242       caught = false;
243       try
244       {
245          OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[]
246          {
247             new OpenMBeanParameterInfoSupport(
248                "name", "description", SimpleType.STRING)
249          };
250          OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
251             "name", null, parms);
252       }
253       catch (IllegalArgumentException e)
254       {
255          caught = true;
256       }
257       if (caught == false)
258          fail("Expected IllegalArgumentException for null description");
259
260       caught = false;
261       try
262       {
263          OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[]
264          {
265             new OpenMBeanParameterInfoSupport(
266                "name", "description", SimpleType.STRING)
267          };
268          OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
269             "name", "", parms);
270       }
271       catch (IllegalArgumentException e)
272       {
273          caught = true;
274       }
275       if (caught == false)
276          fail("Expected IllegalArgumentException for empty description");
277
278       caught = false;
279       try
280       {
281          OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
282             "name", "description", new MyOpenMBeanParameterInfo[] { new MyOpenMBeanParameterInfo() });
283       }
284       catch (ArrayStoreException e)
285       {
286          caught = true;
287       }
288       if (caught == false)
289          fail("Expected ArrayStoreException for non MBeanParameterInfo array");
290    }
291
292    public static class MyOpenMBeanParameterInfo
293       implements OpenMBeanParameterInfo
294    {
295       public boolean equals(Object o) { return false; }
296       public Object getDefaultValue() { return null; }
297       public String getDescription() { return null; }
298       public Set getLegalValues() { return null; }
299       public Comparable getMaxValue() { return null; }
300       public Comparable getMinValue() { return null; }
301       public String getName() { return null; }
302       public OpenType getOpenType() { return null; }
303       public boolean hasDefaultValue() { return false; }
304       public boolean hasLegalValues() { return false; }
305       public int hashCode() { return 0; }
306       public boolean hasMaxValue() { return false; }
307       public boolean hasMinValue() { return false; }
308       public boolean isValue(Object o) { return false; }
309       public String toString() { return null; }
310    }
311 }
312
Popular Tags