KickJava   Java API By Example, From Geeks To Geeks.

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


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 HTTPConfigurationTestCase extends TestCase
28 {
29    private String JavaDoc transport = "http";
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=\"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 JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
56       //connector.setInvokerLocator(locator.getLocatorURI());
57
connector.setConfiguration(xml.getDocumentElement());
58       connector.create();
59       connector.start();
60
61    }
62
63    public void testClientConfiguration() throws Exception JavaDoc
64    {
65
66       // make sure the client's view of the locator will be as configured
67
ServerInvoker[] serverInvokers = InvokerRegistry.getServerInvokers();
68
69       if(serverInvokers != null && serverInvokers.length > 0)
70       {
71          InvokerLocator locator = serverInvokers[0].getLocator();
72          String JavaDoc 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       // check for server bind port (assume is the server since no exception thrown before)
82
boolean portInUse = PortUtil.checkPort(serverPort);
83       assertTrue(!portInUse);
84
85       // make sure can call ont
86
Client client = new Client(new InvokerLocator(transport + "://" + hostName + ":" + serverPort));
87       String JavaDoc param = "foobar";
88       Object JavaDoc ret = null;
89       try
90       {
91          ret = client.invoke(param);
92       }
93       catch(Throwable JavaDoc throwable)
94       {
95          throwable.printStackTrace();
96       }
97       assertEquals(param, ret);
98
99    }
100
101    public void tearDown() throws Exception JavaDoc
102    {
103       if(connector != null)
104       {
105          connector.stop();
106          connector.destroy();
107       }
108    }
109
110    public static void main(String JavaDoc[] args)
111    {
112       HTTPConfigurationTestCase testCase = new HTTPConfigurationTestCase();
113       try
114       {
115          testCase.setUp();
116          testCase.testClientConfiguration();
117          testCase.tearDown();
118       }
119       catch(Exception JavaDoc e)
120       {
121          e.printStackTrace();
122       }
123    }
124 }
Popular Tags