1 7 package org.jboss.test.remoting.transport.socket; 8 9 import java.io.ByteArrayInputStream ; 10 import java.rmi.server.UID ; 11 import javax.xml.parsers.DocumentBuilderFactory ; 12 import org.jboss.logging.Logger; 13 import org.jboss.remoting.Client; 14 import org.jboss.remoting.InvokerLocator; 15 import org.jboss.remoting.InvokerRegistry; 16 import org.jboss.remoting.invocation.NameBasedInvocation; 17 import org.jboss.remoting.transport.Connector; 18 import org.jboss.test.remoting.ComplexReturn; 19 import org.jboss.test.remoting.TestUtil; 20 import org.jboss.test.remoting.transport.mock.MockClientInvoker; 21 import org.jboss.test.remoting.transport.mock.MockInvokerCallbackHandler; 22 import org.jboss.test.remoting.transport.mock.MockServerInvoker; 23 import org.jboss.test.remoting.transport.mock.MockTest; 24 import org.w3c.dom.Document ; 25 26 import junit.framework.TestCase; 27 28 29 34 public class InvokerClientTest extends TestCase implements InvokerConstants 35 { 36 private String sessionId = new UID ().toString(); 37 private Client client; 38 private Connector connector; 39 private InvokerLocator locator; 40 41 private static final Logger log = Logger.getLogger(InvokerClientTest.class); 42 43 public void init() 44 { 45 try 46 { 47 InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port); 48 client = new Client(locator, "mock"); 49 client.connect(); 50 } 51 catch(Exception e) 52 { 53 log.error(e.getMessage(), e); 54 } 55 } 56 57 64 private InvokerLocator initServer(int port) throws Exception 65 { 66 if(port < 0) 67 { 68 port = TestUtil.getRandomPort(); 69 } 70 log.debug("port = " + port); 71 72 InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class); 73 connector = new Connector(); 74 InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port); 75 StringBuffer buf = new StringBuffer (); 76 buf.append("<?xml version=\"1.0\"?>\n"); 77 buf.append("<handlers>\n"); 78 buf.append(" <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n"); 79 buf.append("</handlers>\n"); 80 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 81 connector.setInvokerLocator(locator.getLocatorURI()); 82 connector.setConfiguration(xml.getDocumentElement()); 83 connector.start(); 85 return locator; 86 } 87 88 89 public void setUp() throws Exception 90 { 91 locator = initServer(-1); 92 init(); 93 } 94 95 public void tearDown() throws Exception 96 { 97 if(connector != null) 98 { 99 connector.stop(); 100 connector.destroy(); 101 connector = null; 102 } 103 locator = null; 104 if(client != null) 105 { 106 client.disconnect(); 107 client = null; 108 } 109 } 110 111 117 public void testLocalPushCallback() throws Throwable 118 { 119 log.debug("running testLocalPushCallback()"); 120 121 sessionId = new UID ().toString(); 122 123 sessionId = client.getSessionId(); 124 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 125 126 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 127 128 Object ret = makeInvocation("foo", "bar"); 130 assertTrue("Result of testLocalPushCallback() invocation of foo.", "bar".equals(ret)); 131 if("bar".equals(ret)) 132 { 133 log.debug("PASS"); 134 } 135 else 136 { 137 log.debug("FAILED - testLocalPushCallback1"); 138 } 139 140 client.addListener(handler, locator); 141 ret = makeInvocation("test", "test"); 143 Thread.sleep(3000); 145 log.debug("done sleeping."); 146 int callbacksPerformed = handler.isCallbackReceived(); 147 log.debug("callbacksPerformed after adding listener is " + callbacksPerformed); 148 assertTrue("Result of testLocalPushCallback() failed since did not get callback.", 149 (callbacksPerformed == 1)); 150 if(callbacksPerformed == 1) 151 { 152 log.debug("PASS"); 153 } 154 else 155 { 156 log.debug("FAILED - testLocalPushCallback2"); 157 } 158 client.removeListener(handler); 160 ret = makeInvocation("test", "test"); 162 Thread.sleep(2000); 164 log.debug("done sleeping."); 165 callbacksPerformed = handler.isCallbackReceived(); 166 log.debug("callbackPerformed after removing listener is " + callbacksPerformed); 167 assertTrue("Result of testLocalPushCallback() failed since did get callback " + 168 "but have been removed as listener.", 169 (callbacksPerformed == 1)); 170 if(callbacksPerformed == 1) 171 { 172 log.debug("PASS"); 173 } 174 else 175 { 176 log.debug("FAILED - testLocalPushCallback3"); 177 } 178 179 } 180 181 187 public void testRemotePushCallback() throws Throwable 188 { 189 log.debug("running testRemotePushCallback()"); 190 191 sessionId = new UID ().toString(); 192 sessionId = client.getSessionId(); 194 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 195 196 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 197 198 Object ret = makeInvocation("foo", "bar"); 200 assertTrue("Result of testRemotePushCallback() invocation of foo.", "bar".equals(ret)); 201 if("bar".equals(ret)) 202 { 203 log.debug("PASS"); 204 } 205 else 206 { 207 log.debug("FAILED - testRemotePushCallback1"); 208 } 209 210 client.addListener(handler, locator); 211 ret = makeInvocation("test", "test"); 213 Thread.sleep(3000); 215 log.debug("done sleeping."); 216 224 client.removeListener(handler); 226 ret = makeInvocation("test", "test"); 228 Thread.sleep(2000); 230 log.debug("done sleeping."); 231 238 } 239 240 246 public void testPullCallback() throws Throwable 247 { 248 log.debug("running testPullCallback()"); 249 250 253 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 254 255 Object ret = makeInvocation("bar", "foo"); 257 assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret)); 258 if("foo".equals(ret)) 259 { 260 log.debug("PASS"); 261 } 262 else 263 { 264 log.debug("FAILED - testPullCallback1"); 265 } 266 267 client.addListener(handler); 268 ret = makeInvocation("test", "test"); 270 Thread.sleep(5000); 272 ret = client.getCallbacks(handler); 273 log.debug("getCallbacks returned " + ret); 274 log.debug("should have something."); 275 assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.", 276 ret != null); 277 if(ret != null) 278 { 279 log.debug("PASS"); 280 } 281 else 282 { 283 log.debug("FAILED - testPullCallback2"); 284 } 285 286 client.removeListener(handler); 289 ret = makeInvocation("getCallbacks", null); 290 log.debug("getCallbacks returned " + ret); 291 log.debug("should have been empty."); 292 assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.", 293 ret == null); 294 if(ret == null) 295 { 296 log.debug("PASS"); 297 } 298 else 299 { 300 log.debug("FAILED - testPullCallback3"); 301 } 302 303 } 304 305 310 public void testArrayReturn() throws Throwable 311 { 312 Object ret = makeInvocation("testComplexReturn", null); 314 ComplexReturn complexRet = (ComplexReturn) ret; 315 MockTest[] mockTests = complexRet.getMockTests(); 316 assertTrue("ComplexReturn's array should contain 2 items", 317 2 == mockTests.length); 318 if(2 == mockTests.length) 319 { 320 log.debug("PASS"); 321 } 322 else 323 { 324 log.debug("FAILED - testArrayReturn1"); 325 } 326 327 for(int x = 0; x < mockTests.length; x++) 328 { 329 System.err.println(mockTests[x]); 330 MockTest test = mockTests[x]; 331 assertNotNull("MockTest should not be null", test); 332 if(test != null) 333 { 334 log.debug("PASS"); 335 } 336 else 337 { 338 log.debug("FAILED - testArrayReturn2"); 339 } 340 341 } 342 343 } 346 347 private Object makeInvocation(String method, String param) throws Throwable 348 { 349 Object ret = client.invoke(new NameBasedInvocation(method, 350 new Object []{param}, 351 new String []{String .class.getName()}), 352 null); 353 354 return ret; 355 } 356 } 357 | Popular Tags |