KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Method JavaDoc;
28
29 import org.jacorb.test.common.ORBSetup;
30 import org.jacorb.test.UnionDefaultIntType;
31 import org.jacorb.test.UnionDefaultIntTypeHelper;
32 import org.jacorb.test.UnionNoDefaultIntType;
33 import org.jacorb.test.UnionNoDefaultIntTypeHelper;
34
35 /**
36  * DynAnyUnionIntTest.java
37  *
38  * DynAny tests for union types using an integer discriminator.
39  *
40  */

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

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

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

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

141    public void testCompareDynAnyUnamedMember ()
142    {
143       String JavaDoc msg;
144       UnionNoDefaultIntType type;
145       org.omg.CORBA.Any JavaDoc any = null;
146       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
147       org.omg.DynamicAny.DynUnion JavaDoc dynAny2 = null;
148
149       type = new UnionNoDefaultIntType ();
150
151       // use reflection to avoid ORB portability issues
152
Class JavaDoc unionClass = type.getClass ();
153       Method JavaDoc method = null;
154
155       // determine method to invoke
156
try
157       {
158          method = unionClass.getDeclaredMethod ("__default", new Class JavaDoc [0]);
159       }
160       catch (NoSuchMethodException JavaDoc ex)
161       {
162          try
163          {
164             method = unionClass.getDeclaredMethod ("_default", new Class JavaDoc [0]);
165          }
166          catch (Throwable JavaDoc e)
167          {
168             msg = "Unexpected error trying to determine method to invoke: " + e;
169             fail (msg);
170          }
171       }
172
173       // invoke the method
174
try
175       {
176          method.invoke (type, new Object JavaDoc [0]);
177       }
178       catch (Throwable JavaDoc ex)
179       {
180          fail ("Failed to invoke method via reflection: " + ex);
181       }
182
183       any = orb.create_any ();
184       UnionNoDefaultIntTypeHelper.insert (any, type);
185       dynAny = createDynAnyFromAny (any);
186       dynAny2 = createDynAnyFromAny (any);
187
188       msg = "Comparing two equal DynAny values using DynAny::equal failed";
189       assertTrue (msg, dynAny.equal (dynAny2));
190    }
191
192
193    /**
194     * Test comparing DynAny values. The DynUnion has a named member.
195     */

196    public void testCompareDynAnyNamedMember ()
197    {
198       String JavaDoc msg;
199       UnionDefaultIntType type;
200       org.omg.CORBA.Any JavaDoc any = null;
201       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
202       org.omg.DynamicAny.DynUnion JavaDoc dynAny2 = null;
203
204       type = new UnionDefaultIntType ();
205       type.win (10);
206       any = orb.create_any ();
207       UnionDefaultIntTypeHelper.insert (any, type);
208       dynAny = createDynAnyFromAny (any);
209       dynAny2 = createDynAnyFromAny (any);
210
211       msg = "Comparing two equal DynAny values using DynAny::equal failed";
212       assertTrue (msg, dynAny.equal (dynAny2));
213    }
214
215
216    /**
217     * Test iterating through components of a DynAny. The DynUnion has an
218     * active member.
219     */

220    public void testIterateDynAnyNamedMember ()
221    {
222       String JavaDoc msg;
223       int compCount = -1;
224       boolean seek;
225       org.omg.CORBA.TypeCode JavaDoc tc = null;
226       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
227       org.omg.DynamicAny.DynAny JavaDoc compSeek = null;
228       org.omg.DynamicAny.DynAny JavaDoc compRewind = null;
229
230       tc = orb.create_union_tc
231       (
232          ID,
233          NAME,
234          orb.get_primitive_tc (TCKind.tk_short),
235          getUnionMembers ()
236       );
237       dynAny = createDynAnyFromTypeCode (tc);
238
239       // test the component count
240
msg = "The number of components returned from the ";
241       msg += "DynAny::component_count method is incorrect";
242       try
243       {
244          compCount = dynAny.component_count ();
245       }
246       catch (Throwable JavaDoc ex)
247       {
248          // should not be needed, but it prevents compiler errors
249
fail ("Unexpected error raised by DynAny::component_count operation");
250       }
251       assertEquals (msg, 2, compCount);
252
253       // seek an invalid position
254
msg = "The DynAny::seek operation indicates a valid current position ";
255       msg += "when the current position should be invalid";
256       seek = dynAny.seek (-1);
257       assertTrue (msg, !seek);
258
259       // seek the first position
260
msg = "The DynAny::seek operation indicates an invalid current position ";
261       msg += "when the current position should be valid";
262       seek = dynAny.seek (0);
263       assertTrue (msg, seek);
264
265       // extract a value from the current position
266
try
267       {
268          compSeek = dynAny.current_component ();
269       }
270       catch (Throwable JavaDoc ex)
271       {
272          msg = "Failed to get the current component using the ";
273          msg += "DynAny::current_component operation after calling the ";
274          msg += "DynAny::seek operation";
275          fail (msg);
276       }
277
278       // seek the next position
279
msg = "The DynAny::next operation indicates an invalid current position ";
280       msg += "when the current position should be valid";
281       seek = dynAny.next ();
282       assertTrue (msg, seek);
283
284       // return to the first position
285
dynAny.rewind ();
286
287       // extract a value from the current position
288
try
289       {
290          compRewind = dynAny.current_component ();
291       }
292       catch (Throwable JavaDoc ex)
293       {
294          msg = "Failed to get the current component using the ";
295          msg += "DynAny::current_component operation after calling the ";
296          msg += "DynAny::rewind operation";
297          fail (msg);
298       }
299       msg = "The component at DynAny::seek(0) is not equal to the ";
300       msg += "component at DynAny::rewind";
301       assertTrue (msg, compSeek.equal (compRewind));
302    }
303
304
305    /**
306     * Test iterating through components of a DynAny. The DynUnion does not
307     * have an active member.
308     */

