KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jacorb.test.orb.connection;
2
3 import java.util.Properties JavaDoc;
4
5 import junit.framework.*;
6
7 import org.jacorb.test.*;
8 import org.jacorb.test.common.*;
9
10 /**
11  * @author Andre Spiegel
12  * @version $Id: BiDirTest.java,v 1.2 2003/08/17 10:53:44 andre.spiegel Exp $
13  */

14 public class BiDirTest extends ClientServerTestCase
15 {
16     private BiDirServer server = null;
17     private org.omg.PortableServer.POA JavaDoc biDirPOA = null;
18     
19     private Object JavaDoc callbackLock = new Object JavaDoc();
20     private boolean callbackReceived = false;
21     private String JavaDoc callbackMessage = null;
22     
23     public BiDirTest (String JavaDoc name, ClientServerSetup setup)
24     {
25         super (name, setup);
26     }
27     
28     public void setUp() throws Exception JavaDoc
29     {
30         server = BiDirServerHelper.narrow (setup.getServerObject());
31         biDirPOA = ((BiDirSetup)setup).getBiDirPOA();
32     }
33
34     private class ClientCallbackImpl extends ClientCallbackPOA
35     {
36         public void hello (String JavaDoc message)
37         {
38             synchronized (callbackLock)
39             {
40                 callbackReceived = true;
41                 callbackMessage = message;
42                 callbackLock.notifyAll();
43             }
44         }
45     }
46     
47     private String JavaDoc waitForCallback (int timeout)
48     {
49         synchronized (callbackLock)
50         {
51             if (callbackReceived)
52                 return callbackMessage;
53             else
54             {
55                 try
56                 {
57                     callbackLock.wait (timeout);
58                 }
59                 catch (InterruptedException JavaDoc ex)
60                 {
61                     // ignore
62
}
63                 if (callbackReceived)
64                     return callbackMessage;
65                 else
66                     throw new org.omg.CORBA.TIMEOUT JavaDoc
67                                  ("no callback received within timeout");
68             }
69         }
70     }
71
72     public static Test suite()
73     {
74         TestSuite suite = new TestSuite ("Bidirectional GIOP Test");
75
76         Properties JavaDoc properties = new Properties JavaDoc();
77         properties.setProperty
78             ("org.omg.PortableInterceptor.ORBInitializerClass.bidir_init",
79              "org.jacorb.orb.giop.BiDirConnectionInitializer" );
80         
81         BiDirSetup setup = new BiDirSetup (suite, properties, properties);
82
83         suite.addTest (new BiDirTest ("test_callback", setup));
84             
85         return setup;
86     }
87     
88      
89     public void test_callback()
90     {
91         ClientCallback c = null;
92         try
93         {
94             c = ClientCallbackHelper.narrow (
95                   biDirPOA.servant_to_reference
96                                           (new ClientCallbackImpl()));
97         }
98         catch (Exception JavaDoc e)
99         {
100             fail ("exception creating callback object: " + e);
101         }
102         
103         server.register_callback (c);
104         server.callback_hello ("This is a test");
105         String JavaDoc result = waitForCallback (1000);
106         int n = server.get_open_client_transports();
107
108         assertEquals ("This is a test", result);
109
110         // if this was bidirectional, then the server must not have
111
// any open client transports now
112
assertEquals ("Server has too many client transports", 0, n);
113     }
114
115 }
116
Popular Tags