KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > orb > connection > GIOPConnectionTest


1 package org.jacorb.test.orb.connection;
2
3 import org.jacorb.orb.giop.*;
4 import org.jacorb.orb.iiop.*;
5
6 import java.io.*;
7 import java.util.*;
8
9 import org.omg.ETF.BufferHolder;
10 import org.omg.ETF.Profile;
11 import org.omg.GIOP.*;
12 import org.jacorb.orb.*;
13
14 import junit.framework.*;
15 import org.jacorb.test.common.*;
16
17 import org.jacorb.config.Configuration;
18
19 /**
20  * GIOPConnectionTest.java
21  *
22  *
23  * Created: Sat Jun 22 14:26:15 2002
24  *
25  * @jacorb-client-since 2.2
26  * @author Nicolas Noffke
27  * @version $Id: GIOPConnectionTest.java,v 1.23 2005/05/13 13:30:53 andre.spiegel Exp $
28  */

29
30 public class GIOPConnectionTest
31     extends JacORBTestCase
32 {
33     private Configuration config;
34     private ORB orb;
35
36     public void setUp()
37         throws Exception JavaDoc
38     {
39         orb = (ORB) ORB.init(new String JavaDoc[0], null);
40         config = Configuration.getConfiguration(null, orb, false);
41     }
42
43     public static Test suite()
44     {
45         TestSuite suite = new JacORBTestSuite ("GIOPConnection Test",
46                                                GIOPConnectionTest.class);
47         suite.addTest (new GIOPConnectionTest ("testGIOP_1_0_CorrectRefusing"));
48         suite.addTest (new GIOPConnectionTest ("testGIOP_1_1_IllegalMessageType"));
49         suite.addTest (new GIOPConnectionTest ("testGIOP_1_1_NoImplement"));
50         suite.addTest (new GIOPConnectionTest ("testGIOP_1_2_CorrectFragmentedRequest"));
51
52         return suite;
53     }
54     
55
56     private class DummyTransport extends org.omg.ETF._ConnectionLocalBase
57     {
58         private boolean closed = false;
59         private byte[] data = null;
60         private int index = 0;
61         private ByteArrayOutputStream b_out = new ByteArrayOutputStream();
62         private org.omg.ETF.Profile profile = new IIOPProfile
63         (
64             new IIOPAddress ("127.0.0.1", 4711),
65             null
66         );
67
68         public DummyTransport( List messages )
69         {
70             // convert the message list into a plain byte array
71

72             int size = 0;
73             for (Iterator i = messages.iterator(); i.hasNext();)
74             {
75                 size += ((byte[])i.next()).length;
76             }
77             data = new byte[size];
78             int index = 0;
79             for (Iterator i = messages.iterator(); i.hasNext();)
80             {
81                 byte[] msg = (byte[])i.next();
82                 System.arraycopy(msg, 0, data, index, msg.length);
83                 index += msg.length;
84             }
85         }
86
87         public byte[] getWrittenMessage()
88         {
89             return b_out.toByteArray();
90         }
91
92         public void connect (org.omg.ETF.Profile profile, long time_out)
93         {
94             // nothing
95
}
96
97         public boolean hasBeenClosed()
98         {
99             return closed;
100         }
101
102         public boolean is_connected()
103         {
104             return !closed;
105         }
106
107         public void write( boolean is_first, boolean is_last,
108                            byte[] message, int start, int size,
109                            long timeout )
110         {
111             b_out.write( message, start, size );
112         }
113
114
115         public void flush()
116         {
117         }
118
119         public void close()
120         {
121             closed = true;
122         }
123
124         public boolean isSSL()
125         {
126             return false;
127         }
128
129         public void turnOnFinalTimeout()
130         {
131         }
132
133         public Profile get_server_profile()
134         {
135             return profile;
136         }
137
138         public void read (BufferHolder data, int offset,
139                           int min_length, int max_length, long time_out)
140         {
141             if (this.index + min_length > this.data.length)
142             {
143                 throw new org.omg.CORBA.COMM_FAILURE JavaDoc ("end of stream");
144             }
145             System.arraycopy(this.data, this.index, data.value, offset, min_length);
146             this.index += min_length;
147         }
148
149         public boolean is_data_available()
150         {
151             return true;
152         }
153
154         public boolean supports_callback()
155         {
156             return false;
157         }
158
159         public boolean use_handle_time_out()
160         {
161             return false;
162         }
163
164         public boolean wait_next_data(long time_out)
165         {
166             return false;
167         }
168
169     }
170
171
172     private class DummyRequestListener
173         implements RequestListener
174     {
175         private byte[] request = null;
176
177         public DummyRequestListener()
178         {
179         }
180
181         public byte[] getRequest()
182         {
183             return request;
184         }
185
186         public void requestReceived( byte[] request,
187                                      GIOPConnection connection )
188         {
189             this.request = request;
190         }
191
192         public void locateRequestReceived( byte[] request,
193                                            GIOPConnection connection )
194         {
195             this.request = request;
196         }
197         public void cancelRequestReceived( byte[] request,
198                                            GIOPConnection connection )
199         {
200             this.request = request;
201         }
202     }
203
204     private class DummyReplyListener
205         implements ReplyListener
206     {
207         private byte[] reply = null;
208
209         public DummyReplyListener()
210         {
211         }
212
213         public byte[] getReply()
214         {
215             return reply;
216         }
217
218         public void replyReceived( byte[] reply,
219                                    GIOPConnection connection )
220         {
221             this.reply = reply;
222         }
223
224         public void locateReplyReceived( byte[] reply,
225                                          GIOPConnection connection )
226         {
227             this.reply = reply;
228         }
229
230         public void closeConnectionReceived( byte[] close_conn,
231                                              GIOPConnection connection )
232         {
233             this.reply = close_conn;
234         }
235
236     }
237
238     public GIOPConnectionTest( String JavaDoc name )
239     {
240         super( name );
241     }
242
243     public void testGIOP_1_2_CorrectFragmentedRequest()
244     {
245         List messages = new Vector();
246
247         RequestOutputStream r_out =
248             new RequestOutputStream( (ClientConnection) null, //ClientConnection
249
0, //request id
250
"foo", //operation
251
true, // response expected
252
(short)-1, // SYNC_SCOPE (irrelevant)
253
null, //request start time
254
null, //request end time
255
null, //reply start time
256
new byte[1], //object key
257
2 // giop minor
258
);
259
260         //manually write the first half of the string "barbaz"
261
r_out.write_ulong( 6 ); //string length
262
r_out.write_octet( (byte) 'b' );
263         r_out.write_octet( (byte) 'a' );
264         r_out.write_octet( (byte) 'r' );
265         r_out.insertMsgSize();
266
267         byte[] b = r_out.getBufferCopy();
268
269         b[6] |= 0x02; //set "more fragments follow"
270

271         messages.add( b );
272
273         MessageOutputStream m_out =
274             new MessageOutputStream();
275         m_out.writeGIOPMsgHeader( MsgType_1_1._Fragment,
276                                      2 // giop minor
277
);
278         m_out.write_ulong( 0 ); // Fragment Header (request id)
279
m_out.write_octet( (byte) 'b' );
280         m_out.write_octet( (byte) 'a' );
281         m_out.write_octet( (byte) 'z' );
282         m_out.insertMsgSize();
283
284         messages.add( m_out.getBufferCopy() );
285
286         DummyTransport transport =
287             new DummyTransport( messages );
288
289         DummyRequestListener request_listener =
290             new DummyRequestListener();
291
292         DummyReplyListener reply_listener =
293             new DummyReplyListener();
294
295         GIOPConnectionManager giopconn_mg =
296             new GIOPConnectionManager();
297         try
298         {
299             giopconn_mg.configure (config);
300         }
301         catch (Exception JavaDoc e)
302         {
303         }
304
305         ServerGIOPConnection conn =
306             giopconn_mg.createServerGIOPConnection( null,
307                                                     transport,
308                                                     request_listener,
309                                                     reply_listener );
310
311         try
312         {
313             //will not return until an IOException is thrown (by the
314
//DummyTransport)
315
conn.receiveMessages();
316         }
317         catch( IOException e )
318         {
319             //o.k., thrown by DummyTransport
320
}
321         catch( Exception JavaDoc e )
322         {
323             e.printStackTrace();
324             fail( "Caught exception: " + e );
325         }
326
327         //did the GIOPConnection hand the complete request over to the
328
//listener?
329
assertTrue( request_listener.getRequest() != null );
330
331         RequestInputStream r_in =
332             new RequestInputStream( null, request_listener.getRequest() );
333
334         //is the body correct?
335
assertEquals( "barbaz", r_in.read_string() );
336     }
337
338     public void testGIOP_1_0_CorrectRefusing()
339     {
340         List messages = new Vector();
341
342         RequestOutputStream r_out =
343             new RequestOutputStream( null, //ClientConnection
344
0, //request id
345
"foo", //operation
346
true, //response expected
347
(short)-1, //SYNC_SCOPE (irrelevant)
348
null, //request start time
349
null, //request end time
350
null, //reply end time
351
new byte[1], //object key
352
0 // giop minor
353
);
354
355         r_out.write_string( "bar" );
356         r_out.insertMsgSize();
357
358         byte[] b = r_out.getBufferCopy();
359
360         b[6] |= 0x02; //set "more fragments follow"
361

362         messages.add( b );
363
364         DummyTransport transport =
365             new DummyTransport( messages );
366
367         DummyRequestListener request_listener =
368             new DummyRequestListener();
369
370         DummyReplyListener reply_listener =
371             new DummyReplyListener();
372
373         GIOPConnectionManager giopconn_mg =
374             new GIOPConnectionManager();
375         try
376         {
377             giopconn_mg.configure (config);
378         }
379         catch (Exception JavaDoc e)
380         {
381         }
382
383         GIOPConnection conn =
384             giopconn_mg.createServerGIOPConnection( null,
385                                                     transport,
386                                                     request_listener,
387                                                     reply_listener );
388
389         try
390         {
391             //will not return until an IOException is thrown (by the
392
//DummyTransport)
393
conn.receiveMessages();
394         }
395         catch( IOException e )
396         {
397             //o.k., thrown by DummyTransport
398
}
399         catch( Exception JavaDoc e )
400         {
401             e.printStackTrace();
402             fail( "Caught exception: " + e );
403         }
404
405         //no request or reply must have been handed over
406
assertTrue( request_listener.getRequest() == null );
407         assertTrue( reply_listener.getReply() == null );
408
409         //instead, an error message have must been sent via the
410
//transport
411
assertTrue( transport.getWrittenMessage() != null );
412
413         byte[] result = transport.getWrittenMessage();
414
415         assertTrue( Messages.getMsgType( result ) == MsgType_1_1._MessageError );
416         MessageOutputStream m_out =
417             new MessageOutputStream();
418         m_out.writeGIOPMsgHeader( MsgType_1_1._Fragment,
419                                      0 // giop minor
420
);
421         m_out.write_ulong( 0 ); // Fragment Header (request id)
422
m_out.write_octet( (byte) 'b' );
423         m_out.write_octet( (byte) 'a' );
424         m_out.write_octet( (byte) 'z' );
425         m_out.insertMsgSize();
426
427         messages.add( m_out.getBufferCopy() );
428
429         try
430         {
431             //will not return until an IOException is thrown (by the
432
//DummyTransport)
433
conn.receiveMessages();
434         }
435         catch( IOException e )
436         {
437             //o.k., thrown by DummyTransport
438
}
439         catch( Exception JavaDoc e )
440         {
441             e.printStackTrace();
442             fail( "Caught exception: " + e );
443         }
444
445         //no request or reply must have been handed over
446
assertTrue( request_listener.getRequest() == null );
447         assertTrue( reply_listener.getReply() == null );
448
449         //instead, an error message have must been sent via the
450
//transport
451
assertTrue( transport.getWrittenMessage() != null );
452
453         //must be a new one
454
assertTrue( transport.getWrittenMessage() != result );
455         result = transport.getWrittenMessage();
456
457         assertTrue( Messages.getMsgType( result ) == MsgType_1_1._MessageError );
458
459     }
460
461     public void testGIOP_1_1_IllegalMessageType()
462     {
463         List messages = new Vector();
464
465         LocateRequestOutputStream r_out =
466             new LocateRequestOutputStream(
467                 new byte[1], //object key
468
0, //request id
469
1 // giop minor
470
);
471
472         r_out.insertMsgSize();
473
474         byte[] b = r_out.getBufferCopy();
475
476         b[6] |= 0x02; //set "more fragments follow"
477

478         messages.add( b );
479
480 // MessageOutputStream m_out =
481
// new MessageOutputStream();
482

483         DummyTransport transport =
484             new DummyTransport( messages );
485
486         DummyRequestListener request_listener =
487             new DummyRequestListener();
488
489         DummyReplyListener reply_listener =
490             new DummyReplyListener();
491
492         GIOPConnectionManager giopconn_mg =
493             new GIOPConnectionManager();
494         try
495         {
496             giopconn_mg.configure (config);
497         }
498         catch (Exception JavaDoc e)
499         {
500         }
501
502         GIOPConnection conn =
503             giopconn_mg.createServerGIOPConnection( null,
504                                                     transport,
505                                                     request_listener,
506                                                     reply_listener );
507
508         try
509         {
510             //will not return until an IOException is thrown (by the
511
//DummyTransport)
512
conn.receiveMessages();
513         }
514         catch( IOException e )
515         {
516             //o.k., thrown by DummyTransport
517
}
518         catch( Exception JavaDoc e )
519         {
520             e.printStackTrace();
521             fail( "Caught exception: " + e );
522         }
523
524         //no request or reply must have been handed over
525
assertTrue( request_listener.getRequest() == null );
526         assertTrue( reply_listener.getReply() == null );
527
528         //instead, an error message have must been sent via the
529
//transport
530
assertTrue( transport.getWrittenMessage() != null );
531
532         byte[] result = transport.getWrittenMessage();
533
534         assertTrue( Messages.getMsgType( result ) == MsgType_1_1._MessageError );
535     }
536
537     public void testGIOP_1_1_NoImplement()
538     {
539         List messages = new Vector();
540
541         RequestOutputStream r_out =
542             new RequestOutputStream( null, //ClientConnection
543
0, //request id
544
"foo", //operation
545
true, //response expected
546
(short)-1, //SYNC_SCOPE (irrelevant)
547
null, //request start time
548
null, //request end time
549
null, //reply end time
550
new byte[1], //object key
551
1 // giop minor
552
);
553
554         r_out.write_string( "bar" );
555         r_out.insertMsgSize();
556
557         byte[] b = r_out.getBufferCopy();
558
559         b[6] |= 0x02; //set "more fragments follow"
560

561         messages.add( b );
562
563         DummyTransport transport =
564             new DummyTransport( messages );
565
566         DummyRequestListener request_listener =
567             new DummyRequestListener();
568
569         DummyReplyListener reply_listener =
570             new DummyReplyListener();
571
572         GIOPConnectionManager giopconn_mg =
573             new GIOPConnectionManager();
574         try
575         {
576             giopconn_mg.configure (config);
577         }
578         catch (Exception JavaDoc e)
579         {
580         }
581
582         GIOPConnection conn =
583             giopconn_mg.createServerGIOPConnection( null,
584                                                     transport,
585                                                     request_listener,
586                                                     reply_listener );
587
588         try
589         {
590             //will not return until an IOException is thrown (by the
591
//DummyTransport)
592
conn.receiveMessages();
593         }
594         catch( IOException e )
595         {
596             //o.k., thrown by DummyTransport
597
}
598         catch( Exception JavaDoc e )
599         {
600             e.printStackTrace();
601             fail( "Caught exception: " + e );
602         }
603
604         //no request or reply must have been handed over
605
assertTrue( request_listener.getRequest() == null );
606         assertTrue( reply_listener.getReply() == null );
607
608         //instead, an error message have must been sent via the
609
//transport
610
assertTrue( transport.getWrittenMessage() != null );
611
612         byte[] result = transport.getWrittenMessage();
613
614         ReplyInputStream r_in = new ReplyInputStream( null, result );
615
616         Exception JavaDoc ex = r_in.getException();
617         if ( ex != null && ex.getClass() == org.omg.CORBA.NO_IMPLEMENT JavaDoc.class )
618         {
619             // o.k.
620
}
621         else
622         {
623             fail();
624         }
625
626         MessageOutputStream m_out =
627             new MessageOutputStream();
628         m_out.writeGIOPMsgHeader( MsgType_1_1._Fragment,
629                                   1 // giop minor
630
);
631         m_out.write_ulong( 0 ); // Fragment Header (request id)
632
m_out.write_octet( (byte) 'b' );
633         m_out.write_octet( (byte) 'a' );
634         m_out.write_octet( (byte) 'z' );
635         m_out.insertMsgSize();
636
637         messages.add( m_out.getBufferCopy() );
638
639         try
640         {
641             //will not return until an IOException is thrown (by the
642
//DummyTransport)
643
conn.receiveMessages();
644         }
645         catch( IOException e )
646         {
647             //o.k., thrown by DummyTransport
648
}
649         catch( Exception JavaDoc e )
650         {
651             e.printStackTrace();
652             fail( "Caught exception: " + e );
653         }
654
655         //no request or reply must have been handed over
656
assertTrue( request_listener.getRequest() == null );
657         assertTrue( reply_listener.getReply() == null );
658
659         //can't check more, message is discarded
660
}
661 }// GIOPConnectionTest
662
Popular Tags