KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import org.omg.CORBA.TCKind JavaDoc;
27 import org.omg.DynamicAny.NameValuePair JavaDoc;
28 import org.omg.DynamicAny.NameDynAnyPair JavaDoc;
29
30 import org.jacorb.test.common.ORBSetup;
31 import org.jacorb.test.NonEmptyException;
32 import org.jacorb.test.NonEmptyExceptionHelper;
33
34 /**
35  * DynAnyNonEmptyExTest.java
36  *
37  * DynAny tests for (non-empty) exception types.
38  *
39  */

40
41 public class DynAnyNonEmptyExTest extends TestCase
42 {
43    private static org.omg.DynamicAny.DynAnyFactory JavaDoc factory = null;
44    private static org.omg.CORBA.ORB JavaDoc orb = null;
45
46    private static final String JavaDoc ID = "IDL:test:1.0";
47    private static final String JavaDoc NAME = "MyNonEmptyException";
48
49    public DynAnyNonEmptyExTest (String JavaDoc name)
50    {
51       super (name);
52    }
53
54
55    public static Test suite ()
56    {
57       TestSuite suite = new TestSuite ("Non-empty Exception DynStruct Tests");
58       Setup setup = new Setup (suite);
59       ORBSetup osetup = new ORBSetup (setup);
60
61       suite.addTest (new DynAnyNonEmptyExTest ("testFactoryCreateFromAny"));
62       suite.addTest (new DynAnyNonEmptyExTest ("testFactoryCreateFromTypeCode"));
63       suite.addTest (new DynAnyNonEmptyExTest ("testFactoryCreateFromIDLTypeCode"));
64       suite.addTest (new DynAnyNonEmptyExTest ("testCompareDynAny"));
65       suite.addTest (new DynAnyNonEmptyExTest ("testIterateDynAny"));
66       suite.addTest (new DynAnyNonEmptyExTest ("testAccessStructMembers"));
67       suite.addTest (new DynAnyNonEmptyExTest ("testAccessStructNameValuePairs"));
68       suite.addTest (new DynAnyNonEmptyExTest ("testAccessStructDynAnyPairs"));
69       suite.addTest (new DynAnyNonEmptyExTest ("testAccessStructPairsEx"));
70       suite.addTest (new DynAnyNonEmptyExTest ("testDynAnyTypeCode"));
71       suite.addTest (new DynAnyNonEmptyExTest ("testInitDynAnyFromDynAny"));
72       suite.addTest (new DynAnyNonEmptyExTest ("testInitDynAnyFromAny"));
73       suite.addTest (new DynAnyNonEmptyExTest ("testInitFromAnyTypeMismatchEx"));
74       suite.addTest (new DynAnyNonEmptyExTest ("testGenerateAnyFromDynAny"));
75       suite.addTest (new DynAnyNonEmptyExTest ("testDestroyDynAny"));
76       suite.addTest (new DynAnyNonEmptyExTest ("testDestroyComponent"));
77       suite.addTest (new DynAnyNonEmptyExTest ("testCopyDynAny"));
78
79       return osetup;
80    }
81
82
83    /**
84     * Tests creating a DynAny object from an Any object using the
85     * DynAnyFactory object.
86     */

87    public void testFactoryCreateFromAny ()
88    {
89       NonEmptyException type = null;
90       org.omg.CORBA.Any JavaDoc any = null;
91
92       type = new NonEmptyException (1, "Hello");
93       any = orb.create_any ();
94       NonEmptyExceptionHelper.insert (any, type);
95
96       createDynAnyFromAny (any);
97    }
98
99
100    /**
101     * Tests creating a DynAny object from a TypeCode object using the
102     * DynAnyFactory object.
103     */

104    public void testFactoryCreateFromTypeCode ()
105    {
106       org.omg.CORBA.TypeCode JavaDoc tc = null;
107
108       tc = orb.create_exception_tc (ID, NAME, getExceptionMembers ());
109       createDynAnyFromTypeCode (tc);
110    }
111
112
113    /**
114     * Tests creating a DynAny object from a TypeCode object generated from
115     * IDL using the DynAnyFactory object.
116     */

117    public void testFactoryCreateFromIDLTypeCode ()
118    {
119       org.omg.CORBA.TypeCode JavaDoc tc = null;
120
121       tc = NonEmptyExceptionHelper.type ();
122       createDynAnyFromTypeCode (tc);
123    }
124
125
126    /**
127     * Test comparing DynAny values.
128     */

129    public void testCompareDynAny ()
130    {
131       String JavaDoc msg;
132       NonEmptyException type;
133       org.omg.CORBA.Any JavaDoc any = null;
134       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
135       org.omg.DynamicAny.DynStruct JavaDoc dynAny2 = null;
136
137       type = new NonEmptyException (1, "Hello");
138       any = orb.create_any ();
139       NonEmptyExceptionHelper.insert (any, type);
140       dynAny = createDynAnyFromAny (any);
141       dynAny2 = createDynAnyFromAny (any);
142
143       msg = "Comparing two equal DynAny values using DynAny::equal failed";
144       assertTrue (msg, dynAny.equal (dynAny2));
145    }
146
147
148    /**
149     * Test iterating through components of a DynAny.
150     */

151    public void testIterateDynAny ()
152    {
153       String JavaDoc msg;
154       int compCount = -1;
155       boolean seek;
156       org.omg.CORBA.TypeCode JavaDoc tc = null;
157       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
158       org.omg.DynamicAny.DynAny JavaDoc compSeek = null;
159       org.omg.DynamicAny.DynAny JavaDoc compRewind = null;
160
161       tc = orb.create_exception_tc (ID, NAME, getExceptionMembers ());
162       dynAny = createDynAnyFromTypeCode (tc);
163
164       // test the component count
165
msg = "The number of components returned from the ";
166       msg += "DynAny::component_count method is incorrect";
167       try
168       {
169          compCount = dynAny.component_count ();
170       }
171       catch (Throwable JavaDoc ex)
172       {
173          // should not be needed, but it prevents compiler errors
174
fail ("Unexpected error raised by DynAny::component_count operation");
175       }
176       assertEquals (msg, 2, compCount); // specific to IDL
177

178       // seek an invalid position
179
msg = "The DynAny::seek operation indicates a valid current position ";
180       msg += "when the current position should be invalid";
181       seek = dynAny.seek (-1);
182       assertTrue (msg, !seek);
183
184       // seek the first position
185
msg = "The DynAny::seek operation indicates an invalid current position ";
186       msg += "when the current position should be valid";
187       seek = dynAny.seek (0);
188       assertTrue (msg, seek);
189
190       // extract a value from the current position
191
try
192       {
193          compSeek = dynAny.current_component ();
194       }
195       catch (Throwable JavaDoc ex)
196       {
197          msg = "Failed to get the current component using the ";
198          msg += "DynAny::current_component operation after calling the ";
199          msg += "DynAny::seek operation";
200          fail (msg);
201       }
202
203       // seek the next position
204
msg = "The DynAny::next operation indicates an invalid current position ";
205       msg += "when the current position should be valid";
206       seek = dynAny.next ();
207       assertTrue (msg, seek);
208
209       // return to the first position
210
dynAny.rewind ();
211
212       // extract a value from the current position
213
try
214       {
215          compRewind = dynAny.current_component ();
216       }
217       catch (Throwable JavaDoc ex)
218       {
219          msg = "Failed to get the current component using the ";
220          msg += "DynAny::current_component operation after calling the ";
221          msg += "DynAny::rewind operation";
222          fail (msg);
223       }
224       msg = "The component at DynAny::seek(0) is not equal to the ";
225       msg += "component at DynAny::rewind";
226       assertTrue (msg, compSeek.equal (compRewind));
227    }
228
229
230    /**
231     * Test accessing the names and types of members in a DynStruct object.
232     */

233    public void testAccessStructMembers ()
234    {
235       String JavaDoc msg;
236       String JavaDoc memberName = null;
237       TCKind JavaDoc memberKind = null;
238       org.omg.CORBA.TypeCode JavaDoc tc = null;
239       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
240
241       tc = NonEmptyExceptionHelper.type ();
242       dynAny = createDynAnyFromTypeCode (tc);
243
244       // test getting the name of the current member
245
msg = "Failed to get the correct name of the first member using ";
246       msg += "DynStruct::current_member_name operation";
247       try
248       {
249          memberName = dynAny.current_member_name ();
250
251          assertEquals (msg, "field1", memberName); // specific to IDL
252
}
253       catch (AssertionFailedError ex)
254       {
255          throw ex;
256       }
257       catch (Throwable JavaDoc ex)
258       {
259          fail (msg + ": " + ex);
260       }
261
262       // test getting the kind of the current member
263
msg = "Failed to get the correct kind of the first member using ";
264       msg += "DynStruct::current_member_kind operation";
265       try
266       {
267          memberKind = dynAny.current_member_kind ();
268
269          // specific to IDL
270
assertEquals (msg, TCKind._tk_long, memberKind.value ());
271       }
272       catch (AssertionFailedError ex)
273       {
274          throw ex;
275       }
276       catch (Throwable JavaDoc ex)
277       {
278          fail (msg + ": " + ex);
279       }
280
281       // move to the next position
282
dynAny.next ();
283
284       // test getting the name of the current member
285
msg = "Failed to get the correct name of the second member using ";
286       msg += "DynStruct::current_member_name operation";
287       try
288       {
289          memberName = dynAny.current_member_name ();
290
291          assertEquals (msg, "field2", memberName); // specific to IDL
292
}
293       catch (AssertionFailedError ex)
294       {
295          throw ex;
296       }
297       catch (Throwable JavaDoc ex)
298       {
299          fail (msg + ": " + ex);
300       }
301
302       // test getting the kind of the current member
303
msg = "Failed to get the correct kind of the second member using ";
304       msg += "DynStruct::current_member_kind operation";
305       try
306       {
307          memberKind = dynAny.current_member_kind ();
308
309          // specific to IDL
310
assertEquals (msg, TCKind._tk_string, memberKind.value ());
311       }
312       catch (AssertionFailedError ex)
313       {
314          throw ex;
315       }
316       catch (Throwable JavaDoc ex)
317       {
318          fail (msg + ": " + ex);
319       }
320
321       // move to an invalid position
322
dynAny.seek (-1);
323
324       // test getting the name of the current member
325
msg = "Failed to throw an InvalidValue exception when calling ";
326       msg += "DynStruct::current_member_name operation at an invalid position";
327       try
328       {
329          memberName = dynAny.current_member_name ();
330
331          fail (msg);
332       }
333       catch (AssertionFailedError ex)
334       {
335          throw ex;
336       }
337       catch (Throwable JavaDoc ex)
338       {
339          if (!(ex instanceof org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc))
340          {
341             fail (msg + ": " + ex);
342          }
343       }
344
345       // test getting the kind of the current member
346
msg = "Failed to throw an InvalidValue exception when calling ";
347       msg += "DynStruct::current_member_kind operation at an invalid position";
348       try
349       {
350          memberKind = dynAny.current_member_kind ();
351
352          fail (msg);
353       }
354       catch (AssertionFailedError ex)
355       {
356          throw ex;
357       }
358       catch (Throwable JavaDoc ex)
359       {
360          if (!(ex instanceof org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc))
361          {
362             fail (msg + ": " + ex);
363          }
364       }
365    }
366
367
368    /**
369     * Test accessing the member Name/Value pairs in a DynStruct object.
370     */

371    public void testAccessStructNameValuePairs ()
372    {
373       final String JavaDoc name1 = "field1"; // specific to IDL
374
final String JavaDoc name2 = "field2"; // specific to IDL
375
String JavaDoc msg;
376       org.omg.CORBA.Any JavaDoc any = null;
377       NonEmptyException type = null;
378       int oldInt = 1;
379       String JavaDoc oldStr = "Hello";
380       int newInt = 2;
381       String JavaDoc newStr = "GoodBye";
382       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
383       org.omg.DynamicAny.NameValuePair JavaDoc [] pairs = null;
384
385       type = new NonEmptyException (oldInt, oldStr);
386       any = orb.create_any ();
387       NonEmptyExceptionHelper.insert (any, type);
388       dynAny = createDynAnyFromAny (any);
389
390       // test extracting the name/value pairs as Anys
391
pairs = dynAny.get_members ();
392       msg = "Old name associated with first name/value pair is incorrect";
393       assertEquals (msg, name1, pairs [0].id);
394       msg = "Old value associated with first name/value pair is incorrect";
395       assertEquals (msg, oldInt, pairs [0].value.extract_long ());
396       msg = "Old name associated with second name/value pair is incorrect";
397       assertEquals (msg, name2, pairs [1].id);
398       msg = "Old value associated with second name/value pair is incorrect";
399       assertEquals (msg, oldStr, pairs [1].value.extract_string ());
400
401       pairs = new NameValuePair JavaDoc [2]; // specific to IDL
402
any = orb.create_any ();
403       any.insert_long (newInt);
404       pairs [0] = new NameValuePair JavaDoc (name1, any);
405       any = orb.create_any ();
406       any.insert_string (newStr);
407       pairs [1] = new NameValuePair JavaDoc (name2, any);
408
409       try
410       {
411          dynAny.set_members (pairs);
412       }
413       catch (Throwable JavaDoc ex)
414       {
415          msg = "Failed to set DynStruct members using DynStruct::set_members ";
416          msg += "operation";
417          fail (msg + ": " + ex);
418       }
419
420       // test extracting the name/value pairs as Anys
421
pairs = dynAny.get_members ();
422       msg = "New name associated with first name/value pair is incorrect";
423       assertEquals (msg, name1, pairs [0].id);
424       msg = "New value associated with first name/value pair is incorrect ";
425       assertEquals (msg, newInt, pairs [0].value.extract_long ());
426       msg = "New name associated with second name/value pair is incorrect";
427       assertEquals (msg, name2, pairs [1].id);
428       msg = "New value associated with second name/value pair is incorrect";
429       assertEquals (msg, newStr, pairs [1].value.extract_string ());
430    }
431
432
433    /**
434     * Test accessing the member Name/Value DynAny pairs in a DynStruct object.
435     */

436    public void testAccessStructDynAnyPairs ()
437    {
438       final String JavaDoc name1 = "field1"; // specific to IDL
439
final String JavaDoc name2 = "field2"; // specific to IDL
440
String JavaDoc msg;
441       org.omg.CORBA.Any JavaDoc any = null;
442       NonEmptyException type = null;
443       int oldInt = 1;
444       String JavaDoc oldStr = "Hello";
445       int newInt = 2;
446       String JavaDoc newStr = "GoodBye";
447       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
448       org.omg.DynamicAny.DynAny JavaDoc dynAny1 = null;
449       org.omg.DynamicAny.DynAny JavaDoc dynAny2 = null;
450       org.omg.DynamicAny.NameDynAnyPair JavaDoc [] pairs = null;
451
452       type = new NonEmptyException (oldInt, oldStr);
453       any = orb.create_any ();
454       NonEmptyExceptionHelper.insert (any, type);
455       dynAny = createDynAnyFromAny (any);
456
457       // test extracting the name/value pairs as DynAnys
458
pairs = dynAny.get_members_as_dyn_any ();
459       msg = "Old name associated with first name/value pair is incorrect";
460       assertEquals (msg, name1, pairs [0].id);
461       msg = "Old value associated with first name/value pair is incorrect";
462       assertEquals (msg, oldInt, pairs [0].value.to_any ().extract_long ());
463       msg = "Old name associated with second name/value pair is incorrect";
464       assertEquals (msg, name2, pairs [1].id);
465       msg = "Old value associated with second name/value pair is incorrect";
466       assertEquals (msg, oldStr, pairs [1].value.to_any ().extract_string ());
467
468       any = orb.create_any ();
469       any.insert_long (newInt);
470       try
471       {
472          dynAny1 = factory.create_dyn_any (any);
473       }
474       catch (Throwable JavaDoc ex)
475       {
476          msg = "Factory failed to create DynAny from Any for first member ";
477          msg += "using DynAny::create_dyn_any operation: " + ex;
478          fail (msg);
479       }
480
481       any = orb.create_any ();
482       any.insert_string (newStr);
483       try
484       {
485          dynAny2 = factory.create_dyn_any (any);
486       }
487       catch (Throwable JavaDoc ex)
488       {
489          msg = "Factory failed to create DynAny from Any for second member ";
490          msg += "using DynAny::create_dyn_any operation: " + ex;
491          fail (msg);
492       }
493
494       pairs = new NameDynAnyPair JavaDoc [2]; // specific to IDL
495
pairs [0] = new NameDynAnyPair JavaDoc (name1, dynAny1);
496       pairs [1] = new NameDynAnyPair JavaDoc (name2, dynAny2);
497
498       msg = "Failed to set DynStruct members using ";
499       msg += "DynStruct::set_members_as_dyn_any operation";
500       try
501       {
502          dynAny.set_members_as_dyn_any (pairs);
503       }
504       catch (Throwable JavaDoc ex)
505       {
506          fail (msg + ": " + ex);
507       }
508
509       // test extracting the name/value pairs as DynAnys
510
pairs = dynAny.get_members_as_dyn_any ();
511       msg = "New name associated with first name/value pair is incorrect";
512       assertEquals (msg, name1, pairs [0].id);
513       msg = "New value associated with first name/value pair is incorrect ";
514       assertEquals (msg, newInt, pairs [0].value.to_any ().extract_long ());
515       msg = "New name associated with second name/value pair is incorrect";
516       assertEquals (msg, name2, pairs [1].id);
517       msg = "New value associated with second name/value pair is incorrect";
518       assertEquals (msg, newStr, pairs [1].value.to_any ().extract_string ());
519    }
520
521
522    /**
523     * Test the exceptions raised while accessing the member Name/Value
524     * DynAny pairs in a DynStruct object.
525     */

526    public void testAccessStructPairsEx ()
527    {
528       final String JavaDoc name1 = "field1";
529       final String JavaDoc name2 = "field2";
530       String JavaDoc msg;
531       org.omg.CORBA.Any JavaDoc any = null;
532       org.omg.CORBA.TypeCode JavaDoc tc = null;
533       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
534       org.omg.DynamicAny.NameValuePair JavaDoc [] pairs = null;
535
536       tc = orb.create_exception_tc (ID, NAME, getExceptionMembers ());
537       dynAny = createDynAnyFromTypeCode (tc);
538
539       // test inserting a member with an incorrect type
540
pairs = new NameValuePair JavaDoc [2];
541       any = orb.create_any ();
542       any.insert_char ('a');
543       pairs [0] = new NameValuePair JavaDoc (name1, any);
544       any = orb.create_any ();
545       any.insert_string ("WrongType");
546       pairs [0] = new NameValuePair JavaDoc (name2, any);
547
548       msg = "Failed to raise a TypeMismatch exception when passing in a ";
549       msg += "name/value pair containing an incorrect member value";
550       try
551       {
552          dynAny.set_members (pairs);
553
554          fail (msg);
555       }
556       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
557       {
558          // success
559
}
560       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
561       {
562          fail (msg + ": " + ex);
563       }
564
565       // test inserting a member with an incorrect name
566
pairs = new NameValuePair JavaDoc [2];
567       any = orb.create_any ();
568       any.insert_char ('a');
569       pairs [0] = new NameValuePair JavaDoc (name1, any);
570       any = orb.create_any ();
571       any.insert_longlong ((long) 1);
572       pairs [1] = new NameValuePair JavaDoc ("WrongName", any);
573
574       msg = "Failed to raise a TypeMismatch exception when passing in a ";
575       msg += "name/value pair containing an incorrect member name";
576       try
577       {
578          dynAny.set_members (pairs);
579
580          fail (msg);
581       }
582       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
583       {
584          // success
585
}
586       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
587       {
588          fail (msg + ": " + ex);
589       }
590
591       // test inserting a sequence with the wrong number of members
592
pairs = new NameValuePair JavaDoc [3];
593       any = orb.create_any ();
594       any.insert_char ('a');
595       pairs [0] = new NameValuePair JavaDoc (name1, any);
596       any = orb.create_any ();
597       any.insert_longlong ((long) 1);
598       pairs [1] = new NameValuePair JavaDoc (name2, any);
599       any = orb.create_any ();
600       any.insert_longlong ((long) 1);
601       pairs [2] = new NameValuePair JavaDoc (name2, any);
602
603       msg = "Failed to raise an InvalidValue exception when passing in a ";
604       msg += "name/value sequence containing too many members";
605       try
606       {
607          dynAny.set_members (pairs);
608
609          fail (msg);
610       }
611       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
612       {
613          // success
614
}
615       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
616       {
617          fail (msg + ": " + ex);
618       }
619    }
620
621
622    /**
623     * Test obtaining the TypeCode associated with a DynAny object.
624     */

625    public void testDynAnyTypeCode ()
626    {
627       String JavaDoc msg;
628       org.omg.CORBA.TypeCode JavaDoc tc = null;
629       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
630
631       tc = orb.create_exception_tc (ID, NAME, getExceptionMembers ());
632       dynAny = createDynAnyFromTypeCode (tc);
633
634       msg = "Incorrect TypeCode retrieved from DynAny::type operation";
635       assertTrue (msg, dynAny.type ().equal (tc));
636    }
637
638
639    /**
640     * Test initializing a DynAny object from another DynAny object.
641     */

642    public void testInitDynAnyFromDynAny ()
643    {
644       String JavaDoc msg;
645       NonEmptyException type;
646       org.omg.CORBA.Any JavaDoc any = null;
647       org.omg.CORBA.TypeCode JavaDoc tc = null;
648       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
649       org.omg.DynamicAny.DynStruct JavaDoc dynAny2 = null;
650
651       tc = NonEmptyExceptionHelper.type ();
652       dynAny = createDynAnyFromTypeCode (tc);
653
654       type = new NonEmptyException (1, "Hello");
655       any = orb.create_any ();
656       NonEmptyExceptionHelper.insert (any, type);
657       dynAny2 = createDynAnyFromAny (any);
658
659       msg = "Failed to initialize a DynAny object from another DynAny ";
660       msg += "object using the DynAny::assign operation";
661       try
662       {
663          dynAny.assign (dynAny2);
664       }
665       catch (Throwable JavaDoc ex)
666       {
667          fail (msg + ": " + ex);
668       }
669       assertTrue (msg, dynAny.equal (dynAny2));
670    }
671
672
673    /**
674     * Test initializing a DynAny object from an Any value.
675     */

676    public void testInitDynAnyFromAny ()
677    {
678       String JavaDoc msg;
679       NonEmptyException type;
680       org.omg.CORBA.Any JavaDoc any = null;
681       org.omg.CORBA.TypeCode JavaDoc tc = null;
682       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
683       org.omg.DynamicAny.DynStruct JavaDoc dynAny2 = null;
684
685       tc = NonEmptyExceptionHelper.type ();
686       dynAny = createDynAnyFromTypeCode (tc);
687
688       type = new NonEmptyException (1, "Hello");
689       any = orb.create_any ();
690       NonEmptyExceptionHelper.insert (any, type);
691       dynAny2 = createDynAnyFromAny (any);
692
693       msg = "Failed to initialize a DynAny object from an Any object ";
694       msg += "using the DynAny::from_any operation";
695       try
696       {
697          dynAny.from_any (any);
698       }
699       catch (Throwable JavaDoc ex)
700       {
701          fail (msg + ": " + ex);
702       }
703       assertTrue (msg, dynAny.equal (dynAny2));
704    }
705
706
707    /**
708     * Test that a TypeMismatch exception is raised if there is a type
709     * mismatch between the DynAny and Any types in an assignment.
710     */

711    public void testInitFromAnyTypeMismatchEx ()
712    {
713       String JavaDoc msg;
714       org.omg.CORBA.Any JavaDoc any = null;
715       org.omg.CORBA.TypeCode JavaDoc tc = null;
716       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
717
718       any = orb.create_any ();
719       any.insert_string ("Hello");
720
721       tc = orb.create_exception_tc (ID, NAME, getExceptionMembers ());
722       dynAny = createDynAnyFromTypeCode (tc);
723
724       msg = "TypeMismatch exception not thrown by DynAny::from_any ";
725       msg += "operation when DynAny and Any operands have different types";
726       try
727       {
728          dynAny.from_any (any);
729
730          fail (msg);
731       }
732       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
733       {
734          // success
735
}
736       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
737       {
738          fail (msg + ": " + ex);
739       }
740    }
741
742
743    /**
744     * Test generating an Any value from a DynAny object.
745     */

746    public void testGenerateAnyFromDynAny ()
747    {
748       String JavaDoc msg;
749       org.omg.CORBA.Any JavaDoc any = null;
750       org.omg.CORBA.TypeCode JavaDoc tc = null;
751       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
752       org.omg.DynamicAny.DynStruct JavaDoc dynAny2 = null;
753
754       tc = NonEmptyExceptionHelper.type ();
755       dynAny = createDynAnyFromTypeCode (tc);
756
757       any = orb.create_any ();
758       any = dynAny.to_any ();
759       dynAny2 = createDynAnyFromAny (any);
760
761       msg = "The DynAny::to_any operation failed to create an Any ";
762       msg += "object with the same value as the DynAny object";
763       assertTrue (msg, dynAny.equal (dynAny2));
764    }
765
766
767    /**
768     * Test destroying a DynAny object.
769     */

770    public void testDestroyDynAny ()
771    {
772       String JavaDoc msg;
773       NonEmptyException type;
774       org.omg.CORBA.Any JavaDoc any = null;
775       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
776
777       type = new NonEmptyException (1, "Hello");
778       any = orb.create_any ();
779       NonEmptyExceptionHelper.insert (any, type);
780       dynAny = createDynAnyFromAny (any);
781       dynAny.destroy ();
782
783       try
784       {
785          dynAny.type ();
786
787          msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
788          msg += "calling DynAny::type operation on a destroyed DynAny object ";
789          msg += "did not raise OBJECT_NOT_EXIST exception";
790          fail (msg);
791       }
792       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
793       {
794          // success
795
}
796
797       msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
798       msg += "calling DynAny::current_component operation on a destroyed ";
799       msg += "DynAny object did not raise OBJECT_NOT_EXIST exception";
800       try
801       {
802          dynAny.current_component ();
803
804          fail (msg);
805       }
806       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
807       {
808          // success
809
}
810       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
811       {
812          fail (msg + ": " + ex);
813       }
814    }
815
816
817    /**
818     * Test destroying a component of a DynAny object.
819     */

820    public void testDestroyComponent ()
821    {
822       String JavaDoc msg;
823       NonEmptyException type;
824       org.omg.CORBA.Any JavaDoc any = null;
825       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
826       org.omg.DynamicAny.DynAny JavaDoc comp = null;
827
828       type = new NonEmptyException (1, "Hello");
829       any = orb.create_any ();
830       NonEmptyExceptionHelper.insert (any, type);
831       dynAny = createDynAnyFromAny (any);
832
833       try
834       {
835          comp = dynAny.current_component ();
836       }
837       catch (Throwable JavaDoc ex)
838       {
839          msg = "Failed to get the current component of the DynAny using the ";
840          msg += "DynAny::current_component operation before calling the ";
841          msg += "DynAny::destroy operation";
842          fail (msg + ": " + ex);
843       }
844
845       comp.destroy ();
846       try
847       {
848          comp = dynAny.current_component ();
849       }
850       catch (Throwable JavaDoc ex)
851       {
852          msg = "Failed to get the current component of the DynAny using the ";
853          msg += "DynAny::current_component operation after calling the ";
854          msg += "DynAny::destroy operation";
855          fail (msg + ": " + ex);
856       }
857
858       try
859       {
860          comp.type ();
861       }
862       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
863       {
864          msg = "Calling destroy on a component resulted in destroying the ";
865          msg += "component object";
866          fail (msg + ": " + ex);
867       }
868    }
869
870
871    /**
872     * Test creating a copy of a DynAny object.
873     */

874    public void testCopyDynAny ()
875    {
876       String JavaDoc msg;
877       org.omg.CORBA.TypeCode JavaDoc tc = null;
878       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
879       org.omg.DynamicAny.DynStruct JavaDoc dynAny2 = null;
880
881       tc = NonEmptyExceptionHelper.type ();
882       dynAny = createDynAnyFromTypeCode (tc);
883       dynAny2 = (org.omg.DynamicAny.DynStruct JavaDoc) dynAny.copy ();
884
885       msg = "The DynAny object created with the DynAny::copy operation ";
886       msg += "is not equal to the DynAny object it was copied from";
887       assertTrue (msg, dynAny.equal (dynAny2));
888    }
889
890
891    private static class Setup extends TestSetup
892    {
893       public Setup (Test test)
894       {
895          super (test);
896       }
897
898       protected void setUp ()
899       {
900          org.omg.CORBA.Object JavaDoc obj = null;
901
902          orb = ORBSetup.getORB ();
903          try
904          {
905             obj = orb.resolve_initial_references ("DynAnyFactory");
906          }
907          catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc ex)
908          {
909             fail ("Failed to resolve DynAnyFactory: " + ex);
910          }
911          try
912          {
913             factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj);
914          }
915          catch (Throwable JavaDoc ex)
916          {
917             fail ("Failed to narrow to DynAnyFactory: " + ex);
918          }
919       }
920
921       protected void tearDown ()
922       {
923       }
924    }
925
926
927    /**
928     * Create a DynAny object from an Any object.
929     */

