KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.math.BigDecimal JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 /**
31  * DynAnyFixedTest.java
32  *
33  * DynAny tests for fixed types.
34  *
35  */

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

78    public void testFactoryCreateFromAny ()
79    {
80       BigDecimal JavaDoc fixedVal;
81       org.omg.CORBA.Any JavaDoc any = null;
82       org.omg.CORBA.TypeCode JavaDoc tc = null;
83
84       tc = orb.create_fixed_tc ((short) 2, (short) 1);
85       fixedVal = new BigDecimal JavaDoc ("1.0");
86       any = orb.create_any ();
87       any.insert_fixed (fixedVal, tc);
88
89       createDynAnyFromAny (any);
90    }
91
92
93    /**
94     * Tests creating a DynAny object from a TypeCode object using the
95     * DynAnyFactory object.
96     */

97    public void testFactoryCreateFromTypeCode ()
98    {
99       org.omg.CORBA.TypeCode JavaDoc tc = null;
100
101       tc = orb.create_fixed_tc ((short) 2, (short) 1);
102
103       createDynAnyFromTypeCode (tc);
104    }
105
106
107    /**
108     * Test comparing DynAny values.
109     */

110    public void testCompareDynAny ()
111    {
112       String JavaDoc msg;
113       BigDecimal JavaDoc fixedVal;
114       org.omg.CORBA.Any JavaDoc any = null;
115       org.omg.CORBA.TypeCode JavaDoc tc = null;
116       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
117       org.omg.DynamicAny.DynFixed JavaDoc dynAny2 = null;
118       
119       tc = orb.create_fixed_tc ((short) 2, (short) 1);
120       fixedVal = new BigDecimal JavaDoc ("1.0");
121       any = orb.create_any ();
122       any.insert_fixed (fixedVal, tc);
123       dynAny = createDynAnyFromAny (any);
124       dynAny2 = createDynAnyFromAny (any);
125       
126       msg = "Comparing two equal DynAny values using DynAny::equal failed";
127       assertTrue (msg, dynAny.equal (dynAny2));
128    }
129
130
131    /**
132     * Test accessing a value in a DynFixed object.
133     */

134    public void testAccessFixedValue ()
135    {
136       String JavaDoc msg;
137       Boolean JavaDoc setVal;
138       BigDecimal JavaDoc fixedVal;
139       BigDecimal JavaDoc fixedVal2;
140       org.omg.CORBA.TypeCode JavaDoc tc = null;
141       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
142
143       // use reflection to avoid ORB portability issues
144
Class JavaDoc fixedClass = null;
145       Method JavaDoc method = null;
146       Class JavaDoc [] paramTypes = new Class JavaDoc [1];
147       Object JavaDoc [] params = new Object JavaDoc [1];
148
149       fixedVal = new BigDecimal JavaDoc ("1.0");
150       fixedVal2 = null;
151       tc = orb.create_fixed_tc ((short) 2, (short) 1);
152       dynAny = createDynAnyFromTypeCode (tc);
153       
154       fixedClass = dynAny.getClass ();
155       paramTypes [0] = String JavaDoc.class;
156       params [0] = fixedVal.toString ();
157
158       // get method to invoke via reflection
159
try
160       {
161          method = fixedClass.getDeclaredMethod ("set_value", paramTypes);
162       }
163       catch (Throwable JavaDoc ex)
164       {
165          fail ("Unexpected error trying to obtain method to invoke: " + ex);
166       }
167
168       msg = "Failed to set value of DynFixed object with ";
169       msg += "DynFixed::set_value operation when truncation was not required";
170       setVal = null;
171       try
172       {
173          setVal = (Boolean JavaDoc) method.invoke (dynAny, params);
174       }
175       catch (Throwable JavaDoc ex)
176       {
177          fail (msg + ": " + ex);
178       }
179       // verify return type if not void
180
if (setVal != null)
181       {
182          assertTrue (msg, setVal.booleanValue ());
183       }
184       
185       fixedVal = new BigDecimal JavaDoc ("1.01");
186       params [0] = fixedVal.toString ();
187
188       msg = "Failed to set value of DynFixed object with ";
189       msg += "DynFixed::set_value operation when truncation was required";
190       setVal = null;
191       try
192       {
193          setVal = (Boolean JavaDoc) method.invoke (dynAny, params);
194       }
195       catch (Throwable JavaDoc ex)
196       {
197          fail (msg + ": " + ex);
198       }
199       // verify return type if not void
200
if (setVal != null)
201       {
202          assertTrue (msg, !setVal.booleanValue ());
203       }
204
205       try
206       {
207          fixedVal2 = new BigDecimal JavaDoc (dynAny.get_value ());
208       }
209       catch (Throwable JavaDoc ex)
210       {
211          msg = "Failed to get value of DynFixed object with ";
212          msg += "DynFixed::get_value operation";
213          fail (msg + ": " + ex);
214       }
215       msg = "Value inserted into DynAny object is not equal to value ";
216       msg += "extracted from same DynAny object";
217       assertTrue (msg, fixedVal2.toString ().equals ("1.0"));
218    }
219
220
221    /**
222     * Test that a TypeMismatch exception is raised if the fixed value is
223     * invalid.
224     */

