1 7 package org.jboss.test.remoting.callback.push; 8 9 import java.util.ArrayList ; 10 import java.util.Collections ; 11 import java.util.List ; 12 import javax.management.MBeanServer ; 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 32 public class MultipleCallbackServersTestCase extends TestCase 33 { 34 36 private static final Logger log = Logger.getLogger(MultipleCallbackServersTestCase.class); 37 38 39 41 43 45 public MultipleCallbackServersTestCase(String name) 46 { 47 super(name); 48 } 49 50 52 public void setUp() throws Exception 53 { 54 super.setUp(); 55 } 56 57 public void tearDown() throws Exception 58 { 59 super.tearDown(); 60 } 61 62 public void testIfDisconnectClearsInvokerRegistry() throws Throwable 63 { 64 65 String 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 callbackServer1URI = "socket://localhost:1111"; 74 Connector callbackServer1 = new Connector(); 75 callbackServer1.setInvokerLocator(callbackServer1URI); 76 callbackServer1.start(); 77 78 String 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 InvokerCallbackHandlerImpl listener1 = new InvokerCallbackHandlerImpl("ONE"); 87 client.addListener(listener1, new InvokerLocator(callbackServer1URI)); 88 89 assertEquals(1, serverHandler.size()); 90 91 InvokerCallbackHandlerImpl listener2 = new InvokerCallbackHandlerImpl("TWO"); 93 client.addListener(listener2, new InvokerLocator(callbackServer2URI)); 94 95 assertEquals(2, serverHandler.size()); 96 97 99 106 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 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 thr) 132 { 133 assertTrue("Expected to get exception from removing second listener twice.", true); 134 } 135 136 } 137 138 140 142 144 146 private class InvokerCallbackHandlerImpl implements InvokerCallbackHandler 147 { 148 private String name; 149 150 public InvokerCallbackHandlerImpl(String name) 151 { 152 this.name = name; 153 } 154 public void handleCallback(Callback c) 155 { 156 157 } 158 159 public String getName() 160 { 161 return name; 162 } 163 } 164 165 private class ServerInvocationHandlerImpl implements ServerInvocationHandler 166 { 167 private List listeners = Collections.synchronizedList(new ArrayList ()); 168 169 public void setMBeanServer(MBeanServer server) 170 { 171 } 172 173 public void setInvoker(ServerInvoker invoker) 174 { 175 } 176 177 public Object invoke(InvocationRequest invocation) throws Throwable 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 |