1 7 package org.jboss.test.remoting.configuration; 8 9 import java.io.ByteArrayInputStream ; 10 import java.net.InetAddress ; 11 import javax.xml.parsers.DocumentBuilderFactory ; 12 import org.jboss.remoting.Client; 13 import org.jboss.remoting.InvokerLocator; 14 import org.jboss.remoting.InvokerRegistry; 15 import org.jboss.remoting.ServerInvoker; 16 import org.jboss.remoting.transport.Connector; 17 import org.jboss.remoting.transport.PortUtil; 18 import org.jboss.test.remoting.transport.mock.MockServerInvocationHandler; 19 import org.w3c.dom.Document ; 20 21 import junit.framework.TestCase; 22 23 24 27 public class RMIConfigurationTestCase extends TestCase 28 { 29 private String transport = "rmi"; 30 private int serverPort = 6666; 31 private int clientPort = 7777; 32 private int registryPort = 8888; 33 private String hostName = null; 34 private String hostIP = null; 35 private Connector connector = null; 36 37 public void setUp() throws Exception 38 { 39 hostName = InetAddress.getLocalHost().getHostName(); 40 hostIP = InetAddress.getLocalHost().getHostAddress(); 41 42 connector = new Connector(); 43 StringBuffer buf = new StringBuffer (); 44 buf.append("<?xml version=\"1.0\"?>\n"); 45 buf.append("<config>"); 46 buf.append("<invoker transport=\"" + transport + "\">"); 47 buf.append("<attribute name=\"registryPort\" isParam=\"true\">" + registryPort + "</attribute>"); 48 buf.append("<attribute name=\"serverBindAddress\">" + hostName + "</attribute>"); 49 buf.append("<attribute name=\"serverBindPort\">" + serverPort + "</attribute>"); 50 buf.append("<attribute name=\"clientConnectAddress\">" + hostIP + "</attribute>"); 51 buf.append("<attribute name=\"clientConnectPort\">" + clientPort + "</attribute>"); 52 buf.append("</invoker>"); 53 buf.append("<handlers>"); 54 buf.append(" <handler subsystem=\"mock\">" + MockServerInvocationHandler.class.getName() + "</handler>\n"); 55 buf.append("</handlers>"); 56 buf.append("</config>"); 57 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 58 connector.setConfiguration(xml.getDocumentElement()); 59 connector.create(); 60 connector.start(); 61 62 } 63 64 public void testClientConfiguration() throws Exception 65 { 66 67 ServerInvoker[] serverInvokers = InvokerRegistry.getServerInvokers(); 69 70 if(serverInvokers != null && serverInvokers.length > 0) 71 { 72 InvokerLocator locator = serverInvokers[0].getLocator(); 73 String locatorHost = locator.getHost(); 74 int locatorPort = locator.getPort(); 75 76 System.out.println("locator host = " + locatorHost); 77 System.out.println("locator port = " + locatorPort); 78 assertEquals(hostIP, locatorHost); 79 assertEquals(clientPort, locatorPort); 80 } 81 82 boolean portInUse = PortUtil.checkPort(serverPort); 84 assertTrue(!portInUse); 85 portInUse = PortUtil.checkPort(registryPort); 86 assertTrue(!portInUse); 87 88 Client client = new Client(new InvokerLocator(transport + "://" + hostName + ":" + serverPort + "/?registryPort=" + registryPort)); 90 String param = "foobar"; 91 Object ret = null; 92 try 93 { 94 ret = client.invoke(param); 95 } 96 catch(Throwable throwable) 97 { 98 throw new Exception ("Call on server failed.", throwable); 99 } 100 assertEquals(param, ret); 101 102 } 103 104 public void tearDown() throws Exception 105 { 106 if(connector != null) 107 { 108 connector.stop(); 109 connector.destroy(); 110 } 111 } 112 113 public static void main(String [] args) 114 { 115 RMIConfigurationTestCase testCase = new RMIConfigurationTestCase(); 116 try 117 { 118 testCase.setUp(); 119 testCase.testClientConfiguration(); 120 testCase.tearDown(); 121 } 122 catch(Exception e) 123 { 124 e.printStackTrace(); 125 } 126 } 127 } | Popular Tags |