KickJava   Java API By Example, From Geeks To Geeks.

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


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
31 import javax.management.openmbean.ArrayType JavaDoc;
32 import javax.management.openmbean.CompositeDataSupport JavaDoc;
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.TabularType JavaDoc;
38 import javax.management.openmbean.TabularDataSupport JavaDoc;
39
40 /**
41  * Array type tests.<p>
42  *
43  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
44  */

45 public class ArrayTypeTestCase
46   extends TestCase
47 {
48    // Static --------------------------------------------------------------------
49

50    // Attributes ----------------------------------------------------------------
51

52    // Constructor ---------------------------------------------------------------
53

54    /**
55     * Construct the test
56     */

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

64    public void testArrayTypeOpenType()
65       throws Exception JavaDoc
66    {
67       ArrayType JavaDoc arrayType = new ArrayType JavaDoc(3, SimpleType.STRING);
68       assertEquals("[[[Ljava.lang.String;", arrayType.getClassName());
69       assertEquals("3-dimension array of java.lang.String", arrayType.getDescription());
70       assertEquals("[[[Ljava.lang.String;", arrayType.getTypeName());
71       assertTrue("Composite type should be an array", arrayType.isArray());
72    }
73
74    public void testGetDimension()
75       throws Exception JavaDoc
76    {
77       ArrayType JavaDoc arrayType = new ArrayType JavaDoc(3, SimpleType.STRING);
78       assertTrue("Dimension should be 3", arrayType.getDimension() == 3);
79    }
80
81    public void testElementOpenType()
82       throws Exception JavaDoc
83    {
84       ArrayType JavaDoc arrayType = new ArrayType JavaDoc(3, SimpleType.STRING);
85       assertTrue("Element OpenType should be " + SimpleType.STRING, arrayType.getElementOpenType().equals(SimpleType.STRING));
86    }
87
88    public void testIsValue()
89       throws Exception JavaDoc
90    {
91       ArrayType JavaDoc arrayType = new ArrayType JavaDoc(3, SimpleType.STRING);
92
93       assertTrue("null is not a value of array type", arrayType.isValue(null) == false);
94       assertTrue("object is not a value of array type", arrayType.isValue(new Object JavaDoc()) == false);
95
96       String JavaDoc[][][] data = new String JavaDoc[1][2][3];
97       assertTrue("data should be a value of array type", arrayType.isValue(data));
98
99       String JavaDoc[][] data2 = new String JavaDoc[1][2];
100       assertTrue("data should not be a value of array type, wrong number of dimensions", arrayType.isValue(data2) == false);
101
102       Object JavaDoc[][][] data3 = new Object JavaDoc[1][2][3];
103       assertTrue("data should not be a value of array type, wrong element type", arrayType.isValue(data3) == false);
104
105       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
106       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
107       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
108       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
109          itemNames, itemDescriptions, itemTypes);
110       Object JavaDoc[] itemValues = new Object JavaDoc[] { "string", new Integer JavaDoc(1) };
111       CompositeDataSupport JavaDoc cds = new CompositeDataSupport JavaDoc(compositeType, itemNames, itemValues);
112       CompositeDataSupport JavaDoc[][] compData1 = new CompositeDataSupport JavaDoc[][]
113       {
114          { cds, null }, { cds, cds }
115       };
116
117       ArrayType JavaDoc compArrayType1 = new ArrayType JavaDoc(2, compositeType);
118       assertTrue("compData1 should be a value of array type", compArrayType1.isValue(compData1));
119
120       ArrayType JavaDoc compArrayType2 = new ArrayType JavaDoc(1, compositeType);
121       assertTrue("compData1 should not be a value of array type, wrong dimension", compArrayType2.isValue(compData1) == false);
122
123       CompositeType JavaDoc compositeType2 = new CompositeType JavaDoc("typeName2", "description",
124          itemNames, itemDescriptions, itemTypes);
125       ArrayType JavaDoc compArrayType3 = new ArrayType JavaDoc(2, compositeType2);
126       assertTrue("compData1 should not be a value of array type, wrong element type", compArrayType3.isValue(compData1) == false);
127
128       TabularType JavaDoc tabularType = new TabularType JavaDoc("typeName", "description", compositeType, new String JavaDoc[] { "name1" });
129       TabularDataSupport JavaDoc tds = new TabularDataSupport JavaDoc(tabularType);
130       TabularDataSupport JavaDoc[][] tabData1 = new TabularDataSupport JavaDoc[][]
131       {
132          { tds, null }, { tds, tds }
133       };
134
135       ArrayType JavaDoc tabArrayType1 = new ArrayType JavaDoc(2, tabularType);
136       assertTrue("tabData1 should be a value of array type", tabArrayType1.isValue(tabData1));
137
138       ArrayType JavaDoc tabArrayType2 = new ArrayType JavaDoc(1, tabularType);
139       assertTrue("tabData1 should not be a value of array type, wrong number of dimensions", tabArrayType2.isValue(tabData1) == false);
140
141       TabularType JavaDoc tabularType2 = new TabularType JavaDoc("typeName2", "description", compositeType, new String JavaDoc[] { "name1" });
142       ArrayType JavaDoc tabArrayType3 = new ArrayType JavaDoc(2, tabularType2);
143       assertTrue("tabData1 should not be a value of array type, wrong element type", tabArrayType3.isValue(tabData1) == false);
144    }
145
146    public void testEquals()
147       throws Exception JavaDoc
148    {
149       ArrayType JavaDoc arrayType = new ArrayType JavaDoc(3, SimpleType.STRING);
150       assertTrue("null is not an array type", arrayType.equals(null) == false);
151       assertTrue("object is not an array type", arrayType.equals(new Object JavaDoc()) == false);
152
153       assertTrue("should be equal", arrayType.equals(arrayType));
154
155       ArrayType JavaDoc arrayType2 = new ArrayType JavaDoc(3, SimpleType.STRING);
156       assertTrue("should be equal, even though different instances", arrayType.equals(arrayType2));
157       assertTrue("should be equal, even though different instances", arrayType2.equals(arrayType));
158
159       arrayType2 = new ArrayType JavaDoc(2, SimpleType.STRING);
160       assertTrue("should not be equal, wrong number of dimensions", arrayType.equals(arrayType2) == false);
161       assertTrue("should not be equal, wrong number of dimensions", arrayType2.equals(arrayType) == false);
162
163       arrayType2 = new ArrayType JavaDoc(3, SimpleType.INTEGER);
164       assertTrue("should not be equal, wrong element type", arrayType.equals(arrayType2) == false);
165       assertTrue("should not be equal, wrong element type", arrayType2.equals(arrayType) == false);
166    }
167
168    public void testHashCode()
169       throws Exception JavaDoc
170    {
171       ArrayType JavaDoc arrayType = new ArrayType JavaDoc(3, SimpleType.STRING);
172
173       int myHashCode = 3 + SimpleType.STRING.hashCode();
174       assertTrue("Wrong hash code generated", myHashCode == arrayType.hashCode());
175    }
176
177    public void testToString()
178       throws Exception JavaDoc
179    {
180       ArrayType JavaDoc arrayType = new ArrayType JavaDoc(3, SimpleType.STRING);
181
182       String JavaDoc toString = arrayType.toString();
183
184       assertTrue("toString() should contain the array type class name",
185          toString.indexOf(ArrayType JavaDoc.class.getName()) != -1);
186       assertTrue("toString() should contain the dimension",
187          toString.indexOf("3") != -1);
188       assertTrue("toString() should contain the element type",
189          toString.indexOf(SimpleType.STRING.toString()) != -1);
190    }
191
192    public void testSerialization()
193       throws Exception JavaDoc
194    {
195       ArrayType JavaDoc arrayType = new ArrayType JavaDoc(3, SimpleType.STRING);
196
197       // Serialize it
198
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
199       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
200       oos.writeObject(arrayType);
201     
202       // Deserialize it
203
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
204       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
205       Object JavaDoc result = ois.readObject();
206
207       assertEquals(arrayType, result);
208    }
209
210    public void testErrors()
211       throws Exception JavaDoc
212    {
213       boolean caught = false;
214       try
215       {
216          new ArrayType JavaDoc(-1, SimpleType.STRING);
217       }
218       catch (IllegalArgumentException JavaDoc e)
219       {
220          caught = true;
221       }
222       if (caught == false)
223          fail("Excepted IllegalArgumentException for negative dimension");
224
225       caught = false;
226       try
227       {
228          new ArrayType JavaDoc(1, new ArrayType JavaDoc(2, SimpleType.STRING));
229       }
230       catch (OpenDataException JavaDoc e)
231       {
232          caught = true;
233       }
234       if (caught == false)
235          fail("Excepted OpenDataException for ArrayType element type");
236    }
237
238    public void testErrors2()
239       throws Exception JavaDoc
240    {
241       boolean caught = false;
242       try
243       {
244          new ArrayType JavaDoc(1, null);
245       }
246       catch (NullPointerException JavaDoc e)
247       {
248          fail("FAILS IN RI: expected IllegalArgumentException for null element type");
249       }
250       catch (IllegalArgumentException JavaDoc e)
251       {
252          caught = true;
253       }
254       if (caught == false)
255          fail("Excepted IllegalArgumentException for null element type");
256    }
257 }
258
Popular Tags