2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.callback.push;
25
import junit.framework.TestCase;
26
import org.jboss.logging.Logger;
27
import org.jboss.remoting.Client;
28
import org.jboss.remoting.InvocationRequest;
29
import org.jboss.remoting.InvokerLocator;
30
import org.jboss.remoting.ServerInvocationHandler;
31
import org.jboss.remoting.ServerInvoker;
32
import org.jboss.remoting.callback.Callback;
33
import org.jboss.remoting.callback.InvokerCallbackHandler;
34
import org.jboss.remoting.transport.Connector;
36
import javax.management.MBeanServer;
37
import java.util.ArrayList;
38
import java.util.Collections;
39
import java.util.List;
42
* @author <a href="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
43
* @version <tt>$Revision: 1036 $</tt>
45
* $Id: MultipleCallbackServersTestCase.java 1036 2006-05-21 04:47:32Z telrod $
47
public class MultipleCallbackServersTestCase extends TestCase
49
// Constants -----------------------------------------------------
51
private static final Logger log = Logger.getLogger(MultipleCallbackServersTestCase.class);
53
// Static --------------------------------------------------------
55
// Attributes ----------------------------------------------------
57
// Constructors --------------------------------------------------
59
public MultipleCallbackServersTestCase(String name)
64
// Public --------------------------------------------------------
66
public void setUp() throws Exception
71
public void tearDown() throws Exception
76
public void testIfDisconnectClearsInvokerRegistry() throws Throwable
79
String serverlocatorURI = "socket://localhost:5555";
81
Connector server = new Connector();
82
ServerInvocationHandlerImpl serverHandler = new ServerInvocationHandlerImpl();
83
server.setInvokerLocator(serverlocatorURI);
85
server.addInvocationHandler("TEST", serverHandler);
87
String callbackServer1URI = "socket://localhost:1111";
88
Connector callbackServer1 = new Connector();
89
callbackServer1.setInvokerLocator(callbackServer1URI);
90
callbackServer1.start();
92
String callbackServer2URI = "socket://localhost:2222";
93
Connector callbackServer2 = new Connector();
94
callbackServer2.setInvokerLocator(callbackServer2URI);
95
callbackServer2.start();
97
Client client = new Client(new InvokerLocator(serverlocatorURI), "TEST");
100
// add a listener that uses callbackServer1
101
InvokerCallbackHandlerImpl listener1 = new InvokerCallbackHandlerImpl("ONE");
102
client.addListener(listener1, new InvokerLocator(callbackServer1URI));
104
assertEquals(1, serverHandler.size());
106
// add a listener that uses callbackServer2
107
InvokerCallbackHandlerImpl listener2 = new InvokerCallbackHandlerImpl("TWO");
108
client.addListener(listener2, new InvokerLocator(callbackServer2URI));
110
assertEquals(2, serverHandler.size());
115
* Note, if uncomment the following, the test will fail.
116
* This is because each Client has its own session id which
117
* is used as part of the key for each callback listener regsitered
118
* on the server. Therefore a new Client, means the callback listener
119
* key used for the remove will be different than the original one.
121
// client = new Client(new InvokerLocator(serverlocatorURI), "TEST");
123
client.removeListener(listener1);
125
assertEquals(1, serverHandler.size());
129
client.removeListener(listener1);
130
assertTrue("Expected to get exception for removing same listener twice.", false);
134
assertTrue("Expected to get exception for removing same listener twice.", true);
137
client.removeListener(listener2);
139
assertEquals(0, serverHandler.size());
143
client.removeListener(listener2);
144
assertTrue("Excpected to get exception from removing second listener twice.", false);
148
assertTrue("Expected to get exception from removing second listener twice.", true);
153
// Package protected ---------------------------------------------
155
// Protected -----------------------------------------------------
157
// Private -------------------------------------------------------
159
// Inner classes -------------------------------------------------
161
private class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
165
public InvokerCallbackHandlerImpl(String name)
170
public void handleCallback(Callback c)
175
public String getName()
181
private class ServerInvocationHandlerImpl implements ServerInvocationHandler
183
private List listeners = Collections.synchronizedList(new ArrayList());
185
public void setMBeanServer(MBeanServer server)
189
public void setInvoker(ServerInvoker invoker)
193
public Object invoke(InvocationRequest invocation) throws Throwable
199
public void addListener(InvokerCallbackHandler callbackHandler)
201
listeners.add(callbackHandler);
204
public void removeListener(InvokerCallbackHandler callbackHandler)
206
log.info("Trying to remove listener: " + callbackHandler);
207
boolean removed = listeners.remove(callbackHandler);
210
log.info("Listener removed");
214
log.info("Listener NOT removed");
221
return listeners.size();