KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 import javax.management.openmbean.CompositeDataSupport JavaDoc;
32 import javax.management.openmbean.CompositeType JavaDoc;
33 import javax.management.openmbean.InvalidKeyException JavaDoc;
34 import javax.management.openmbean.OpenDataException JavaDoc;
35 import javax.management.openmbean.OpenType JavaDoc;
36 import javax.management.openmbean.SimpleType JavaDoc;
37
38 import junit.framework.TestCase;
39
40 /**
41  * Composite data support tests.<p>
42  *
43  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
44  */

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

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

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

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

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

64    public void testCompositeDataSupport()
65       throws Exception JavaDoc
66    {
67       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
68       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
69       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
70       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
71          itemNames, itemDescriptions, itemTypes);
72       HashMap JavaDoc map = new HashMap JavaDoc();
73       map.put("name1", "value1");
74       map.put("name2", new Integer JavaDoc(2));
75       new CompositeDataSupport JavaDoc(compositeType, map);
76       new CompositeDataSupport JavaDoc(compositeType, new String JavaDoc[] { "name1", "name2" }, new Object JavaDoc[] { "value1", new Integer JavaDoc(2) });
77    }
78
79    public void testGetCompositeType()
80       throws Exception JavaDoc
81    {
82       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
83       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
84       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
85       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
86          itemNames, itemDescriptions, itemTypes);
87       HashMap JavaDoc map = new HashMap JavaDoc();
88       map.put("name1", "value1");
89       map.put("name2", new Integer JavaDoc(2));
90       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
91       assertEquals(compositeType, data.getCompositeType());
92    }
93
94    public void testGet()
95       throws Exception JavaDoc
96    {
97       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
98       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
99       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
100       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
101          itemNames, itemDescriptions, itemTypes);
102       HashMap JavaDoc map = new HashMap JavaDoc();
103       map.put("name1", "value1");
104       map.put("name2", new Integer JavaDoc(2));
105       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
106       assertEquals("value1", data.get("name1"));
107       assertEquals(new Integer JavaDoc(2), data.get("name2"));
108    }
109
110    public void testGetAll()
111       throws Exception JavaDoc
112    {
113       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
114       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
115       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
116       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
117          itemNames, itemDescriptions, itemTypes);
118       HashMap JavaDoc map = new HashMap JavaDoc();
119       map.put("name1", "value1");
120       map.put("name2", new Integer JavaDoc(2));
121       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
122       Object JavaDoc[] result = data.getAll(new String JavaDoc[] { "name1", "name2" });
123       assertEquals("value1", result[0]);
124       assertEquals(new Integer JavaDoc(2), result[1]);
125       result = data.getAll(new String JavaDoc[] { "name2", "name1" });
126       assertEquals("value1", result[1]);
127       assertEquals(new Integer JavaDoc(2), result[0]);
128       result = data.getAll(new String JavaDoc[] { "name1" });
129       assertEquals("value1", result[0]);
130       result = data.getAll(new String JavaDoc[] { "name2" });
131       assertEquals(new Integer JavaDoc(2), result[0]);
132    }
133
134    public void testContainsKey()
135       throws Exception JavaDoc
136    {
137       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
138       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
139       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
140       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
141          itemNames, itemDescriptions, itemTypes);
142       HashMap JavaDoc map = new HashMap JavaDoc();
143       map.put("name1", "value1");
144       map.put("name2", new Integer JavaDoc(2));
145       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
146       assertTrue("data should contain key name1", data.containsKey("name1") == true);
147       assertTrue("data should contain key name2", data.containsKey("name2") == true);
148       assertTrue("data should not contain key nameX", data.containsKey("nameX") == false);
149       assertTrue("data should not contain key null", data.containsKey(null) == false);
150       assertTrue("data should not contain key <empty>", data.containsKey("") == false);
151    }
152
153    public void testContainsValue()
154       throws Exception JavaDoc
155    {
156       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
157       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
158       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
159       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
160          itemNames, itemDescriptions, itemTypes);
161       HashMap JavaDoc map = new HashMap JavaDoc();
162       map.put("name1", "value1");
163       map.put("name2", new Integer JavaDoc(2));
164       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
165       assertTrue("data should contain value value1", data.containsValue("value1") == true);
166       assertTrue("data should contain value 2", data.containsValue(new Integer JavaDoc(2)) == true);
167       assertTrue("data should not contain value name1", data.containsValue("name1") == false);
168       assertTrue("data should not contain key null", data.containsValue(null) == false);
169       assertTrue("data should not contain key <empty>", data.containsValue("") == false);
170
171       map.clear();
172       map.put("name1", "value1");
173       map.put("name2", null);
174       data = new CompositeDataSupport JavaDoc(compositeType, map);
175       assertTrue("data should contain value null", data.containsValue(null) == true);
176    }
177
178    public void testValues()
179       throws Exception JavaDoc
180    {
181       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
182       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
183       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
184       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
185          itemNames, itemDescriptions, itemTypes);
186       HashMap JavaDoc map = new HashMap JavaDoc();
187       map.put("name1", "value1");
188       map.put("name2", new Integer JavaDoc(2));
189       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
190       Collection JavaDoc values = data.values();
191       assertTrue("data values contain 2 elements", values.size() == 2);
192       assertTrue("data values should have value1", values.contains("value1"));
193       assertTrue("data values should have 2", values.contains(new Integer JavaDoc(2)));
194       assertTrue("data values should not have name1", values.contains("name1") == false);
195       assertTrue("data values should not have null", values.contains(null) == false);
196       assertTrue("data values should not have <empty>", values.contains("") == false);
197
198       map.clear();
199       map.put("name1", "value1");
200       map.put("name2", null);
201       data = new CompositeDataSupport JavaDoc(compositeType, map);
202       values = data.values();
203       assertTrue("data values should contain value null", values.contains(null) == true);
204    }
205
206    public void testEquals()
207       throws Exception JavaDoc
208    {
209       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
210       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
211       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
212       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
213          itemNames, itemDescriptions, itemTypes);
214       HashMap JavaDoc map = new HashMap JavaDoc();
215       map.put("name1", "value1");
216       map.put("name2", new Integer JavaDoc(2));
217       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
218
219       assertTrue("data should equal itself", data.equals(data));
220       assertTrue("data should not equal null", data.equals(null) == false);
221       assertTrue("data should not equal non CompositeData", data.equals(new Object JavaDoc()) == false);
222
223       String JavaDoc[] itemNames2 = new String JavaDoc[] { "name1", "name2" };
224       String JavaDoc[] itemDescriptions2 = new String JavaDoc[] { "desc1", "desc2" };
225       OpenType JavaDoc[] itemTypes2 = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
226       CompositeType JavaDoc compositeType2 = new CompositeType JavaDoc("typeName", "description",
227          itemNames2, itemDescriptions2, itemTypes2);
228       HashMap JavaDoc map2 = new HashMap JavaDoc();
229       map2.put("name1", "value1");
230       map2.put("name2", new Integer JavaDoc(2));
231       CompositeDataSupport JavaDoc data2 = new CompositeDataSupport JavaDoc(compositeType2, map2);
232
233       assertTrue("data should equal with data2 with different instance of the same composite type", data.equals(data2));
234       assertTrue("data2 should equal with data with different instance of the same composite type", data2.equals(data));
235
236       compositeType2 = new CompositeType JavaDoc("typeName2", "description",
237          itemNames2, itemDescriptions2, itemTypes2);
238       data2 = new CompositeDataSupport JavaDoc(compositeType2, map2);
239
240       assertTrue("data should not be equal with data2 with different composite type", data.equals(data2) == false);
241       assertTrue("data2 should not be equal with data with different composite type", data2.equals(data) == false);
242
243       map2 = new HashMap JavaDoc();
244       map2.put("name1", "value1");
245       map2.put("name2", new Integer JavaDoc(3));
246       data2 = new CompositeDataSupport JavaDoc(compositeType, map2);
247
248       assertTrue("data should not be equal with data2 with different values", data.equals(data2) == false);
249       assertTrue("data2 should not be equal with data with different value", data2.equals(data) == false);
250    }
251
252    public void testHashCode()
253       throws Exception JavaDoc
254    {
255       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
256       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
257       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
258       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
259          itemNames, itemDescriptions, itemTypes);
260       HashMap JavaDoc map = new HashMap JavaDoc();
261       map.put("name1", "value1");
262       map.put("name2", new Integer JavaDoc(2));
263       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
264
265       int myHashCode = compositeType.hashCode() + "value1".hashCode() + new Integer JavaDoc(2).hashCode();
266       assertTrue("Wrong hash code generated", myHashCode == data.hashCode());
267    }
268
269    public void testToString()
270       throws Exception JavaDoc
271    {
272       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
273       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
274       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
275       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
276          itemNames, itemDescriptions, itemTypes);
277       HashMap JavaDoc map = new HashMap JavaDoc();
278       map.put("name1", "value1");
279       map.put("name2", new Integer JavaDoc(2));
280       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
281
282       String JavaDoc toString = data.toString();
283
284       assertTrue("toString() should contain the composite type",
285          toString.indexOf(compositeType.toString()) != -1);
286       assertTrue("toString() should contain name1=value1",
287          toString.indexOf("name1=value1") != -1);
288       assertTrue("toString() should contain name2=" + new Integer JavaDoc(2),
289          toString.indexOf("name2=" + new Integer JavaDoc(2)) != -1);
290    }
291
292    public void testSerialization()
293       throws Exception JavaDoc
294    {
295       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
296       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
297       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
298       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
299          itemNames, itemDescriptions, itemTypes);
300       HashMap JavaDoc map = new HashMap JavaDoc();
301       map.put("name1", "value1");
302       map.put("name2", new Integer JavaDoc(2));
303       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
304
305       // Serialize it
306
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
307       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
308       oos.writeObject(data);
309     
310       // Deserialize it
311
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
312       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
313       Object JavaDoc result = ois.readObject();
314
315       assertEquals(data, result);
316    }
317
318    public void testErrorsArray()
319       throws Exception JavaDoc
320    {
321       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
322       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
323       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
324       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
325          itemNames, itemDescriptions, itemTypes);
326       Object JavaDoc[] itemValues = new Object JavaDoc[] { "value1", new Integer JavaDoc(2) };
327
328       boolean caught = false;
329       try
330       {
331          new CompositeDataSupport JavaDoc(null, itemNames, itemValues);
332       }
333       catch (IllegalArgumentException JavaDoc e)
334       {
335          caught = true;
336       }
337       if (caught == false)
338          fail("Excepted IllegalArgumentException for null composite type");
339
340       caught = false;
341       try
342       {
343          new CompositeDataSupport JavaDoc(compositeType, null, itemValues);
344       }
345       catch (IllegalArgumentException JavaDoc e)
346       {
347          caught = true;
348       }
349       if (caught == false)
350          fail("Excepted IllegalArgumentException for null item names");
351
352       caught = false;
353       try
354       {
355          new CompositeDataSupport JavaDoc(compositeType, new String JavaDoc[0], itemValues);
356       }
357       catch (IllegalArgumentException JavaDoc e)
358       {
359          caught = true;
360       }
361       if (caught == false)
362          fail("Excepted IllegalArgumentException for empty item names");
363
364       caught = false;
365       try
366       {
367          new CompositeDataSupport JavaDoc(compositeType, itemNames, null);
368       }
369       catch (IllegalArgumentException JavaDoc e)
370       {
371          caught = true;
372       }
373       if (caught == false)
374          fail("Excepted IllegalArgumentException for null item values");
375
376       caught = false;
377       try
378       {
379          new CompositeDataSupport JavaDoc(compositeType, itemNames, new Object JavaDoc[0]);
380       }
381       catch (IllegalArgumentException JavaDoc e)
382       {
383          caught = true;
384       }
385       if (caught == false)
386          fail("Excepted IllegalArgumentException for empty item values");
387
388       caught = false;
389       try
390       {
391          new CompositeDataSupport JavaDoc(compositeType, new String JavaDoc[] { "name1", null }, itemValues);
392       }
393       catch (IllegalArgumentException JavaDoc e)
394       {
395          caught = true;
396       }
397       if (caught == false)
398          fail("Excepted IllegalArgumentException for a null item name");
399
400       caught = false;
401       try
402       {
403          new CompositeDataSupport JavaDoc(compositeType, new String JavaDoc[] { "name1", "" }, itemValues);
404       }
405       catch (IllegalArgumentException JavaDoc e)
406       {
407          caught = true;
408       }
409       if (caught == false)
410          fail("Excepted IllegalArgumentException for an empty item name");
411
412       caught = false;
413       try
414       {
415          new CompositeDataSupport JavaDoc(compositeType, itemNames, new Object JavaDoc[] { "wrong" });
416       }
417       catch (IllegalArgumentException JavaDoc e)
418       {
419          caught = true;
420       }
421       if (caught == false)
422          fail("Excepted IllegalArgumentException for mismatch in number of itemNames/itemValues");
423
424       caught = false;
425       try
426       {
427          new CompositeDataSupport JavaDoc(compositeType, new String JavaDoc[] { "name1" }, new Object JavaDoc[] { "value1" });
428       }
429       catch (OpenDataException JavaDoc e)
430       {
431          caught = true;
432       }
433       if (caught == false)
434          fail("Excepted OpenDataException for mismatch in number of itemNames for CompositeType/CompositeData");
435
436       caught = false;
437       try
438       {
439          new CompositeDataSupport JavaDoc(compositeType, new String JavaDoc[] { "name1", "wrongName" }, itemValues);
440       }
441       catch (OpenDataException JavaDoc e)
442       {
443          caught = true;
444       }
445       if (caught == false)
446          fail("Excepted OpenDataException for an item name not in the composite type");
447
448       caught = false;
449       try
450       {
451          new CompositeDataSupport JavaDoc(compositeType, itemNames, new Object JavaDoc[] { "value1", "wrong" });
452       }
453       catch (OpenDataException JavaDoc e)
454       {
455          caught = true;
456       }
457       if (caught == false)
458          fail("Excepted OpenDataException for an item value of the wrong type");
459
460       new CompositeDataSupport JavaDoc(compositeType, itemNames, new Object JavaDoc[] { "value1", null });
461    }
462
463    public void testErrorsMap()
464       throws Exception JavaDoc
465    {
466       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
467       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
468       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
469       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
470          itemNames, itemDescriptions, itemTypes);
471       HashMap JavaDoc map = new HashMap JavaDoc();
472       map.put("name1", "value1");
473       map.put("name2", new Integer JavaDoc(2));
474
475       boolean caught = false;
476       try
477       {
478          new CompositeDataSupport JavaDoc(null, map);
479       }
480       catch (IllegalArgumentException JavaDoc e)
481       {
482          caught = true;
483       }
484       if (caught == false)
485          fail("Excepted IllegalArgumentException for null composite type");
486
487       caught = false;
488       try
489       {
490          new CompositeDataSupport JavaDoc(compositeType, null);
491       }
492       catch (IllegalArgumentException JavaDoc e)
493       {
494          caught = true;
495       }
496       if (caught == false)
497          fail("Excepted IllegalArgumentException for null map");
498
499       caught = false;
500       try
501       {
502          new CompositeDataSupport JavaDoc(compositeType, new HashMap JavaDoc());
503       }
504       catch (IllegalArgumentException JavaDoc e)
505       {
506          caught = true;
507       }
508       if (caught == false)
509          fail("Excepted IllegalArgumentException for empty map");
510
511       caught = false;
512       try
513       {
514          HashMap JavaDoc map2 = new HashMap JavaDoc();
515          map2.put("name1", "value1");
516          map2.put(null, new Integer JavaDoc(2));
517          new CompositeDataSupport JavaDoc(compositeType, map2);
518       }
519       catch (IllegalArgumentException JavaDoc e)
520       {
521          caught = true;
522       }
523       if (caught == false)
524          fail("Excepted IllegalArgumentException for a null key in map");
525
526       caught = false;
527       try
528       {
529          HashMap JavaDoc map2 = new HashMap JavaDoc();
530          map2.put("name1", "value1");
531          map2.put("", new Integer JavaDoc(2));
532          new CompositeDataSupport JavaDoc(compositeType, map2);
533       }
534       catch (IllegalArgumentException JavaDoc e)
535       {
536          caught = true;
537       }
538       if (caught == false)
539          fail("Excepted IllegalArgumentException for an empty key in map");
540
541       caught = false;
542       try
543       {
544          HashMap JavaDoc map2 = new HashMap JavaDoc();
545          map2.put("name1", "value1");
546          new CompositeDataSupport JavaDoc(compositeType, map2);
547       }
548       catch (OpenDataException JavaDoc e)
549       {
550          caught = true;
551       }
552       if (caught == false)
553          fail("Excepted OpenDataException for mismatch in number of items for CompositeType/CompositeData");
554
555       caught = false;
556       try
557       {
558          HashMap JavaDoc map2 = new HashMap JavaDoc();
559          map2.put("name1", "value1");
560          map2.put("wrongName", new Integer JavaDoc(2));
561          new CompositeDataSupport JavaDoc(compositeType, map2);
562       }
563       catch (OpenDataException JavaDoc e)
564       {
565          caught = true;
566       }
567       if (caught == false)
568          fail("Excepted OpenDataException for an item name not in the composite type");
569
570       caught = false;
571       try
572       {
573          HashMap JavaDoc map2 = new HashMap JavaDoc();
574          map2.put("name1", "value1");
575          map2.put("name2", "wrong");
576          new CompositeDataSupport JavaDoc(compositeType, map2);
577       }
578       catch (OpenDataException JavaDoc e)
579       {
580          caught = true;
581       }
582       if (caught == false)
583          fail("Excepted OpenDataException for an item value of the wrong type");
584
585       caught = false;
586       try
587       {
588          HashMap JavaDoc map2 = new HashMap JavaDoc();
589          map2.put("name1", "value1");
590          map2.put(new Integer JavaDoc(2), new Integer JavaDoc(2));
591          new CompositeDataSupport JavaDoc(compositeType, map2);
592       }
593       catch (ArrayStoreException JavaDoc e)
594       {
595          caught = true;
596       }
597       if (caught == false)
598          fail("Excepted ArrayStoreException for a non String key in map");
599
600       HashMap JavaDoc map2 = new HashMap JavaDoc();
601       map2.put("name1", "value1");
602       map2.put("name2", null);
603       new CompositeDataSupport JavaDoc(compositeType, map2);
604    }
605
606    public void testErrors()
607       throws Exception JavaDoc
608    {
609       String JavaDoc[] itemNames = new String JavaDoc[] { "name1", "name2" };
610       String JavaDoc[] itemDescriptions = new String JavaDoc[] { "desc1", "desc2" };
611       OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[] { SimpleType.STRING, SimpleType.INTEGER };
612       CompositeType JavaDoc compositeType = new CompositeType JavaDoc("typeName", "description",
613          itemNames, itemDescriptions, itemTypes);
614       HashMap JavaDoc map = new HashMap JavaDoc();
615       map.put("name1", "value1");
616       map.put("name2", new Integer JavaDoc(2));
617       CompositeDataSupport JavaDoc data = new CompositeDataSupport JavaDoc(compositeType, map);
618
619       boolean caught = false;
620       try
621       {
622          data.get(null);
623       }
624       catch (IllegalArgumentException JavaDoc e)
625       {
626          caught = true;
627       }
628       if (caught == false)
629          fail("Excepted IllegalArgumentException for get and a null key");
630
631       caught = false;
632       try
633       {
634          data.get("");
635       }
636       catch (IllegalArgumentException JavaDoc e)
637       {
638          caught = true;
639       }
640       if (caught == false)
641          fail("Excepted IllegalArgumentException for get and an empty key");
642
643       caught = false;
644       try
645       {
646          data.get("wrong");
647       }
648       catch (InvalidKeyException JavaDoc e)
649       {
650          caught = true;
651       }
652       if (caught == false)
653          fail("Excepted InvalidKeyException for get and a wrong key");
654
655       caught = false;
656       try
657       {
658          data.getAll(new String JavaDoc[] { "name1", null });
659       }
660       catch (IllegalArgumentException JavaDoc e)
661       {
662          caught = true;
663       }
664       if (caught == false)
665          fail("Excepted IllegalArgumentException for getAll and a null key");
666
667       caught = false;
668       try
669       {
670          data.getAll(new String JavaDoc[] { "name1", "" });
671       }
672       catch (IllegalArgumentException JavaDoc e)
673       {
674          caught = true;
675       }
676       if (caught == false)
677          fail("Excepted IllegalArgumentException for getAll and an empty key");
678
679       caught = false;
680       try
681       {
682          data.getAll(new String JavaDoc[] { "name1", "wrong" });
683       }
684       catch (IllegalArgumentException JavaDoc e)
685       {
686          caught = true;
687       }
688       if (caught == false)
689          fail("Excepted InvalidKeyException for getAll and an invalid key");
690    }
691 }
692
Popular Tags