KickJava   Java API By Example, From Geeks To Geeks.

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


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.CORBA.StructMember JavaDoc;
28 import org.omg.DynamicAny.NameValuePair JavaDoc;
29 import org.omg.DynamicAny.NameDynAnyPair JavaDoc;
30
31 import org.jacorb.test.common.ORBSetup;
32 import org.jacorb.test.EmptyException;
33 import org.jacorb.test.EmptyExceptionHelper;
34
35 /**
36  * DynAnyEmptyExTest.java
37  *
38  * DynAny tests for (empty) exception types.
39  *
40  */

41
42 public class DynAnyEmptyExTest 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 = "MyEmptyException";
49    private static final StructMember JavaDoc [] MEMBERS = new StructMember JavaDoc [0];
50
51    public DynAnyEmptyExTest (String JavaDoc name)
52    {
53       super (name);
54    }
55
56
57    public static Test suite ()
58    {
59       TestSuite suite = new TestSuite ("Empty Exception DynStruct Tests");
60       Setup setup = new Setup (suite);
61       ORBSetup osetup = new ORBSetup (setup);
62
63       suite.addTest (new DynAnyEmptyExTest ("testFactoryCreateFromAny"));
64       suite.addTest (new DynAnyEmptyExTest ("testFactoryCreateFromTypeCode"));
65       suite.addTest (new DynAnyEmptyExTest ("testFactoryCreateFromIDLTypeCode"));
66       suite.addTest (new DynAnyEmptyExTest ("testCompareDynAny"));
67       suite.addTest (new DynAnyEmptyExTest ("testIterateDynAny"));
68       suite.addTest (new DynAnyEmptyExTest ("testAccessStructMembers"));
69       suite.addTest (new DynAnyEmptyExTest ("testAccessStructNameValuePairs"));
70       suite.addTest (new DynAnyEmptyExTest ("testAccessStructDynAnyPairs"));
71       suite.addTest (new DynAnyEmptyExTest ("testAccessStructPairsEx"));
72       suite.addTest (new DynAnyEmptyExTest ("testDynAnyTypeCode"));
73       suite.addTest (new DynAnyEmptyExTest ("testInitDynAnyFromDynAny"));
74       suite.addTest (new DynAnyEmptyExTest ("testInitDynAnyFromAny"));
75       suite.addTest (new DynAnyEmptyExTest ("testInitFromAnyTypeMismatchEx"));
76       suite.addTest (new DynAnyEmptyExTest ("testGenerateAnyFromDynAny"));
77       suite.addTest (new DynAnyEmptyExTest ("testDestroyDynAny"));
78       suite.addTest (new DynAnyEmptyExTest ("testCopyDynAny"));
79
80       return osetup;
81    }
82
83
84    /**
85     * Tests creating a DynAny object from an Any object using the
86     * DynAnyFactory object.
87     */

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

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

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

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

152    public void testIterateDynAny ()
153    {
154       String JavaDoc msg;
155       int compCount = -1;
156       boolean seek;
157       org.omg.CORBA.TypeCode JavaDoc tc = null;
158       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
159
160       tc = EmptyExceptionHelper.type ();
161       dynAny = createDynAnyFromTypeCode (tc);
162
163       // test the component count
164
try
165       {
166          compCount = dynAny.component_count ();
167       }
168       catch (Throwable JavaDoc ex)
169       {
170          // should not be needed, but it prevents compiler errors
171
fail ("Unexpected error raised by DynAny::component_count operation");
172       }
173       msg = "The number of components returned from the ";
174       msg += "DynAny::component_count operation is incorrect";
175       assertEquals (msg, 0, compCount);
176
177       // test if there is a first component
178
msg = "The DynAny::seek operation indicates that a valid component ";
179       msg += "exists but the DynAny should have no components";
180       seek = dynAny.seek (0);
181       assertTrue (msg, !seek);
182
183       // test getting the current component
184
try
185       {
186          dynAny.current_component ();
187
188          msg = "A TypeMismatch exception was not raised by the ";
189          msg += "DynAny::current_component operation when trying to access ";
190          msg += "the current component of a DynAny with no components";
191          fail (msg);
192       }
193       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
194       {
195          // success
196
}
197    }
198
199
200    /**
201     * Test accessing the names and types of members in a DynStruct object.
202     */

