001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jnp.test;
023:
024: import java.io.IOException;
025: import java.io.Serializable;
026: import java.net.Socket;
027: import java.net.ServerSocket;
028: import java.rmi.server.RMIClientSocketFactory;
029: import java.rmi.server.RMIServerSocketFactory;
030: import java.util.Properties;
031: import javax.naming.InitialContext;
032:
033: import junit.framework.TestSuite;
034: import junit.framework.TestCase;
035:
036: import org.jnp.server.Main;
037:
038: /** A test of RMI custom sockets with the jnp JNDI provider.
039:
040: @author Scott_Stark@displayscape.com
041: @version $Revision: 57199 $
042: */
043: public class TestJNPSockets extends TestCase {
044: static Main server;
045: static int serverPort;
046:
047: public TestJNPSockets(String name) {
048: super (name);
049: }
050:
051: protected void setUp() throws Exception {
052: if (server != null)
053: return;
054:
055: server = new Main();
056: server.setPort(0);
057: server.setBindAddress("localhost");
058: server.setClientSocketFactory(ClientSocketFactory.class
059: .getName());
060: server.setServerSocketFactory(ServerSocketFactory.class
061: .getName());
062: server.start();
063: serverPort = server.getPort();
064: }
065:
066: public void testAccess() throws Exception {
067: Properties env = new Properties();
068: env.setProperty("java.naming.factory.initial",
069: "org.jnp.interfaces.NamingContextFactory");
070: env.setProperty("java.naming.provider.url", "localhost:"
071: + serverPort);
072: env.setProperty("java.naming.factory.url.pkgs",
073: "org.jnp.interfaces");
074: InitialContext ctx = new InitialContext(env);
075: System.out.println("Connected to jnp service");
076: ctx.list("");
077: ctx.close();
078: if (ClientSocketFactory.created == false)
079: fail("No ClientSocketFactory was created");
080: if (ServerSocketFactory.created == false)
081: fail("No ServerSocketFactory was created");
082: server.stop();
083: }
084:
085: public static void main(String[] args) throws Exception {
086: System.setErr(System.out);
087: org.apache.log4j.BasicConfigurator.configure();
088: TestSuite suite = new TestSuite(TestJNPSockets.class);
089: junit.textui.TestRunner.run(suite);
090: }
091:
092: public static class ClientSocketFactory implements
093: RMIClientSocketFactory, Serializable {
094: static final long serialVersionUID = -3951228824124738736L;
095: static boolean created;
096:
097: public Socket createSocket(String host, int port)
098: throws IOException {
099: Socket clientSocket = new Socket(host, port);
100: System.out.println("createSocket -> " + clientSocket);
101: created = true;
102: return clientSocket;
103: }
104: }
105:
106: public static class ServerSocketFactory implements
107: RMIServerSocketFactory, Serializable {
108: static final long serialVersionUID = -2632886892871952872L;
109: static boolean created;
110:
111: public ServerSocket createServerSocket(int port)
112: throws IOException {
113: ServerSocket serverSocket = new ServerSocket(port);
114: System.out.println("createServerSocket -> " + serverSocket);
115: created = true;
116: return serverSocket;
117: }
118: }
119: }
|