KickJava   Java API By Example, From Geeks To Geeks.

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


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