KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > callback > push > MultipleCallbackServersTestCase


1 /**
2  * JBoss, the OpenSource J2EE WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.callback.push;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.List JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
13 import org.jboss.logging.Logger;
14 import org.jboss.remoting.Client;
15 import org.jboss.remoting.InvocationRequest;
16 import org.jboss.remoting.InvokerLocator;
17 import org.jboss.remoting.ServerInvocationHandler;
18 import org.jboss.remoting.ServerInvoker;
19 import org.jboss.remoting.callback.Callback;
20 import org.jboss.remoting.callback.InvokerCallbackHandler;
21 import org.jboss.remoting.transport.Connector;
22
23 import junit.framework.TestCase;
24
25 /**
26  * @author <a HREF="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
27  *
28  * @version <tt>$Revision: 1.4 $</tt>
29  *
30  * $Id: MultipleCallbackServersTestCase.java,v 1.4 2005/06/20 03:38:42 telrod Exp $
31  */

32 public class MultipleCallbackServersTestCase extends TestCase
33 {
34    // Constants -----------------------------------------------------
35

36    private static final Logger log = Logger.getLogger(MultipleCallbackServersTestCase.class);
37
38
39    // Static --------------------------------------------------------
40

41    // Attributes ----------------------------------------------------
42

43    // Constructors --------------------------------------------------
44

45    public MultipleCallbackServersTestCase(String JavaDoc name)
46    {
47       super(name);
48    }
49
50    // Public --------------------------------------------------------
51

52    public void setUp() throws Exception JavaDoc
53    {
54       super.setUp();
55    }
56
57    public void tearDown() throws Exception JavaDoc
58    {
59       super.tearDown();
60    }
61
62    public void testIfDisconnectClearsInvokerRegistry() throws Throwable JavaDoc
63    {
64
65       String JavaDoc serverlocatorURI = "socket://localhost:5555";
66
67       Connector server = new Connector();
68       ServerInvocationHandlerImpl serverHandler = new ServerInvocationHandlerImpl();
69       server.setInvokerLocator(serverlocatorURI);
70       server.start();
71       server.addInvocationHandler("TEST", serverHandler);
72
73       String JavaDoc callbackServer1URI = "socket://localhost:1111";
74       Connector callbackServer1 = new Connector();
75       callbackServer1.setInvokerLocator(callbackServer1URI);
76       callbackServer1.start();
77
78       String JavaDoc callbackServer2URI = "socket://localhost:2222";
79       Connector callbackServer2 = new Connector();
80       callbackServer2.setInvokerLocator(callbackServer2URI);
81       callbackServer2.start();
82
83       Client client = new Client(new InvokerLocator(serverlocatorURI), "TEST");
84
85       // add a listener that uses callbackServer1
86
InvokerCallbackHandlerImpl listener1 = new InvokerCallbackHandlerImpl("ONE");
87       client.addListener(listener1, new InvokerLocator(callbackServer1URI));
88
89       assertEquals(1, serverHandler.size());
90
91       // add a listener that uses callbackServer2
92
InvokerCallbackHandlerImpl listener2 = new InvokerCallbackHandlerImpl("TWO");
93       client.addListener(listener2, new InvokerLocator(callbackServer2URI));
94
95       assertEquals(2, serverHandler.size());
96
97       // remove them
98

99       /**
100        * Note, if uncomment the following, the test will fail.
101        * This is because each Client has its own session id which
102        * is used as part of the key for each callback listener regsitered
103        * on the server. Therefore a new Client, means the callback listener
104        * key used for the remove will be different than the original one.
105        */

106       // client = new Client(new InvokerLocator(serverlocatorURI), "TEST");
107

108       client.removeListener(listener1);
109
110       assertEquals(1, serverHandler.size());
111
112       try
113       {
114          client.removeListener(listener1);
115          assertTrue("Expected to get exception for removing same listener twice.", false);
116       }
117       catch(Throwable JavaDoc thr)
118       {
119          assertTrue("Expected to get exception for removing same listener twice.", true);
120       }
121
122       client.removeListener(listener2);
123
124       assertEquals(0, serverHandler.size());
125
126       try
127       {
128          client.removeListener(listener2);
129          assertTrue("Excpected to get exception from removing second listener twice.", false);
130       }
131       catch(Throwable JavaDoc thr)
132       {
133          assertTrue("Expected to get exception from removing second listener twice.", true);
134       }
135
136    }
137
138    // Package protected ---------------------------------------------
139

140    // Protected -----------------------------------------------------
141

142    // Private -------------------------------------------------------
143

144    // Inner classes -------------------------------------------------
145

146    private class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
147    {
148       private String JavaDoc name;
149
150       public InvokerCallbackHandlerImpl(String JavaDoc name)
151       {
152          this.name = name;
153       }
154       public void handleCallback(Callback c)
155       {
156
157       }
158
159       public String JavaDoc getName()
160       {
161          return name;
162       }
163    }
164
165    private class ServerInvocationHandlerImpl implements ServerInvocationHandler
166    {
167       private List JavaDoc listeners = Collections.synchronizedList(new ArrayList JavaDoc());
168
169       public void setMBeanServer(MBeanServer JavaDoc server)
170       {
171       }
172
173       public void setInvoker(ServerInvoker invoker)
174       {
175       }
176
177       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
178       {
179          return null;
180       }
181
182
183       public void addListener(InvokerCallbackHandler callbackHandler)
184       {
185          listeners.add(callbackHandler);
186       }
187
188       public void removeListener(InvokerCallbackHandler callbackHandler)
189       {
190          log.info("Trying to remove listener: " + callbackHandler);
191          boolean removed = listeners.remove(callbackHandler);
192          if (removed)
193          {
194             log.info("Listener removed");
195          }
196          else
197          {
198             log.info("Listener NOT removed");
199          }
200
201       }
202
203       public int size()
204       {
205          return listeners.size();
206       }
207    }
208
209 }
210
Popular Tags