KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 /**
28  * DynAnyBaseTest.java
29  *
30  * DynAny tests for basic types.
31  *
32  */

33
34 public class DynAnyBaseTest extends TestCase
35 {
36    private static org.omg.DynamicAny.DynAnyFactory JavaDoc factory = null;
37    private static org.omg.CORBA.ORB JavaDoc orb = null;
38
39
40    public DynAnyBaseTest (String JavaDoc name)
41    {
42       super (name);
43    }
44
45
46    public static Test suite ()
47    {
48       TestSuite suite = new TestSuite ("DynAny Base Tests");
49       Setup setup = new Setup (suite);
50       ORBSetup osetup = new ORBSetup (setup);
51
52       suite.addTest (new DynAnyBaseTest ("testFactoryCreateFromAny"));
53       suite.addTest (new DynAnyBaseTest ("testFactoryCreateFromTypeCode"));
54       suite.addTest (new DynAnyBaseTest ("testFactoryInconsistentTypeCodeEx"));
55       suite.addTest (new DynAnyBaseTest ("testDynAnyLocalityConstraint"));
56       suite.addTest (new DynAnyBaseTest ("testCompareDynAny"));
57       suite.addTest (new DynAnyBaseTest ("testAccessBasicValue"));
58       suite.addTest (new DynAnyBaseTest ("testAccessTypeMismatchEx"));
59       suite.addTest (new DynAnyBaseTest ("testDynAnyTypeCode"));
60       suite.addTest (new DynAnyBaseTest ("testInitDynAnyFromDynAny"));
61       suite.addTest (new DynAnyBaseTest ("testInitFromDynAnyTypeMismatchEx"));
62       suite.addTest (new DynAnyBaseTest ("testInitDynAnyFromAny"));
63       suite.addTest (new DynAnyBaseTest ("testInitFromAnyTypeMismatchEx"));
64       suite.addTest (new DynAnyBaseTest ("testGenerateAnyFromDynAny"));
65       suite.addTest (new DynAnyBaseTest ("testDestroyDynAny"));
66       suite.addTest (new DynAnyBaseTest ("testCopyDynAny"));
67       suite.addTest (new DynAnyBaseTest ("testIterateDynAny"));
68       
69       return osetup;
70    }
71
72
73    /**
74     * Tests creating a DynAny object from an Any object using the
75     * DynAnyFactory object.
76     */

77    public void testFactoryCreateFromAny ()
78    {
79       org.omg.CORBA.Any JavaDoc any = null;
80
81       any = orb.create_any ();
82       any.insert_long (700);
83
84       // TODO: Test using all possible TypeCodes
85
createDynAnyFromAny (any);
86    }
87
88
89    /**
90     * Tests creating a DynAny object from a TypeCode object using the
91     * DynAnyFactory object.
92     */

93    public void testFactoryCreateFromTypeCode ()
94    {
95       org.omg.CORBA.TypeCode JavaDoc tc = null;
96
97       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long);
98
99       // TODO: Test using all possible TypeCodes
100
createDynAnyFromTypeCode (tc);
101    }
102
103
104    /**
105     * Test that an InconsistentTypeCode exception is raised if an invalid
106     * TypeCode is used to create a DynAny object.
107     */

108    public void testFactoryInconsistentTypeCodeEx ()
109    {
110       String JavaDoc msg;
111       org.omg.CORBA.Any JavaDoc any = null;
112       org.omg.CORBA.TypeCode JavaDoc tc = null;
113       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
114
115       any = orb.create_any ();
116       any.type (orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_Principal));
117
118       // Principal TypeCode
119
msg = "Failed to throw InconsistentTypeCode exception when ";
120       msg += "creating DynAny from Any with TypeCode tk_Principal";
121       try
122       {
123          dynAny = factory.create_dyn_any (any);
124          
125          fail (msg);
126       }
127       catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc ex)
128       {
129          // success
130
}
131    }
132
133
134    /**
135     * Tests locality constraints on DynAny objects.
136     */

137    public void testDynAnyLocalityConstraint ()
138    {
139       String JavaDoc msg;
140       org.omg.CORBA.TypeCode JavaDoc tc = null;
141       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
142
143       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long);
144       dynAny = createDynAnyFromTypeCode (tc);
145
146       msg = "MARSHAL system exception not raised by ORB::object_to_string ";
147       msg += "operation";
148       try
149       {
150          orb.object_to_string (dynAny);
151          
152          fail (msg);
153       }
154       catch (org.omg.CORBA.MARSHAL JavaDoc ex)
155       {
156          // success
157
}
158    }
159
160
161    /**
162     * Test comparing DynAny values.
163     */

