KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > openmbean > CompositeTypeTest


1 /**
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8 package test.javax.management.openmbean;
9
10 import java.io.IOException JavaDoc;
11 import javax.management.openmbean.CompositeType JavaDoc;
12 import javax.management.openmbean.OpenDataException JavaDoc;
13 import javax.management.openmbean.OpenType JavaDoc;
14 import javax.management.openmbean.SimpleType JavaDoc;
15
16 import junit.framework.TestCase;
17 import test.javax.management.compliance.serialization.support.Serializer;
18
19 /**
20  * @version $Revision: 1.12 $
21  */

22 public class CompositeTypeTest extends TestCase
23 {
24    private String JavaDoc[] itemNames = null;
25    private String JavaDoc[] itemDescriptions = null;
26    private OpenType JavaDoc[] itemTypes;
27    private String JavaDoc[] indexNames;
28    private CompositeType JavaDoc tShirtType;
29
30    public CompositeTypeTest(String JavaDoc s)
31    {
32       super(s);
33    }
34
35    protected void setUp() throws Exception JavaDoc
36    {
37       super.setUp();
38       itemNames = new String JavaDoc[]{"model", "color", "size", "price"};
39       itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
40       itemTypes = new OpenType JavaDoc[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT};
41       indexNames = new String JavaDoc[]{"model", "color", "size"};
42       tShirtType = new CompositeType JavaDoc("tShirt", "a TShirt", itemNames, itemDescriptions, itemTypes);
43    }
44
45    protected void tearDown() throws Exception JavaDoc
46    {
47       super.tearDown();
48    }
49
50    public void testCreation()
51    {
52       assertTrue(tShirtType != null);
53    }
54
55    public void testInvalidCreation()
56    {
57       try
58       {
59          // duplicate names
60
itemNames = new String JavaDoc[]{"model", "color", "size", "model"};
61          tShirtType = new CompositeType JavaDoc("tShirt", "a TShirt", itemNames, itemDescriptions, itemTypes);
62          fail("Expect exception, invalid itemDescriptions (not the same size as itemNames - we should not see this");
63       }
64       catch (OpenDataException JavaDoc e)
65       {
66       }
67
68       try
69       {
70          // empty typeName
71
String JavaDoc[] itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
72          tShirtType = new CompositeType JavaDoc("", "a TShirt", itemNames, itemDescriptions, itemTypes);
73          fail("No exception thrown");
74       }
75       catch (IllegalArgumentException JavaDoc x)
76       {
77          assertTrue(true); // success
78
}
79       catch (Exception JavaDoc x)
80       {
81          fail("Expecting IllegalArgumentException");
82       }
83
84       try
85       {
86          // null typeName
87
String JavaDoc[] itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
88          tShirtType = new CompositeType JavaDoc(null, "a TShirt", itemNames, itemDescriptions, itemTypes);
89          fail("No exception thrown");
90       }
91       catch (IllegalArgumentException JavaDoc x)
92       {
93          assertTrue(true); // success
94
}
95       catch (Exception JavaDoc x)
96       {
97          fail("Expecting IllegalArgumentException");
98       }
99
100       try
101       {
102          // empty description
103
String JavaDoc[] itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
104          tShirtType = new CompositeType JavaDoc("tShirt", "", itemNames, itemDescriptions, itemTypes);
105          fail("No exception thrown");
106       }
107       catch (IllegalArgumentException JavaDoc x)
108       {
109          assertTrue(true); // success
110
}
111       catch (Exception JavaDoc x)
112       {
113          fail("Expecting IllegalArgumentException");
114       }
115
116       try
117       {
118          // null description
119
String JavaDoc[] itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
120          tShirtType = new CompositeType JavaDoc("tShirt", null, itemNames, itemDescriptions, itemTypes);
121          fail("No exception thrown");
122       }
123       catch (IllegalArgumentException JavaDoc x)
124       {
125          assertTrue(true); // success
126
}
127       catch (Exception JavaDoc x)
128       {
129          fail("Expecting IllegalArgumentException");
130       }
131
132       try
133       {
134          // null itemName entry
135
String JavaDoc[] itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
136          itemNames = new String JavaDoc[]{"model", null, "size", "price"};
137          tShirtType = new CompositeType JavaDoc("tShirt", "", itemNames, itemDescriptions, itemTypes);
138          fail("No exception thrown");
139       }
140       catch (IllegalArgumentException JavaDoc x)
141       {
142          assertTrue(true); // success
143
}
144       catch (Exception JavaDoc x)
145       {
146          fail("Expecting IllegalArgumentException");
147       }
148
149       try
150       {
151          // empty itemName entry
152
String JavaDoc[] itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"};
153          itemNames = new String JavaDoc[]{"model", "color", "", "price"};
154          tShirtType = new CompositeType JavaDoc("tShirt", "", itemNames, itemDescriptions, itemTypes);
155          fail("No exception thrown");
156       }
157       catch (IllegalArgumentException JavaDoc x)
158       {
159          assertTrue(true); // success
160
}
161       catch (Exception JavaDoc x)
162       {
163          fail("Expecting IllegalArgumentException");
164       }
165
166       try
167       {
168          // null itemDescription entry
169
String JavaDoc[] itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", null, "TShirt's price"};
170          tShirtType = new CompositeType JavaDoc("tShirt", "", itemNames, itemDescriptions, itemTypes);
171          fail("No exception thrown");
172       }
173       catch (IllegalArgumentException JavaDoc x)
174       {
175          assertTrue(true); // success
176
}
177       catch (Exception JavaDoc x)
178       {
179          fail("Expecting IllegalArgumentException");
180       }
181
182       try
183       {
184          // empty itemDescription entry
185
String JavaDoc[] itemDescriptions = new String JavaDoc[]{"TShirt's model name", "", "TShirt's size", "TShirt's price"};
186          tShirtType = new CompositeType JavaDoc("tShirt", "", itemNames, itemDescriptions, itemTypes);
187          fail("No exception thrown");
188       }
189       catch (IllegalArgumentException JavaDoc x)
190       {
191          assertTrue(true); // success
192
}
193       catch (Exception JavaDoc x)
194       {
195          fail("Expecting IllegalArgumentException");
196       }
197
198       try
199       {
200          // null itemTypes
201
tShirtType = new CompositeType JavaDoc("tShirt", "a TShirt", itemNames, itemDescriptions, null);
202          fail("No exception thrown");
203       }
204       catch (IllegalArgumentException JavaDoc x)
205       {
206          assertTrue(true); // success
207
}
208       catch (Exception JavaDoc x)
209       {
210          fail("Expecting IllegalArgumentException");
211       }
212
213       try
214       {
215          // mismatched lengths
216
String JavaDoc[] itemDescriptions = new String JavaDoc[]{"TShirt's model name", "TShirt's color", "TShirt's price"};
217          itemNames = new String JavaDoc[]{"model", "color", "size", "price"};
218          tShirtType = new CompositeType JavaDoc("tShirt", "a TShirt", itemNames, itemDescriptions, itemTypes);
219          fail("No exception thrown");
220       }
221       catch (IllegalArgumentException JavaDoc x)
222       {
223          assertTrue(true); // success
224
}
225       catch (Exception JavaDoc x)
226       {
227          fail("Expecting IllegalArgumentException");
228       }
229
230    }
231
232    public void testContainsKey()
233    {
234       boolean valid = tShirtType.containsKey("model");
235       assertTrue(valid);
236
237       // test fail for same name different case
238
assertTrue(tShirtType.containsKey("Model") == false);
239    }
240
241    public void testGetDescription()
242    {
243       String JavaDoc expected = "TShirt's color";
244       String JavaDoc result = tShirtType.getDescription("color");
245       assertTrue(expected == result);
246    }
247
248    public void testGetType()
249    {
250       OpenType JavaDoc expected = SimpleType.FLOAT;
251       OpenType JavaDoc result = tShirtType.getType("price");
252       assertEquals(expected, result);
253    }
254
255    public void testSerialization()
256    {
257       // write out
258

259       try
260       {
261          Serializer serializer = new Serializer();
262          byte[] data = serializer.serialize(tShirtType);
263          Object JavaDoc obj = serializer.deserialize(data);
264          // assert instanceof
265
assertTrue(obj instanceof CompositeType JavaDoc);
266
267          // if instanceof passes continue otherwise we will not get to the rest
268
CompositeType JavaDoc type = (CompositeType JavaDoc)obj;
269          // assert hashcodes are equal
270
assertEquals(type.hashCode(), tShirtType.hashCode());
271          assertTrue(type.getType("price").equals(SimpleType.FLOAT));
272          assertEquals(type.getType("size"), tShirtType.getType("size"));
273          assertEquals(type.getDescription("model"), tShirtType.getDescription("model"));
274          assertEquals(type.keySet(), tShirtType.keySet());
275       }
276       catch (IOException JavaDoc e)
277       {
278          e.printStackTrace();
279       }
280       catch (ClassNotFoundException JavaDoc e)
281       {
282       }
283    }
284
285    public void testHashCode() throws Exception JavaDoc
286    {
287       int ehc = 0;
288       ehc += tShirtType.getTypeName().hashCode();
289       for (int i = 0; i < itemNames.length; i++)
290       {
291          ehc += itemNames[i].hashCode();
292          ehc += itemTypes[i].hashCode();
293       }
294       int hc = tShirtType.hashCode();
295       assertTrue("Unexpected hashcode", hc == ehc);
296    }
297
298    public void testEquals() throws Exception JavaDoc
299    {
300       CompositeType JavaDoc undershirt =
301               new CompositeType JavaDoc("UnderShirt",
302                                 "a TShirt",
303                                 itemNames,
304                                 itemDescriptions,
305                                 itemTypes);
306       assertFalse("tShirtType and undershirt should not be equal",
307                   undershirt.equals(tShirtType));
308       String JavaDoc[] italianDescriptions =
309               {"modello", "nome", "colore", "prezzo"};
310       CompositeType JavaDoc italianshirt =
311               new CompositeType JavaDoc("UnderShirt",
312                                 "una Camicia",
313                                 itemNames,
314                                 italianDescriptions,
315                                 itemTypes);
316       assertTrue("undershirt and italianshirt should be equal",
317                  italianshirt.equals(undershirt));
318    }
319 }
320
Popular Tags