KickJava   Java API By Example, From Geeks To Geeks.

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


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.EnumType;
29 import org.jacorb.test.EnumTypeHelper;
30 import org.jacorb.test.UnionDefaultType;
31 import org.jacorb.test.UnionDefaultTypeHelper;
32 import org.jacorb.test.UnionNoDefaultType;
33 import org.jacorb.test.UnionNoDefaultTypeHelper;
34 import org.jacorb.test.UnionFullRangeType;
35 import org.jacorb.test.UnionFullRangeTypeHelper;
36
37 /**
38  * DynAnyUnionTest.java
39  *
40  * DynAny tests for union types with an enumeration discriminator.
41  *
42  */

43
44 public class DynAnyUnionTest extends TestCase
45 {
46    private static org.omg.DynamicAny.DynAnyFactory JavaDoc factory = null;
47    private static org.omg.CORBA.ORB JavaDoc orb = null;
48
49    private static final String JavaDoc ID = "IDL:test:1.0";
50    private static final String JavaDoc NAME = "MyUnion";
51
52    public DynAnyUnionTest (String JavaDoc name)
53    {
54       super (name);
55    }
56
57
58    public static Test suite ()
59    {
60       TestSuite suite = new TestSuite
61          ("DynUnion Tests using Enumeration Discriminator");
62       Setup setup = new Setup (suite);
63       ORBSetup osetup = new ORBSetup (setup);
64
65       suite.addTest (new DynAnyUnionTest ("testFactoryCreateFromAny"));
66       suite.addTest (new DynAnyUnionTest ("testFactoryCreateFromTypeCode"));
67       suite.addTest (new DynAnyUnionTest ("testFactoryCreateFromIDLTypeCode"));
68       suite.addTest (new DynAnyUnionTest ("testCompareDynAnyUnamedMember"));
69       suite.addTest (new DynAnyUnionTest ("testCompareDynAnyNamedMember"));
70       suite.addTest (new DynAnyUnionTest ("testIterateDynAnyNamedMember"));
71       suite.addTest (new DynAnyUnionTest ("testIterateDynAnyUnamedMember"));
72       suite.addTest (new DynAnyUnionTest ("testAccessUnionDisc"));
73       suite.addTest (new DynAnyUnionTest ("testUnionDefaultCase"));
74       suite.addTest (new DynAnyUnionTest ("testUnionNoDefaultCase"));
75       suite.addTest (new DynAnyUnionTest ("testUnionFullRange"));
76       suite.addTest (new DynAnyUnionTest ("testAccessNamedUnionMember"));
77       suite.addTest (new DynAnyUnionTest ("testAccessUnamedUnionMember"));
78       suite.addTest (new DynAnyUnionTest ("testDynAnyTypeCode"));
79       suite.addTest (new DynAnyUnionTest ("testInitDynAnyFromDynAny"));
80       suite.addTest (new DynAnyUnionTest ("testInitDynAnyFromAny"));
81       suite.addTest (new DynAnyUnionTest ("testInitFromAnyTypeMismatchEx"));
82       suite.addTest (new DynAnyUnionTest ("testGenerateAnyFromDynAny"));
83       suite.addTest (new DynAnyUnionTest ("testDestroyDynAny"));
84       suite.addTest (new DynAnyUnionTest ("testDestroyComponent"));
85       suite.addTest (new DynAnyUnionTest ("testCopyDynAny"));
86
87       return osetup;
88    }
89
90
91    /**
92     * Tests creating a DynAny object from an Any object using the
93     * DynAnyFactory object.
94     */

95    public void testFactoryCreateFromAny ()
96    {
97       UnionDefaultType type = null;
98       org.omg.CORBA.Any JavaDoc any = null;
99
100       type = new UnionDefaultType ();
101       type.win (10);
102       any = orb.create_any ();
103       UnionDefaultTypeHelper.insert (any, type);
104
105       createDynAnyFromAny (any);
106    }
107
108
109    /**
110     * Tests creating a DynAny object from a TypeCode object using the
111     * DynAnyFactory object.
112     */

113    public void testFactoryCreateFromTypeCode ()
114    {
115       org.omg.CORBA.TypeCode JavaDoc tc = null;
116
117       tc = orb.create_union_tc
118       (
119          ID,
120          NAME,
121          EnumTypeHelper.type (),
122          getUnionMembers ()
123       );
124       createDynAnyFromTypeCode (tc);
125    }
126
127
128    /**
129     * Tests creating a DynAny object from a TypeCode object generated from
130     * IDL using the DynAnyFactory object.
131     */

132    public void testFactoryCreateFromIDLTypeCode ()
133    {
134       org.omg.CORBA.TypeCode JavaDoc tc = null;
135
136       tc = UnionDefaultTypeHelper.type ();
137       createDynAnyFromTypeCode (tc);
138    }
139
140
141    /**
142     * Test comparing DynAny values. The DynUnion does not have a named member.
143     */

144    public void testCompareDynAnyUnamedMember ()
145    {
146       String JavaDoc msg;
147       UnionNoDefaultType type;
148       org.omg.CORBA.Any JavaDoc any = null;
149       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
150       org.omg.DynamicAny.DynUnion JavaDoc dynAny2 = null;
151
152       type = new UnionNoDefaultType ();
153       type.__default ();
154       any = orb.create_any ();
155       UnionNoDefaultTypeHelper.insert (any, type);
156       dynAny = createDynAnyFromAny (any);
157       dynAny2 = createDynAnyFromAny (any);
158
159       msg = "Comparing two equal DynAny values using DynAny::equal failed";
160       assertTrue (msg, dynAny.equal (dynAny2));
161    }
162
163
164    /**
165     * Test comparing DynAny values. The DynUnion has a named member.
166     */

167    public void testCompareDynAnyNamedMember ()
168    {
169       String JavaDoc msg;
170       UnionDefaultType type;
171       org.omg.CORBA.Any JavaDoc any = null;
172       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
173       org.omg.DynamicAny.DynUnion JavaDoc dynAny2 = null;
174
175       type = new UnionDefaultType ();
176       type.win (10);
177       any = orb.create_any ();
178       UnionDefaultTypeHelper.insert (any, type);
179       dynAny = createDynAnyFromAny (any);
180       dynAny2 = createDynAnyFromAny (any);
181
182       msg = "Comparing two equal DynAny values using DynAny::equal failed";
183       assertTrue (msg, dynAny.equal (dynAny2));
184    }
185
186
187    /**
188     * Test iterating through components of a DynAny. The DynUnion has an
189     * active member.
190     */

191    public void testIterateDynAnyNamedMember ()
192    {
193       String JavaDoc msg;
194       int compCount = -1;
195       boolean seek;
196       org.omg.CORBA.TypeCode JavaDoc tc = null;
197       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
198       org.omg.DynamicAny.DynAny JavaDoc compSeek = null;
199       org.omg.DynamicAny.DynAny JavaDoc compRewind = null;
200
201       tc = orb.create_union_tc
202       (
203          ID,
204          NAME,
205          EnumTypeHelper.type (),
206          getUnionMembers ()
207       );
208       dynAny = createDynAnyFromTypeCode (tc);
209
210       // test the component count
211
msg = "The number of components returned from the ";
212       msg += "DynAny::component_count method is incorrect";
213       try
214       {
215          compCount = dynAny.component_count ();
216       }
217       catch (Throwable JavaDoc ex)
218       {
219          // should not be needed, but it prevents compiler errors
220
fail ("Unexpected error raised by DynAny::component_count operation");
221       }
222       assertEquals (msg, 2, compCount);
223
224       // seek an invalid position
225
msg = "The DynAny::seek operation indicates a valid current position ";
226       msg += "when the current position should be invalid";
227       seek = dynAny.seek (-1);
228       assertTrue (msg, !seek);
229
230       // seek the first position
231
msg = "The DynAny::seek operation indicates an invalid current position ";
232       msg += "when the current position should be valid";
233       seek = dynAny.seek (0);
234       assertTrue (msg, seek);
235
236       // extract a value from the current position
237
try
238       {
239          compSeek = dynAny.current_component ();
240       }
241       catch (Throwable JavaDoc ex)
242       {
243          msg = "Failed to get the current component using the ";
244          msg += "DynAny::current_component operation after calling the ";
245          msg += "DynAny::seek operation";
246          fail (msg);
247       }
248
249       // seek the next position
250
msg = "The DynAny::next operation indicates an invalid current position ";
251       msg += "when the current position should be valid";
252       seek = dynAny.next ();
253       assertTrue (msg, seek);
254
255       // return to the first position
256
dynAny.rewind ();
257
258       // extract a value from the current position
259
try
260       {
261          compRewind = dynAny.current_component ();
262       }
263       catch (Throwable JavaDoc ex)
264       {
265          msg = "Failed to get the current component using the ";
266          msg += "DynAny::current_component operation after calling the ";
267          msg += "DynAny::rewind operation";
268          fail (msg);
269       }
270       msg = "The component at DynAny::seek(0) is not equal to the ";
271       msg += "component at DynAny::rewind";
272       assertTrue (msg, compSeek.equal (compRewind));
273    }
274
275
276    /**
277     * Test iterating through components of a DynAny. The DynUnion does not
278     * have an active member.
279     */

280    public void testIterateDynAnyUnamedMember ()
281    {
282       String JavaDoc msg;
283       int compCount = -1;
284       boolean seek;
285       UnionNoDefaultType type;
286       org.omg.CORBA.Any JavaDoc any = null;
287       org.omg.CORBA.TypeCode JavaDoc tc = null;
288       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
289       org.omg.DynamicAny.DynEnum JavaDoc disc = null;
290
291       type = new UnionNoDefaultType ();
292       type.__default ();
293       any = orb.create_any ();
294       UnionNoDefaultTypeHelper.insert (any, type);
295       dynAny = createDynAnyFromAny (any);
296
297       // test that the discriminator is correctly set
298
disc = (org.omg.DynamicAny.DynEnum JavaDoc) dynAny.get_discriminator ();
299
300       // specific to IDL
301
msg = "The discriminator is incorrectly set for a DynAny created ";
302       msg += "from a union with no active member";
303       assertEquals (msg, EnumType._fifth, disc.get_as_ulong ());
304
305       // test the component count
306
try
307       {
308          compCount = dynAny.component_count ();
309       }
310       catch (Throwable JavaDoc ex)
311       {
312          // should not be needed, but it prevents compiler errors
313
fail ("Unexpected error raised by DynAny::component_count operation");
314       }
315
316       msg = "The number of components returned from the ";
317       msg += "DynAny::component_count operation is incorrect";
318       assertEquals (msg, 1, compCount);
319
320       // seek the first position
321
msg = "The DynAny::seek operation indicates an invalid current position ";
322       msg += "when the current position should be valid";
323       seek = dynAny.seek (0);
324       assertTrue (msg, seek);
325
326       // seek the next position
327
msg = "The DynAny::next operation indicates a valid current position ";
328       msg += "when the current position should be invalid";
329       seek = dynAny.next ();
330       assertTrue (msg, !seek);
331
332       // test getting the current component
333
msg = "The object returned from the DynAny::current_component ";
334       msg += "operation should be null because the current position is ";
335       msg += "invalid (-1)";
336       try
337       {
338          dynAny = (org.omg.DynamicAny.DynUnion JavaDoc) dynAny.current_component ();
339
340          assertNull (msg, dynAny);
341       }
342       catch (Throwable JavaDoc ex)
343       {
344          fail (msg + ": " + ex);
345       }
346    }
347
348
349    /**
350     * Test accessing the discriminator of a DynUnion object.
351     */

352    public void testAccessUnionDisc ()
353    {
354       String JavaDoc msg;
355       TCKind JavaDoc discKind = null;
356       org.omg.CORBA.TypeCode JavaDoc tc = null;
357       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
358       org.omg.DynamicAny.DynEnum JavaDoc disc = null;
359       org.omg.DynamicAny.DynAny JavaDoc invalidDisc = null;
360
361       tc = UnionDefaultTypeHelper.type ();
362       dynAny = createDynAnyFromTypeCode (tc);
363
364       // test setting the discriminator
365
try
366       {
367          disc = (org.omg.DynamicAny.DynEnum JavaDoc)
368             factory.create_dyn_any_from_type_code (EnumTypeHelper.type ());
369       }
370       catch (Throwable JavaDoc ex)
371       {
372          fail ("Failed to create DynAny with correct TypeCode: " + ex);
373       }
374
375       try
376       {
377          disc.set_as_string ("second"); // specific to IDL
378
}
379       catch (Throwable JavaDoc ex)
380       {
381          fail ("Failed to set value of discriminator: " + ex);
382       }
383
384       try
385       {
386          dynAny.set_discriminator (disc);
387       }
388       catch (Throwable JavaDoc ex)
389       {
390          msg = "Failed to set the value of the discriminator using the ";
391          msg += "DynUnion::set_discriminator operation";
392          fail (msg + ": " + ex);
393       }
394
395       // test getting the discriminator
396
msg = "Failed to get the correct value of the discriminator using the ";
397       msg += "DynUnion::get_discriminator operation";
398       disc = (org.omg.DynamicAny.DynEnum JavaDoc) dynAny.get_discriminator ();
399
400       // specific to IDL
401
assertEquals (msg, EnumType.second.value (), disc.get_as_ulong ());
402
403       // test getting the kind of the discriminator
404
msg = "Failed to get the correct kind of the discriminator using ";
405       msg += "DynUnion::discriminator_kind operation";
406       discKind = dynAny.discriminator_kind ();
407
408       // specific to IDL
409
assertEquals (msg, TCKind._tk_enum, discKind.value ());
410
411       // test setting an invalid discriminator
412
tc = orb.get_primitive_tc (TCKind.tk_long);
413       try
414       {
415          invalidDisc = factory.create_dyn_any_from_type_code (tc);
416       }
417       catch (Throwable JavaDoc ex)
418       {
419          fail ("Failed to create DynAny with incorrect TypeCode: " + ex);
420       }
421
422       try
423       {
424          dynAny.set_discriminator (invalidDisc);
425
426          msg = "Failed to throw a TypeMismatch exception when calling ";
427          msg += "DynStruct::set_discriminator operation with an incorrect ";
428          msg += "TypeCode";
429          fail (msg);
430       }
431       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
432       {
433          // success
434
}
435    }
436
437
438    /**
439     * Test setting the discriminator to the default member for a union with
440     * an explicit default case.
441     */

442    public void testUnionDefaultCase ()
443    {
444       String JavaDoc msg;
445       int compCount = -1;
446       UnionDefaultType type;
447       boolean hasNoActiveMember = true;
448       org.omg.CORBA.Any JavaDoc any = null;
449       org.omg.DynamicAny.DynEnum JavaDoc disc = null;
450       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
451
452       type = new UnionDefaultType ();
453       type.win (10);
454       any = orb.create_any ();
455       UnionDefaultTypeHelper.insert (any, type);
456       dynAny = createDynAnyFromAny (any);
457
458       // test activating the default member
459
msg = "Failed to set the discriminator to the default member using the ";
460       msg += "DynUnion::set_to_default_member operation";
461       try
462       {
463          dynAny.set_to_default_member ();
464       }
465       catch (Throwable JavaDoc ex)
466       {
467          fail (msg + ": " + ex);
468       }
469       disc = (org.omg.DynamicAny.DynEnum JavaDoc)dynAny.get_discriminator ();
470
471       // specific to IDL
472
// assertEquals (msg, (byte) 0, disc.to_any ().extract_octet ());
473
assertEquals (msg, EnumType.fifth.value (), disc.get_as_ulong ());
474
475       // test the component count
476
try
477       {
478          compCount = dynAny.component_count ();
479       }
480       catch (Throwable JavaDoc ex)
481       {
482          // should not be needed, but it prevents compiler errors
483
fail ("Unexpected error raised by DynAny::component_count operation");
484       }
485       msg = "Wrong number of components returned from DynAny::component_count ";
486       msg += "operation after calling DynUnion::set_to_default_member ";
487       msg += "operation";
488       assertEquals (msg, 2, compCount);
489
490       // test attempting to deactivate the default member
491
try
492       {
493          dynAny.set_to_no_active_member ();
494
495          msg = "Failed to raise a TypeMismatch exception when calling the ";
496          msg += "DynUnion::set_to_no_active_member operation on a union with ";
497          msg += "an explicit default case";
498          fail (msg);
499       }
500       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
501       {
502          // success
503
}
504
505       msg = "The DynUnion::has_no_active_member operation did not return ";
506       msg += "FALSE when called on a union with an explicit default case";
507       try
508       {
509          hasNoActiveMember = dynAny.has_no_active_member ();
510       }
511       catch (Throwable JavaDoc ex)
512       {
513          fail (msg + ": " + ex);
514       }
515       assertTrue (msg, !hasNoActiveMember);
516    }
517
518
519    /**
520     * Test setting the discriminator to no active member for a union without
521     * an explicit default case.
522     */

523    public void testUnionNoDefaultCase ()
524    {
525       String JavaDoc msg;
526       int compCount = -1;
527       UnionNoDefaultType type;
528       boolean hasNoActiveMember = false;
529       org.omg.CORBA.Any JavaDoc any = null;
530       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
531       org.omg.DynamicAny.DynEnum JavaDoc disc = null;
532
533       type = new UnionNoDefaultType ();
534       type.win (10);
535       any = orb.create_any ();
536       UnionNoDefaultTypeHelper.insert (any, type);
537       dynAny = createDynAnyFromAny (any);
538
539       // test deactivating the active member
540
msg = "Failed to set the discriminator to no active member using the ";
541       msg += "DynUnion::set_to_no_active_member operation";
542       try
543       {
544          dynAny.set_to_no_active_member ();
545       }
546       catch (Throwable JavaDoc ex)
547       {
548          fail (msg + ": " + ex);
549       }
550       disc = (org.omg.DynamicAny.DynEnum JavaDoc) dynAny.get_discriminator ();
551
552       // specific to IDL
553
assertEquals (msg, EnumType._fifth, disc.get_as_ulong ());
554
555       // test the component count
556
try
557       {
558          compCount = dynAny.component_count ();
559       }
560       catch (Throwable JavaDoc ex)
561       {
562          // should not be needed, but it prevents compiler errors
563
fail ("Unexpected error raised by DynAny::component_count operation");
564       }
565
566       msg = "Wrong number of components returned from DynAny::component_count ";
567       msg += "operation after calling DynUnion::set_to_no_active_member ";
568       msg += "operation";
569       assertEquals (msg, 1, compCount);
570
571       // test attempting to activate the default member
572
try
573       {
574          dynAny.set_to_default_member ();
575
576          msg = "Failed to raise a TypeMismatch exception when calling the ";
577          msg += "DynUnion::set_to_default_member operation on a union without ";
578          msg += "an explicit default case";
579          fail (msg);
580       }
581       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
582       {
583          // success
584
}
585
586       msg = "The DynUnion::has_no_active_member operation did not return ";
587       msg += "TRUE when called on a union without an explicit default case";
588       try
589       {
590          hasNoActiveMember = dynAny.has_no_active_member ();
591       }
592       catch (Throwable JavaDoc ex)
593       {
594          fail (msg + ": " + ex);
595       }
596       assertTrue (msg, hasNoActiveMember);
597    }
598
599
600    /**
601     * Test trying to set the discriminator to no active member or the default
602     * member for a union that uses the entire range of discriminator values.
603     */

604    public void testUnionFullRange ()
605    {
606       String JavaDoc msg;
607       int compCount;
608       UnionFullRangeType type;
609       boolean hasNoActiveMember = true;;
610       org.omg.CORBA.Any JavaDoc any = null;
611       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
612       int discVal; // specific to IDL
613

614       type = new UnionFullRangeType ();
615       type.win (10);
616       any = orb.create_any ();
617       UnionFullRangeTypeHelper.insert (any, type);
618       dynAny = createDynAnyFromAny (any);
619
620       // test attempting to deactivate the active member
621
try
622       {
623          dynAny.set_to_no_active_member ();
624
625          msg = "Failed to raise a TypeMismatch exception when calling the ";
626          msg += "DynUnion::set_to_no_active_member operation on a union ";
627          msg += "that uses the full range of discriminator values";
628          fail (msg);
629       }
630       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
631       {
632          // success
633
}
634
635       // test attempting to activate the default member
636
try
637       {
638          dynAny.set_to_default_member ();
639
640          msg = "Failed to raise a TypeMismatch exception when calling the ";
641          msg += "DynUnion::set_to_default_member operation on a union that ";
642          msg += "uses the full range of discriminator values";
643          fail (msg);
644       }
645       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
646       {
647          // success
648
}
649
650       msg = "The DynUnion::has_no_active_member operation did not return ";
651       msg += "FALSE when called on a union that uses the full range of ";
652       msg += "discriminator values";
653       try
654       {
655          hasNoActiveMember = dynAny.has_no_active_member ();
656       }
657       catch (Throwable JavaDoc ex)
658       {
659          fail (msg + ": " + ex);
660       }
661       assertTrue (msg, !hasNoActiveMember);
662    }
663
664
665    /**
666     * Test accessing the active member of a DynUnion object.
667     */

668    public void testAccessNamedUnionMember ()
669    {
670       String JavaDoc msg;
671       int testVal = 10;
672       UnionDefaultType type;
673       TCKind JavaDoc memberKind = null;
674       String JavaDoc memberName = null;
675       int memberVal = -1;
676       org.omg.CORBA.Any JavaDoc any = null;
677       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
678       org.omg.DynamicAny.DynAny JavaDoc member = null; // specific to IDL
679

680       type = new UnionDefaultType ();
681       type.win (testVal);
682       any = orb.create_any ();
683       UnionDefaultTypeHelper.insert (any, type);
684       dynAny = createDynAnyFromAny (any);
685
686       // test getting the kind of the active member
687
msg = "Failed to get the correct kind of the active member using ";
688       msg += "DynUnion::member_kind operation";
689       try
690       {
691          memberKind = dynAny.member_kind ();
692       }
693       catch (Throwable JavaDoc ex)
694       {
695          fail (msg + ": " + ex);
696       }
697
698       // specific to IDL
699
assertEquals (msg, TCKind._tk_long, memberKind.value ());
700
701       // test getting the name of the active member
702
msg = "Failed to get the correct name of the active member using ";
703       msg += "DynUnion::member_name operation";
704       try
705       {
706          memberName = dynAny.member_name ();
707       }
708       catch (Throwable JavaDoc ex)
709       {
710          fail (msg + ": " + ex);
711       }
712
713       assertEquals (msg, "win", memberName); // specific to IDL
714

715       // test getting the value of the active member
716
try
717       {
718          member = dynAny.member ();
719       }
720       catch (Throwable JavaDoc ex)
721       {
722          msg = "Failed to get the correct active union member using ";
723          msg += "DynUnion::member operation";
724          fail (msg + ": " + ex);
725       }
726
727       msg = "Failed to get the correct value of active union member using ";
728       msg += "DynUnion::member operation";
729       memberVal = member.to_any ().extract_long ();
730
731       // specific to IDL
732
assertEquals (msg, testVal, memberVal);
733    }
734
735
736    /**
737     * Test attempting to access the active member of a DynUnion object when
738     * there is no active member.
739     */

740    public void testAccessUnamedUnionMember ()
741    {
742       String JavaDoc msg;
743       org.omg.CORBA.TypeCode JavaDoc tc = null;
744       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
745
746       tc = UnionNoDefaultTypeHelper.type ();
747       dynAny = createDynAnyFromTypeCode (tc);
748
749       try
750       {
751          dynAny.set_to_no_active_member ();
752       }
753       catch (Throwable JavaDoc ex)
754       {
755          fail ("Failed to set the union to have no active member: " + ex);
756       }
757
758       // test attempting to get the kind with no active member
759
try
760       {
761          dynAny.member_kind ();
762
763          msg = "Failed to raise an InvalidValue exception when calling the ";
764          msg += "DynUnion::member_kind operation on a union with no active ";
765          msg += "member";
766          fail (msg);
767       }
768       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
769       {
770          // success
771
}
772
773       // test attempting to get the name with no active member
774
try
775       {
776          dynAny.member_name ();
777
778          msg = "Failed to raise an InvalidValue exception when calling the ";
779          msg += "DynUnion::member_name operation on a union with no active ";
780          msg += "member";
781          fail (msg);
782       }
783       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
784       {
785          // success
786
}
787
788       // test attempting to get the member with no active member
789
try
790       {
791          dynAny.member ();
792
793          msg = "Failed to raise an InvalidValue exception when calling the ";
794          msg += "DynUnion::member operation on a union with no active member";
795          fail (msg);
796       }
797       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
798       {
799          // success
800
}
801    }
802
803
804    /**
805     * Test obtaining the TypeCode associated with a DynAny object.
806     */

807    public void testDynAnyTypeCode ()
808    {
809       String JavaDoc msg;
810       org.omg.CORBA.TypeCode JavaDoc tc = null;
811       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
812
813       tc = orb.create_union_tc
814       (
815          ID,
816          NAME,
817          EnumTypeHelper.type (),
818          getUnionMembers ()
819       );
820       dynAny = createDynAnyFromTypeCode (tc);
821
822       msg = "Incorrect TypeCode retrieved from DynAny::type operation";
823       assertTrue (msg, dynAny.type ().equal (tc));
824    }
825
826
827    /**
828     * Test initializing a DynAny object from another DynAny object.
829     */

830    public void testInitDynAnyFromDynAny ()
831    {
832       String JavaDoc msg;
833       UnionDefaultType type;
834       org.omg.CORBA.Any JavaDoc any = null;
835       org.omg.CORBA.TypeCode JavaDoc tc = null;
836       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
837       org.omg.DynamicAny.DynUnion JavaDoc dynAny2 = null;
838
839       tc = UnionDefaultTypeHelper.type ();
840       dynAny = createDynAnyFromTypeCode (tc);
841
842       type = new UnionDefaultType ();
843       type.win (10);
844       any = orb.create_any ();
845       UnionDefaultTypeHelper.insert (any, type);
846       dynAny2 = createDynAnyFromAny (any);
847
848       msg = "Failed to initialize a DynAny object from another DynAny ";
849       msg += "object using the DynAny::assign operation";
850       try
851       {
852          dynAny.assign (dynAny2);
853       }
854       catch (Throwable JavaDoc ex)
855       {
856          fail (msg + ": " + ex);
857       }
858       assertTrue (msg, dynAny.equal (dynAny2));
859    }
860
861
862    /**
863     * Test initializing a DynAny object from an Any value.
864     */

865    public void testInitDynAnyFromAny ()
866    {
867       String JavaDoc msg;
868       UnionDefaultType type;
869       org.omg.CORBA.Any JavaDoc any = null;
870       org.omg.CORBA.TypeCode JavaDoc tc = null;
871       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
872       org.omg.DynamicAny.DynUnion JavaDoc dynAny2 = null;
873
874       tc = UnionDefaultTypeHelper.type ();
875       dynAny = createDynAnyFromTypeCode (tc);
876
877       type = new UnionDefaultType ();
878       type.win (10);
879       any = orb.create_any ();
880       UnionDefaultTypeHelper.insert (any, type);
881       dynAny2 = createDynAnyFromAny (any);
882
883       msg = "Failed to initialize a DynAny object from an Any object ";
884       msg += "using the DynAny::from_any operation";
885       try
886       {
887          dynAny.from_any (any);
888       }
889       catch (Throwable JavaDoc ex)
890       {
891          fail (msg + ": " + ex);
892       }
893       assertTrue (msg, dynAny.equal (dynAny2));
894    }
895
896
897    /**
898     * Test that a TypeMismatch exception is raised if there is a type
899     * mismatch between the DynAny and Any types in an assignment.
900     */

901    public void testInitFromAnyTypeMismatchEx ()
902    {
903       String JavaDoc msg;
904       org.omg.CORBA.Any JavaDoc any = null;
905       org.omg.CORBA.TypeCode JavaDoc tc = null;
906       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
907
908       any = orb.create_any ();
909       any.insert_string ("Hello");
910
911       tc = orb.create_union_tc
912       (
913          ID,
914          NAME,
915          EnumTypeHelper.type (),
916          getUnionMembers ()
917       );
918       dynAny = createDynAnyFromTypeCode (tc);
919
920       msg = "TypeMismatch exception not thrown by DynAny::from_any ";
921       msg += "operation when DynAny and Any operands have different types";
922       try
923       {
924          dynAny.from_any (any);
925
926          fail (msg);
927       }
928       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
929       {
930          // success
931
}
932       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
933       {
934          fail (msg + ": " + ex);
935       }
936    }
937
938
939    /**
940     * Test generating an Any value from a DynAny object.
941     */

942    public void testGenerateAnyFromDynAny ()
943    {
944       String JavaDoc msg;
945       org.omg.CORBA.Any JavaDoc any = null;
946       org.omg.CORBA.TypeCode JavaDoc tc = null;
947       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
948       org.omg.DynamicAny.DynUnion JavaDoc dynAny2 = null;
949
950       tc = UnionDefaultTypeHelper.type ();
951       dynAny = createDynAnyFromTypeCode (tc);
952
953       any = orb.create_any ();
954       any = dynAny.to_any ();
955       dynAny2 = createDynAnyFromAny (any);
956
957       msg = "The DynAny::to_any operation failed to create an Any ";
958       msg += "object with the same value as the DynAny object";
959       assertTrue (msg, dynAny.equal (dynAny2));
960    }
961
962
963    /**
964     * Test destroying a DynAny object.
965     */

966    public void testDestroyDynAny ()
967    {
968       String JavaDoc msg;
969       UnionDefaultType type;
970       org.omg.CORBA.Any JavaDoc any = null;
971       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
972
973       type = new UnionDefaultType ();
974       type.win (10);
975       any = orb.create_any ();
976       UnionDefaultTypeHelper.insert (any, type);
977       dynAny = createDynAnyFromAny (any);
978       dynAny.destroy ();
979
980       try
981       {
982          dynAny.type ();
983
984          msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
985          msg += "calling DynAny::type operation on a destroyed DynAny object ";
986          msg += "did not raise OBJECT_NOT_EXIST exception";
987          fail (msg);
988       }
989       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
990       {
991          // success
992
}
993
994       msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
995       msg += "calling DynAny::current_component operation on a destroyed ";
996       msg += "DynAny object did not raise OBJECT_NOT_EXIST exception";
997       try
998       {
999          dynAny.current_component ();
1000
1001         fail (msg);
1002      }
1003      catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
1004      {
1005         // success
1006
}
1007      catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
1008      {
1009         fail (msg + ": " + ex);
1010      }
1011   }
1012
1013
1014   /**
1015    * Test destroying a component of a DynAny object.
1016    */

1017   public void testDestroyComponent ()
1018   {
1019      String JavaDoc msg;
1020      UnionDefaultType type;
1021      org.omg.CORBA.Any JavaDoc any = null;
1022      org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
1023      org.omg.DynamicAny.DynAny JavaDoc comp = null;
1024
1025      type = new UnionDefaultType ();
1026      type.win (10);
1027      any = orb.create_any ();
1028      UnionDefaultTypeHelper.insert (any, type);
1029      dynAny = createDynAnyFromAny (any);
1030
1031      try
1032      {
1033         comp = dynAny.current_component ();
1034      }
1035      catch (Throwable JavaDoc ex)
1036      {
1037         msg = "Failed to get the current component of the DynAny using the ";
1038         msg += "DynAny::current_component operation before calling the ";
1039         msg += "DynAny::destroy operation";
1040         fail (msg + ": " + ex);
1041      }
1042
1043      comp.destroy ();
1044      try
1045      {
1046         comp = dynAny.current_component ();
1047      }
1048      catch (Throwable JavaDoc ex)
1049      {
1050         msg = "Failed to get the current component of the DynAny using the ";
1051         msg += "DynAny::current_component operation after calling the ";
1052         msg += "DynAny::destroy operation";
1053         fail (msg + ": " + ex);
1054      }
1055
1056      try
1057      {
1058         comp.type ();
1059      }
1060      catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
1061      {
1062         msg = "Calling destroy on a component resulted in destroying the ";
1063         msg += "component object";
1064         fail (msg + ": " + ex);
1065      }
1066   }
1067
1068
1069   /**
1070    * Test creating a copy of a DynAny object.
1071    */

1072   public void testCopyDynAny ()
1073   {
1074      String JavaDoc msg;
1075      org.omg.CORBA.TypeCode JavaDoc tc = null;
1076      org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
1077      org.omg.DynamicAny.DynUnion JavaDoc dynAny2 = null;
1078
1079      tc = UnionDefaultTypeHelper.type ();
1080      dynAny = createDynAnyFromTypeCode (tc);
1081      dynAny2 = (org.omg.DynamicAny.DynUnion JavaDoc) dynAny.copy ();
1082
1083      msg = "The DynAny object created with the DynAny::copy operation ";
1084      msg += "is not equal to the DynAny object it was copied from";
1085      assertTrue (msg, dynAny.equal (dynAny2));
1086   }
1087
1088
1089   private static class Setup extends TestSetup
1090   {
1091      public Setup (Test test)
1092      {
1093         super (test);
1094      }
1095
1096      protected void setUp ()
1097      {
1098         org.omg.CORBA.Object JavaDoc obj = null;
1099
1100         orb = ORBSetup.getORB ();
1101         try
1102         {
1103            obj = orb.resolve_initial_references ("DynAnyFactory");
1104         }
1105         catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc ex)
1106         {
1107            fail ("Failed to resolve DynAnyFactory: " + ex);
1108         }
1109         try
1110         {
1111            factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj);
1112         }
1113         catch (Throwable JavaDoc ex)
1114         {
1115            fail ("Failed to narrow to DynAnyFactory: " + ex);
1116         }
1117      }
1118
1119      protected void tearDown ()
1120      {
1121      }
1122   }
1123
1124
1125   /**
1126    * Create a DynAny object from an Any object.
1127    */