309    public void testIterateDynAnyUnamedMember ()
310    {
311       String JavaDoc msg;
312       int compCount = -1;
313       boolean seek;
314       UnionNoDefaultIntType type;
315       org.omg.CORBA.Any JavaDoc any = null;
316       org.omg.CORBA.TypeCode JavaDoc tc = null;
317       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
318       org.omg.DynamicAny.DynAny JavaDoc disc = null;
319
320       type = new UnionNoDefaultIntType ();
321
322       // use reflection to avoid ORB portability issues
323
Class JavaDoc unionClass = type.getClass ();
324       Method JavaDoc method = null;
325
326       // determine method to invoke
327
try
328       {
329          method = unionClass.getDeclaredMethod ("__default", new Class JavaDoc [0]);
330       }
331       catch (NoSuchMethodException JavaDoc ex)
332       {
333          try
334          {
335             method = unionClass.getDeclaredMethod ("_default", new Class JavaDoc [0]);
336          }
337          catch (Throwable JavaDoc e)
338          {
339             msg = "Unexpected error trying to determine method to invoke: " + e;
340             fail (msg);
341          }
342       }
343
344       // invoke the method
345
try
346       {
347          method.invoke (type, new Object JavaDoc [0]);
348       }
349       catch (Throwable JavaDoc ex)
350       {
351          fail ("Failed to invoke method via reflection: " + ex);
352       }
353
354       any = orb.create_any ();
355       UnionNoDefaultIntTypeHelper.insert (any, type);
356       dynAny = createDynAnyFromAny (any);
357
358       // test that the discriminator is correctly set
359
disc = dynAny.get_discriminator ();
360
361       // specific to IDL
362
msg = "The discriminator is incorrectly set for a DynAny created ";
363       msg += "from a union with no active member";
364       assertEquals (msg, (short) 4, disc.to_any ().extract_short ());
365
366       // test the component count
367
try
368       {
369          compCount = dynAny.component_count ();
370       }
371       catch (Throwable JavaDoc ex)
372       {
373          // should not be needed, but it prevents compiler errors
374
fail ("Unexpected error raised by DynAny::component_count operation");
375       }
376
377       msg = "The number of components returned from the ";
378       msg += "DynAny::component_count operation is incorrect";
379       assertEquals (msg, 1, compCount);
380
381       // seek the first position
382
msg = "The DynAny::seek operation indicates an invalid current position ";
383       msg += "when the current position should be valid";
384       seek = dynAny.seek (0);
385       assertTrue (msg, seek);
386
387       // seek the next position
388
msg = "The DynAny::next operation indicates a valid current position ";
389       msg += "when the current position should be invalid";
390       seek = dynAny.next ();
391       assertTrue (msg, !seek);
392
393       // test getting the current component
394
msg = "The object returned from the DynAny::current_component ";
395       msg += "operation should be null because the current position is ";
396       msg += "invalid (-1)";
397       try
398       {
399          dynAny = (org.omg.DynamicAny.DynUnion JavaDoc) dynAny.current_component ();
400
401          assertNull (msg, dynAny);
402       }
403       catch (Throwable JavaDoc ex)
404       {
405          fail (msg + ": " + ex);
406       }
407    }
408
409
410    /**
411     * Test accessing the discriminator of a DynUnion object.
412     */

