KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.management.openmbean.CompositeType JavaDoc;
34 import javax.management.openmbean.OpenDataException JavaDoc;
35 import javax.management.openmbean.OpenType JavaDoc;
36 import javax.management.openmbean.SimpleType JavaDoc;
37 import javax.management.openmbean.TabularData JavaDoc;
38 import javax.management.openmbean.TabularDataSupport JavaDoc;
39 import javax.management.openmbean.TabularType JavaDoc;
40
41 /**
42  * Tabular type tests.<p>
43  *
44  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
45  */

46 public class TabularTypeTestCase
47   extends TestCase
48 {
49    // Static --------------------------------------------------------------------
50

51    // Attributes ----------------------------------------------------------------
52

53    // Constructor ---------------------------------------------------------------
54

55    /**
56     * Construct the test
57     */

58    public TabularTypeTestCase(String JavaDoc s)
59    {
60       super(s);
61    }
62
63    // Tests ---------------------------------------------------------------------
64

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