164    public void testCompareDynAny ()
165    {
166       String JavaDoc msg;
167       org.omg.CORBA.Any JavaDoc any = null;
168       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
169       org.omg.DynamicAny.DynAny JavaDoc dynAny2 = null;
170       
171       any = orb.create_any ();
172       any.insert_long (700);
173       dynAny = createDynAnyFromAny (any);
174       dynAny2 = createDynAnyFromAny (any);
175       
176       msg = "Comparing two equal DynAny values using DynAny::equal failed";
177       assertTrue (msg, dynAny.equal (dynAny2));
178    }
179
180
181    /**
182     * Test accessing a value of some basic type in a DynAny object.
183     */

184    public void testAccessBasicValue ()
185    {
186       String JavaDoc msg;
187       int intVal;
188       int intVal2;
189       org.omg.CORBA.TypeCode JavaDoc tc = null;
190       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
191
192       intVal = 700;
193       intVal2 = 0;
194       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long);
195       dynAny = createDynAnyFromTypeCode (tc);
196       
197       try
198       {
199          dynAny.insert_long (intVal);
200       }
201       catch (Throwable JavaDoc ex)
202       {
203          fail ("Failed to insert value into DynAny object: " + ex);
204       }
205
206       try
207       {
208          intVal2 = dynAny.get_long ();
209       }
210       catch (Throwable JavaDoc ex)
211       {
212          fail ("Failed to extract value from DynAny object: " + ex);
213       }
214
215       msg = "Value inserted into DynAny object is not equal to value ";
216       msg += "extracted from same DynAny object";
217       assertEquals (msg, intVal, intVal2);
218    }
219
220
221    /**
222     * Test that a TypeMismatch exception is raised if there is a type
223     * mismatch between the TypeCode of a DynAny object and the accessor
224     * methods used to access its value.
225     */

226    public void testAccessTypeMismatchEx ()
227    {
228       String JavaDoc msg;
229       org.omg.CORBA.TypeCode JavaDoc tc = null;
230       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
231
232       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_string);
233       dynAny = createDynAnyFromTypeCode (tc);
234
235       msg = "TypeMismatch exception not thrown by insert operation when ";
236       msg += "insert operation is not valid for TypeCode of DynAny";
237       try
238       {
239          dynAny.insert_long (700);
240
241          fail (msg);
242       }
243       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
244       {
245          // success
246
}
247       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
248       {
249          fail (msg + ": " + ex);
250       }
251
252       msg = "TypeMismatch exception not thrown by get operation when ";
253       msg += "get operation is not valid for TypeCode of DynAny";
254       try
255       {
256          dynAny.get_long ();
257       }
258       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
259       {
260          // success
261
}
262       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
263       {
264          fail (msg + ": " + ex);
265       }
266    }
267
268    
269    /**
270     * Test obtaining the TypeCode associated with a DynAny object.
271     */

272    public void testDynAnyTypeCode ()
273    {
274       String JavaDoc msg;
275       org.omg.CORBA.TypeCode JavaDoc tc = null;
276       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
277
278       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long);
279       dynAny = createDynAnyFromTypeCode (tc);
280          
281       msg = "Incorrect TypeCode retrieved from DynAny::type operation";
282       assertTrue (msg, dynAny.type ().equal (tc));
283    }
284
285
286    /**
287     * Test initializing a DynAny object from another DynAny object.
288     */

289    public void testInitDynAnyFromDynAny ()
290    {
291       String JavaDoc msg;
292       org.omg.CORBA.Any JavaDoc any = null;
293       org.omg.CORBA.TypeCode JavaDoc tc = null;
294       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
295       org.omg.DynamicAny.DynAny JavaDoc dynAny2 = null;
296
297       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long);
298       dynAny = createDynAnyFromTypeCode (tc);
299
300       any = orb.create_any ();
301       any.insert_long (700);
302       dynAny2 = createDynAnyFromAny (any);
303
304       msg = "Failed to initialize a DynAny object from another DynAny ";
305       msg += "object using the DynAny::assign operation";
306       try
307       {
308          dynAny.assign (dynAny2);
309       }
310       catch (Throwable JavaDoc ex)
311       {
312          fail (msg + ": " + ex);
313       }
314       assertTrue (msg, dynAny.equal (dynAny2));
315    }
316
317
318    /**
319     * Test that a TypeMismatch exception is raised if there is a type
320     * mismatch between the two DynAny types in an assignment operation.
321     */

