KickJava   Java API By Example, From Geeks To Geeks.

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


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.jacorb.test.common.ORBSetup;
26 import org.jacorb.test.EnumType;
27 import org.jacorb.test.EnumTypeHelper;
28
29 /**
30  * DynAnyEnumTest.java
31  *
32  * DynAny tests for enumeration types.
33  *
34  */

35
36 public class DynAnyEnumTest extends TestCase
37 {
38    private static org.omg.DynamicAny.DynAnyFactory JavaDoc factory = null;
39    private static org.omg.CORBA.ORB JavaDoc orb = null;
40
41    private static final String JavaDoc ID = "IDL:test:1.0";
42    private static final String JavaDoc NAME = "MyEnum";
43    private static final String JavaDoc [] ENUM = {"one", "two", "three"};
44
45
46    public DynAnyEnumTest (String JavaDoc name)
47    {
48       super (name);
49    }
50
51
52    public static Test suite ()
53    {
54       TestSuite suite = new TestSuite ("DynEnum Tests");
55       Setup setup = new Setup (suite);
56       ORBSetup osetup = new ORBSetup (setup);
57
58       suite.addTest (new DynAnyEnumTest ("testFactoryCreateFromAny"));
59       suite.addTest (new DynAnyEnumTest ("testFactoryCreateFromTypeCode"));
60       suite.addTest (new DynAnyEnumTest ("testFactoryCreateFromIDLTypeCode"));
61       suite.addTest (new DynAnyEnumTest ("testCompareDynAny"));
62       suite.addTest (new DynAnyEnumTest ("testAccessEnumValue"));
63       suite.addTest (new DynAnyEnumTest ("testAccessInvalidValueEx"));
64       suite.addTest (new DynAnyEnumTest ("testDynAnyTypeCode"));
65       suite.addTest (new DynAnyEnumTest ("testInitDynAnyFromDynAny"));
66       suite.addTest (new DynAnyEnumTest ("testInitDynAnyFromAny"));
67       suite.addTest (new DynAnyEnumTest ("testInitFromAnyTypeMismatchEx"));
68       suite.addTest (new DynAnyEnumTest ("testGenerateAnyFromDynAny"));
69       suite.addTest (new DynAnyEnumTest ("testDestroyDynAny"));
70       suite.addTest (new DynAnyEnumTest ("testCopyDynAny"));
71       suite.addTest (new DynAnyEnumTest ("testIterateDynAny"));
72
73       return osetup;
74    }
75
76
77    /**
78     * Tests creating a DynAny object from an Any object using the
79     * DynAnyFactory object.
80     */

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

96    public void testFactoryCreateFromTypeCode ()
97    {
98       org.omg.CORBA.TypeCode JavaDoc tc = null;
99
100       tc = orb.create_enum_tc (ID, NAME, ENUM);
101       createDynAnyFromTypeCode (tc);
102    }
103
104
105    /**
106     * Tests creating a DynAny object from a TypeCode object generated from
107     * IDL using the DynAnyFactory object.
108     */

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

121    public void testCompareDynAny ()
122    {
123       String JavaDoc msg;
124       org.omg.CORBA.Any JavaDoc any = null;
125       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
126       org.omg.DynamicAny.DynEnum JavaDoc dynAny2 = null;
127
128       any = orb.create_any ();
129       EnumTypeHelper.insert (any, EnumType.from_int (0));
130       dynAny = createDynAnyFromAny (any);
131       dynAny2 = createDynAnyFromAny (any);
132
133       msg = "Comparing two equal DynAny values using DynAny::equal failed";
134       assertTrue (msg, dynAny.equal (dynAny2));
135    }
136
137
138    /**
139     * Test accessing a value in a DynEnum object.
140     */

141    public void testAccessEnumValue ()
142    {
143       String JavaDoc msg;
144       String JavaDoc type1;
145       int type2;
146       org.omg.CORBA.Any JavaDoc any = null;
147       org.omg.CORBA.TypeCode JavaDoc tc = null;
148       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
149       org.omg.DynamicAny.DynEnum JavaDoc dynAny2 = null;
150
151       tc = EnumTypeHelper.type ();
152       dynAny = createDynAnyFromTypeCode (tc);
153
154       any = orb.create_any ();
155       EnumTypeHelper.insert (any, EnumType.from_int (0));
156       dynAny2 = createDynAnyFromAny (any);
157
158       type1 = "first"; // specific to enum
159
try
160       {
161          dynAny.set_as_string (type1);
162       }
163       catch (Throwable JavaDoc ex)
164       {
165          msg = "Failed to set value as string in DynEnum object with ";
166          msg += "DynEnum::set_as_string";
167          fail (msg + ": " + ex);
168       }
169
170       type2 = 1;
171       try
172       {
173          dynAny2.set_as_ulong (type2);
174       }
175       catch (Throwable JavaDoc ex)
176       {
177          msg = "Failed to set value as int in DynEnum object with ";
178          msg += "DynEnum::set_as_ulong";
179          fail (msg + ": " + ex);
180       }
181
182       msg = "String value inserted into DynEnum object is not equal to value ";
183       msg += "extracted from same DynEnum object with DynEnum::get_as_string";
184       assertEquals (msg, type1, dynAny.get_as_string ());
185
186       msg = "Integer value inserted into DynEnum object is not equal to value ";
187       msg += "extracted from same DynEnum object with DynEnum::get_as_ulong";
188       assertEquals (msg, type2, dynAny2.get_as_ulong ());
189    }
190
191
192    /**
193     * Test that an InvalidValue exception is raised if values inserted into
194     * DynEnum objects are out of range.
195     */

196    public void testAccessInvalidValueEx ()
197    {
198       String JavaDoc msg;
199       org.omg.CORBA.TypeCode JavaDoc tc = null;
200       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
201
202       tc = orb.create_enum_tc (ID, NAME, ENUM);
203       dynAny = createDynAnyFromTypeCode (tc);
204
205       try
206       {
207          dynAny.set_as_string ("BadValue");
208
209          msg = "InvalidValue exception not thrown by DynEnum::set_as_string ";
210          msg += "operation when value is out of range";
211          fail (msg);
212       }
213       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
214       {
215          // success
216
}
217
218       try
219       {
220          dynAny.set_as_ulong (-1);
221
222          msg = "InvalidValue exception not thrown by DynEnum::set_as_ulong ";
223          msg += "operation when value is out of range";
224          fail (msg);
225       }
226       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
227       {
228          // success
229
}
230    }
231
232
233    /**
234     * Test obtaining the TypeCode associated with a DynAny object.
235     */

236    public void testDynAnyTypeCode ()
237    {
238       String JavaDoc msg;
239       org.omg.CORBA.TypeCode JavaDoc tc = null;
240       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
241
242       tc = orb.create_enum_tc (ID, NAME, ENUM);
243       dynAny = createDynAnyFromTypeCode (tc);
244
245       msg = "Incorrect TypeCode retrieved from DynAny::type operation";
246       assertTrue (msg, dynAny.type ().equal (tc));
247    }
248
249
250    /**
251     * Test initializing a DynAny object from another DynAny object.
252     */

253    public void testInitDynAnyFromDynAny ()
254    {
255       String JavaDoc msg;
256       org.omg.CORBA.Any JavaDoc any = null;
257       org.omg.CORBA.TypeCode JavaDoc tc = null;
258       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
259       org.omg.DynamicAny.DynEnum JavaDoc dynAny2 = null;
260
261       tc = EnumTypeHelper.type ();
262       dynAny = createDynAnyFromTypeCode (tc);
263
264       any = orb.create_any ();
265       EnumTypeHelper.insert (any, EnumType.from_int (1));
266       dynAny2 = createDynAnyFromAny (any);
267
268       msg = "Failed to initialize a DynAny object from another DynAny ";
269       msg += "object using the DynAny::assign operation";
270       try
271       {
272          dynAny.assign (dynAny2);
273       }
274       catch (Throwable JavaDoc ex)
275       {
276          fail (msg + ": " + ex);
277       }
278       assertTrue (msg, dynAny.equal (dynAny2));
279    }
280
281
282    /**
283     * Test initializing a DynAny object from an Any value.
284     */

285    public void testInitDynAnyFromAny ()
286    {
287       String JavaDoc msg;
288       org.omg.CORBA.Any JavaDoc any = null;
289       org.omg.CORBA.TypeCode JavaDoc tc = null;
290       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
291       org.omg.DynamicAny.DynEnum JavaDoc dynAny2 = null;
292
293       tc = EnumTypeHelper.type ();
294       dynAny = createDynAnyFromTypeCode (tc);
295
296       any = orb.create_any ();
297       EnumTypeHelper.insert (any, EnumType.from_int (1));
298       dynAny2 = createDynAnyFromAny (any);
299
300       msg = "Failed to initialize a DynAny object from an Any object ";
301       msg += "using the DynAny::from_any operation";
302       try
303       {
304          dynAny.from_any (any);
305       }
306       catch (Throwable JavaDoc ex)
307       {
308          fail (msg + ": " + ex);
309       }
310       assertTrue (msg, dynAny.equal (dynAny2));
311    }
312
313
314    /**
315     * Test that a TypeMismatch exception is raised if there is a type
316     * mismatch between the DynAny and Any types in an assignment.
317     */

318    public void testInitFromAnyTypeMismatchEx ()
319    {
320       String JavaDoc msg;
321       org.omg.CORBA.Any JavaDoc any = null;
322       org.omg.CORBA.TypeCode JavaDoc tc = null;
323       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
324
325       any = orb.create_any ();
326       any.insert_string ("Hello");
327
328       tc = orb.create_enum_tc (ID, NAME, ENUM);
329       dynAny = createDynAnyFromTypeCode (tc);
330
331       msg = "TypeMismatch exception not thrown by DynAny::from_any ";
332       msg += "operation when DynAny and Any operands have different types";
333       try
334       {
335          dynAny.from_any (any);
336
337          fail (msg);
338       }
339       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
340       {
341          // success
342
}
343       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
344       {
345          fail (msg + ": " + ex);
346       }
347    }
348
349
350    /**
351     * Test generating an Any value from a DynAny object.
352     */

353    public void testGenerateAnyFromDynAny ()
354    {
355       String JavaDoc msg;
356       org.omg.CORBA.Any JavaDoc any = null;
357       org.omg.CORBA.TypeCode JavaDoc tc = null;
358       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
359       org.omg.DynamicAny.DynEnum JavaDoc dynAny2 = null;
360
361       tc = EnumTypeHelper.type ();
362       dynAny = createDynAnyFromTypeCode (tc);
363
364       any = orb.create_any ();
365       any = dynAny.to_any ();
366       dynAny2 = createDynAnyFromAny (any);
367
368       msg = "Failed to initialize an Any object from a DynAny object ";
369       msg += "using the DynAny::to_any operation";
370       assertTrue (msg, dynAny.equal (dynAny2));
371    }
372
373
374    /**
375     * Test destroying a DynAny object.
376     */

377    public void testDestroyDynAny ()
378    {
379       String JavaDoc msg;
380       org.omg.CORBA.Any JavaDoc any = null;
381       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
382
383       any = orb.create_any ();
384       EnumTypeHelper.insert (any, EnumType.from_int (1));
385       dynAny = createDynAnyFromAny (any);
386       dynAny.destroy ();
387
388       try
389       {
390          dynAny.type ();
391
392          msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
393          msg += "calling DynAny::type operation on a destroyed DynAny object ";
394          msg += "did not raise OBJECT_NOT_EXIST exception";
395          fail (msg);
396       }
397       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
398       {
399          // success
400
}
401
402       msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
403       msg += "calling DynAny::current_component operation on a destroyed ";
404       msg += "DynAny object did not raise OBJECT_NOT_EXIST exception";
405       try
406       {
407          dynAny.current_component ();
408
409          fail (msg);
410       }
411       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
412       {
413          // success
414
}
415       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
416       {
417          fail (msg + ": " + ex);
418       }
419    }
420
421
422    /**
423     * Test creating a copy of a DynAny object.
424     */

425    public void testCopyDynAny ()
426    {
427       String JavaDoc msg;
428       org.omg.CORBA.TypeCode JavaDoc tc = null;
429       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
430       org.omg.DynamicAny.DynEnum JavaDoc dynAny2 = null;
431
432       tc = EnumTypeHelper.type ();
433       dynAny = createDynAnyFromTypeCode (tc);
434       dynAny2 = (org.omg.DynamicAny.DynEnum JavaDoc) dynAny.copy ();
435
436       msg = "The DynAny object created with the DynAny::copy operation ";
437       msg += "is not equal to the DynAny object it was copied from";
438       assertTrue (msg, dynAny.equal (dynAny2));
439    }
440
441
442    /**
443     * Test iterating through components of a DynAny.
444     */

445    public void testIterateDynAny ()
446    {
447       String JavaDoc msg;
448       int compCount = -1;
449       boolean seek;
450       org.omg.CORBA.TypeCode JavaDoc tc = null;
451       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
452
453       tc = EnumTypeHelper.type ();
454       dynAny = createDynAnyFromTypeCode (tc);
455
456       // test the component count
457
try
458       {
459          compCount = dynAny.component_count ();
460       }
461       catch (Throwable JavaDoc ex)
462       {
463          // should not be needed, but it prevents compiler errors
464
fail ("Unexpected error raised by DynAny::component_count operation");
465       }
466       msg = "The number of components returned from the ";
467       msg += "DynAny::component_count operation is incorrect";
468       assertEquals (msg, 0, compCount);
469
470       // test if there is a first component
471
msg = "The DynAny::seek operation indicates that a valid component ";
472       msg += "exists but the DynAny should have no components";
473       seek = dynAny.seek (0);
474       assertTrue (msg, !seek);
475
476       // test getting the current component
477
try
478       {
479          dynAny = (org.omg.DynamicAny.DynEnum JavaDoc) dynAny.current_component ();
480
481          msg = "A TypeMismatch exception was not raised by the ";
482          msg += "DynAny::current_component operation when trying to access ";
483          msg += "the current component of a DynAny with no components";
484          fail (msg);
485       }
486       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
487       {
488          // success
489
}
490    }
491
492
493    private static class Setup extends TestSetup
494    {
495       public Setup (Test test)
496       {
497          super (test);
498       }
499
500       protected void setUp ()
501       {
502          org.omg.CORBA.Object JavaDoc obj = null;
503
504          orb = ORBSetup.getORB ();
505          try
506          {
507             obj = orb.resolve_initial_references ("DynAnyFactory");
508          }
509          catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc ex)
510          {
511             fail ("Failed to resolve DynAnyFactory: " + ex);
512          }
513          try
514          {
515             factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj);
516          }
517          catch (Throwable JavaDoc ex)
518          {
519             fail ("Failed to narrow to DynAnyFactory: " + ex);
520          }
521       }
522
523       protected void tearDown ()
524       {
525       }
526    }
527
528
529    /**
530     * Create a DynAny object from an Any object.
531     */

