KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > common > CallbackTestCase


1 package org.jacorb.test.common;
2
3 import org.omg.Messaging.*;
4
5 import junit.framework.*;
6
7 /**
8  * A special <code>ClientServerTestCase</code> for testing asynchronous
9  * method invocations following the callback model.
10  * <p>
11  * This class makes it convenient to issue an asynchronous invocation
12  * and wait for the reply within the same test case. There is an abstract
13  * <code>ReplyHandler</code> defined as an inner class of this class.
14  * It provides a synchronization mechanism so that the main thread of
15  * control that executes the test case can wait on the reply handler
16  * until it has received and analyzed the reply.
17  * <p>
18  * As an example, assume that you want to test asynchronous invocations
19  * of an interface <code>MyServer</code>. You would create a subclass
20  * of <code>CallbackTestCase</code>, and extend the generic
21  * <code>ReplyHandler</code> for <code>MyServer</code> as follows:
22  *
23  * <p><blockquote><pre>
24  * public class MyServerTest extends CallbackTestCase
25  * {
26  * private ReplyHandler extends CallbackTestCase.ReplyHandler
27  * implements MyServerOperations
28  * {
29  * // ...
30  * }
31  *
32  * }
33  * </pre></blockquote><p>
34  *
35  * Each method from <code>MyServerOperations</code> should be
36  * implemented to call <code>wrong_reply()</code> (for reply methods) or
37  * <code>wrong_exception()</code> (for <code>_excep</code> methods). In
38  * the actual test cases, these methods will in turn be overwritten
39  * anonymously for the correct replies.
40  * <p>
41  * Your type-specific <code>ReplyHandler</code> will be turned into a
42  * CORBA object using the tie approach. This is best done in a
43  * convenience method such as the following:
44  *
45  * <p><blockquote><pre>
46  * private AMI_MyServerHandler ref ( ReplyHandler handler )
47  * {
48  * AMI_MyServerHandlerPOATie tie =
49  * new AMI_MyServerHandlerPOATie( handler )
50  * {
51  * public org.omg.CORBA.portable.OutputStream
52  * _invoke( String method,
53  * org.omg.CORBA.portable.InputStream _input,
54  * org.omg.CORBA.portable.ResponseHandler handler )
55  * throws org.omg.CORBA.SystemException
56  * {
57  * try
58  * {
59  * return super._invoke( method, _input, handler );
60  * }
61  * catch( AssertionFailedError e )
62  * {
63  * return null;
64  * }
65  * }
66  * };
67  * return tie._this( setup.getClientOrb() );
68  * }
69  * </pre></blockquote><p>
70  *
71  * The <code>_invoke()</code> method of the POATie is overridden anonymously
72  * here to catch JUnit's <code>AssertionFailedError</code>s. Without this,
73  * a failed test case would cause all subsequent test cases to fail as well.
74  * You can copy this method verbatim into your own source, except for
75  * changing the name of the AMI classes.
76  * <p>
77  * An individual test case may then look like this:
78  *
79  * <p><blockquote><pre>
80  * public void test_some_operation()
81  * {
82  * ReplyHandler handler = new ReplyHandler()
83  * {
84  * public void some_operation( int ami_return_val )
85  * {
86  * assertEquals( 123, ami_return_val );
87  * pass();
88  * }
89  * };
90  * ( ( _CallbackServerStub ) server )
91  * .sendc_some_operation( ref( handler ) );
92  * handler.wait_for_reply( 200 );
93  * }
94  * </pre></blockquote><p>
95  *
96  * The <code>ReplyHandler</code> is extended anonymously to handle and
97  * analyze the expected reply. You may use any of the
98  * <code>assert...()</code> methods defined in
99  * <code>junit.framework.Assert</code> to do this (they are redefined in
100  * the generic <code>ReplyHandler</code>). Unlike in a normal test case,
101  * you must also call the <code>pass()</code> method explicitly if the
102  * test case did not fail. After the <code>ReplyHandler</code> has been
103  * defined, the asynchronous operation is invoked using the corresponding
104  * <code>sendc_</code> method. The calling thread then waits for a reply
105  * for at most 200 milliseconds. If no reply is received within that
106  * time, the test case fails.
107  *
108  * @author Andre Spiegel <spiegel@gnu.org>
109  * @version $Id: CallbackTestCase.java,v 1.5 2003/07/26 17:42:09 alphonse.bendt Exp $
110  */