225    public void testAccessTypeMismatchEx ()
226    {
227       String JavaDoc msg;
228       String JavaDoc badFixedVal;
229       org.omg.CORBA.TypeCode JavaDoc tc = null;
230       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
231
232       badFixedVal = "j.0";
233       tc = orb.create_fixed_tc ((short) 2, (short) 1);
234       dynAny = createDynAnyFromTypeCode (tc);
235
236       msg = "TypeMismatch exception not thrown by DynFixed::set_value ";
237       msg += "operation when a DynFixed object is set to an invalid value";
238       try
239       {
240          dynAny.set_value (badFixedVal);
241
242          fail (msg);
243       }
244       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
245       {
246          // success
247
}
248       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
249       {
250          fail (msg + ": " + ex);
251       }
252    }
253
254
255    /**
256     * Test that an InvalidValue exception is raised if there are too many
257     * digits in the fixed value.
258     */

259    public void testAccessInvalidValueEx ()
260    {
261       String JavaDoc msg;
262       BigDecimal JavaDoc fixedVal;
263       org.omg.CORBA.TypeCode JavaDoc tc = null;
264       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
265
266       fixedVal = new BigDecimal JavaDoc ("10.01");
267       tc = orb.create_fixed_tc ((short) 2, (short) 1);
268       dynAny = createDynAnyFromTypeCode (tc);
269
270       msg = "InvalidValue exception not thrown by DynFixed::set_value ";
271       msg += "operation when there are too many digits in the fixed value";
272       try
273       {
274          dynAny.set_value (fixedVal.toString ());
275
276          fail (msg);
277       }
278       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
279       {
280          // success
281
}
282       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
283       {
284          fail (msg + ": " + ex);
285       }
286    }
287
288    
289    /**
290     * Test obtaining the TypeCode associated with a DynAny object.
291     */

292    public void testDynAnyTypeCode ()
293    {
294       String JavaDoc msg;
295       org.omg.CORBA.TypeCode JavaDoc tc = null;
296       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
297
298       tc = orb.create_fixed_tc ((short) 2, (short) 1);
299       dynAny = createDynAnyFromTypeCode (tc);
300          
301       msg = "Incorrect TypeCode retrieved from DynAny::type operation";
302       assertTrue (msg, dynAny.type ().equal (tc));
303    }
304
305
306    /**
307     * Test initializing a DynAny object from another DynAny object.
308     */

309    public void testInitDynAnyFromDynAny ()
310    {
311       String JavaDoc msg;
312       BigDecimal JavaDoc fixedVal;
313       org.omg.CORBA.Any JavaDoc any = null;
314       org.omg.CORBA.TypeCode JavaDoc tc = null;
315       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
316       org.omg.DynamicAny.DynFixed JavaDoc dynAny2 = null;
317
318       tc = orb.create_fixed_tc ((short) 2, (short) 1);
319       dynAny = createDynAnyFromTypeCode (tc);
320
321       fixedVal = new BigDecimal JavaDoc ("1.0");
322       any = orb.create_any ();
323       any.insert_fixed (fixedVal, tc);
324       dynAny2 = createDynAnyFromAny (any);
325
326       msg = "Failed to initialize a DynAny object from another DynAny ";
327       msg += "object using the DynAny::assign operation";
328       try
329       {
330          dynAny.assign (dynAny2);
331       }
332       catch (Throwable JavaDoc ex)
333       {
334          fail (msg + ": " + ex);
335       }
336       assertTrue (msg, dynAny.equal (dynAny2));
337    }
338
339
340    /**
341     * Test initializing a DynAny object from an Any value.
342     */