203    public void testAccessStructMembers ()
204    {
205       String JavaDoc msg;
206       String JavaDoc memberName = null;
207       TCKind JavaDoc memberKind = null;
208       org.omg.CORBA.Any JavaDoc any = null;
209       EmptyException exception = null;
210       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
211
212       exception = new EmptyException ();
213       any = orb.create_any ();
214       EmptyExceptionHelper.insert (any, exception);
215       dynAny = createDynAnyFromAny (any);
216
217       // test getting the name of the current member
218
msg = "Failed to throw a TypeMismatch exception when calling ";
219       msg += "DynStruct::current_member_name operation on an empty exception";
220       try
221       {
222          memberName = dynAny.current_member_name ();
223
224          fail (msg);
225       }
226       catch (AssertionFailedError ex)
227       {
228          throw ex;
229       }
230       catch (Throwable JavaDoc ex)
231       {
232          if (!(ex instanceof org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc))
233          {
234             fail (msg + ": " + ex);
235          }
236       }
237
238       // test getting the kind of the current member
239
msg = "Failed to throw a TypeMismatch exception when calling ";
240       msg += "DynStruct::current_member_kind operation on an empty exception";
241       try
242       {
243          memberKind = dynAny.current_member_kind ();
244
245          fail (msg);
246       }
247       catch (AssertionFailedError ex)
248       {
249          throw ex;
250       }
251       catch (Throwable JavaDoc ex)
252       {
253          if (!(ex instanceof org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc))
254          {
255             fail (msg + ": " + ex);
256          }
257       }
258    }
259
260
261    /**
262     * Test accessing the member Name/Value pairs in a DynStruct object.
263     */

264    public void testAccessStructNameValuePairs ()
265    {
266       String JavaDoc msg;
267       org.omg.CORBA.Any JavaDoc any = null;
268       EmptyException type = null;
269       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
270       org.omg.DynamicAny.NameValuePair JavaDoc [] pairs = null;
271
272       type = new EmptyException ();
273       any = orb.create_any ();
274       EmptyExceptionHelper.insert (any, type);
275       dynAny = createDynAnyFromAny (any);
276
277       // test extracting the name/value pairs as Anys
278
pairs = dynAny.get_members ();
279       msg = "Returned number of members from DynStruct::get_members operation ";
280       msg += "is non-zero";
281       assertEquals (msg, 0, pairs.length);
282
283       pairs = new NameValuePair JavaDoc [0];
284       try
285       {
286          dynAny.set_members (pairs);
287       }
288       catch (Throwable JavaDoc ex)
289       {
290          msg = "Failed to set DynStruct members using DynStruct::set_members ";
291          msg += "operation";
292          fail (msg + ": " + ex);
293       }
294    }
295
296
297    /**
298     * Test accessing the member Name/Value DynAny pairs in a DynStruct object.
299     */

300    public void testAccessStructDynAnyPairs ()
301    {
302       String JavaDoc msg;
303       org.omg.CORBA.Any JavaDoc any = null;
304       EmptyException type = null;
305       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
306       org.omg.DynamicAny.NameDynAnyPair JavaDoc [] pairs = null;
307
308       type = new EmptyException ();
309       any = orb.create_any ();
310       EmptyExceptionHelper.insert (any, type);
311       dynAny = createDynAnyFromAny (any);
312
313       // test extracting the name/value pairs as Anys
314
pairs = dynAny.get_members_as_dyn_any ();
315       msg = "Returned number of members from DynStruct::get_members operation ";
316       msg += "is non-zero";
317       assertEquals (msg, 0, pairs.length);
318
319       pairs = new NameDynAnyPair JavaDoc [0];
320       try
321       {
322          dynAny.set_members_as_dyn_any (pairs);
323       }
324       catch (Throwable JavaDoc ex)
325       {
326          msg = "Failed to set DynStruct members using DynStruct::set_members ";
327          msg += "operation";
328          fail (msg + ": " + ex);
329       }
330    }
331
332
333    /**
334     * Test the exceptions raised while accessing the member Name/Value
335     * DynAny pairs in a DynStruct object.
336     */

337    public void testAccessStructPairsEx ()
338    {
339       String JavaDoc msg;
340       org.omg.CORBA.Any JavaDoc any = null;
341       org.omg.CORBA.TypeCode JavaDoc tc = null;
342       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
343       org.omg.DynamicAny.NameValuePair JavaDoc [] pairs = null;
344
345       tc = orb.create_exception_tc (ID, NAME, MEMBERS);
346       dynAny = createDynAnyFromTypeCode (tc);
347
348       // test inserting a sequence with the wrong number of members
349
pairs = new NameValuePair JavaDoc [1];
350       any = orb.create_any ();
351       any.insert_char ('a');
352       pairs [0] = new NameValuePair JavaDoc ("name", any);
353
354       msg = "Failed to raise an InvalidValue exception when passing in a ";
355       msg += "name/value sequence containing too many members";
356       try
357       {
358          dynAny.set_members (pairs);
359
360          fail (msg);
361       }
362       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
363       {
364          // success
365
}
366       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
367       {
368          fail (msg + ": " + ex);
369       }
370    }
371
372
373    /**
374     * Test obtaining the TypeCode associated with a DynAny object.
375     */