111 public class CallbackTestCase extends ClientServerTestCase
112 {
113
114     public CallbackTestCase(String JavaDoc name, ClientServerSetup setup)
115     {
116         super(name, setup);
117     }
118
119     protected abstract class ReplyHandler
120     {
121         private boolean replyReceived = false;
122         private boolean testFailed = false;
123         private String JavaDoc failureMessage = null;
124
125     protected ReplyHandler() {
126     }
127
128         public synchronized void wait_for_reply(long timeout)
129         {
130             try
131             {
132                 long start = System.currentTimeMillis();
133                 if ( !replyReceived )
134                 {
135                     this.wait( timeout );
136                     if ( !replyReceived )
137                         junit.framework.Assert.fail
138                             ( "no reply within timeout ("
139                               + timeout + "ms)" );
140                 }
141                 //System.out.println( "waiting time: " +
142
// ( System.currentTimeMillis() - start ) );
143
if ( testFailed )
144                     junit.framework.Assert.fail( failureMessage );
145                 else
146                     ; // ok
147
}
148             catch ( InterruptedException JavaDoc e )
149             {
150                 junit.framework.Assert.fail
151                     ( "interrupted while waiting for reply" );
152             }
153             finally
154             {
155                 replyReceived = false;
156                 testFailed = false;
157                 failureMessage = null;
158             }
159         }
160
161         public synchronized void fail(String JavaDoc message)
162         {
163             replyReceived = true;
164             testFailed = true;
165             failureMessage = message;
166             this.notifyAll();
167             throw new AssertionFailedError();
168         }
169
170         public void wrong_reply( String JavaDoc name )
171         {
172             fail( "wrong reply method: " + name );
173         }
174
175         public void wrong_exception( String JavaDoc methodName,
176                                      ExceptionHolder excep_holder )
177         {
178             try
179             {
180                 excep_holder.raise_exception();
181             }
182             catch( Exception JavaDoc e )
183             {
184                 fail( "unexpected exception: "
185                       + methodName + ", " + e );
186             }
187         }
188
189         public Exception JavaDoc getException( ExceptionHolder excep_holder )
190         {
191             try
192             {
193                 excep_holder.raise_exception();
194                 return null;
195             }
196             catch( Exception JavaDoc e )
197             {
198                 return e;
199             }
200         }
201
202         public synchronized void pass()
203         {
204             replyReceived = true;
205             testFailed = false;
206             failureMessage = null;
207             this.notifyAll();
208         }
209
210         // The assert methods below are lifted from junit.framework.Assert.
211
// The only change is to make them non-static here.
212

213         /**
214          * Asserts that two bytes are equal.
215         */

216         public void assertEquals(byte expected, byte actual)
217         {
218             assertEquals(null, expected, actual);
219         }
220
221         /**
222          * Asserts that two chars are equal.
223          */

224         public void assertEquals(char expected, char actual)
225         {
226             assertEquals(null, expected, actual);
227         }
228
229         /**
230          * Asserts that two doubles are equal concerning a delta. If the expected
231          * value is infinity then the delta value is ignored.
232          */

233         public void assertEquals(
234             double expected,
235             double actual,
236             double delta)
237         {
238             assertEquals(null, expected, actual, delta);
239         }
240
241         /**
242          * Asserts that two floats are equal concerning a delta. If the expected
243          * value is infinity then the delta value is ignored.
244          */

245         public void assertEquals(float expected, float actual, float delta)
246         {
247             assertEquals(null, expected, actual, delta);
248         }
249
250         /**
251          * Asserts that two ints are equal.
252          */

253         public void assertEquals(int expected, int actual)
254         {
255             assertEquals(null, expected, actual);
256         }
257
258         /**
259          * Asserts that two longs are equal.
260          */

261         public void assertEquals(long expected, long actual)
262         {
263             assertEquals(null, expected, actual);
264         }
265
266         /**
267          * Asserts that two objects are equal. If they are not
268          * an AssertionFailedError is thrown.
269          */

270         public void assertEquals(Object JavaDoc expected, Object JavaDoc actual)
271         {
272             assertEquals(null, expected, actual);
273         }
274
275         /**
276          * Asserts that two bytes are equal.
277          */

278         public void assertEquals(String JavaDoc message, byte expected, byte actual)
279         {
280             assertEquals(message, new Byte JavaDoc(expected), new Byte JavaDoc(actual));
281         }
282
283         /**
284          * Asserts that two chars are equal.
285          */

286         public void assertEquals(String JavaDoc message, char expected, char actual)
287         {
288             assertEquals(
289                 message,
290                 new Character JavaDoc(expected),
291                 new Character JavaDoc(actual));
292         }
293
294         /**
295          * Asserts that two doubles are equal concerning a delta. If the expected
296          * value is infinity then the delta value is ignored.
297          */

298         public void assertEquals(
299             String JavaDoc message,
300             double expected,
301             double actual,
302             double delta)
303         {
304             // handle infinity specially since subtracting to infinite values gives NaN and the
305
// the following test fails
306
if (Double.isInfinite(expected))
307             {
308                 if (!(expected == actual))
309                     failNotEquals(
310                         message,
311                         new Double JavaDoc(expected),
312                         new Double JavaDoc(actual));
313             }
314             else if (
315                 !(Math.abs(expected - actual) <= delta))
316                 // Because comparison with NaN always returns false
317
failNotEquals(
318                     message,
319                     new Double JavaDoc(expected),
320                     new Double JavaDoc(actual));
321         }
322
323         /**
324          * Asserts that two floats are equal concerning a delta. If the expected
325          * value is infinity then the delta value is ignored.
326          */

327         public void assertEquals(
328             String JavaDoc message,
329             float expected,
330             float actual,
331             float delta)
332         {
333             // handle infinity specially since subtracting to infinite values gives NaN and the
334
// the following test fails
335
if (Float.isInfinite(expected))
336             {
337                 if (!(expected == actual))
338                     failNotEquals(
339                         message,
340                         new Float JavaDoc(expected),
341                         new Float JavaDoc(actual));
342             }
343             else if (!(Math.abs(expected - actual) <= delta))
344                 failNotEquals(message, new Float JavaDoc(expected), new Float JavaDoc(actual));
345         }
346
347         /**
348          * Asserts that two ints are equal.
349          */

350         public void assertEquals(String JavaDoc message, int expected, int actual)
351         {
352             assertEquals(message, new Integer JavaDoc(expected), new Integer JavaDoc(actual));
353         }
354
355         /**
356          * Asserts that two longs are equal.
357          */

358         public void assertEquals(String JavaDoc message, long expected, long actual)
359         {
360             assertEquals(message, new Long JavaDoc(expected), new Long JavaDoc(actual));
361         }
362
363         /**
364          * Asserts that two objects are equal. If they are not
365          * an AssertionFailedError is thrown.
366          */

367         public void assertEquals(
368             String JavaDoc message,
369             Object JavaDoc expected,
370             Object JavaDoc actual)
371         {
372             if (expected == null && actual == null)
373                 return;
374             if (expected != null && expected.equals(actual))
375                 return;
376             failNotEquals(message, expected, actual);
377         }
378
379         /**
380          * Asserts that two shorts are equal.
381          */

382         public void assertEquals(String JavaDoc message, short expected, short actual)
383         {
384             assertEquals(message, new Short JavaDoc(expected), new Short JavaDoc(actual));
385         }
386
387         /**
388          * Asserts that two booleans are equal.
389          */

390         public void assertEquals(
391             String JavaDoc message,
392             boolean expected,
393             boolean actual)
394         {
395             assertEquals(message, new Boolean JavaDoc(expected), new Boolean JavaDoc(actual));
396         }
397
398         /**
399          * Asserts that two shorts are equal.
400          */

401         public void assertEquals(short expected, short actual)
402         {
403             assertEquals(null, expected, actual);
404         }
405
406         /**
407          * Asserts that two booleans are equal.
408          */

409         public void assertEquals(boolean expected, boolean actual)
410         {
411             assertEquals(null, expected, actual);
412         }
413
414         /**
415          * Asserts that an object isn't null.
416          */

417         public void assertNotNull(Object JavaDoc object)
418         {
419             assertNotNull(null, object);
420         }
421
422         /**
423          * Asserts that an object isn't null.
424          */

425         public void assertNotNull(String JavaDoc message, Object JavaDoc object)
426         {
427             assertTrue(message, object != null);
428         }
429
430         /**
431          * Asserts that an object is null.
432          */

433         public void assertNull(Object JavaDoc object)
434         {
435             assertNull(null, object);
436         }
437
438         /**
439          * Asserts that an object is null.
440          */

441         public void assertNull(String JavaDoc message, Object JavaDoc object)
442         {
443             assertTrue(message, object == null);
444         }
445
446         /**
447          * Asserts that two objects refer to the same object. If they are not
448          * the same an AssertionFailedError is thrown.
449          */

450         public void assertSame(Object JavaDoc expected, Object JavaDoc actual)
451         {
452             assertSame(null, expected, actual);
453         }
454
455         /**
456          * Asserts that two objects refer to the same object. If they are not
457          * an AssertionFailedError is thrown.
458          */

459         public void assertSame(String JavaDoc message, Object JavaDoc expected, Object JavaDoc actual)
460         {
461             if (expected == actual)
462                 return;
463             failNotSame(message, expected, actual);
464         }
465
466         /**
467          * Asserts that a condition is true. If it isn't it throws
468          * an AssertionFailedError with the given message.
469          */

470         public void assertTrue(String JavaDoc message, boolean condition)
471         {
472             if (!condition)
473                 fail(message);
474         }
475
476         /**
477          * Asserts that a condition is true. If it isn't it throws
478          * an AssertionFailedError.
479          */

480         public void assertTrue(boolean condition)
481         {
482             assertTrue(null, condition);
483         }
484         
485         private void failNotEquals( String JavaDoc message,
486                                     Object JavaDoc expected,
487                                     Object JavaDoc actual )
488         {
489             String JavaDoc formatted = "";
490             if (message != null)
491                 formatted = message + " ";
492             fail( formatted + "expected:<" + expected
493                             + "> but was:<" + actual + ">");
494         }
495
496         private void failNotSame( String JavaDoc message,
497                                   Object JavaDoc expected,
498                                   Object JavaDoc actual )
499         {
500             String JavaDoc formatted = "";
501             if (message != null)
502                 formatted = message + " ";
503             fail(formatted + "expected same");
504         }
505
506     }
507
508 }
509
Popular Tags