1 7 package org.jboss.test.remoting.transport.rmi; 8 9 import java.io.ByteArrayInputStream ; 10 import java.rmi.server.UID ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import java.util.Set ; 15 import javax.xml.parsers.DocumentBuilderFactory ; 16 import org.jboss.logging.Logger; 17 import org.jboss.remoting.Client; 18 import org.jboss.remoting.InvokerLocator; 19 import org.jboss.remoting.InvokerRegistry; 20 import org.jboss.remoting.invocation.NameBasedInvocation; 21 import org.jboss.remoting.transport.Connector; 22 import org.jboss.remoting.transport.rmi.RMIServerInvoker; 23 import org.jboss.test.remoting.ComplexReturn; 24 import org.jboss.test.remoting.TestUtil; 25 import org.jboss.test.remoting.transport.mock.MockClientInvoker; 26 import org.jboss.test.remoting.transport.mock.MockInvokerCallbackHandler; 27 import org.jboss.test.remoting.transport.mock.MockServerInvoker; 28 import org.jboss.test.remoting.transport.mock.MockTest; 29 import org.w3c.dom.Document ; 30 31 import junit.framework.TestCase; 32 33 38 public class RMIInvokerClientTest extends TestCase 39 { 40 41 private String sessionId = new UID ().toString(); 42 private Client client; 43 private Connector connector; 44 private InvokerLocator locator; 45 private int port = RMIServerInvoker.DEFAULT_REGISTRY_PORT - 1; 46 private String transport = "rmi"; 47 48 private static final Logger log = Logger.getLogger(RMIInvokerClientTest.class); 49 50 public void init(Map metadata) 51 { 52 try 53 { 54 InvokerLocator locator = new InvokerLocator(buildLocatorURI(metadata, this.port)); 55 client = new Client(locator, "mock"); 57 client.connect(); 58 } 59 catch(Exception e) 60 { 61 log.error(e.getMessage(), e); 62 } 63 } 64 65 private String buildLocatorURI(Map metadata, int port) 66 { 67 if(metadata == null || metadata.size() == 0) 68 { 69 return transport + "://localhost:" + port; 70 } 71 else 72 { 73 StringBuffer uriBuffer = new StringBuffer (transport + "://localhost:" + port + "/?"); 74 75 Set keys = metadata.keySet(); 76 Iterator itr = keys.iterator(); 77 while(itr.hasNext()) 78 { 79 String key = (String ) itr.next(); 80 String value = (String ) metadata.get(key); 81 uriBuffer.append(key + "=" + value + ","); 82 } 83 return uriBuffer.substring(0, uriBuffer.length() - 1); 84 } 85 } 86 87 private InvokerLocator initServer(Map metadata, int serverPort) throws Exception 88 { 89 if(serverPort < 0) 90 { 91 serverPort = TestUtil.getRandomPort(); 92 } 93 log.debug("port = " + serverPort); 94 95 InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class); 96 connector = new Connector(); 97 98 InvokerLocator locator = new InvokerLocator(buildLocatorURI(metadata, serverPort)); 99 100 StringBuffer buf = new StringBuffer (); 102 buf.append("<?xml version=\"1.0\"?>\n"); 103 buf.append("<handlers>\n"); 104 buf.append(" <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n"); 105 buf.append("</handlers>\n"); 106 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 107 connector.setInvokerLocator(locator.getLocatorURI()); 108 connector.setConfiguration(xml.getDocumentElement()); 109 connector.start(); 111 return locator; 112 } 113 114 115 public void setUp() throws Exception 116 { 117 Map metadata = new HashMap (); 118 metadata.put(RMIServerInvoker.REGISTRY_PORT_KEY, String.valueOf(port + 1)); 119 locator = initServer(metadata, -1); 120 init(metadata); 121 } 122 123 public void tearDown() throws Exception 124 { 125 if(connector != null) 126 { 127 connector.stop(); 128 connector.destroy(); 129 connector = null; 130 } 131 locator = null; 132 if(client != null) 133 { 134 client.disconnect(); 135 client = null; 136 } 137 } 138 139 145 public void testLocalPushCallback() throws Throwable 146 { 147 log.debug("running testLocalPushCallback()"); 148 149 sessionId = new UID ().toString(); 150 151 sessionId = client.getSessionId(); 152 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 153 154 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 155 156 Object ret = makeInvocation("foo", "bar"); 158 assertTrue("Result of testLocalPushCallback() invocation of foo.", "bar".equals(ret)); 159 client.addListener(handler, locator); 160 ret = makeInvocation("test", "test"); 162 Thread.sleep(3000); 164 log.debug("done sleeping."); 165 int callbacksPerformed = handler.isCallbackReceived(); 166 log.debug("callbacksPerformed after adding listener is " + callbacksPerformed); 167 assertTrue("Result of testLocalPushCallback() failed since did not get callback.", 168 (callbacksPerformed == 1)); 169 client.removeListener(handler); 171 ret = makeInvocation("test", "test"); 173 Thread.sleep(2000); 175 log.debug("done sleeping."); 176 callbacksPerformed = handler.isCallbackReceived(); 177 log.debug("callbackPerformed after removing listener is " + callbacksPerformed); 178 assertTrue("Result of testLocalPushCallback() failed since did get callback " + 179 "but have been removed as listener.", 180 (callbacksPerformed == 1)); 181 } 182 183 189 public void testRemotePushCallback() throws Throwable 190 { 191 log.debug("running testRemotePushCallback()"); 192 193 sessionId = new UID ().toString(); 194 sessionId = client.getSessionId(); 196 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 197 198 log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator()); 199 200 Object ret = makeInvocation("foo", "bar"); 202 assertTrue("Result of testRemotePushCallback() invocation of foo.", "bar".equals(ret)); 203 client.addListener(handler, locator); 204 ret = makeInvocation("test", "test"); 206 Thread.sleep(3000); 208 log.debug("done sleeping."); 209 217 client.removeListener(handler); 219 ret = makeInvocation("test", "test"); 221 Thread.sleep(2000); 223 log.debug("done sleeping."); 224 231 } 232 233 239 public void testPullCallback() throws Throwable 240 { 241 log.debug("running testPullCallback()"); 242 243 246 MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId); 247 248 Object ret = makeInvocation("bar", "foo"); 250 assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret)); 251 client.addListener(handler); 252 ret = makeInvocation("test", "test"); 254 Thread.sleep(2000); 256 ret = client.getCallbacks(handler); 257 log.debug("getCallbacks returned " + ret); 258 log.debug("should have something."); 259 assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.", 260 ret != null); 261 client.removeListener(handler); 264 ret = makeInvocation("getCallbacks", null); 265 log.debug("getCallbacks returned " + ret); 266 log.debug("should have been empty."); 267 assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.", 268 ret == null); 269 } 270 271 276 public void testArrayReturn() throws Throwable 277 { 278 Object ret = makeInvocation("testComplexReturn", null); 280 ComplexReturn complexRet = (ComplexReturn) ret; 281 MockTest[] mockTests = complexRet.getMockTests(); 282 assertTrue("ComplexReturn's array should contain 2 items", 283 2 == mockTests.length); 284 for(int x = 0; x < mockTests.length; x++) 285 { 286 System.err.println(mockTests[x]); 287 MockTest test = mockTests[x]; 288 assertNotNull("MockTest should not be null", test); 289 } 290 291 } 294 295 private Object makeInvocation(String method, String param) throws Throwable 296 { 297 Object ret = client.invoke(new NameBasedInvocation(method, 298 new Object []{param}, 299 new String []{String .class.getName()}), 300 null); 301 302 return ret; 303 } 304 305 } 306 | Popular Tags |