KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > orb > dynany > DynAnyBoundedSeqTest


1 package org.jacorb.test.orb.dynany;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2001 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import junit.framework.*;
24 import junit.extensions.TestSetup;
25 import org.omg.CORBA.TCKind JavaDoc;
26
27 import org.jacorb.test.common.ORBSetup;
28 import org.jacorb.test.Bound;
29 import org.jacorb.test.BoundedDataHelper;
30
31 /**
32  * DynAnyBoundedSeqTest.java
33  *
34  * DynAny tests for (bounded) sequence types.
35  *
36  */

37
38 public class DynAnyBoundedSeqTest extends TestCase
39 {
40    private static org.omg.DynamicAny.DynAnyFactory JavaDoc factory = null;
41    private static org.omg.CORBA.ORB JavaDoc orb = null;
42
43    public DynAnyBoundedSeqTest (String JavaDoc name)
44    {
45       super (name);
46    }
47
48
49    public static Test suite ()
50    {
51       TestSuite suite = new TestSuite ("Bounded DynSequence Tests");
52       Setup setup = new Setup (suite);
53       ORBSetup osetup = new ORBSetup (setup);
54
55       suite.addTest (new DynAnyBoundedSeqTest ("testFactoryCreateFromAny"));
56       suite.addTest (new DynAnyBoundedSeqTest ("testFactoryCreateFromTypeCode"));
57       suite.addTest (new DynAnyBoundedSeqTest ("testFactoryCreateFromIDLTypeCode"));
58       suite.addTest (new DynAnyBoundedSeqTest ("testCompareDynAny"));
59       suite.addTest (new DynAnyBoundedSeqTest ("testIterateDynAny"));
60       suite.addTest (new DynAnyBoundedSeqTest ("testAccessSeqElements"));
61       suite.addTest (new DynAnyBoundedSeqTest ("testAccessSeqDynAnyElements"));
62       suite.addTest (new DynAnyBoundedSeqTest ("testAccessSeqElementsEx"));
63       suite.addTest (new DynAnyBoundedSeqTest ("testDynAnyTypeCode"));
64       suite.addTest (new DynAnyBoundedSeqTest ("testInitDynAnyFromDynAny"));
65       suite.addTest (new DynAnyBoundedSeqTest ("testInitDynAnyFromAny"));
66       suite.addTest (new DynAnyBoundedSeqTest ("testInitFromAnyTypeMismatchEx"));
67       suite.addTest (new DynAnyBoundedSeqTest ("testGenerateAnyFromDynAny"));
68       suite.addTest (new DynAnyBoundedSeqTest ("testDestroyDynAny"));
69       suite.addTest (new DynAnyBoundedSeqTest ("testDestroyComponent"));
70       suite.addTest (new DynAnyBoundedSeqTest ("testCopyDynAny"));
71
72       return osetup;
73    }
74
75
76    /**
77     * Tests creating a DynAny object from an Any object using the
78     * DynAnyFactory object.
79     */

80    public void testFactoryCreateFromAny ()
81    {
82       int [] type = null;
83       org.omg.CORBA.Any JavaDoc any = null;
84
85       type = getIntSeq ();
86       any = orb.create_any ();
87       BoundedDataHelper.insert (any, type);
88
89       createDynAnyFromAny (any);
90    }
91
92
93    /**
94     * Tests creating a DynAny object from a TypeCode object using the
95     * DynAnyFactory object.
96     */

97    public void testFactoryCreateFromTypeCode ()
98    {
99       org.omg.CORBA.TypeCode JavaDoc tc = null;
100
101       tc = orb.get_primitive_tc (TCKind.tk_long);
102       tc = orb.create_sequence_tc (Bound.value, tc);
103       createDynAnyFromTypeCode (tc);
104    }
105
106
107    /**
108     * Tests creating a DynAny object from a TypeCode object generated from
109     * IDL using the DynAnyFactory object.
110     */

111    public void testFactoryCreateFromIDLTypeCode ()
112    {
113       org.omg.CORBA.TypeCode JavaDoc tc = null;
114
115       tc = BoundedDataHelper.type ();
116       createDynAnyFromTypeCode (tc);
117    }
118
119
120    /**
121     * Test comparing DynAny values.
122     */

123    public void testCompareDynAny ()
124    {
125       String JavaDoc msg;
126       int [] type;
127       org.omg.CORBA.Any JavaDoc any = null;
128       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
129       org.omg.DynamicAny.DynSequence JavaDoc dynAny2 = null;
130
131       type = getIntSeq ();
132       any = orb.create_any ();
133       BoundedDataHelper.insert (any, type);
134       dynAny = createDynAnyFromAny (any);
135       dynAny2 = createDynAnyFromAny (any);
136
137       msg = "Comparing two equal DynAny values using DynAny::equal failed";
138       assertTrue (msg, dynAny.equal (dynAny2));
139    }
140
141
142    /**
143     * Test iterating through components of a DynAny.
144     */

145    public void testIterateDynAny ()
146    {
147       String JavaDoc msg;
148       int [] type;
149       int compCount = -1;
150       boolean seek;
151       org.omg.CORBA.Any JavaDoc any = null;
152       org.omg.CORBA.TypeCode JavaDoc tc = null;
153       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
154       org.omg.DynamicAny.DynAny JavaDoc compSeek = null;
155       org.omg.DynamicAny.DynAny JavaDoc compRewind = null;
156
157       type = getIntSeq ();
158       any = orb.create_any ();
159       BoundedDataHelper.insert (any, type);
160       dynAny = createDynAnyFromAny (any);
161
162       // test the component count
163
msg = "The number of components returned from the ";
164       msg += "DynAny::component_count method is incorrect";
165       try
166       {
167          compCount = dynAny.component_count ();
168       }
169       catch (Throwable JavaDoc ex)
170       {
171          // should not be needed, but it prevents compiler errors
172
fail ("Unexpected error raised by DynAny::component_count operation");
173       }
174       assertEquals (msg, Bound.value, compCount);
175
176       // seek an invalid position
177
msg = "The DynAny::seek operation indicates a valid current position ";
178       msg += "when the current position should be invalid";
179       seek = dynAny.seek (-1);
180       assertTrue (msg, !seek);
181
182       // seek the first position
183
msg = "The DynAny::seek operation indicates an invalid current position ";
184       msg += "when the current position should be valid";
185       seek = dynAny.seek (0);
186       assertTrue (msg, seek);
187
188       // extract a value from the current position
189
try
190       {
191          compSeek = dynAny.current_component ();
192       }
193       catch (Throwable JavaDoc ex)
194       {
195          msg = "Failed to get the current component using the ";
196          msg += "DynAny::current_component operation after calling the ";
197          msg += "DynAny::seek operation";
198          fail (msg + ": " + ex);
199       }
200
201       // seek the next position
202
msg = "The DynAny::next operation indicates an invalid current position ";
203       msg += "when the current position should be valid";
204       seek = dynAny.next ();
205       assertTrue (msg, seek);
206
207       // return to the first position
208
dynAny.rewind ();
209
210       // extract a value from the current position
211
try
212       {
213          compRewind = dynAny.current_component ();
214       }
215       catch (Throwable JavaDoc ex)
216       {
217          msg = "Failed to get the current component using the ";
218          msg += "DynAny::current_component operation after calling the ";
219          msg += "DynAny::rewind operation";
220          fail (msg + ": " + ex);
221       }
222       msg = "The component at DynAny::seek(0) is not equal to the ";
223       msg += "component at DynAny::rewind";
224       assertTrue (msg, compSeek.equal (compRewind));
225    }
226
227
228    /**
229     * Test accessing the elements in a DynSequence object.
230     */

231    public void testAccessSeqElements ()
232    {
233       String JavaDoc msg;
234       int len;
235       int newLen;
236       int curVal;
237       boolean next;
238       org.omg.CORBA.TypeCode JavaDoc tc = null;
239       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
240       org.omg.CORBA.Any JavaDoc [] anys = null;
241
242       tc = BoundedDataHelper.type ();
243       dynAny = createDynAnyFromTypeCode (tc);
244
245       // test the default length is correct
246
msg = "The default length of DynSequence created from a TypeCode is ";
247       msg += "incorrect";
248       len = dynAny.get_length ();
249       assertEquals (msg, 0, len);
250
251       // test setting the elements
252
len = Bound.value / 2;
253       anys = new org.omg.CORBA.Any JavaDoc [len];
254       for (int i = 0; i < len; i++)
255       {
256          anys [i] = orb.create_any ();
257          anys [i].insert_long (i);
258       }
259
260       try
261       {
262          dynAny.set_elements (anys);
263       }
264       catch (Throwable JavaDoc ex)
265       {
266          msg = "Failed to set the elements using the DynSequence:set_elements ";
267          msg += "operation";
268          fail (msg + ": " + ex);
269       }
270
271       // test increasing the length
272
newLen = Bound.value;
273       try
274       {
275          dynAny.set_length (newLen);
276       }
277       catch (Throwable JavaDoc ex)
278       {
279          msg = "Failed to set the length of the sequence using ";
280          msg += "DynSequence::set_length operation";
281          fail (msg + ": " + ex);
282       }
283
284       for (int i = 0; i < newLen; i++)
285       {
286          msg = "Failed to get the correct value of the DynSequence at ";
287          msg += "position " + i;
288          curVal = -1;
289          try
290          {
291             curVal = dynAny.get_long ();
292          }
293          catch (Throwable JavaDoc ex)
294          {
295             fail (msg + ": " + ex);
296          }
297
298          if (i < len)
299          {
300             assertEquals (msg, i, curVal);
301          }
302          else
303          {
304             assertEquals (msg, 0, curVal);
305          }
306          dynAny.next ();
307       }
308
309       // test decreasing the length
310
newLen = 1;
311       try
312       {
313          dynAny.set_length (newLen);
314       }
315       catch (Throwable JavaDoc ex)
316       {
317          msg = "Failed to set the length of the sequence using ";
318          msg += "DynSequence::set_length operation";
319          fail (msg + ": " + ex);
320       }
321       anys = dynAny.get_elements ();
322       msg = "The wrong number of elements were returned from the ";
323       msg += "DynSequence::get_elements operation";
324       assertEquals (msg, newLen, anys.length);
325
326       msg = "Failed to get the correct value of a DynSequence";
327       curVal = anys [0].extract_long ();
328       assertEquals (msg, 0, curVal);
329    }
330
331
332    /**
333     * Test accessing the elements in a DynSequence object as DynAnys.
334     */

335    public void testAccessSeqDynAnyElements ()
336    {
337       String JavaDoc msg;
338       int len;
339       int newLen;
340       int curVal;
341       boolean next;
342       org.omg.CORBA.TypeCode JavaDoc tc = null;
343       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
344       org.omg.DynamicAny.DynAny JavaDoc [] dynAnys = null;
345
346       tc = BoundedDataHelper.type ();
347       dynAny = createDynAnyFromTypeCode (tc);
348
349       // test the default length is correct
350
msg = "The default length of DynSequence created from a TypeCode is ";
351       msg += "incorrect";
352       len = dynAny.get_length ();
353       assertEquals (msg, 0, len);
354
355       // test setting the elements
356
len = Bound.value / 2;
357       dynAnys = new org.omg.DynamicAny.DynAny JavaDoc [len];
358       tc = orb.get_primitive_tc (TCKind.tk_long);
359       for (int i = 0; i < len; i++)
360       {
361          try
362          {
363             dynAnys [i] = factory.create_dyn_any_from_type_code (tc);
364          }
365          catch (Throwable JavaDoc ex)
366          {
367             fail ("Failed to create a DynAny at position " + i + ": " + ex);
368          }
369
370          try
371          {
372             dynAnys [i].insert_long (i);
373          }
374          catch (Throwable JavaDoc ex)
375          {
376             msg = "Failed to insert a value into a DynAny at position " + i;
377             msg += ": " + ex;
378             fail (msg);
379          }
380       }
381
382       try
383       {
384          dynAny.set_elements_as_dyn_any (dynAnys);
385       }
386       catch (Throwable JavaDoc ex)
387       {
388          msg = "Failed to set the elements using the ";
389          msg += "DynSequence:set_elements_as_dyn_any operation";
390          fail (msg + ": " + ex);
391       }
392
393       // test increasing the length
394
newLen = Bound.value;
395       try
396       {
397          dynAny.set_length (newLen);
398       }
399       catch (Throwable JavaDoc ex)
400       {
401          msg = "Failed to set the length of the sequence using ";
402          msg += "DynSequence::set_length operation";
403          fail (msg + ": " + ex);
404       }
405
406       for (int i = 0; i < newLen; i++)
407       {
408          msg = "Failed to get the correct value of the DynSequence at ";
409          msg += "position " + i;
410          curVal = -1;
411          try
412          {
413             curVal = dynAny.get_long ();
414          }
415          catch (Throwable JavaDoc ex)
416          {
417             fail (msg + ": " + ex);
418          }
419
420          if (i < len)
421          {
422             assertEquals (msg, i, curVal);
423          }
424          else
425          {
426             assertEquals (msg, 0, curVal);
427          }
428          dynAny.next ();
429       }
430
431       // test decreasing the length
432
newLen = 1;
433       try
434       {
435          dynAny.set_length (newLen);
436       }
437       catch (Throwable JavaDoc ex)
438       {
439          msg = "Failed to set the length of the sequence using ";
440          msg += "DynSequence::set_length operation";
441          fail (msg + ": " + ex);
442       }
443       dynAnys = dynAny.get_elements_as_dyn_any ();
444
445       msg = "The wrong number of elements were returned from the ";
446       msg += "DynSequence::get_elements_as_dyn_any operation";
447       assertEquals (msg, newLen, dynAnys.length);
448
449       msg = "Failed to get the correct value of a DynSequence";
450       curVal = -1;
451       try
452       {
453          curVal = dynAnys [0].get_long ();
454       }
455       catch (Throwable JavaDoc ex)
456       {
457          fail (msg + ": " + ex);
458       }
459       assertEquals (msg, 0, curVal);
460    }
461
462
463    /**
464     * Test that the correct exceptions are raised when accessing the elements
465     * in a DynSequence object incorrectly.
466     */

467    public void testAccessSeqElementsEx ()
468    {
469       String JavaDoc msg;
470       int len;
471       org.omg.CORBA.TypeCode JavaDoc tc = null;
472       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
473       org.omg.CORBA.Any JavaDoc [] anys = null;
474
475       tc = BoundedDataHelper.type ();
476       dynAny = createDynAnyFromTypeCode (tc);
477
478       // test increasing the length of the sequence beyond its bounds
479
msg = "Failed to raise an InvalidValue exception when setting the ";
480       msg += "length of a DynSequence object beyond its bounds using ";
481       msg += "DynSequence::set_length";
482       try
483       {
484          dynAny.set_length (Bound.value + 1);
485       }
486       catch (Throwable JavaDoc ex)
487       {
488          if (!(ex instanceof org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc))
489          {
490             fail (msg + ": " + ex);
491          }
492       }
493
494       // test setting the elements with components of an invalid type
495
anys = new org.omg.CORBA.Any JavaDoc [1];
496       anys [0] = orb.create_any ();
497       anys [0].insert_string ("BadType");
498
499       msg = "Failed to raise a TypeMismatch exception when setting a ";
500       msg += "DynSequence object with components of the wrong type using ";
501       msg += "DynSequence::set_elements";
502       try
503       {
504          dynAny.set_elements (anys);
505
506          fail (msg);
507       }
508       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
509       {
510          // success
511
}
512       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
513       {
514          fail (msg + ": " + ex);
515       }
516
517       // test setting the elements with more elements than the bounds
518
len = Bound.value + 1;
519       anys = new org.omg.CORBA.Any JavaDoc [len];
520       for (int i = 0; i < len; i++)
521       {
522          anys [i] = orb.create_any ();
523          anys [i].insert_long (i);
524       }
525
526       msg = "Failed to raise an InvalidValue exception when setting a ";
527       msg += "DynSequence object with more elements than the sequence bounds";
528       msg += "using DynSequence::set_elements";
529       try
530       {
531          dynAny.set_elements (anys);
532       }
533       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
534       {
535          // success
536
}
537       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
538       {
539          fail (msg + ": " + ex);
540       }
541    }
542
543
544    /**
545     * Test obtaining the TypeCode associated with a DynAny object.
546     */

547    public void testDynAnyTypeCode ()
548    {
549       String JavaDoc msg;
550       org.omg.CORBA.TypeCode JavaDoc tc = null;
551       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
552
553       tc = orb.get_primitive_tc (TCKind.tk_long);
554       tc = orb.create_sequence_tc (Bound.value, tc);
555       dynAny = createDynAnyFromTypeCode (tc);
556
557       msg = "Incorrect TypeCode retrieved from DynAny::type operation";
558       assertTrue (msg, dynAny.type ().equal (tc));
559    }
560
561
562    /**
563     * Test initializing a DynAny object from another DynAny object.
564     */

565    public void testInitDynAnyFromDynAny ()
566    {
567       String JavaDoc msg;
568       int [] type;
569       org.omg.CORBA.Any JavaDoc any = null;
570       org.omg.CORBA.TypeCode JavaDoc tc = null;
571       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
572       org.omg.DynamicAny.DynSequence JavaDoc dynAny2 = null;
573
574       tc = BoundedDataHelper.type ();
575       dynAny = createDynAnyFromTypeCode (tc);
576
577       type = getIntSeq ();
578       any = orb.create_any ();
579       BoundedDataHelper.insert (any, type);
580       dynAny2 = createDynAnyFromAny (any);
581
582       msg = "Failed to initialize a DynAny object from another DynAny ";
583       msg += "object using the DynAny::assign operation";
584       try
585       {
586          dynAny.assign (dynAny2);
587       }
588       catch (Throwable JavaDoc ex)
589       {
590          fail (msg + ": " + ex);
591       }
592       assertTrue (msg, dynAny.equal (dynAny2));
593    }
594
595
596    /**
597     * Test initializing a DynAny object from an Any value.
598     */

599    public void testInitDynAnyFromAny ()
600    {
601       String JavaDoc msg;
602       int [] type;
603       org.omg.CORBA.Any JavaDoc any = null;
604       org.omg.CORBA.TypeCode JavaDoc tc = null;
605       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
606       org.omg.DynamicAny.DynSequence JavaDoc dynAny2 = null;
607
608       tc = BoundedDataHelper.type ();
609       dynAny = createDynAnyFromTypeCode (tc);
610
611       type = getIntSeq ();
612       any = orb.create_any ();
613       BoundedDataHelper.insert (any, type);
614       dynAny2 = createDynAnyFromAny (any);
615
616       msg = "Failed to initialize a DynAny object from an Any object ";
617       msg += "using the DynAny::from_any operation";
618       try
619       {
620          dynAny.from_any (any);
621       }
622       catch (Throwable JavaDoc ex)
623       {
624          fail (msg + ": " + ex);
625       }
626       assertTrue (msg, dynAny.equal (dynAny2));
627    }
628
629
630    /**
631     * Test that a TypeMismatch exception is raised if there is a type
632     * mismatch between the DynAny and Any types in an assignment.
633     */

634    public void testInitFromAnyTypeMismatchEx ()
635    {
636       String JavaDoc msg;
637       org.omg.CORBA.Any JavaDoc any = null;
638       org.omg.CORBA.TypeCode JavaDoc tc = null;
639       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
640
641       any = orb.create_any ();
642       any.insert_string ("Hello");
643
644       tc = orb.get_primitive_tc (TCKind.tk_long);
645       tc = orb.create_sequence_tc (Bound.value, tc);
646       dynAny = createDynAnyFromTypeCode (tc);
647
648       msg = "TypeMismatch exception not thrown by DynAny::from_any ";
649       msg += "operation when DynAny and Any operands have different types";
650       try
651       {
652          dynAny.from_any (any);
653
654          fail (msg);
655       }
656       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
657       {
658          // success
659
}
660       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
661       {
662          fail (msg + ": " + ex);
663       }
664    }
665
666
667    /**
668     * Test generating an Any value from a DynAny object.
669     */

670    public void testGenerateAnyFromDynAny ()
671    {
672       String JavaDoc msg;
673       org.omg.CORBA.Any JavaDoc any = null;
674       org.omg.CORBA.TypeCode JavaDoc tc = null;
675       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
676       org.omg.DynamicAny.DynSequence JavaDoc dynAny2 = null;
677
678       tc = BoundedDataHelper.type ();
679       dynAny = createDynAnyFromTypeCode (tc);
680
681       any = orb.create_any ();
682       any = dynAny.to_any ();
683       dynAny2 = createDynAnyFromAny (any);
684
685       msg = "The DynAny::to_any operation failed to create an Any ";
686       msg += "object with the same value as the DynAny object";
687       assertTrue (msg, dynAny.equal (dynAny2));
688    }
689
690
691    /**
692     * Test destroying a DynAny object.
693     */

694    public void testDestroyDynAny ()
695    {
696       String JavaDoc msg;
697       int [] type;
698       org.omg.CORBA.Any JavaDoc any = null;
699       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
700
701       type = getIntSeq ();
702       any = orb.create_any ();
703       BoundedDataHelper.insert (any, type);
704       dynAny = createDynAnyFromAny (any);
705       dynAny.destroy ();
706
707       try
708       {
709          dynAny.type ();
710
711          msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
712          msg += "calling DynAny::type operation on a destroyed DynAny object ";
713          msg += "did not raise OBJECT_NOT_EXIST exception";
714          fail (msg);
715       }
716       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
717       {
718          // success
719
}
720
721       msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
722       msg += "calling DynAny::current_component operation on a destroyed ";
723       msg += "DynAny object did not raise OBJECT_NOT_EXIST exception";
724       try
725       {
726          dynAny.current_component ();
727
728          fail (msg);
729       }
730       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
731       {
732          // success
733
}
734       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
735       {
736          fail (msg + ": " + ex);
737       }
738    }
739
740
741    /**
742     * Test destroying a component of a DynAny object.
743     */

744    public void testDestroyComponent ()
745    {
746       String JavaDoc msg;
747       int [] type;
748       org.omg.CORBA.Any JavaDoc any = null;
749       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
750       org.omg.DynamicAny.DynAny JavaDoc comp = null;
751
752       type = getIntSeq ();
753       any = orb.create_any ();
754       BoundedDataHelper.insert (any, type);
755       dynAny = createDynAnyFromAny (any);
756
757       try
758       {
759          comp = dynAny.current_component ();
760       }
761       catch (Throwable JavaDoc ex)
762       {
763          msg = "Failed to get the current component of the DynAny using the ";
764          msg += "DynAny::current_component operation before calling the ";
765          msg += "DynAny::destroy operation";
766          fail (msg + ": " + ex);
767       }
768
769       comp.destroy ();
770       try
771       {
772          comp = dynAny.current_component ();
773       }
774       catch (Throwable JavaDoc ex)
775       {
776          msg = "Failed to get the current component of the DynAny using the ";
777          msg += "DynAny::current_component operation after calling the ";
778          msg += "DynAny::destroy operation";
779          fail (msg + ": " + ex);
780       }
781
782       try
783       {
784          comp.type ();
785       }
786       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
787       {
788          msg = "Calling destroy on a component resulted in destroying the ";
789          msg += "component object";
790          fail (msg + ": " + ex);
791       }
792    }
793
794
795    /**
796     * Test creating a copy of a DynAny object.
797     */

798    public void testCopyDynAny ()
799    {
800       String JavaDoc msg;
801       org.omg.CORBA.TypeCode JavaDoc tc = null;
802       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
803       org.omg.DynamicAny.DynSequence JavaDoc dynAny2 = null;
804
805       tc = BoundedDataHelper.type ();
806       dynAny = createDynAnyFromTypeCode (tc);
807       dynAny2 = (org.omg.DynamicAny.DynSequence JavaDoc) dynAny.copy ();
808
809       msg = "The DynAny object created with the DynAny::copy operation ";
810       msg += "is not equal to the DynAny object it was copied from";
811       assertTrue (msg, dynAny.equal (dynAny2));
812    }
813
814
815    private static class Setup extends TestSetup
816    {
817       public Setup (Test test)
818       {
819          super (test);
820       }
821
822       protected void setUp ()
823       {
824          org.omg.CORBA.Object JavaDoc obj = null;
825
826          orb = ORBSetup.getORB ();
827          try
828          {
829             obj = orb.resolve_initial_references ("DynAnyFactory");
830          }
831          catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc ex)
832          {
833             fail ("Failed to resolve DynAnyFactory: " + ex);
834          }
835          try
836          {
837             factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj);
838          }
839          catch (Throwable JavaDoc ex)
840          {
841             fail ("Failed to narrow to DynAnyFactory: " + ex);
842          }
843       }
844
845       protected void tearDown ()
846       {
847       }
848    }
849
850
851    /**
852     * Create a DynAny object from an Any object.
853     */