343    public void testInitDynAnyFromAny ()
344    {
345       String JavaDoc msg;
346       BigDecimal JavaDoc fixedVal;
347       org.omg.CORBA.Any JavaDoc any = null;
348       org.omg.CORBA.TypeCode JavaDoc tc = null;
349       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
350       org.omg.DynamicAny.DynFixed JavaDoc dynAny2 = null;
351       
352       tc = orb.create_fixed_tc ((short) 2, (short) 1);
353       dynAny = createDynAnyFromTypeCode (tc);
354
355       fixedVal = new BigDecimal JavaDoc ("1.0");
356       any = orb.create_any ();
357       any.insert_fixed (fixedVal, tc);
358       dynAny2 = createDynAnyFromAny (any);
359
360       msg = "Failed to initialize a DynAny object from an Any object ";
361       msg += "using the DynAny::from_any operation";
362       try
363       {
364          dynAny.from_any (any);
365       }
366       catch (Throwable JavaDoc ex)
367       {
368          fail (msg + ": " + ex);
369       }
370       assertTrue (msg, dynAny.equal (dynAny2));
371    }
372
373
374    /**
375     * Test that a TypeMismatch exception is raised if there is a type
376     * mismatch between the DynAny and Any types in an assignment.
377     */

378    public void testInitFromAnyTypeMismatchEx ()
379    {
380       String JavaDoc msg;
381       org.omg.CORBA.Any JavaDoc any = null;
382       org.omg.CORBA.TypeCode JavaDoc tc = null;
383       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
384       
385       any = orb.create_any ();
386       any.insert_string ("Hello");
387
388       tc = orb.create_fixed_tc ((short) 2, (short) 1);
389       dynAny = createDynAnyFromTypeCode (tc);
390             
391       msg = "TypeMismatch exception not thrown by DynAny::from_any ";
392       msg += "operation when DynAny and Any operands have different types";
393       try
394       {
395          dynAny.from_any (any);
396
397          fail (msg);
398       }
399       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
400       {
401          // success
402
}
403       catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc ex)
404       {
405          fail (msg + ": " + ex);
406       }
407    }
408
409
410    /**
411     * Test generating an Any value from a DynAny object.
412     */

413    public void testGenerateAnyFromDynAny ()
414    {
415       String JavaDoc msg;
416       BigDecimal JavaDoc fixedVal;
417       org.omg.CORBA.Any JavaDoc any = null;
418       org.omg.CORBA.TypeCode JavaDoc tc = null;
419       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
420       org.omg.DynamicAny.DynFixed JavaDoc dynAny2 = null;
421       
422       tc = orb.create_fixed_tc ((short) 2, (short) 1);
423       fixedVal = new BigDecimal JavaDoc ("1.0");
424       dynAny = createDynAnyFromTypeCode (tc);
425
426       any = orb.create_any ();
427       any = dynAny.to_any ();
428       dynAny2 = createDynAnyFromAny (any);
429       
430       msg = "The DynAny::to_any operation failed to create an Any ";
431       msg += "object with the same value as the DynAny object";
432       assertTrue (msg, dynAny.equal (dynAny2));
433    }
434
435
436    /**
437     * Test destroying a DynAny object.
438     */

439    public void testDestroyDynAny ()
440    {
441       String JavaDoc msg;
442       BigDecimal JavaDoc fixedVal;
443       org.omg.CORBA.Any JavaDoc any = null;
444       org.omg.CORBA.TypeCode JavaDoc tc = null;
445       org.omg.DynamicAny.DynFixed JavaDoc dynAny = null;
446       
447       tc = orb.create_fixed_tc ((short) 2, (short) 1);
448       fixedVal = new BigDecimal JavaDoc ("1.0");
449       any = orb.create_any ();
450       any.insert_fixed (fixedVal, tc);
451       dynAny = createDynAnyFromAny (any);
452       dynAny.destroy ();
453
454       try
455       {
456          dynAny.type ();
457
458          msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
459          msg += "calling DynAny::type operation on a destroyed DynAny object ";
460          msg += "did not raise OBJECT_NOT_EXIST exception";
461          fail (msg);
462       }
463       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
464       {
465          // success
466
}
467
468       msg = "Failed to destroy DynAny using DynAny::destroy operation - ";
469       msg += "calling DynAny::current_component operation on a destroyed ";
470       msg += "DynAny object did not raise OBJECT_NOT_EXIST exception";
471       try
472       {
473          dynAny.current_component ();
474
475          fail (msg);
476       }
477       catch (org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc ex)
478       {
479          // success
480
}
481       catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc ex)
482       {
483          fail (msg + ": " + ex);
484       }
485    }
486
487
488    /**
489     * Test creating a copy of a DynAny object.
490     */

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

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

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

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