KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator;
17 import java.util.List;
18
19 import javax.management.openmbean.CompositeType;
20 import javax.management.openmbean.OpenDataException;
21 import javax.management.openmbean.OpenType;
22 import javax.management.openmbean.SimpleType;
23 import javax.management.openmbean.TabularData;
24 import javax.management.openmbean.TabularDataSupport;
25 import javax.management.openmbean.TabularType;
26
27 /**
28  * Tabular type tests.<p>
29  *
30  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
31  */

32 public class TabularTypeTestCase
33   extends TestCase
34 {
35    // Static --------------------------------------------------------------------
36

37    // Attributes ----------------------------------------------------------------
38

39    // Constructor ---------------------------------------------------------------
40

41    /**
42     * Construct the test
43     */

44    public TabularTypeTestCase(String s)
45    {
46       super(s);
47    }
48
49    // Tests ---------------------------------------------------------------------
50

51    public void testTabularTypeOpenType()
52       throws Exception
53    {
54       String[] itemNames = new String[] { "name1", "name2" };
55       String[] itemDescriptions = new String[] { "desc1", "desc2" };
56       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
57       CompositeType rowType = new CompositeType("rowTypeName", "rowDescription",
58          itemNames, itemDescriptions, itemTypes);
59
60       String[] indexNames = new String[] { "name1", "name2" };
61       TabularType tabularType = new TabularType("typeName", "description", rowType, indexNames);
62
63       assertEquals(TabularData.class.getName(), tabularType.getClassName());
64       assertEquals("description", tabularType.getDescription());
65       assertEquals("typeName", tabularType.getTypeName());
66       assertTrue("Tabular type should not be an array", tabularType.isArray() == false);
67    }
68
69    public void testGetRowType()
70       throws Exception
71    {
72       String[] itemNames = new String[] { "name1", "name2" };
73       String[] itemDescriptions = new String[] { "desc1", "desc2" };
74       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
75       CompositeType rowType = new CompositeType("rowTypeName", "rowDescription",
76          itemNames, itemDescriptions, itemTypes);
77
78       String[] indexNames = new String[] { "name1", "name2" };
79       TabularType tabularType = new TabularType("typeName", "description", rowType, indexNames);
80
81       assertEquals(rowType, tabularType.getRowType());
82    }
83
84    public void testIndexNames()
85       throws Exception
86    {
87       String[] itemNames = new String[] { "name1", "name2" };
88       String[] itemDescriptions = new String[] { "desc1", "desc2" };
89       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
90       CompositeType rowType = new CompositeType("rowTypeName", "rowDescription",
91          itemNames, itemDescriptions, itemTypes);
92
93       String[] indexNames = new String[] { "name1", "name2" };
94       TabularType tabularType = new TabularType("typeName", "description", rowType, indexNames);
95
96       List indexList = tabularType.getIndexNames();
97       assertTrue("wrong number of index names", indexList.size() == 2);
98       assertTrue("index list should contain name1", indexList.contains("name1"));
99       assertTrue("index list should contain name2", indexList.contains("name2"));
100       Iterator i = indexList.iterator();
101       assertTrue("first index is name1", i.next().equals("name1"));
102       assertTrue("second index is name2", i.next().equals("name2"));
103    }
104
105    public void testIsValue()
106       throws Exception
107    {
108       String[] itemNames = new String[] { "name1", "name2" };
109       String[] itemDescriptions = new String[] { "desc1", "desc2" };
110       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
111       CompositeType rowType = new CompositeType("rowTypeName", "rowDescription",
112          itemNames, itemDescriptions, itemTypes);
113
114       String[] indexNames = new String[] { "name1", "name2" };
115       TabularType tabularType = new TabularType("typeName", "description", rowType, indexNames);
116
117       assertTrue("null is not a value of tabular type", tabularType.isValue(null) == false);
118       assertTrue("object is not a value of tabular type", tabularType.isValue(new Object()) == false);
119
120       TabularDataSupport data = new TabularDataSupport(tabularType);
121       assertTrue("data should is a value", tabularType.isValue(data));
122
123       TabularType tabularType2 = new TabularType("typeName", "description", rowType, indexNames);
124       data = new TabularDataSupport(tabularType2);
125       assertTrue("data is a value, even though the tabular type is a different instance",
126                  tabularType.isValue(data));
127
128       tabularType2 = new TabularType("typeName2", "description", rowType, indexNames);
129       data = new TabularDataSupport(tabularType2);
130       assertTrue("data should not be a value, they have different type names",
131                  tabularType.isValue(data) == false);
132
133       CompositeType rowType2 = new CompositeType("rowTypeName2", "rowDescription",
134          itemNames, itemDescriptions, itemTypes);
135       tabularType2 = new TabularType("typeName", "description", rowType2, indexNames);
136       data = new TabularDataSupport(tabularType2);
137       assertTrue("data should not be a value, they have different row types",
138                  tabularType.isValue(data) == false);
139
140       String[] indexNames2 = new String[] { "name2", "name1" };
141       tabularType2 = new TabularType("typeName", "description", rowType, indexNames2);
142       data = new TabularDataSupport(tabularType2);
143       assertTrue("data should not be a value, they have different index names",
144                  tabularType.isValue(data) == false);
145    }
146
147    public void testEquals()
148       throws Exception
149    {
150       String[] itemNames = new String[] { "name1", "name2" };
151       String[] itemDescriptions = new String[] { "desc1", "desc2" };
152       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
153       CompositeType rowType = new CompositeType("rowTypeName", "rowDescription",
154          itemNames, itemDescriptions, itemTypes);
155
156       String[] indexNames = new String[] { "name1", "name2" };
157       TabularType tabularType = new TabularType("typeName", "description", rowType, indexNames);
158
159       assertTrue("null is not equal to tabular type", tabularType.equals(null) == false);
160       assertTrue("object is not a equal to tabular type", tabularType.equals(new Object()) == false);
161
162       TabularType tabularType2 = new TabularType("typeName", "description", rowType, indexNames);
163       assertTrue("Should be equal, even though the tabular type is a different instance",
164                  tabularType.equals(tabularType2));
165       assertTrue("Should be equal, even though the tabular type is a different instance",
166                  tabularType2.equals(tabularType));
167
168       tabularType2 = new TabularType("typeName2", "description", rowType, indexNames);
169       assertTrue("should not be equal, they have different type names",
170                  tabularType.equals(tabularType2) == false);
171       assertTrue("should not be equal, they have different type names",
172                  tabularType2.equals(tabularType) == false);
173
174       CompositeType rowType2 = new CompositeType("rowTypeName2", "rowDescription",
175          itemNames, itemDescriptions, itemTypes);
176       tabularType2 = new TabularType("typeName", "description", rowType2, indexNames);
177       assertTrue("should not be a equal, they have different row types",
178                  tabularType.equals(tabularType2) == false);
179       assertTrue("should not be a equal, they have different row types",
180                  tabularType2.equals(tabularType) == false);
181
182       String[] indexNames2 = new String[] { "name2", "name1" };
183       tabularType2 = new TabularType("typeName", "description", rowType, indexNames2);
184       assertTrue("should not be equal, they have different index names",
185                  tabularType.equals(tabularType2) == false);
186       assertTrue("should not be equal, they have different index names",
187                  tabularType2.equals(tabularType) == false);
188    }
189
190    public void testHashCode()
191       throws Exception
192    {
193       String[] itemNames = new String[] { "name1", "name2" };
194       String[] itemDescriptions = new String[] { "desc1", "desc2" };
195       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
196       CompositeType rowType = new CompositeType("rowTypeName", "rowDescription",
197          itemNames, itemDescriptions, itemTypes);
198
199       String[] indexNames = new String[] { "name1", "name2" };
200       TabularType tabularType = new TabularType("typeName", "description", rowType, indexNames);
201
202       int myHashCode = "typeName".hashCode() + rowType.hashCode()
203          + "name1".hashCode() + "name2".hashCode();
204       assertTrue("Wrong hash code generated", myHashCode == tabularType.hashCode());
205    }
206
207    public void testToString()
208       throws Exception
209    {
210       String[] itemNames = new String[] { "name1", "name2" };
211       String[] itemDescriptions = new String[] { "desc1", "desc2" };
212       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
213       CompositeType rowType = new CompositeType("rowTypeName", "rowDescription",
214          itemNames, itemDescriptions, itemTypes);
215
216       String[] indexNames = new String[] { "name1", "name2" };
217       TabularType tabularType = new TabularType("typeName", "description", rowType, indexNames);
218
219       String toString = tabularType.toString();
220
221       assertTrue("toString() should contain the tabular type class name",
222          toString.indexOf(TabularType.class.getName()) != -1);
223       assertTrue("toString() should contain the type name",
224          toString.indexOf("typeName") != -1);
225       assertTrue("toString() should contain the row type " + rowType,
226          toString.indexOf(rowType.toString()) != -1);
227       assertTrue("toString() should contain the index name1",
228          toString.indexOf("name1") != -1);
229       assertTrue("toString() should contain the index name2",
230          toString.indexOf("name2") != -1);
231    }
232
233    public void testSerialization()
234       throws Exception
235    {
236       String[] itemNames = new String[] { "name1", "name2" };
237       String[] itemDescriptions = new String[] { "desc1", "desc2" };
238       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
239       CompositeType rowType = new CompositeType("rowTypeName", "rowDescription",
240          itemNames, itemDescriptions, itemTypes);
241
242       String[] indexNames = new String[] { "name1", "name2" };
243       TabularType tabularType = new TabularType("typeName", "description", rowType, indexNames);
244
245       // Serialize it
246
ByteArrayOutputStream baos = new ByteArrayOutputStream();
247       ObjectOutputStream oos = new ObjectOutputStream(baos);
248       oos.writeObject(tabularType);
249     
250       // Deserialize it
251
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
252       ObjectInputStream ois = new ObjectInputStream(bais);
253       Object result = ois.readObject();
254
255       assertEquals(tabularType, result);
256    }
257
258    public void testErrors()
259       throws Exception
260    {
261       String[] itemNames = new String[] { "name1", "name2" };
262       String[] itemDescriptions = new String[] { "desc1", "desc2" };
263       OpenType[] itemTypes = new OpenType[] { SimpleType.STRING, SimpleType.INTEGER };
264       CompositeType rowType = new CompositeType("rowTypeName", "rowDescription",
265          itemNames, itemDescriptions, itemTypes);
266
267       String[] indexNames = new String[] { "name1", "name2" };
268
269       boolean caught = false;
270       try
271       {
272          TabularType tabularType = new TabularType(null, "description", rowType, indexNames);
273       }
274       catch (IllegalArgumentException e)
275       {
276          caught = true;
277       }
278       if (caught == false)
279          fail("Expected IllegalArgumentException for null type name");
280
281       caught = false;
282       try
283       {
284          TabularType tabularType = new TabularType("", "description", rowType, indexNames);
285       }
286       catch (IllegalArgumentException e)
287       {
288          caught = true;
289       }
290       if (caught == false)
291          fail("Expected IllegalArgumentException for empty type name");
292
293       caught = false;
294       try
295       {
296          TabularType tabularType = new TabularType("typeName", null, rowType, indexNames);
297       }
298       catch (IllegalArgumentException e)
299       {
300          caught = true;
301       }
302       if (caught == false)
303          fail("Expected IllegalArgumentException for null description");
304
305       caught = false;
306       try
307       {
308          TabularType tabularType = new TabularType("typeName", "", rowType, indexNames);
309       }
310       catch (IllegalArgumentException e)
311       {
312          caught = true;
313       }
314       if (caught == false)
315          fail("Expected IllegalArgumentException for empty description");
316
317       caught = false;
318       try
319       {
320          TabularType tabularType = new TabularType("typeName", "description", null, indexNames);
321       }
322       catch (IllegalArgumentException e)
323       {
324          caught = true;
325       }
326       if (caught == false)
327          fail("Expected IllegalArgumentException for null row type");
328
329       caught = false;
330       try
331       {
332          TabularType tabularType = new TabularType("typeName", "description", rowType, null);
333       }
334       catch (IllegalArgumentException e)
335       {
336          caught = true;
337       }
338       if (caught == false)
339          fail("Expected IllegalArgumentException for null index names");
340
341       caught = false;
342       try
343       {
344          TabularType tabularType = new TabularType("typeName", "description", rowType, new String[0]);
345       }
346       catch (IllegalArgumentException e)
347       {
348          caught = true;
349       }
350       if (caught == false)
351          fail("Expected IllegalArgumentException for empty index names");
352
353       caught = false;
354       try
355       {
356          TabularType tabularType = new TabularType("typeName", "description", rowType, new String[] { "name1", null });
357       }
358       catch (IllegalArgumentException e)
359       {
360          caught = true;
361       }
362       if (caught == false)
363          fail("Expected IllegalArgumentException for null index name element");
364
365       caught = false;
366       try
367       {
368          TabularType tabularType = new TabularType("typeName", "description", rowType, new String[] { "name1", "" });
369       }
370       catch (IllegalArgumentException e)
371       {
372          caught = true;
373       }
374       if (caught == false)
375          fail("Expected IllegalArgumentException for empty index name element");
376
377       caught = false;
378       try
379       {
380          TabularType tabularType = new TabularType("typeName", "description", rowType, new String[] { "name1", "nameX" });
381       }
382       catch (OpenDataException e)
383       {
384          caught = true;
385       }
386       if (caught == false)
387          fail("Expected OpenDataException for invalid index name");
388    }
389
390    // Support -------------------------------------------------------------------
391
}
392
Popular Tags