322    public void testInitFromDynAnyTypeMismatchEx ()
323    {
324       String JavaDoc msg;
325       org.omg.CORBA.Any JavaDoc any = null;
326       org.omg.CORBA.TypeCode JavaDoc tc = null;
327       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
328       org.omg.DynamicAny.DynAny JavaDoc dynAny2 = null;
329
330       any = orb.create_any ();
331       any.insert_long (700);
332       dynAny = createDynAnyFromAny (any);
333
334       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_string);
335       dynAny2 = createDynAnyFromTypeCode (tc);
336
337       msg = "TypeMismatch exception not thrown by DynAny::assign ";
338       msg += "operation when DynAny operands have different types";
339       try
340       {
341          dynAny.assign (dynAny2);
342          
343          fail (msg);
344       }
345       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
346       {
347          // success
348
}
349    }
350
351
352    /**
353     * Test initializing a DynAny object from an Any value.
354     */

355    public void testInitDynAnyFromAny ()
356    {
357       String JavaDoc msg;
358       org.omg.CORBA.Any JavaDoc any = null;
359       org.omg.CORBA.TypeCode JavaDoc tc = null;
360       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
361       org.omg.DynamicAny.DynAny JavaDoc dynAny2 = null;
362       
363       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long);
364       dynAny = createDynAnyFromTypeCode (tc);
365
366       any = orb.create_any ();
367       any.insert_long (700);
368       dynAny2 = createDynAnyFromAny (any);
369
370       msg = "Failed to initialize a DynAny object from an Any object ";
371       msg += "using the DynAny::from_any operation";
372       try
373       {
374          dynAny.from_any (any);
375       }
376       catch (Throwable JavaDoc ex)
377       {
378          fail (msg + ": " + ex);
379       }
380       assertTrue (msg, dynAny.equal (dynAny2));
381    }
382
383
384    /**
385     * Test that a TypeMismatch exception is raised if there is a type
386     * mismatch between the DynAny and Any types in an assignment.
387     */

388    public void testInitFromAnyTypeMismatchEx ()
389    {
390       String JavaDoc msg;
391       org.omg.CORBA.Any JavaDoc any = null;
392       org.omg.CORBA.TypeCode JavaDoc tc = null;
393       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
394       
395       any = orb.create_any ();
396       any.insert_long (700);
397
398       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_string);
399       dynAny = createDynAnyFromTypeCode (tc);
400       
401       msg = "TypeMismatch exception not thrown by DynAny::from_any ";
402       msg += "operation when DynAny and Any operands have different types";
403       try
404       {
405          dynAny.from_any (any);
406
407          fail (msg);
408       }
409       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
410       {
411          // success
412
}
413       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
414       {
415          fail (msg + ": " + ex);
416       }
417    }
418
419
420    /**
421     * Test generating an Any value from a DynAny object.
422     */

423    public void testGenerateAnyFromDynAny ()
424    {
425       String JavaDoc msg;
426       org.omg.CORBA.Any JavaDoc any = null;
427       org.omg.CORBA.TypeCode JavaDoc tc = null;
428       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
429       org.omg.DynamicAny.DynAny JavaDoc dynAny2 = null;
430       
431       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long);
432       dynAny = createDynAnyFromTypeCode (tc);
433
434       any = orb.create_any ();
435       any = dynAny.to_any ();
436       dynAny2 = createDynAnyFromAny (any);
437       
438       msg = "The DynAny::to_any operation failed to create an Any ";
439       msg += "object with the same value as the DynAny object";
440       assertTrue (msg, dynAny.equal (dynAny2));
441    }
442
443
444    /**
445     * Test destroying a DynAny object.
446     */

447    public void testDestroyDynAny ()
448    {
449       String JavaDoc msg;
450       org.omg.CORBA.Any JavaDoc any = null;
451       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
452       
453       any = orb.create_any ();
454       any.insert_long (700);
455       dynAny = createDynAnyFromAny (any);
456       dynAny.destroy ();
457
458       try
459       {
460          dynAny.type ();
461
462          msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
463          msg += "calling DynAny::type operation on a destroyed DynAny object ";
464          msg += "did not raise OBJECT_NOT_EXIST exception";
465          fail (msg);
466       }
467       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
468       {
469          // success
470
}
471
472       msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
473       msg += "calling DynAny::current_component operation on a destroyed ";
474       msg += "DynAny object did not raise OBJECT_NOT_EXIST exception";
475       try
476       {
477          dynAny.current_component ();
478
479          fail (msg);
480       }
481       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
482       {
483          // success
484
}
485       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
486       {
487          fail (msg + ": " + ex);
488       }
489    }
490
491
492    /**
493     * Test creating a copy of a DynAny object.
494     */

