1 7 package org.jboss.test.remoting.transport.local; 8 9 import java.io.ByteArrayInputStream ; 10 import java.rmi.server.UID ; 11 import javax.xml.parsers.DocumentBuilderFactory ; 12 import org.apache.log4j.Level; 13 import org.jboss.logging.Logger; 14 import org.jboss.remoting.Client; 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.test.remoting.TestUtil; 20 import org.jboss.test.remoting.transport.mock.MockClientInvoker; 21 import org.jboss.test.remoting.transport.mock.MockServerInvoker; 22 import org.w3c.dom.Document ; 23 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 34 public class LocalInvokerTestCase extends TestCase 35 { 36 private String sessionId = new UID ().toString(); 37 private Client client; 38 private String transport = "socket"; 39 private int port = -1; 40 private InvokerLocator locator; 41 private static final String NAME = "LocalInvokerTest.class"; 42 private static final Logger log = Logger.getLogger(LocalInvokerTestCase.class); 43 44 public LocalInvokerTestCase() 45 { 46 super(NAME); 47 } 48 49 public LocalInvokerTestCase(String name) 50 { 51 super(name); 52 } 53 54 public LocalInvokerTestCase(String transport, int port) 55 { 56 super(NAME); 57 this.transport = transport; 58 this.port = port; 59 } 60 61 public void init() 62 { 63 try 64 { 65 client = new Client(locator, "mock"); 66 client.connect(); 67 } 68 catch(Exception e) 69 { 70 log.error(e.getMessage(), e); 71 } 72 } 73 74 81 private InvokerLocator initServer(int port) throws Exception 82 { 83 if(port < 0) 84 { 85 port = TestUtil.getRandomPort(); 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.test.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 this.port = port; 103 this.locator = connector.getLocator(); 104 return locator; 105 } 106 107 108 public void testLocalInvoke() throws Throwable 109 { 110 try 111 { 112 log.debug("running testLocalInvoke()"); 113 114 InvokerLocator locator = initServer(-1); 115 init(); 116 117 119 log.debug("client.getInvoker()" + client.getInvoker()); 120 121 Object ret = makeInvocation("foo", "bar"); 123 assertTrue("Result of testLocalInvoke() invocation of foo.", "bar".equals(ret)); 124 150 } 151 finally 152 { 153 if(client != null) 154 { 155 client.disconnect(); 156 } 157 } 158 } 159 160 165 210 211 216 249 250 private Object makeInvocation(String method, String param) throws Throwable 251 { 252 Object ret = client.invoke(new NameBasedInvocation(method, 253 new Object []{param}, 254 new String []{String .class.getName()}), 255 null); 256 257 return ret; 258 } 259 260 public static Test suite() 261 { 262 return new TestSuite(LocalInvokerTestCase.class); 263 } 264 265 266 public static void main(String [] args) 267 { 268 org.apache.log4j.BasicConfigurator.configure(); 269 org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG); 270 271 LocalInvokerTestCase client = null; 272 if(args.length == 2) 273 { 274 String transport = args[0]; 275 int port = Integer.parseInt(args[1]); 276 client = new LocalInvokerTestCase(transport, port); 277 } 278 else 279 { 280 client = new LocalInvokerTestCase(LocalInvokerTestCase.class.getName()); 281 System.out.println("Using default transport " + 282 "and default port." + 283 "\nCan enter transport and port."); 284 } 285 286 try 287 { 288 junit.textui.TestRunner.run(suite()); 294 295 } 296 catch(Throwable e) 297 { 298 e.printStackTrace(); 299 System.exit(1); 300 } 301 System.exit(0); 302 } 303 } 304 | Popular Tags |