1 7 package org.jboss.remoting.transport.rmi; 8 9 import org.apache.log4j.Level; 10 import org.jboss.dtf.MultipleTestRunner; 11 import org.jboss.logging.Logger; 12 import org.jboss.remoting.AbstractInvokerTest; 13 import org.jboss.remoting.Client; 14 import org.jboss.remoting.ComplexReturn; 15 import org.jboss.remoting.InvokerLocator; 16 import org.jboss.remoting.InvokerRegistry; 17 import org.jboss.remoting.invocation.NameBasedInvocation; 18 import org.jboss.remoting.transport.Connector; 19 import org.jboss.remoting.transport.mock.MockClientInvoker; 20 import org.jboss.remoting.transport.mock.MockInvokerCallbackHandler; 21 import org.jboss.remoting.transport.mock.MockServerInvoker; 22 import org.jboss.remoting.transport.mock.MockTest; 23 import org.w3c.dom.Document ; 24 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import java.io.ByteArrayInputStream ; 27 import java.rmi.server.UID ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Random ; 32 import java.util.Set ; 33 34 39 public class RMIInvokerClientTest extends AbstractInvokerTest 40 { 41 private static final Logger log = Logger.getLogger(RMIInvokerClientTest.class); 42 43 private String sessionId = new UID ().toString(); 44 45 private Client client; 46 47 private Map rmiMetaData; 48 49 private static final String NAME = "InvokerClientTest.class"; 50 51 public RMIInvokerClientTest(String name) 52 { 53 super(NAME); 54 } 55 56 public RMIInvokerClientTest(int numberOfInstances) 57 { 58 super(NAME, numberOfInstances); 59 } 60 61 public RMIInvokerClientTest(String transport, int port) 62 { 63 super(NAME, transport, port); 64 } 65 66 public RMIInvokerClientTest(String transport, int port, int numberOfInstances) 67 { 68 super(NAME, transport, port, numberOfInstances); 69 } 70 71 public void init(Map metadata) 72 { 73 try 74 { 75 InvokerLocator locator = new InvokerLocator(buildLocatorURI(metadata, this.port)); 76 client = new Client(locator, "mock"); 78 client.connect(); 79 } 80 catch(Exception e) 81 { 82 log.error(e.getMessage(), e); 83 } 84 } 85 86 private String buildLocatorURI(Map metadata, int port) 87 { 88 if(metadata == null || metadata.size() == 0) 89 { 90 return getTransport() + "://localhost:" + port; 91 } 92 else 93 { 94 StringBuffer uriBuffer = new StringBuffer (getTransport() + "://localhost:" + port + "/?"); 95 96 Set keys = metadata.keySet(); 97 Iterator itr = keys.iterator(); 98 while(itr.hasNext()) 99 { 100 String key = (String ) itr.next(); 101 String value = (String ) metadata.get(key); 102 uriBuffer.append(key + "=" + value + ","); 103 } 104 return uriBuffer.substring(0, uriBuffer.length() - 1); 105 } 106 } 107 108 private InvokerLocator initServer(Map metadata, int serverPort) throws Exception 110 { 111 if(serverPort < 0) 112 { 113 serverPort = Math.abs(new Random ().nextInt(2000)); 114 } 115 log.debug("port = " + serverPort); 116 117 InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class); 118 Connector connector = new Connector(); 119 120 InvokerLocator locator = new InvokerLocator(buildLocatorURI(metadata, serverPort)); 121 122 StringBuffer buf = new StringBuffer (); 124 buf.append("<?xml version=\"1.0\"?>\n"); 125 buf.append("<handlers>\n"); 126 buf.append(" <handler subsystem=\"mock\">org.jboss.remoting.transport.mock.MockServerInvocationHandler</handler>\n"); 127 buf.append("</handlers>\n"); 128 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 129 connector.setInvokerLocator(locator.getLocatorURI()); 130 connector.setConfiguration(xml.getDocumentElement()); 131 connector.start(); 133 return locator; 134 } 135 136 137 public void runInvokers() throws Throwable 138 { 139 startup(getNumberOfInstances()); 140 try 141 { 142 testPullCallback(); 143 testLocalPushCallback(); 144 } 145 finally 146 { 147 shutdown(); 148 } 149 } 150 151 157 public void testLocalPushCallback() throws Throwable 158 { 159 try 160 { 161 log.debug("running testLocalPushCallback()"); 162 163 sessionId = new UID ().toString(); 164 InvokerLocator locator = initServer(rmiMetaData, -1); 165 init(rmiMetaData); 166 167 sessionId = client.getSessionId(); 168 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 169 170 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 171 172 Object ret = makeInvocation("foo", "bar"); 174 assertTrue("Result of testLocalPushCallback() invocation of foo.", "bar".equals(ret)); 175 client.addListener(handler, locator); 176 ret = makeInvocation("test", "test"); 178 Thread.sleep(3000); 180 log.debug("done sleeping."); 181 int callbacksPerformed = handler.isCallbackReceived(); 182 log.debug("callbacksPerformed after adding listener is " + callbacksPerformed); 183 assertTrue("Result of testLocalPushCallback() failed since did not get callback.", 184 (callbacksPerformed == 1)); 185 client.removeListener(handler); 187 ret = makeInvocation("test", "test"); 189 Thread.sleep(2000); 191 log.debug("done sleeping."); 192 callbacksPerformed = handler.isCallbackReceived(); 193 log.debug("callbackPerformed after removing listener is " + callbacksPerformed); 194 assertTrue("Result of testLocalPushCallback() failed since did get callback " + 195 "but have been removed as listener.", 196 (callbacksPerformed == 1)); 197 } 198 finally 199 { 200 if(client != null) 201 { 202 client.disconnect(); 203 } 204 } 205 } 206 207 213 public void testRemotePushCallback() throws Throwable 214 { 215 try 216 { 217 log.debug("running testRemotePushCallback()"); 218 219 sessionId = new UID ().toString(); 220 initServer(rmiMetaData, -1); 221 init(rmiMetaData); 222 InvokerLocator locator = client.getInvoker().getLocator(); 223 sessionId = client.getSessionId(); 224 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 225 226 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 227 228 Object ret = makeInvocation("foo", "bar"); 230 assertTrue("Result of testRemotePushCallback() invocation of foo.", "bar".equals(ret)); 231 client.addListener(handler, locator); 232 ret = makeInvocation("test", "test"); 234 Thread.sleep(3000); 236 log.debug("done sleeping."); 237 245 client.removeListener(handler); 247 ret = makeInvocation("test", "test"); 249 Thread.sleep(2000); 251 log.debug("done sleeping."); 252 259 } 260 finally 261 { 262 if(client != null) 263 { 264 client.disconnect(); 265 } 266 } 267 } 268 269 275 public void testPullCallback() throws Throwable 276 { 277 try 278 { 279 log.debug("running testPullCallback()"); 280 281 init(rmiMetaData); 282 285 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 286 287 Object ret = makeInvocation("bar", "foo"); 289 assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret)); 290 client.addListener(handler); 291 ret = makeInvocation("test", "test"); 293 Thread.sleep(2000); 295 ret = client.getCallbacks(); 296 log.debug("getCallbacks returned " + ret); 297 log.debug("should have something."); 298 assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.", 299 ret != null); 300 client.removeListener(handler); 303 ret = makeInvocation("getCallbacks", null); 304 log.debug("getCallbacks returned " + ret); 305 log.debug("should have been empty."); 306 assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.", 307 ret == null); 308 } 309 finally 310 { 311 if(client != null) 312 { 313 client.disconnect(); 314 client = null; 315 } 316 } 317 } 318 319 324 public void testArrayReturn() throws Throwable 325 { 326 try 327 { 328 init(rmiMetaData); 329 330 Object ret = makeInvocation("testComplexReturn", null); 332 ComplexReturn complexRet = (ComplexReturn) ret; 333 MockTest[] mockTests = complexRet.getMockTests(); 334 assertTrue("ComplexReturn's array should contain 2 items", 335 2 == mockTests.length); 336 for(int x = 0; x < mockTests.length; x++) 337 { 338 System.err.println(mockTests[x]); 339 MockTest test = mockTests[x]; 340 assertNotNull("MockTest should not be null", test); 341 } 342 343 } 346 finally 347 { 348 if(client != null) 349 { 350 client.disconnect(); 351 client = null; 352 } 353 } 354 } 355 356 private Object makeInvocation(String method, String param) throws Throwable 357 { 358 Object ret = client.invoke(new NameBasedInvocation(method, 359 new Object []{param}, 360 new String []{String .class.getName()}), 361 null); 362 363 return ret; 364 } 365 366 public static void main(String [] args) 367 { 368 369 org.apache.log4j.BasicConfigurator.configure(); 370 org.apache.log4j.Category.getRoot().setLevel(Level.INFO); 371 org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO); 372 org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG); 373 org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG); 374 375 Map metadata = new HashMap (); 376 int port = RMIServerInvoker.DEFAULT_REGISTRY_PORT - 1; 377 String transport = "rmi"; 378 379 RMIInvokerClientTest client = null; 380 if(args.length == 1) 381 { 382 port = Integer.parseInt(args[0]); 383 client = new RMIInvokerClientTest(transport, port); 384 } 385 else if(args.length == 2) 386 { 387 int instances = Integer.parseInt(args[0]); 388 port = Integer.parseInt(args[1]); 389 client = new RMIInvokerClientTest(transport, port, instances); 390 } 391 else 392 { 393 client = new RMIInvokerClientTest(transport, port); 394 System.out.println("Using default transport (" + client.getTransport() + 395 ") and default port (" + client.getPort() + ") and " + 396 "default number of instances (" + client.getNumberOfInstances() + ")" + 397 "\nCan enter port and instances via command line."); 398 } 399 400 metadata.put(RMIServerInvoker.REGISTRY_PORT_KEY, String.valueOf(port + 1)); 401 client.rmiMetaData = metadata; 402 403 try 404 { 405 406 MultipleTestRunner runner = new MultipleTestRunner(); 409 runner.doRun(client, true); 410 } 411 catch(Throwable e) 412 { 413 e.printStackTrace(); 414 System.exit(1); 415 } 416 System.exit(0); 417 } 418 } 419 | Popular Tags |