KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > openmbean > OpenMBeanConstructorInfoSupportTestCase


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.openmbean;
23
24 import junit.framework.TestCase;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.management.openmbean.OpenMBeanParameterInfo JavaDoc;
34 import javax.management.openmbean.OpenMBeanParameterInfoSupport JavaDoc;
35 import javax.management.openmbean.OpenMBeanConstructorInfoSupport JavaDoc;
36 import javax.management.openmbean.OpenType JavaDoc;
37 import javax.management.openmbean.SimpleType JavaDoc;
38
39 /**
40  * Open MBean Constructor Info tests.<p>
41  *
42  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
43  */

44 public class OpenMBeanConstructorInfoSupportTestCase
45   extends TestCase
46 {
47    // Static --------------------------------------------------------------------
48

49    // Attributes ----------------------------------------------------------------
50

51    // Constructor ---------------------------------------------------------------
52

53    /**
54     * Construct the test
55     */

56    public OpenMBeanConstructorInfoSupportTestCase(String JavaDoc s)
57    {
58       super(s);
59    }
60
61    // Tests ---------------------------------------------------------------------
62

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