413    public void testAccessUnionDisc ()
414    {
415       String JavaDoc msg;
416       TCKind JavaDoc discKind = null;
417       org.omg.CORBA.TypeCode JavaDoc tc = null;
418       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
419       org.omg.DynamicAny.DynAny JavaDoc disc = null;
420       org.omg.DynamicAny.DynAny JavaDoc invalidDisc = null;
421
422       tc = UnionDefaultIntTypeHelper.type ();
423       dynAny = createDynAnyFromTypeCode (tc);
424
425       // test setting the discriminator
426
tc = orb.get_primitive_tc (TCKind.tk_short);
427       try
428       {
429          disc = factory.create_dyn_any_from_type_code (tc);
430       }
431       catch (Throwable JavaDoc ex)
432       {
433          fail ("Failed to create DynAny with correct TypeCode: " + ex);
434       }
435
436       try
437       {
438          disc.insert_short ((short) 1); // specific to IDL
439
}
440       catch (Throwable JavaDoc ex)
441       {
442          fail ("Failed to set value of discriminator: " + ex);
443       }
444
445       try
446       {
447          dynAny.set_discriminator (disc);
448       }
449       catch (Throwable JavaDoc ex)
450       {
451          msg = "Failed to set the value of the discriminator using the ";
452          msg += "DynUnion::set_discriminator operation";
453          fail (msg + ": " + ex);
454       }
455
456       // test getting the discriminator
457
msg = "Failed to get the correct value of the discriminator using the ";
458       msg += "DynUnion::get_discriminator operation";
459       disc = dynAny.get_discriminator ();
460
461       // specific to IDL
462
assertEquals (msg, (short) 1, disc.to_any ().extract_short ());
463
464       // test getting the kind of the discriminator
465
msg = "Failed to get the correct kind of the discriminator using ";
466       msg += "DynUnion::discriminator_kind operation";
467       discKind = dynAny.discriminator_kind ();
468
469       // specific to IDL
470
assertEquals (msg, TCKind._tk_short, discKind.value ());
471
472       // test setting an invalid discriminator
473
tc = orb.get_primitive_tc (TCKind.tk_boolean);
474       try
475       {
476          invalidDisc = factory.create_dyn_any_from_type_code (tc);
477       }
478       catch (Throwable JavaDoc ex)
479       {
480          fail ("Failed to create DynAny with incorrect TypeCode: " + ex);
481       }
482
483       try
484       {
485          dynAny.set_discriminator (invalidDisc);
486
487          msg = "Failed to throw a TypeMismatch exception when calling ";
488          msg += "DynStruct::set_discriminator operation with an incorrect ";
489          msg += "TypeCode";
490          fail (msg);
491       }
492       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
493       {
494          // success
495
}
496    }
497
498
499    /**
500     * Test setting the discriminator to the default member for a union with
501     * an explicit default case.
502     */

503    public void testUnionDefaultCase ()
504    {
505       String JavaDoc msg;
506       int compCount = -1;
507       short discVal = 0;
508       UnionDefaultIntType type;
509       boolean hasNoActiveMember = true;
510       org.omg.CORBA.Any JavaDoc any = null;
511       org.omg.DynamicAny.DynAny JavaDoc disc = null;
512       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
513
514       type = new UnionDefaultIntType ();
515       type.win (10);
516       any = orb.create_any ();
517       UnionDefaultIntTypeHelper.insert (any, type);
518       dynAny = createDynAnyFromAny (any);
519
520       // test activating the default member
521
msg = "Failed to set the discriminator to the default member using the ";
522       msg += "DynUnion::set_to_default_member operation";
523       try
524       {
525          dynAny.set_to_default_member ();
526       }
527       catch (Throwable JavaDoc ex)
528       {
529          fail (msg + ": " + ex);
530       }
531       disc = dynAny.get_discriminator ();
532
533       // specific to IDL
534
// assertEquals (msg, (byte) 0, disc.to_any ().extract_octet ());
535

536       // specific to IDL
537
discVal = disc.to_any ().extract_short ();
538       assertTrue (msg, discVal < 0 || discVal > 3);
539
540       // test the component count
541
try
542       {
543          compCount = dynAny.component_count ();
544       }
545       catch (Throwable JavaDoc ex)
546       {
547          // should not be needed, but it prevents compiler errors
548
fail ("Unexpected error raised by DynAny::component_count operation");
549       }
550       msg = "Wrong number of components returned from DynAny::component_count ";
551       msg += "operation after calling DynUnion::set_to_default_member ";
552       msg += "operation";
553       assertEquals (msg, 2, compCount);
554
555       // test attempting to deactivate the default member
556
try
557       {
558          dynAny.set_to_no_active_member ();
559
560          msg = "Failed to raise a TypeMismatch exception when calling the ";
561          msg += "DynUnion::set_to_no_active_member operation on a union with ";
562          msg += "an explicit default case";
563          fail (msg);
564       }
565       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
566       {
567          // success
568
}
569
570       msg = "The DynUnion::has_no_active_member operation did not return ";
571       msg += "FALSE when called on a union with an explicit default case";
572       try
573       {
574          hasNoActiveMember = dynAny.has_no_active_member ();
575       }
576       catch (Throwable JavaDoc ex)
577       {
578          fail (msg + ": " + ex);
579       }
580       assertTrue (msg, !hasNoActiveMember);
581    }
582
583
584    /**
585     * Test setting the discriminator to no active member for a union without
586     * an explicit default case.
587     */

