KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import javax.management.openmbean.OpenDataException;
18 import javax.management.openmbean.OpenType;
19
20 import junit.framework.TestCase;
21
22 /**
23  * Open type tests.<p>
24  *
25  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
26  */

27 public class OpenTypeTestCase
28   extends TestCase
29 {
30    // Attributes ----------------------------------------------------------------
31

32    // Constructor ---------------------------------------------------------------
33

34    /**
35     * Construct the test
36     */

37    public OpenTypeTestCase(String s)
38    {
39       super(s);
40    }
41
42    // Tests ---------------------------------------------------------------------
43

44    /**
45     * Test Allowed Classes.
46     */

47    public void testAllowedClasses()
48       throws Exception
49    {
50       String[] allowedClassNames = OpenType.ALLOWED_CLASSNAMES;
51       assertEquals(16, allowedClassNames.length);
52       List names = Arrays.asList(allowedClassNames);
53       checkOpenType(names, java.lang.Void.class);
54       checkOpenType(names, java.lang.Boolean.class);
55       checkOpenType(names, java.lang.Character.class);
56       checkOpenType(names, java.lang.Byte.class);
57       checkOpenType(names, java.lang.Short.class);
58       checkOpenType(names, java.lang.Integer.class);
59       checkOpenType(names, java.lang.Long.class);
60       checkOpenType(names, java.lang.Float.class);
61       checkOpenType(names, java.lang.Double.class);
62       checkOpenType(names, java.lang.String.class);
63       checkOpenType(names, java.util.Date.class);
64       checkOpenType(names, java.math.BigDecimal.class);
65       checkOpenType(names, java.math.BigInteger.class);
66       checkOpenType(names, javax.management.ObjectName.class);
67       checkOpenType(names, javax.management.openmbean.CompositeData.class);
68       checkOpenType(names, javax.management.openmbean.TabularData.class);
69    }
70
71    public void testConstructorSimple()
72       throws Exception
73    {
74       OpenType test = new MyOpenType("java.lang.Void", "type", "description");
75       assertEquals("java.lang.Void", test.getClassName());
76       assertEquals("type", test.getTypeName());
77       assertEquals("description", test.getDescription());
78       assertEquals(false, test.isArray());
79    }
80
81    public void testConstructorArray()
82       throws Exception
83    {
84       OpenType test = new MyOpenType("[[Ljava.lang.Void;", "type", "description");
85       assertEquals("[[Ljava.lang.Void;", test.getClassName());
86       assertEquals("type", test.getTypeName());
87       assertEquals("description", test.getDescription());
88       assertEquals(true, test.isArray());
89    }
90
91    public void testSerializationSimple()
92       throws Exception
93    {
94       testSerialization("java.lang.Void", "type", "description");
95    }
96  
97    public void testSerializationArray()
98       throws Exception
99    {
100       testSerialization("[[Ljava.lang.Void;", "type", "description");
101    }
102
103    /**
104     * Test Errors.
105     */

106    public void testErrors()
107       throws Exception
108    {
109       boolean caught = false;
110       try
111       {
112          new MyOpenType(null, "dummy", "dummy");
113       }
114       catch (IllegalArgumentException e)
115       {
116          caught = true;
117       }
118       assertTrue("className cannot be null", caught);
119
120       caught = false;
121       try
122       {
123          new MyOpenType("", "dummy", "dummy");
124       }
125       catch (IllegalArgumentException e)
126       {
127          caught = true;
128       }
129       assertTrue("className cannot be empty", caught);
130
131       caught = false;
132       try
133       {
134          new MyOpenType("java.lang.Void", null, "dummy");
135       }
136       catch (IllegalArgumentException e)
137       {
138          caught = true;
139       }
140       assertTrue("typeName cannot be null", caught);
141
142       caught = false;
143       try
144       {
145          new MyOpenType("java.lang.Void", null, "dummy");
146       }
147       catch (IllegalArgumentException e)
148       {
149          caught = true;
150       }
151       assertTrue("typeName cannot be empty", caught);
152
153       caught = false;
154       try
155       {
156          new MyOpenType("java.lang.Void", "dummy", null);
157       }
158       catch (IllegalArgumentException e)
159       {
160          caught = true;
161       }
162       assertTrue("description cannot be null", caught);
163
164       caught = false;
165       try
166       {
167          new MyOpenType("java.lang.Void", "dummy", "");
168       }
169       catch (IllegalArgumentException e)
170       {
171          caught = true;
172       }
173       assertTrue("description cannot be empty", caught);
174
175       caught = false;
176       try
177       {
178          new MyOpenType("java.lang.Class", "dummy", "dummy");
179       }
180       catch (OpenDataException e)
181       {
182          caught = true;
183       }
184       assertTrue("className must be an OpenDataType", caught);
185
186       caught = false;
187       try
188       {
189          new MyOpenType("[Ljava.lang.Void", "dummy", "dummy");
190       }
191       catch (OpenDataException e)
192       {
193          caught = true;
194       }
195       assertTrue("[Ljava.lang.Void is not a valid array", caught);
196    }
197
198    /**
199     * Test Errors that fail in the RI.
200     */

201    public void testErrors2()
202       throws Exception
203    {
204       boolean caught = false;
205       try
206       {
207          new MyOpenType("[L", "dummy", "dummy");
208       }
209       catch (StringIndexOutOfBoundsException e)
210       {
211          fail("FAILS IN RI: [L open type should be an OpenDataException not a StringIndexOutOfBoundsException");
212       }
213       catch (OpenDataException e)
214       {
215          caught = true;
216       }
217       assertTrue("[L is not a valid array", caught);
218    }
219
220    /**
221     * Test Errors that fail in the RI.
222     */

223    public void testErrors3()
224       throws Exception
225    {
226       boolean caught = false;
227       try
228       {
229          new MyOpenType("[Xjava.lang.Void;", "dummy", "dummy");
230       }
231       catch (OpenDataException e)
232       {
233          caught = true;
234       }
235       assertTrue("FAILS IN RI: [Xjava.lang.Void; is not a valid array", caught);
236    }
237
238    // Support -------------------------------------------------------------------
239

240    private void checkOpenType(List names, Class clazz)
241       throws Exception
242    {
243       String name = clazz.getName();
244       assertTrue(name + " is an OpenType", names.contains(name));
245
246       new MyOpenType(name, "dummy", "dummy");
247
248       new MyOpenType("[L"+name+";", "dummy", "dummy");
249
250       new MyOpenType("[[[[[L"+name+";", "dummy", "dummy");
251    }
252
253    private void testSerialization(String className, String type, String description)
254       throws Exception
255    {
256       // Create the role
257
OpenType original = new MyOpenType(className, type, description);
258
259       // Serialize it
260
ByteArrayOutputStream baos = new ByteArrayOutputStream();
261       ObjectOutputStream oos = new ObjectOutputStream(baos);
262       oos.writeObject(original);
263     
264       // Deserialize it
265
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
266       ObjectInputStream ois = new ObjectInputStream(bais);
267       OpenType result = (OpenType) ois.readObject();
268
269       // Did it work?
270
assertEquals(original.getClassName(), result.getClassName());
271       assertEquals(original.getTypeName(), result.getTypeName());
272       assertEquals(original.getDescription(), result.getDescription());
273       assertEquals(original.isArray(), result.isArray());
274    }
275
276    public static class MyOpenType
277       extends OpenType
278    {
279       public MyOpenType(String className, String typeName, String description)
280          throws OpenDataException
281       {
282          super(className, typeName, description);
283       }
284       public boolean equals(Object other)
285       {
286          throw new UnsupportedOperationException("irrelevent");
287       }
288       public int hashCode()
289       {
290          throw new UnsupportedOperationException("irrelevent");
291       }
292       public boolean isValue(Object other)
293       {
294          throw new UnsupportedOperationException("irrelevent");
295       }
296       public String toString()
297       {
298          throw new UnsupportedOperationException("irrelevent");
299       }
300    }
301 }
302
Popular Tags