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 SocketClientConfigurationTestCase extends TestCase 28 { 29 private String transport = "socket"; 30 private int serverPort = 6666; 31 private int clientPort = 7777; 32 private String hostName = null; 33 private String hostIP = null; 34 private Connector connector = null; 35 36 public void setUp() throws Exception 37 { 38 hostName = InetAddress.getLocalHost().getHostName(); 39 hostIP = InetAddress.getLocalHost().getHostAddress(); 40 41 connector = new Connector(); 42 StringBuffer buf = new StringBuffer (); 43 buf.append("<?xml version=\"1.0\"?>\n"); 44 buf.append("<config>"); 45 buf.append("<invoker transport=\"" + transport + "\">"); 46 buf.append("<attribute name=\"numAcceptThreads\">1</attribute>"); 47 buf.append("<attribute name=\"maxPoolSize\">303</attribute>"); 48 buf.append("<attribute name=\"clientMaxPoolSize\" isParam=\"true\">304</attribute>"); 49 buf.append("<attribute name=\"socketTimeout\">60000</attribute>"); 50 buf.append("<attribute name=\"serverBindAddress\">" + hostName + "</attribute>"); 51 buf.append("<attribute name=\"serverBindPort\">" + serverPort + "</attribute>"); 52 buf.append("<attribute name=\"clientConnectAddress\">" + hostIP + "</attribute>"); 53 buf.append("<attribute name=\"clientConnectPort\">" + clientPort + "</attribute>"); 54 buf.append("<attribute name=\"enableTcpNoDelay\" isParam=\"true\">false</attribute>"); 55 buf.append("<attribute name=\"backlog\">200</attribute>"); 56 buf.append("</invoker>"); 57 buf.append("<handlers>"); 58 buf.append(" <handler subsystem=\"mock\">" + MockServerInvocationHandler.class.getName() + "</handler>\n"); 59 buf.append("</handlers>"); 60 buf.append("</config>"); 61 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 62 connector.setConfiguration(xml.getDocumentElement()); 64 connector.create(); 65 connector.start(); 66 67 } 68 69 public void testClientConfiguration() throws Exception 70 { 71 72 ServerInvoker[] serverInvokers = InvokerRegistry.getServerInvokers(); 74 75 if(serverInvokers != null && serverInvokers.length > 0) 76 { 77 InvokerLocator locator = serverInvokers[0].getLocator(); 78 String locatorHost = locator.getHost(); 79 int locatorPort = locator.getPort(); 80 81 System.out.println("locator host = " + locatorHost); 82 System.out.println("locator port = " + locatorPort); 83 assertEquals(hostIP, locatorHost); 84 assertEquals(clientPort, locatorPort); 85 } 86 87 boolean portInUse = PortUtil.checkPort(serverPort); 89 assertTrue(!portInUse); 90 91 Client client = new Client(new InvokerLocator(transport + "://" + hostName + ":" + serverPort)); 93 String param = "foobar"; 94 Object ret = null; 95 try 96 { 97 ret = client.invoke(param); 98 } 99 catch(Throwable throwable) 100 { 101 throw new Exception ("Call on server failed.", throwable); 102 } 103 assertEquals(param, ret); 104 105 } 106 107 public void tearDown() throws Exception 108 { 109 if(connector != null) 110 { 111 connector.stop(); 112 connector.destroy(); 113 } 114 } 115 116 public static void main(String [] args) 117 { 118 SocketClientConfigurationTestCase testCase = new SocketClientConfigurationTestCase(); 119 try 120 { 121 testCase.setUp(); 122 testCase.testClientConfiguration(); 123 testCase.tearDown(); 124 } 125 catch(Exception e) 126 { 127 e.printStackTrace(); 128 } 129 } 130 } | Popular Tags |