854    private static org.omg.DynamicAny.DynSequence JavaDoc createDynAnyFromAny
855       (org.omg.CORBA.Any JavaDoc any)
856    {
857       String JavaDoc msg;
858       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
859
860       try
861       {
862          dynAny = (org.omg.DynamicAny.DynSequence JavaDoc) factory.create_dyn_any (any);
863       }
864       catch (Throwable JavaDoc ex)
865       {
866          msg = "Factory failed to create DynAny from Any using ";
867          msg += "DynAny::create_dyn_any operation: " + ex;
868          fail (msg);
869       }
870       return dynAny;
871    }
872
873
874    /**
875     * Create a DynAny object from a TypeCode object.
876     */

877    private static org.omg.DynamicAny.DynSequence JavaDoc createDynAnyFromTypeCode
878       (org.omg.CORBA.TypeCode JavaDoc tc)
879    {
880       String JavaDoc msg;
881       org.omg.DynamicAny.DynSequence JavaDoc dynAny = null;
882
883       try
884       {
885          dynAny = (org.omg.DynamicAny.DynSequence JavaDoc)
886             factory.create_dyn_any_from_type_code (tc);
887       }
888       catch (Throwable JavaDoc ex)
889       {
890          msg = "Factory failed to create DynAny from TypeCode using ";
891          msg += "DynAny::create_dyn_any_from_type_code operation: " + ex;
892          fail (msg);
893       }
894       return dynAny;
895    }
896
897
898    /**
899     * Create a sequence (array) of integers of bounded length.
900     */

901    private static int [] getIntSeq ()
902    {
903       int [] type = new int [Bound.value];
904       for (int i = 0; i < Bound.value; i++)
905       {
906          type [i] = i;
907       }
908       return type;
909    }
910
911 }
912
Popular Tags