KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
31
32 import javax.management.openmbean.CompositeData JavaDoc;
33 import javax.management.openmbean.CompositeDataSupport JavaDoc;
34 import javax.management.openmbean.CompositeType JavaDoc;
35 import javax.management.openmbean.OpenDataException JavaDoc;
36 import javax.management.openmbean.OpenType JavaDoc;
37 import javax.management.openmbean.SimpleType JavaDoc;
38
39 /**
40  * Composite type tests.<p>
41  *
42  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
43  */

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

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

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

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

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

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