588    public void testUnionNoDefaultCase ()
589    {
590       String JavaDoc msg;
591       int compCount = -1;
592       UnionNoDefaultIntType type;
593       boolean hasNoActiveMember = false;
594       org.omg.CORBA.Any JavaDoc any = null;
595       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
596       org.omg.DynamicAny.DynAny JavaDoc disc = null;
597
598       type = new UnionNoDefaultIntType ();
599       type.win (10);
600       any = orb.create_any ();
601       UnionNoDefaultIntTypeHelper.insert (any, type);
602       dynAny = createDynAnyFromAny (any);
603
604       // test deactivating the active member
605
msg = "Failed to set the discriminator to no active member using the ";
606       msg += "DynUnion::set_to_no_active_member operation";
607       try
608       {
609          dynAny.set_to_no_active_member ();
610       }
611       catch (Throwable JavaDoc ex)
612       {
613          fail (msg + ": " + ex);
614       }
615       disc = dynAny.get_discriminator ();
616
617       // specific to IDL
618
assertEquals (msg, (short) 4, disc.to_any ().extract_short ());
619
620       // test the component count
621
try
622       {
623          compCount = dynAny.component_count ();
624       }
625       catch (Throwable JavaDoc ex)
626       {
627          // should not be needed, but it prevents compiler errors
628
fail ("Unexpected error raised by DynAny::component_count operation");
629       }
630
631       msg = "Wrong number of components returned from DynAny::component_count ";
632       msg += "operation after calling DynUnion::set_to_no_active_member ";
633       msg += "operation";
634       assertEquals (msg, 1, compCount);
635
636       // test attempting to activate the default member
637
try
638       {
639          dynAny.set_to_default_member ();
640
641          msg = "Failed to raise a TypeMismatch exception when calling the ";
642          msg += "DynUnion::set_to_default_member operation on a union without ";
643          msg += "an explicit default case";
644          fail (msg);
645       }
646       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
647       {
648          // success
649
}
650
651       msg = "The DynUnion::has_no_active_member operation did not return ";
652       msg += "TRUE when called on a union without an explicit default case";
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       UnionDefaultIntType 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 UnionDefaultIntType ();
681       type.win (testVal);
682       any = orb.create_any ();
683       UnionDefaultIntTypeHelper.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 = UnionNoDefaultIntTypeHelper.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          orb.get_primitive_tc (TCKind.tk_short),
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       UnionDefaultIntType 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 = UnionDefaultIntTypeHelper.type ();
840       dynAny = createDynAnyFromTypeCode (tc);
841
842       type = new UnionDefaultIntType ();
843       type.win (10);
844       any = orb.create_any ();
845       UnionDefaultIntTypeHelper.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       UnionDefaultIntType 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 = UnionDefaultIntTypeHelper.type ();
875       dynAny = createDynAnyFromTypeCode (tc);
876
877       type = new UnionDefaultIntType ();
878       type.win (10);
879       any = orb.create_any ();
880       UnionDefaultIntTypeHelper.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          orb.get_primitive_tc (TCKind.tk_short),
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 = UnionDefaultIntTypeHelper.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       UnionDefaultIntType type;
970       org.omg.CORBA.Any JavaDoc any = null;
971       org.omg.DynamicAny.DynUnion JavaDoc dynAny = null;
972
973       type = new UnionDefaultIntType ();
974       type.win (10);
975       any = orb.create_any ();
976       UnionDefaultIntTypeHelper.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      UnionDefaultIntType 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 UnionDefaultIntType ();
1026      type.win (10);
1027      any = orb.create_any ();
1028      UnionDefaultIntTypeHelper.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 = UnionDefaultIntTypeHelper.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      any.insert_short ((short) 0);
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      any.insert_short ((short) 1);
1193      members [1] = new org.omg.CORBA.UnionMember JavaDoc
1194      (
1195         "one",
1196         any,
1197         orb.get_primitive_tc (TCKind.tk_char),
1198         null
1199      );
1200
1201      return members;
1202   }
1203
1204}
1205
Popular Tags