1128   private static org.omg.DynamicAny.DynUnion JavaDoc createDynAnyFromAny
1129      (org.omg.CORBA.Any JavaDoc any)
1130   {
1131      String JavaDoc msg;
1132      org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
1133
1134      try
1135      {
1136         dynAny = (org.omg.DynamicAny.DynUnion JavaDoc) factory.create_dyn_any (any);
1137      }
1138      catch (Throwable JavaDoc ex)
1139      {
1140         msg = "Factory failed to create DynAny from Any using ";
1141         msg += "DynAny::create_dyn_any operation: " + ex;
1142         fail (msg);
1143      }
1144      return dynAny;
1145   }
1146
1147
1148   /**
1149    * Create a DynAny object from a TypeCode object.
1150    */

1151   private static org.omg.DynamicAny.DynUnion JavaDoc createDynAnyFromTypeCode
1152      (org.omg.CORBA.TypeCode JavaDoc tc)
1153   {
1154      String JavaDoc msg;
1155      org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
1156
1157      try
1158      {
1159         dynAny = (org.omg.DynamicAny.DynUnion JavaDoc)
1160            factory.create_dyn_any_from_type_code (tc);
1161      }
1162      catch (Throwable JavaDoc ex)
1163      {
1164         msg = "Factory failed to create DynAny from TypeCode using ";
1165         msg += "DynAny::create_dyn_any_from_type_code operation: " + ex;
1166         fail (msg);
1167      }
1168      return dynAny;
1169   }
1170
1171
1172   /**
1173    * Create a sequence of fields for use in creating a union TypeCode.
1174    */

1175   private static org.omg.CORBA.UnionMember JavaDoc [] getUnionMembers ()
1176   {
1177      final org.omg.CORBA.UnionMember JavaDoc [] members =
1178         new org.omg.CORBA.UnionMember JavaDoc [2];
1179      org.omg.CORBA.Any JavaDoc any = null;
1180
1181      any = orb.create_any ();
1182      EnumTypeHelper.insert (any, EnumType.first);
1183      members [0] = new org.omg.CORBA.UnionMember JavaDoc
1184      (
1185         "zero",
1186         any,
1187         orb.get_primitive_tc (TCKind.tk_string),
1188         null
1189      );
1190
1191      any = orb.create_any ();
1192      EnumTypeHelper.insert (any, EnumType.second);
1193
1194      // EnumTypeHelper.insert (any, EnumType.first);
1195
members [1] = new org.omg.CORBA.UnionMember JavaDoc
1196      (
1197         "one",
1198         any,
1199         orb.get_primitive_tc (TCKind.tk_char),
1200         null
1201      );
1202
1203      return members;
1204   }
1205
1206}
1207
Popular Tags