495    public void testCopyDynAny ()
496    {
497       String JavaDoc msg;
498       org.omg.CORBA.TypeCode JavaDoc tc = null;
499       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
500       org.omg.DynamicAny.DynAny JavaDoc dynAny2 = null;
501       
502       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long);
503       dynAny = createDynAnyFromTypeCode (tc);
504
505       try
506       {
507          dynAny.insert_long (700);
508       }
509       catch (Throwable JavaDoc ex)
510       {
511          fail ("Failed to insert value into DynAny object: " + ex);
512       }
513       dynAny2 = dynAny.copy ();
514
515       msg = "The DynAny object created with the DynAny::copy operation ";
516       msg += "is not equal to the DynAny object it was copied from";
517       assertTrue (msg, dynAny.equal (dynAny2));
518    }
519
520
521    /**
522     * Test iterating through components of a DynAny.
523     */

524    public void testIterateDynAny ()
525    {
526       String JavaDoc msg;
527       int compCount = -1;
528       boolean seek;
529       org.omg.CORBA.TypeCode JavaDoc tc = null;
530       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
531       
532       tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long);
533       dynAny = createDynAnyFromTypeCode (tc);
534
535       // test the component count
536
try
537       {
538          compCount = dynAny.component_count ();
539       }
540       catch (Throwable JavaDoc ex)
541       {
542          // should not be needed, but it prevents compiler errors
543
fail ("Unexpected error raised by DynAny::component_count operation");
544       }
545       msg = "The number of components returned from the ";
546       msg += "DynAny::component_count operation is incorrect";
547       assertEquals (msg, 0, compCount);
548       
549       // test if there is a first component
550
msg = "The DynAny::seek operation indicates that a valid component ";
551       msg += "exists but the DynAny should have no components";
552       seek = dynAny.seek (0);
553       assertTrue (msg, !seek);
554       
555       // test getting the current component
556
try
557       {
558          dynAny = dynAny.current_component ();
559
560          msg = "A TypeMismatch exception was not raised by the ";
561          msg += "DynAny::current_component operation when trying to access ";
562          msg += "the current component of a DynAny with no components";
563          fail (msg);
564       }
565       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
566       {
567          // success
568
}
569    }
570
571
572    private static class Setup extends TestSetup
573    {
574       public Setup (Test test)
575       {
576          super (test);
577       }
578
579       protected void setUp ()
580       {
581          org.omg.CORBA.Object JavaDoc obj = null;
582
583          orb = ORBSetup.getORB ();
584          try
585          {
586             obj = orb.resolve_initial_references ("DynAnyFactory");
587          }
588          catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc ex)
589          {
590             fail ("Failed to resolve DynAnyFactory: " + ex);
591          }
592          try
593          {
594             factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj);
595          }
596          catch (Throwable JavaDoc ex)
597          {
598             fail ("Failed to narrow to DynAnyFactory: " + ex);
599          }
600       }
601
602       protected void tearDown ()
603       {
604       }
605    }
606
607
608    /**
609     * Create a DynAny object from an Any object.
610     */

611    private static org.omg.DynamicAny.DynAny JavaDoc createDynAnyFromAny
612       (org.omg.CORBA.Any JavaDoc any)
613    {
614       String JavaDoc msg;
615       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
616
617       try
618       {
619          dynAny = factory.create_dyn_any (any);
620       }
621       catch (Throwable JavaDoc ex)
622       {
623          msg = "Factory failed to create DynAny from Any using ";
624          msg += "DynAny::create_dyn_any operation: " + ex;
625          fail (msg);
626       }
627       return dynAny;
628    }
629
630
631    /**
632     * Create a DynAny object from a TypeCode object.
633     */

634    private static org.omg.DynamicAny.DynAny JavaDoc createDynAnyFromTypeCode
635       (org.omg.CORBA.TypeCode JavaDoc tc)
636    {
637       String JavaDoc msg;
638       org.omg.DynamicAny.DynAny JavaDoc dynAny = null;
639
640       try
641       {
642          dynAny = factory.create_dyn_any_from_type_code (tc);
643       }
644       catch (Throwable JavaDoc ex)
645       {
646          msg = "Factory failed to create DynAny from TypeCode using ";
647          msg += "DynAny::create_dyn_any_from_type_code operation: " + ex;
648          fail (msg);
649       }
650       return dynAny;
651    }
652    
653 }
654
Popular Tags