1 7 package org.jboss.remoting.transport.socket; 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.Random ; 29 30 31 36 public class InvokerClientTest extends AbstractInvokerTest 37 { 38 private static final Logger log = Logger.getLogger(InvokerClientTest.class); 39 40 private String sessionId = new UID ().toString(); 41 42 private Client client; 43 44 private static final String NAME = "InvokerClientTest.class"; 45 46 public InvokerClientTest(String name) 47 { 48 super(NAME); 49 } 50 51 public InvokerClientTest(int numberOfInstances) 52 { 53 super(NAME, numberOfInstances); 54 } 55 56 public InvokerClientTest(String transport, int port) 57 { 58 super(NAME, transport, port); 59 } 60 61 public InvokerClientTest(String transport, int port, int numberOfInstances) 62 { 63 super(NAME, transport, port, numberOfInstances); 64 } 65 66 public void init() 67 { 68 try 69 { 70 InvokerLocator locator = new InvokerLocator(getTransport() + "://localhost:" + port); 71 client = new Client(locator, "mock"); 72 client.connect(); 73 } 74 catch(Exception e) 75 { 76 log.error(e.getMessage(), e); 77 } 78 } 79 80 private InvokerLocator initServer(int port) throws Exception 82 { 83 if(port < 0) 84 { 85 port = Math.abs(new Random ().nextInt(2000) + 2000); 86 } 87 log.debug("port = " + port); 88 89 InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class); 90 Connector connector = new Connector(); 91 InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port); 92 StringBuffer buf = new StringBuffer (); 93 buf.append("<?xml version=\"1.0\"?>\n"); 94 buf.append("<handlers>\n"); 95 buf.append(" <handler subsystem=\"mock\">org.jboss.remoting.transport.mock.MockServerInvocationHandler</handler>\n"); 96 buf.append("</handlers>\n"); 97 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 98 connector.setInvokerLocator(locator.getLocatorURI()); 99 connector.setConfiguration(xml.getDocumentElement()); 100 connector.start(); 102 return locator; 103 } 104 105 106 public void runInvokers() throws Throwable 107 { 108 startup(getNumberOfInstances()); 109 try 110 { 111 testPullCallback(); 112 testLocalPushCallback(); 113 } 114 finally 115 { 116 shutdown(); 117 } 118 } 119 120 126 public void testLocalPushCallback() throws Throwable 127 { 128 try 129 { 130 log.debug("running testLocalPushCallback()"); 131 132 sessionId = new UID ().toString(); 133 InvokerLocator locator = initServer(-1); 134 init(); 135 136 sessionId = client.getSessionId(); 137 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 138 139 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 140 141 Object ret = makeInvocation("foo", "bar"); 143 assertTrue("Result of testLocalPushCallback() invocation of foo.", "bar".equals(ret)); 144 if("bar".equals(ret)) 145 { 146 log.debug("PASS"); 147 } 148 else 149 { 150 log.debug("FAILED"); 151 } 152 153 client.addListener(handler, locator); 154 ret = makeInvocation("test", "test"); 156 Thread.sleep(3000); 158 log.debug("done sleeping."); 159 int callbacksPerformed = handler.isCallbackReceived(); 160 log.debug("callbacksPerformed after adding listener is " + callbacksPerformed); 161 assertTrue("Result of testLocalPushCallback() failed since did not get callback.", 162 (callbacksPerformed == 1)); 163 if(callbacksPerformed == 1) 164 { 165 log.debug("PASS"); 166 } 167 else 168 { 169 log.debug("FAILED"); 170 } 171 client.removeListener(handler); 173 ret = makeInvocation("test", "test"); 175 Thread.sleep(2000); 177 log.debug("done sleeping."); 178 callbacksPerformed = handler.isCallbackReceived(); 179 log.debug("callbackPerformed after removing listener is " + callbacksPerformed); 180 assertTrue("Result of testLocalPushCallback() failed since did get callback " + 181 "but have been removed as listener.", 182 (callbacksPerformed == 1)); 183 if(callbacksPerformed == 1) 184 { 185 log.debug("PASS"); 186 } 187 else 188 { 189 log.debug("FAILED"); 190 } 191 192 } 193 finally 194 { 195 if(client != null) 196 { 197 client.disconnect(); 198 } 199 } 200 } 201 202 208 public void testRemotePushCallback() throws Throwable 209 { 210 try 211 { 212 log.debug("running testRemotePushCallback()"); 213 214 sessionId = new UID ().toString(); 215 initServer(-1); 216 init(); 217 InvokerLocator locator = client.getInvoker().getLocator(); 218 sessionId = client.getSessionId(); 219 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 220 221 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 222 223 Object ret = makeInvocation("foo", "bar"); 225 assertTrue("Result of testRemotePushCallback() invocation of foo.", "bar".equals(ret)); 226 if("bar".equals(ret)) 227 { 228 log.debug("PASS"); 229 } 230 else 231 { 232 log.debug("FAILED"); 233 } 234 235 client.addListener(handler, locator); 236 ret = makeInvocation("test", "test"); 238 Thread.sleep(3000); 240 log.debug("done sleeping."); 241 249 client.removeListener(handler); 251 ret = makeInvocation("test", "test"); 253 Thread.sleep(2000); 255 log.debug("done sleeping."); 256 263 } 264 finally 265 { 266 if(client != null) 267 { 268 client.disconnect(); 269 } 270 } 271 } 272 273 279 public void testPullCallback() throws Throwable 280 { 281 try 282 { 283 log.debug("running testPullCallback()"); 284 285 init(); 286 289 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 290 291 Object ret = makeInvocation("bar", "foo"); 293 assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret)); 294 if("foo".equals(ret)) 295 { 296 log.debug("PASS"); 297 } 298 else 299 { 300 log.debug("FAILED"); 301 } 302 303 client.addListener(handler); 304 ret = makeInvocation("test", "test"); 306 Thread.sleep(2000); 308 ret = client.getCallbacks(); 309 log.debug("getCallbacks returned " + ret); 310 log.debug("should have something."); 311 assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.", 312 ret != null); 313 if(ret != null) 314 { 315 log.debug("PASS"); 316 } 317 else 318 { 319 log.debug("FAILED"); 320 } 321 322 client.removeListener(handler); 325 ret = makeInvocation("getCallbacks", null); 326 log.debug("getCallbacks returned " + ret); 327 log.debug("should have been empty."); 328 assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.", 329 ret == null); 330 if(ret == null) 331 { 332 log.debug("PASS"); 333 } 334 else 335 { 336 log.debug("FAILED"); 337 } 338 339 } 340 finally 341 { 342 if(client != null) 343 { 344 client.disconnect(); 345 client = null; 346 } 347 } 348 } 349 350 355 public void testArrayReturn() throws Throwable 356 { 357 try 358 { 359 init(); 360 361 Object ret = makeInvocation("testComplexReturn", null); 363 ComplexReturn complexRet = (ComplexReturn) ret; 364 MockTest[] mockTests = complexRet.getMockTests(); 365 assertTrue("ComplexReturn's array should contain 2 items", 366 2 == mockTests.length); 367 if(1 == mockTests.length) 368 { 369 log.debug("PASS"); 370 } 371 else 372 { 373 log.debug("FAILED"); 374 } 375 376 for(int x = 0; x < mockTests.length; x++) 377 { 378 System.err.println(mockTests[x]); 379 MockTest test = mockTests[x]; 380 assertNotNull("MockTest should not be null", test); 381 if(test != null) 382 { 383 log.debug("PASS"); 384 } 385 else 386 { 387 log.debug("FAILED"); 388 } 389 390 } 391 392 } 395 finally 396 { 397 if(client != null) 398 { 399 client.disconnect(); 400 client = null; 401 } 402 } 403 } 404 405 private Object makeInvocation(String method, String param) throws Throwable 406 { 407 Object ret = client.invoke(new NameBasedInvocation(method, 408 new Object []{param}, 409 new String []{String .class.getName()}), 410 null); 411 412 return ret; 413 } 414 415 public static void main(String [] args) 416 { 417 418 org.apache.log4j.BasicConfigurator.configure(); 419 org.apache.log4j.Category.getRoot().setLevel(Level.INFO); 420 org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO); 421 org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG); 422 org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG); 423 424 InvokerClientTest client = null; 425 if(args.length == 1) 426 { 427 int instances = Integer.parseInt(args[0]); 428 client = new InvokerClientTest(instances); 429 } 430 else if(args.length == 2) 431 { 432 String transport = args[0]; 433 int port = Integer.parseInt(args[1]); 434 client = new InvokerClientTest(transport, port); 435 } 436 else if(args.length == 3) 437 { 438 String transport = args[0]; 439 int port = Integer.parseInt(args[1]); 440 int instances = Integer.parseInt(args[2]); 441 client = new InvokerClientTest(transport, port, instances); 442 } 443 else 444 { 445 client = new InvokerClientTest(InvokerClientTest.class.getName()); 446 System.out.println("Using default transport (" + client.getTransport() + 447 ") and default port (" + client.getPort() + ") and " + 448 "default number of instances (" + client.getNumberOfInstances() + ")" + 449 "\nCan enter transport, port, and instances via command line."); 450 } 451 452 try 453 { 454 MultipleTestRunner runner = new MultipleTestRunner(); 457 runner.doRun(client, true); 458 } 459 catch(Throwable e) 460 { 461 e.printStackTrace(); 462 System.exit(1); 463 } 464 System.exit(0); 465 } 466 } 467 | Popular Tags |