KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jnp > test > TestJNPSockets


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jnp.test;
23
24 import java.io.IOException JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.net.Socket JavaDoc;
27 import java.net.ServerSocket JavaDoc;
28 import java.rmi.server.RMIClientSocketFactory JavaDoc;
29 import java.rmi.server.RMIServerSocketFactory JavaDoc;
30 import java.util.Properties JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32
33 import junit.framework.TestSuite;
34 import junit.framework.TestCase;
35
36 import org.jnp.server.Main;
37
38 /** A test of RMI custom sockets with the jnp JNDI provider.
39
40  @author Scott_Stark@displayscape.com
41  @version $Revision: 37459 $
42  */

43 public class TestJNPSockets extends TestCase
44 {
45    static Main server;
46
47    static int serverPort;
48
49    public TestJNPSockets(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    protected void setUp() throws Exception JavaDoc
55    {
56       if (server != null)
57          return;
58
59       server = new Main();
60       server.setPort(0);
61       server.setBindAddress("localhost");
62       server.setClientSocketFactory(ClientSocketFactory JavaDoc.class.getName());
63       server.setServerSocketFactory(ServerSocketFactory JavaDoc.class.getName());
64       server.start();
65       serverPort = server.getPort();
66    }
67
68    public void testAccess() throws Exception JavaDoc
69    {
70       Properties JavaDoc env = new Properties JavaDoc();
71       env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
72       env.setProperty("java.naming.provider.url", "localhost:" + serverPort);
73       env.setProperty("java.naming.factory.url.pkgs", "org.jnp.interfaces");
74       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
75       System.out.println("Connected to jnp service");
76       ctx.list("");
77       ctx.close();
78       if (ClientSocketFactory.created == false)
79          fail("No ClientSocketFactory was created");
80       if (ServerSocketFactory.created == false)
81          fail("No ServerSocketFactory was created");
82       server.stop();
83    }
84
85    public static void main(String JavaDoc[] args) throws Exception JavaDoc
86    {
87       System.setErr(System.out);
88       org.apache.log4j.BasicConfigurator.configure();
89       TestSuite suite = new TestSuite(TestJNPSockets.class);
90       junit.textui.TestRunner.run(suite);
91    }
92
93    public static class ClientSocketFactory implements RMIClientSocketFactory JavaDoc, Serializable JavaDoc
94    {
95       static final long serialVersionUID = -3951228824124738736L;
96
97       static boolean created;
98
99       public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc
100       {
101          Socket JavaDoc clientSocket = new Socket JavaDoc(host, port);
102          System.out.println("createSocket -> " + clientSocket);
103          created = true;
104          return clientSocket;
105       }
106    }
107
108    public static class ServerSocketFactory implements RMIServerSocketFactory JavaDoc, Serializable JavaDoc
109    {
110       static final long serialVersionUID = -2632886892871952872L;
111
112       static boolean created;
113
114       public ServerSocket JavaDoc createServerSocket(int port) throws IOException JavaDoc
115       {
116          ServerSocket JavaDoc serverSocket = new ServerSocket JavaDoc(port);
117          System.out.println("createServerSocket -> " + serverSocket);
118          created = true;
119          return serverSocket;
120       }
121    }
122 }
123
Popular Tags