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 HTTPConfigurationTestCase extends TestCase 28 { 29 private String transport = "http"; 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=\"serverBindAddress\">" + hostName + "</attribute>"); 47 buf.append("<attribute name=\"serverBindPort\">" + serverPort + "</attribute>"); 48 buf.append("<attribute name=\"clientConnectAddress\">" + hostIP + "</attribute>"); 49 buf.append("<attribute name=\"clientConnectPort\">" + clientPort + "</attribute>"); 50 buf.append("</invoker>"); 51 buf.append("<handlers>"); 52 buf.append(" <handler subsystem=\"mock\">" + MockServerInvocationHandler.class.getName() + "</handler>\n"); 53 buf.append("</handlers>"); 54 buf.append("</config>"); 55 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 56 connector.setConfiguration(xml.getDocumentElement()); 58 connector.create(); 59 connector.start(); 60 61 } 62 63 public void testClientConfiguration() throws Exception 64 { 65 66 ServerInvoker[] serverInvokers = InvokerRegistry.getServerInvokers(); 68 69 if(serverInvokers != null && serverInvokers.length > 0) 70 { 71 InvokerLocator locator = serverInvokers[0].getLocator(); 72 String locatorHost = locator.getHost(); 73 int locatorPort = locator.getPort(); 74 75 System.out.println("locator host = " + locatorHost); 76 System.out.println("locator port = " + locatorPort); 77 assertEquals(hostIP, locatorHost); 78 assertEquals(clientPort, locatorPort); 79 } 80 81 boolean portInUse = PortUtil.checkPort(serverPort); 83 assertTrue(!portInUse); 84 85 Client client = new Client(new InvokerLocator(transport + "://" + hostName + ":" + serverPort)); 87 String param = "foobar"; 88 Object ret = null; 89 try 90 { 91 ret = client.invoke(param); 92 } 93 catch(Throwable throwable) 94 { 95 throwable.printStackTrace(); 96 } 97 assertEquals(param, ret); 98 99 } 100 101 public void tearDown() throws Exception 102 { 103 if(connector != null) 104 { 105 connector.stop(); 106 connector.destroy(); 107 } 108 } 109 110 public static void main(String [] args) 111 { 112 HTTPConfigurationTestCase testCase = new HTTPConfigurationTestCase(); 113 try 114 { 115 testCase.setUp(); 116 testCase.testClientConfiguration(); 117 testCase.tearDown(); 118 } 119 catch(Exception e) 120 { 121 e.printStackTrace(); 122 } 123 } 124 } | Popular Tags |