532    private static org.omg.DynamicAny.DynEnum JavaDoc createDynAnyFromAny
533       (org.omg.CORBA.Any JavaDoc any)
534    {
535       String JavaDoc msg;
536       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
537
538       try
539       {
540          dynAny = (org.omg.DynamicAny.DynEnum JavaDoc) factory.create_dyn_any (any);
541       }
542       catch (Throwable JavaDoc ex)
543       {
544          msg = "Factory failed to create DynAny from Any using ";
545          msg += "DynAny::create_dyn_any operation: " + ex;
546          fail (msg);
547       }
548       return dynAny;
549    }
550
551
552    /**
553     * Create a DynAny object from a TypeCode object.
554     */

555    private static org.omg.DynamicAny.DynEnum JavaDoc createDynAnyFromTypeCode
556       (org.omg.CORBA.TypeCode JavaDoc tc)
557    {
558       String JavaDoc msg;
559       org.omg.DynamicAny.DynEnum JavaDoc dynAny = null;
560
561       try
562       {
563          dynAny = (org.omg.DynamicAny.DynEnum JavaDoc)
564             factory.create_dyn_any_from_type_code (tc);
565       }
566       catch (Throwable JavaDoc ex)
567       {
568          msg = "Factory failed to create DynAny from TypeCode using ";
569          msg += "DynAny::create_dyn_any_from_type_code operation: " + ex;
570          fail (msg);
571       }
572       return dynAny;
573    }
574
575 }
576
Popular Tags