KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > orb > CallbackTest


1 package org.jacorb.test.orb;
2
3 import junit.framework.*;
4 import junit.extensions.*;
5
6 import org.jacorb.test.*;
7
8 import org.jacorb.test.common.*;
9 import org.omg.CORBA.*;
10 import org.omg.Messaging.*;
11
12 public class CallbackTest extends CallbackTestCase
13 {
14     private CallbackServer server;
15
16     private static final char EURO_SIGN = '\u20AC'; // not a CORBA char
17

18     public CallbackTest(String JavaDoc name, ClientServerSetup setup)
19     {
20         super(name, setup);
21     }
22
23     public void setUp() throws Exception JavaDoc
24     {
25         server = CallbackServerHelper.narrow( setup.getServerObject() );
26     }
27
28     public static Test suite()
29     {
30         TestSuite suite = new TestSuite( "Callback Test" );
31         ClientServerSetup setup = new ClientServerSetup
32             ( suite, "org.jacorb.test.orb.CallbackServerImpl" );
33
34         suite.addTest( new CallbackTest( "test_sync_ping", setup ) );
35         suite.addTest( new CallbackTest( "test_ping", setup ) );
36         suite.addTest( new CallbackTest( "test_delayed_ping", setup ) );
37         suite.addTest( new CallbackTest( "test_pass_in_char", setup ) );
38         suite.addTest( new CallbackTest( "test_pass_in_illegal_char", setup ) );
39         suite.addTest( new CallbackTest( "test_return_char", setup ) );
40         suite.addTest( new CallbackTest( "test_return_illegal_char", setup ) );
41         suite.addTest( new CallbackTest( "test_complex_operation", setup ) );
42         suite.addTest( new CallbackTest( "test_empty_exception", setup ) );
43         suite.addTest( new CallbackTest( "test_empty_exception_not_raised", setup ) );
44         suite.addTest( new CallbackTest( "test_non_empty_exception", setup ) );
45         suite.addTest( new CallbackTest( "test_either_exception_1", setup ) );
46         suite.addTest( new CallbackTest( "test_either_exception_2", setup ) );
47             
48         return setup;
49     }
50
51     private class ReplyHandler
52         extends CallbackTestCase.ReplyHandler
53         implements AMI_CallbackServerHandlerOperations
54     {
55         public void delayed_ping_excep(ExceptionHolder excep_holder)
56         {
57             wrong_exception( "delayed_ping_excep", excep_holder );
58         }
59
60         public void delayed_ping()
61         {
62             wrong_reply( "delayed_ping" );
63         }
64
65         public void operation_excep(ExceptionHolder excep_holder)
66         {
67             wrong_exception( "operation_excep", excep_holder );
68         }
69
70         public void operation(int ami_return_val, char p1, int p2)
71         {
72             wrong_reply( "operation" );
73         }
74
75         public void pass_in_char_excep(ExceptionHolder excep_holder)
76         {
77             wrong_exception( "pass_in_char_excep", excep_holder );
78         }
79
80         public void pass_in_char()
81         {
82             wrong_reply( "pass_in_char" );
83         }
84
85         public void ping_excep(ExceptionHolder excep_holder)
86         {
87             wrong_exception( "ping_excep", excep_holder );
88         }
89
90         public void ping()
91         {
92             wrong_reply( "ping" );
93         }
94
95         public void return_char_excep(ExceptionHolder excep_holder)
96         {
97             wrong_exception( "return_char_excep", excep_holder );
98         }
99
100         public void return_char(char ami_return_val)
101         {
102             wrong_reply( "return_char" );
103         }
104
105         public void ex_1_excep(ExceptionHolder excep_holder)
106         {
107             wrong_exception( "ex_1_excep", excep_holder );
108         }
109
110         public void ex_1()
111         {
112             wrong_reply( "ex_1" );
113         }
114
115         public void ex_2_excep(ExceptionHolder excep_holder)
116         {
117             wrong_exception( "ex_2_excep", excep_holder );
118         }
119
120         public void ex_2(int ami_return_val, int p)
121         {
122             wrong_reply( "ex_2" );
123         }
124
125         public void ex_3_excep(ExceptionHolder excep_holder)
126         {
127             wrong_exception( "ex_3_excep", excep_holder );
128         }
129
130         public void ex_3()
131         {
132             wrong_reply( "ex_3" );
133         }
134
135     }
136
137     private AMI_CallbackServerHandler ref ( ReplyHandler handler )
138     {
139         AMI_CallbackServerHandlerPOATie tie =
140             new AMI_CallbackServerHandlerPOATie( handler )
141             {
142                 public org.omg.CORBA.portable.OutputStream JavaDoc
143                     _invoke( String JavaDoc method,
144                              org.omg.CORBA.portable.InputStream JavaDoc _input,
145                              org.omg.CORBA.portable.ResponseHandler JavaDoc handler )
146                     throws org.omg.CORBA.SystemException JavaDoc
147                 {
148                     try
149                     {
150                         return super._invoke( method, _input, handler );
151                     }
152                     catch( AssertionFailedError e )
153                     {
154                         return null;
155                     }
156                 }
157             };
158         return tie._this( setup.getClientOrb() );
159     }
160
161     public void test_sync_ping()
162     {
163         server.ping();
164     }
165     
166     public void test_ping()
167     {
168         ReplyHandler handler = new ReplyHandler()
169         {
170             public void ping()
171             {
172                 pass();
173             }
174         };
175         
176         ( ( _CallbackServerStub ) server ).sendc_ping( ref( handler ) );
177         handler.wait_for_reply( 1000 );
178     }
179
180     public void test_delayed_ping()
181     {
182         ReplyHandler handler = new ReplyHandler()
183         {
184             public void delayed_ping()
185             {
186                 pass();
187             }
188         };
189         
190         ( ( _CallbackServerStub ) server )
191                     .sendc_delayed_ping( ref( handler ), 500 );
192         handler.wait_for_reply( 700 );
193     }
194     
195     public void test_pass_in_char()
196     {
197         ReplyHandler handler = new ReplyHandler()
198         {
199             public void pass_in_char()
200             {
201                 pass();
202             }
203         };
204         
205         ( ( _CallbackServerStub ) server )
206                    .sendc_pass_in_char( ref( handler ), 'x', 100 );
207         handler.wait_for_reply( 200 );
208     }
209     
210     public void test_pass_in_illegal_char()
211     {
212         ReplyHandler handler = new ReplyHandler();
213
214         try
215         {
216             ( ( _CallbackServerStub ) server )
217                    .sendc_pass_in_char( ref( handler ), EURO_SIGN, 100 );
218             fail( "DATA_CONVERSION exception expected" );
219         }
220         catch( org.omg.CORBA.DATA_CONVERSION JavaDoc ex )
221         {
222             // ok
223
}
224     }
225      
226     public void test_return_char()
227     {
228         ReplyHandler handler = new ReplyHandler()
229         {
230             public void return_char( char ami_return_val )
231             {
232                 this.assertEquals( 'a', ami_return_val );
233                 pass();
234             }
235         };
236         ( ( _CallbackServerStub ) server )
237                  .sendc_return_char( ref( handler ), ( short ) 'a', 100 );
238         handler.wait_for_reply( 200 );
239     }
240     
241     public void test_return_illegal_char()
242     {
243         ReplyHandler handler = new ReplyHandler()
244         {
245             public void return_char_excep( ExceptionHolder excep_holder )
246             {
247                 this.assertEquals( org.omg.CORBA.DATA_CONVERSION JavaDoc.class,
248                               getException( excep_holder ).getClass() );
249                 pass();
250             }
251         };
252         ( ( _CallbackServerStub ) server )
253             .sendc_return_char( ref( handler ), ( short ) EURO_SIGN, 100 );
254         handler.wait_for_reply( 500 );
255     }
256     
257     public void test_complex_operation()
258     {
259         ReplyHandler handler = new ReplyHandler()
260         {
261             public void operation( int ami_return_val, char p1, int p2 )
262             {
263                 this.assertEquals( 'A', p1 );
264                 this.assertEquals( 4321, p2 );
265                 this.assertEquals( p2, ami_return_val );
266                 pass();
267             }
268         };
269         ( ( _CallbackServerStub ) server )
270             .sendc_operation( ref( handler ), 'a', false, 100 );
271         handler.wait_for_reply( 200 );
272     }
273     
274     public void test_empty_exception()
275     {
276         ReplyHandler handler = new ReplyHandler()
277         {
278             public void ex_1_excep( ExceptionHolder excep_holder )
279             {
280                 this.assertEquals( EmptyException.class,
281                               getException( excep_holder ).getClass() );
282                 pass();
283             }
284         };
285         ( ( _CallbackServerStub ) server )
286             .sendc_ex_1( ref( handler ), true, 100 );
287         handler.wait_for_reply( 500 );
288     }
289     
290     public void test_empty_exception_not_raised()
291     {
292         ReplyHandler handler = new ReplyHandler()
293         {
294             public void ex_1()
295             {
296                 pass();
297             }
298         };
299         ( ( _CallbackServerStub ) server )
300             .sendc_ex_1( ref( handler ), false, 100 );
301         handler.wait_for_reply( 500 );
302     }
303     
304     public void test_non_empty_exception()
305     {
306         ReplyHandler handler = new ReplyHandler()
307         {
308             public void ex_2_excep( ExceptionHolder excep_holder )
309             {
310                 Exception JavaDoc ex = getException( excep_holder );
311                 if ( !(ex instanceof NonEmptyException) )
312                 {
313                     this.fail( "wrong exception type: " + ex );
314                 }
315                 else
316                 {
317                     NonEmptyException nex = (NonEmptyException)ex;
318                     
319                     // The CORBA Spec and the Java Mapping are not
320
// entirely clear whether the "_reason" parameter
321
// should be marshaled along with the id.
322
// JacORB doesn't do it, and hence we don't check
323
// for it here.
324

325                     // assertTrue( nex.getMessage().endsWith( " just do it" ) );
326

327                     this.assertEquals( 17, nex.field1 );
328                     this.assertEquals( "xxx", nex.field2 );
329                     pass();
330                 }
331             }
332         };
333         ( ( _CallbackServerStub ) server )
334             .sendc_ex_2( ref( handler ), 17, true, 100 );
335         handler.wait_for_reply( 500 );
336     }
337     
338     public void test_either_exception_1()
339     {
340         ReplyHandler handler = new ReplyHandler()
341         {
342             public void ex_3_excep( ExceptionHolder excep_holder )
343             {
344                 this.assertEquals( EmptyException.class,
345                               getException( excep_holder ).getClass() );
346                 pass();
347             }
348         };
349         ( ( _CallbackServerStub ) server )
350             .sendc_ex_3( ref( handler ), false, 100 );
351         handler.wait_for_reply( 500 );
352     }
353
354     public void test_either_exception_2()
355     {
356         ReplyHandler handler = new ReplyHandler()
357         {
358             public void ex_3_excep( ExceptionHolder excep_holder )
359             {
360                 this.assertEquals( NonEmptyException.class,
361                               getException( excep_holder ).getClass() );
362                 pass();
363             }
364         };
365         ( ( _CallbackServerStub ) server )
366             .sendc_ex_3( ref( handler ), true, 100 );
367         handler.wait_for_reply( 500 );
368     }
369
370 }
371
Popular Tags