376    public void testDynAnyTypeCode ()
377    {
378       String JavaDoc msg;
379       org.omg.CORBA.TypeCode JavaDoc tc = null;
380       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
381
382       tc = orb.create_exception_tc (ID, NAME, MEMBERS);
383       dynAny = createDynAnyFromTypeCode (tc);
384
385       msg = "Incorrect TypeCode retrieved from DynAny::type operation";
386       assertTrue (msg, dynAny.type ().equal (tc));
387    }
388
389
390    /**
391     * Test initializing a DynAny object from another DynAny object.
392     */

393    public void testInitDynAnyFromDynAny ()
394    {
395       String JavaDoc msg;
396       EmptyException type;
397       org.omg.CORBA.Any JavaDoc any = null;
398       org.omg.CORBA.TypeCode JavaDoc tc = null;
399       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
400       org.omg.DynamicAny.DynStruct JavaDoc dynAny2 = null;
401
402       tc = EmptyExceptionHelper.type ();
403       dynAny = createDynAnyFromTypeCode (tc);
404
405       type = new EmptyException ();
406       any = orb.create_any ();
407       EmptyExceptionHelper.insert (any, type);
408       dynAny2 = createDynAnyFromAny (any);
409
410       msg = "Failed to initialize a DynAny object from another DynAny ";
411       msg += "object using the DynAny::assign operation";
412       try
413       {
414          dynAny.assign (dynAny2);
415       }
416       catch (Throwable JavaDoc ex)
417       {
418          fail (msg + ": " + ex);
419       }
420       assertTrue (msg, dynAny.equal (dynAny2));
421    }
422
423
424    /**
425     * Test initializing a DynAny object from an Any value.
426     */

427    public void testInitDynAnyFromAny ()
428    {
429       String JavaDoc msg;
430       EmptyException type;
431       org.omg.CORBA.Any JavaDoc any = null;
432       org.omg.CORBA.TypeCode JavaDoc tc = null;
433       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
434       org.omg.DynamicAny.DynStruct JavaDoc dynAny2 = null;
435
436       tc = EmptyExceptionHelper.type ();
437       dynAny = createDynAnyFromTypeCode (tc);
438
439       type = new EmptyException ();
440       any = orb.create_any ();
441       EmptyExceptionHelper.insert (any, type);
442       dynAny2 = createDynAnyFromAny (any);
443
444       msg = "Failed to initialize a DynAny object from an Any object ";
445       msg += "using the DynAny::from_any operation";
446       try
447       {
448          dynAny.from_any (any);
449       }
450       catch (Throwable JavaDoc ex)
451       {
452          fail (msg + ": " + ex);
453       }
454       assertTrue (msg, dynAny.equal (dynAny2));
455    }
456
457
458    /**
459     * Test that a TypeMismatch exception is raised if there is a type
460     * mismatch between the DynAny and Any types in an assignment.
461     */

462    public void testInitFromAnyTypeMismatchEx ()
463    {
464       String JavaDoc msg;
465       org.omg.CORBA.Any JavaDoc any = null;
466       org.omg.CORBA.TypeCode JavaDoc tc = null;
467       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
468
469       any = orb.create_any ();
470       any.insert_string ("Hello");
471
472       tc = orb.create_exception_tc (ID, NAME, MEMBERS);
473       dynAny = createDynAnyFromTypeCode (tc);
474
475       msg = "TypeMismatch exception not thrown by DynAny::from_any ";
476       msg += "operation when DynAny and Any operands have different types";
477       try
478       {
479          dynAny.from_any (any);
480
481          fail (msg);
482       }
483       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
484       {
485          // success
486
}
487       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
488       {
489          fail (msg + ": " + ex);
490       }
491    }
492
493
494    /**
495     * Test generating an Any value from a DynAny object.
496     */

497    public void testGenerateAnyFromDynAny ()
498    {
499       String JavaDoc msg;
500       org.omg.CORBA.Any JavaDoc any = null;
501       org.omg.CORBA.TypeCode JavaDoc tc = null;
502       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
503       org.omg.DynamicAny.DynStruct JavaDoc dynAny2 = null;
504
505       tc = EmptyExceptionHelper.type ();
506       dynAny = createDynAnyFromTypeCode (tc);
507
508       any = orb.create_any ();
509       any = dynAny.to_any ();
510       dynAny2 = createDynAnyFromAny (any);
511
512       msg = "The DynAny::to_any operation failed to create an Any ";
513       msg += "object with the same value as the DynAny object";
514       assertTrue (msg, dynAny.equal (dynAny2));
515    }
516
517
518    /**
519     * Test destroying a DynAny object.
520     */

