KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > configuration > SocketClientConfigurationTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.configuration;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.net.InetAddress JavaDoc;
11 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
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 JavaDoc;
20
21 import junit.framework.TestCase;
22
23
24 /**
25  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
26  */

27 public class SocketClientConfigurationTestCase extends TestCase
28 {
29    private String JavaDoc transport = "socket";
30    private int serverPort = 6666;
31    private int clientPort = 7777;
32    private String JavaDoc hostName = null;
33    private String JavaDoc hostIP = null;
34    private Connector connector = null;
35
36    public void setUp() throws Exception JavaDoc
37    {
38       hostName = InetAddress.getLocalHost().getHostName();
39       hostIP = InetAddress.getLocalHost().getHostAddress();
40
41       connector = new Connector();
42       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
62       //connector.setInvokerLocator(locator.getLocatorURI());
63
connector.setConfiguration(xml.getDocumentElement());
64       connector.create();
65       connector.start();
66
67    }
68
69    public void testClientConfiguration() throws Exception JavaDoc
70    {
71
72       // make sure the client's view of the locator will be as configured
73
ServerInvoker[] serverInvokers = InvokerRegistry.getServerInvokers();
74
75       if(serverInvokers != null && serverInvokers.length > 0)
76       {
77          InvokerLocator locator = serverInvokers[0].getLocator();
78          String JavaDoc 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       // check for server bind port (assume is the server since no exception thrown before)
88
boolean portInUse = PortUtil.checkPort(serverPort);
89       assertTrue(!portInUse);
90
91       // make sure can call ont
92
Client client = new Client(new InvokerLocator(transport + "://" + hostName + ":" + serverPort));
93       String JavaDoc param = "foobar";
94       Object JavaDoc ret = null;
95       try
96       {
97          ret = client.invoke(param);
98       }
99       catch(Throwable JavaDoc throwable)
100       {
101          throw new Exception JavaDoc("Call on server failed.", throwable);
102       }
103       assertEquals(param, ret);
104
105    }
106
107    public void tearDown() throws Exception JavaDoc
108    {
109       if(connector != null)
110       {
111          connector.stop();
112          connector.destroy();
113       }
114    }
115
116    public static void main(String JavaDoc[] args)
117    {
118       SocketClientConfigurationTestCase testCase = new SocketClientConfigurationTestCase();
119       try
120       {
121          testCase.setUp();
122          testCase.testClientConfiguration();
123          testCase.tearDown();
124       }
125       catch(Exception JavaDoc e)
126       {
127          e.printStackTrace();
128       }
129    }
130 }
Popular Tags