930    private static org.omg.DynamicAny.DynStruct JavaDoc createDynAnyFromAny
931       (org.omg.CORBA.Any JavaDoc any)
932    {
933       String JavaDoc msg;
934       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
935
936       try
937       {
938          dynAny = (org.omg.DynamicAny.DynStruct JavaDoc) factory.create_dyn_any (any);
939       }
940       catch (Throwable JavaDoc ex)
941       {
942          msg = "Factory failed to create DynAny from Any using ";
943          msg += "DynAny::create_dyn_any operation: " + ex;
944          fail (msg);
945       }
946       return dynAny;
947    }
948
949
950    /**
951     * Create a DynAny object from a TypeCode object.
952     */

953    private static org.omg.DynamicAny.DynStruct JavaDoc createDynAnyFromTypeCode
954       (org.omg.CORBA.TypeCode JavaDoc tc)
955    {
956       String JavaDoc msg;
957       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
958
959       try
960       {
961          dynAny = (org.omg.DynamicAny.DynStruct JavaDoc)
962             factory.create_dyn_any_from_type_code (tc);
963       }
964       catch (Throwable JavaDoc ex)
965       {
966          msg = "Factory failed to create DynAny from TypeCode using ";
967          msg += "DynAny::create_dyn_any_from_type_code operation: " + ex;
968          fail (msg);
969       }
970       return dynAny;
971    }
972
973
974    /**
975     * Create a sequence of fields for use in creating an exception TypeCode.
976     */

977    private static org.omg.CORBA.StructMember JavaDoc [] getExceptionMembers ()
978    {
979       final org.omg.CORBA.StructMember JavaDoc [] members =
980          new org.omg.CORBA.StructMember JavaDoc [2];
981
982       members [0] = new org.omg.CORBA.StructMember JavaDoc
983       (
984          "field1",
985          orb.get_primitive_tc (TCKind.tk_char),
986          null
987       );
988
989       members [1] = new org.omg.CORBA.StructMember JavaDoc
990       (
991          "field2",
992          orb.get_primitive_tc (TCKind.tk_longlong),
993          null
994       );
995
996       return members;
997    }
998
999 }
1000
Popular Tags