521    public void testDestroyDynAny ()
522    {
523       String JavaDoc msg;
524       EmptyException type;
525       org.omg.CORBA.Any JavaDoc any = null;
526       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
527
528       type = new EmptyException ();
529       any = orb.create_any ();
530       EmptyExceptionHelper.insert (any, type);
531       dynAny = createDynAnyFromAny (any);
532       dynAny.destroy ();
533
534       try
535       {
536          dynAny.type ();
537
538          msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
539          msg += "calling DynAny::type operation on a destroyed DynAny object ";
540          msg += "did not raise OBJECT_NOT_EXIST exception";
541          fail (msg);
542       }
543       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
544       {
545          // success
546
}
547
548       msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
549       msg += "calling DynAny::current_component operation on a destroyed ";
550       msg += "DynAny object did not raise OBJECT_NOT_EXIST exception";
551       try
552       {
553          dynAny.current_component ();
554
555          fail (msg);
556       }
557       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
558       {
559          // success
560
}
561       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
562       {
563          fail (msg + ": " + ex);
564       }
565    }
566
567
568    /**
569     * Test creating a copy of a DynAny object.
570     */

571    public void testCopyDynAny ()
572    {
573       String JavaDoc msg;
574       org.omg.CORBA.TypeCode JavaDoc tc = null;
575       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
576       org.omg.DynamicAny.DynStruct JavaDoc dynAny2 = null;
577
578       tc = EmptyExceptionHelper.type ();
579       dynAny = createDynAnyFromTypeCode (tc);
580       dynAny2 = (org.omg.DynamicAny.DynStruct JavaDoc) dynAny.copy ();
581
582       msg = "The DynAny object created with the DynAny::copy operation ";
583       msg += "is not equal to the DynAny object it was copied from";
584       assertTrue (msg, dynAny.equal (dynAny2));
585    }
586
587
588    private static class Setup extends TestSetup
589    {
590       public Setup (Test test)
591       {
592          super (test);
593       }
594
595       protected void setUp ()
596       {
597          org.omg.CORBA.Object JavaDoc obj = null;
598
599          orb = ORBSetup.getORB ();
600          try
601          {
602             obj = orb.resolve_initial_references ("DynAnyFactory");
603          }
604          catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc ex)
605          {
606             fail ("Failed to resolve DynAnyFactory: " + ex);
607          }
608          try
609          {
610             factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj);
611          }
612          catch (Throwable JavaDoc ex)
613          {
614             fail ("Failed to narrow to DynAnyFactory: " + ex);
615          }
616       }
617
618       protected void tearDown ()
619       {
620       }
621    }
622
623
624    /**
625     * Create a DynAny object from an Any object.
626     */

627    private static org.omg.DynamicAny.DynStruct JavaDoc createDynAnyFromAny
628       (org.omg.CORBA.Any JavaDoc any)
629    {
630       String JavaDoc msg;
631       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
632
633       try
634       {
635          dynAny = (org.omg.DynamicAny.DynStruct JavaDoc) factory.create_dyn_any (any);
636       }
637       catch (Throwable JavaDoc ex)
638       {
639          msg = "Factory failed to create DynAny from Any using ";
640          msg += "DynAny::create_dyn_any operation: " + ex;
641          fail (msg);
642       }
643       return dynAny;
644    }
645
646
647    /**
648     * Create a DynAny object from a TypeCode object.
649     */

650    private static org.omg.DynamicAny.DynStruct JavaDoc createDynAnyFromTypeCode
651       (org.omg.CORBA.TypeCode JavaDoc tc)
652    {
653       String JavaDoc msg;
654       org.omg.DynamicAny.DynStruct JavaDoc dynAny = null;
655
656       try
657       {
658          dynAny = (org.omg.DynamicAny.DynStruct JavaDoc)
659             factory.create_dyn_any_from_type_code (tc);
660       }
661       catch (Throwable JavaDoc ex)
662       {
663          msg = "Factory failed to create DynAny from TypeCode using ";
664          msg += "DynAny::create_dyn_any_from_type_code operation: " + ex;
665          fail (msg);
666       }
667       return dynAny;
668    }
669
670 }
671
Popular Tags