KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set;
17
18 import javax.management.openmbean.CompositeData;
19 import javax.management.openmbean.CompositeDataSupport;
20 import javax.management.openmbean.CompositeType;
21 import javax.management.openmbean.OpenDataException;
22 import javax.management.openmbean.OpenType;
23 import javax.management.openmbean.SimpleType;
24
25 /**
26  * Composite type tests.<p>
27  *
28  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
29  */

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

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

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

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

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

49    public void testCompositeTypeOpenType()
50       throws Exception
51    {
52       String[] itemNames = new String[] { "name1", "name2" };
53       String[] itemDescriptions = new String[] { "desc1", "desc2" };
54       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
55       CompositeType compositeType = new CompositeType("typeName", "description",
56          itemNames, itemDescriptions, itemTypes);
57       assertEquals(CompositeData.class.getName(), compositeType.getClassName());
58       assertEquals("description", compositeType.getDescription());
59       assertEquals("typeName", compositeType.getTypeName());
60       assertTrue("Composite type should not be an array", compositeType.isArray() == false);
61    }
62
63    public void testContainsKey()
64       throws Exception
65    {
66       String[] itemNames = new String[] { "name1", "name2" };
67       String[] itemDescriptions = new String[] { "desc1", "desc2" };
68       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
69       CompositeType compositeType = new CompositeType("typeName", "description",
70          itemNames, itemDescriptions, itemTypes);
71       assertTrue("Composite type should contain key name1", compositeType.containsKey("name1") == true);
72       assertTrue("Composite type should contain key name2", compositeType.containsKey("name2") == true);
73       assertTrue("Composite type should not contain key nameX", compositeType.containsKey("nameX") == false);
74       assertTrue("Composite type should not contain key null", compositeType.containsKey(null) == false);
75       assertTrue("Composite type should not contain key <empty>", compositeType.containsKey("") == false);
76    }
77
78    public void testGetDescriptionForItemName()
79       throws Exception
80    {
81       String[] itemNames = new String[] { "name1", "name2" };
82       String[] itemDescriptions = new String[] { "desc1", "desc2" };
83       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
84       CompositeType compositeType = new CompositeType("typeName", "description",
85          itemNames, itemDescriptions, itemTypes);
86       assertEquals("desc1", compositeType.getDescription("name1"));
87       assertEquals("desc2", compositeType.getDescription("name2"));
88    }
89
90    public void testGetTypeForItemName()
91       throws Exception
92    {
93       String[] itemNames = new String[] { "name1", "name2" };
94       String[] itemDescriptions = new String[] { "desc1", "desc2" };
95       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
96       CompositeType compositeType = new CompositeType("typeName", "description",
97          itemNames, itemDescriptions, itemTypes);
98       assertEquals(SimpleType.STRING, compositeType.getType("name1"));
99       assertEquals(SimpleType.INTEGER, compositeType.getType("name2"));
100    }
101
102    public void testKeySet()
103       throws Exception
104    {
105       String[] itemNames = new String[] { "name1", "name2" };
106       String[] itemDescriptions = new String[] { "desc1", "desc2" };
107       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
108       CompositeType compositeType = new CompositeType("typeName", "description",
109          itemNames, itemDescriptions, itemTypes);
110       Set keys = compositeType.keySet();
111       assertTrue("Should be 2 items", keys.size() == 2);
112       assertTrue("Should contain name1", keys.contains("name1"));
113       assertTrue("Should contain name2", keys.contains("name2"));
114    }
115
116    public void testIsValue()
117       throws Exception
118    {
119       String[] itemNames = new String[] { "name1", "name2" };
120       String[] itemDescriptions = new String[] { "desc1", "desc2" };
121       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
122       CompositeType compositeType = new CompositeType("typeName", "description",
123          itemNames, itemDescriptions, itemTypes);
124
125       assertTrue("null is not a value of composite type", compositeType.isValue(null) == false);
126       assertTrue("object is not a value of composite type", compositeType.isValue(new Object()) == false);
127
128       Object[] itemValues = new Object[] { "string", new Integer(2) };
129       CompositeDataSupport data = new CompositeDataSupport(compositeType, itemNames, itemValues);
130       assertTrue("data should be a value of composite type", compositeType.isValue(data));
131
132       CompositeType compositeType2 = new CompositeType("typeName", "description",
133          itemNames, itemDescriptions, itemTypes);
134       data = new CompositeDataSupport(compositeType2, itemNames, itemValues);
135       assertTrue("data should be a value of composite type, even though not the object instance",
136          compositeType.isValue(data));
137
138       OpenType[] itemTypes2 = new OpenType[] { SimpleType.STRING, SimpleType.LONG };
139       compositeType2 = new CompositeType("typeName", "description",
140          itemNames, itemDescriptions, itemTypes2);
141       Object[] itemValues2 = new Object[] { "string", new Long(2) };
142       data = new CompositeDataSupport(compositeType2, itemNames, itemValues2);
143       assertTrue("data should not be a value of composite type, it has different types",
144          compositeType.isValue(data) == false);
145
146       compositeType2 = new CompositeType("typeName2", "description",
147          itemNames, itemDescriptions, itemTypes);
148       data = new CompositeDataSupport(compositeType2, itemNames, itemValues);
149       assertTrue("data should not be a value of composite type, it has a different type name",
150          compositeType.isValue(data) == false);
151
152       String[] itemNames2 = new String[] { "nameX", "name2" };
153       compositeType2 = new CompositeType("typeName", "description",
154          itemNames2, itemDescriptions, itemTypes);
155       data = new CompositeDataSupport(compositeType2, itemNames2, itemValues);
156       assertTrue("data should not be a value of composite type, it has different item names",
157          compositeType.isValue(data) == false);
158    }
159
160    public void testEquals()
161       throws Exception
162    {
163       String[] itemNames = new String[] { "name1", "name2" };
164       String[] itemDescriptions = new String[] { "desc1", "desc2" };
165       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
166       CompositeType compositeType = new CompositeType("typeName", "description",
167          itemNames, itemDescriptions, itemTypes);
168
169       assertTrue("null is not equal composite type", compositeType.equals(null) == false);
170       assertTrue("object is not equal composite type", compositeType.equals(new Object()) == false);
171
172       CompositeType compositeType2 = new CompositeType("typeName", "description",
173          itemNames, itemDescriptions, itemTypes);
174       assertTrue("compositeType2 should be equal composite type, even though not the object instance",
175          compositeType.equals(compositeType2));
176       assertTrue("compositeType2 should be equal composite type, even though not the object instance",
177          compositeType2.equals(compositeType));
178
179       OpenType[] itemTypes2 = new OpenType[] { SimpleType.STRING, SimpleType.LONG };
180       compositeType2 = new CompositeType("typeName", "description",
181          itemNames, itemDescriptions, itemTypes2);
182       assertTrue("compositeType2 should not be equal composite type, it has different types",
183          compositeType.equals(compositeType2) == false);
184       assertTrue("compositeType2 should not be equal composite type, it has different types",
185          compositeType2.equals(compositeType) == false);
186
187       compositeType2 = new CompositeType("typeName2", "description",
188          itemNames, itemDescriptions, itemTypes);
189       assertTrue("compositeType2 should not be equal composite type, it has a different type name",
190          compositeType.equals(compositeType2) == false);
191       assertTrue("compositeType2 should not be equal composite type, it has a different type name",
192          compositeType2.equals(compositeType) == false);
193
194       String[] itemNames2 = new String[] { "nameX", "name2" };
195       compositeType2 = new CompositeType("typeName", "description",
196          itemNames2, itemDescriptions, itemTypes);
197       assertTrue("compositeType2 should not be equal composite type, it has different item names",
198          compositeType.equals(compositeType2) == false);
199       assertTrue("compositeType2 should not be equal composite type, it has different item names",
200          compositeType2.equals(compositeType) == false);
201    }
202
203    public void testHashCode()
204       throws Exception
205    {
206       String[] itemNames = new String[] { "name1", "name2" };
207       String[] itemDescriptions = new String[] { "desc1", "desc2" };
208       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
209       CompositeType compositeType = new CompositeType("typeName", "description",
210          itemNames, itemDescriptions, itemTypes);
211
212       int myHashCode = "typeName".hashCode() + SimpleType.STRING.hashCode() + SimpleType.INTEGER.hashCode()
213          + "name1".hashCode() + "name2".hashCode();
214       assertTrue("Wrong hash code generated", myHashCode == compositeType.hashCode());
215    }
216
217    public void testToString()
218       throws Exception
219    {
220       String[] itemNames = new String[] { "name1", "name2" };
221       String[] itemDescriptions = new String[] { "desc1", "desc2" };
222       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
223       CompositeType compositeType = new CompositeType("typeName", "description",
224          itemNames, itemDescriptions, itemTypes);
225
226       String toString = compositeType.toString();
227
228       assertTrue("toString() should contain the composite type class name",
229          toString.indexOf(CompositeType.class.getName()) != -1);
230       assertTrue("toString() should contain the item name name1",
231          toString.indexOf("name1") != -1);
232       assertTrue("toString() should contain the item name name2",
233          toString.indexOf("name2") != -1);
234       assertTrue("toString() should contain " + SimpleType.STRING,
235          toString.indexOf(SimpleType.STRING.toString()) != -1);
236       assertTrue("toString() should contain " + SimpleType.INTEGER,
237          toString.indexOf(SimpleType.INTEGER.toString()) != -1);
238    }
239
240    public void testSerialization()
241       throws Exception
242    {
243       String[] itemNames = new String[] { "name1", "name2" };
244       String[] itemDescriptions = new String[] { "desc1", "desc2" };
245       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
246       CompositeType compositeType = new CompositeType("typeName", "description",
247          itemNames, itemDescriptions, itemTypes);
248
249       // Serialize it
250
ByteArrayOutputStream baos = new ByteArrayOutputStream();
251       ObjectOutputStream oos = new ObjectOutputStream(baos);
252       oos.writeObject(compositeType);
253     
254       // Deserialize it
255
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
256       ObjectInputStream ois = new ObjectInputStream(bais);
257       Object result = ois.readObject();
258
259       assertEquals(compositeType, result);
260    }
261
262    public void testErrors()
263       throws Exception
264    {
265       String[] itemNames = new String[] { "name1", "name2" };
266       String[] itemDescriptions = new String[] { "desc1", "desc2" };
267       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
268
269       boolean caught = false;
270       try
271       {
272          new CompositeType(null, "description", itemNames, itemDescriptions, itemTypes);
273       }
274       catch (IllegalArgumentException e)
275       {
276          caught = true;
277       }
278       if (caught == false)
279          fail("Excepted IllegalArgumentException for null typeName");
280
281       caught = false;
282       try
283       {
284          new CompositeType("", "description", itemNames, itemDescriptions, itemTypes);
285       }
286       catch (IllegalArgumentException e)
287       {
288          caught = true;
289       }
290       if (caught == false)
291          fail("Excepted IllegalArgumentException for empty typeName");
292
293       caught = false;
294       try
295       {
296          new CompositeType("typeName", null, itemNames, itemDescriptions, itemTypes);
297       }
298       catch (IllegalArgumentException e)
299       {
300          caught = true;
301       }
302       if (caught == false)
303          fail("Excepted IllegalArgumentException for null description");
304
305       caught = false;
306       try
307       {
308          new CompositeType("typeName", "", itemNames, itemDescriptions, itemTypes);
309       }
310       catch (IllegalArgumentException e)
311       {
312          caught = true;
313       }
314       if (caught == false)
315          fail("Excepted IllegalArgumentException for empty description");
316
317       caught = false;
318       try
319       {
320          new CompositeType("typeName", "description", null, itemDescriptions, itemTypes);
321       }
322       catch (IllegalArgumentException e)
323       {
324          caught = true;
325       }
326       if (caught == false)
327          fail("Excepted IllegalArgumentException for null item names");
328
329       caught = false;
330       try
331       {
332          new CompositeType("typeName", "description", itemNames, null, itemTypes);
333       }
334       catch (IllegalArgumentException e)
335       {
336          caught = true;
337       }
338       if (caught == false)
339          fail("Excepted IllegalArgumentException for null item descriptions");
340
341       caught = false;
342       try
343       {
344          new CompositeType("typeName", "description", itemNames, itemDescriptions, null);
345       }
346       catch (IllegalArgumentException e)
347       {
348          caught = true;
349       }
350       if (caught == false)
351          fail("Excepted IllegalArgumentException for null item types");
352
353       String[] nullItemNames = new String[] { "name1", null };
354       caught = false;
355       try
356       {
357          new CompositeType("typeName", "description", nullItemNames, itemDescriptions, itemTypes);
358       }
359       catch (IllegalArgumentException e)
360       {
361          caught = true;
362       }
363       if (caught == false)
364          fail("Excepted IllegalArgumentException for null element of item names");
365
366       String[] nullItemDescriptions = new String[] { "desc1", null };
367       caught = false;
368       try
369       {
370          new CompositeType("typeName", "description", itemNames, nullItemDescriptions, itemTypes);
371       }
372       catch (IllegalArgumentException e)
373       {
374          caught = true;
375       }
376       if (caught == false)
377          fail("Excepted IllegalArgumentException for null element of item descriptions");
378
379       OpenType[] nullItemTypes = new OpenType[] { SimpleType.STRING, null };
380       caught = false;
381       try
382       {
383          new CompositeType("typeName", "description", itemNames, itemDescriptions, nullItemTypes);
384       }
385       catch (IllegalArgumentException e)
386       {
387          caught = true;
388       }
389       if (caught == false)
390          fail("Excepted IllegalArgumentException for null element of item types");
391
392       String[] wrongItemNames = new String[] { "name1" };
393       caught = false;
394       try
395       {
396          new CompositeType("typeName", "description", wrongItemNames, itemDescriptions, itemTypes);
397       }
398       catch (IllegalArgumentException e)
399       {
400          caught = true;
401       }
402       if (caught == false)
403          fail("Excepted IllegalArgumentException for wrong number of elements for item names");
404
405       String[] wrongItemDescriptions = new String[] { "desc1"};
406       caught = false;
407       try
408       {
409          new CompositeType("typeName", "description", itemNames, wrongItemDescriptions, itemTypes);
410       }
411       catch (IllegalArgumentException e)
412       {
413          caught = true;
414       }
415       if (caught == false)
416          fail("Excepted IllegalArgumentException for wrong number of elements for item descriptions");
417
418       OpenType[] wrongItemTypes = new OpenType[] { SimpleType.STRING };
419       caught = false;
420       try
421       {
422          new CompositeType("typeName", "description", itemNames, itemDescriptions, wrongItemTypes);
423       }
424       catch (IllegalArgumentException e)
425       {
426          caught = true;
427       }
428       if (caught == false)
429          fail("Excepted IllegalArgumentException for wrong number of elements for item types");
430
431       String[] duplicateItemNames = new String[] { "desc1", "desc1" };
432       caught = false;
433       try
434       {
435          new CompositeType("typeName", "description", duplicateItemNames, itemDescriptions, itemTypes);
436       }
437       catch (OpenDataException e)
438       {
439          caught = true;
440       }
441       if (caught == false)
442          fail("Excepted OpenDataException for duplicate item names");
443
444       duplicateItemNames = new String[] { "desc1", " desc1 " };
445       caught = false;
446       try
447       {
448          new CompositeType("typeName", "description", duplicateItemNames, itemDescriptions, itemTypes);
449       }
450       catch (OpenDataException e)
451       {
452          caught = true;
453       }
454       if (caught == false)
455          fail("Excepted OpenDataException for duplicate item names");
456    }
457
458    // Support -------------------------------------------------------------------
459
}
460
Popular Tags