1 22 package org.jnp.test; 23 24 import java.io.IOException ; 25 import java.io.Serializable ; 26 import java.net.Socket ; 27 import java.net.ServerSocket ; 28 import java.rmi.server.RMIClientSocketFactory ; 29 import java.rmi.server.RMIServerSocketFactory ; 30 import java.util.Properties ; 31 import javax.naming.InitialContext ; 32 33 import junit.framework.TestSuite; 34 import junit.framework.TestCase; 35 36 import org.jnp.server.Main; 37 38 43 public class TestJNPSockets extends TestCase 44 { 45 static Main server; 46 47 static int serverPort; 48 49 public TestJNPSockets(String name) 50 { 51 super(name); 52 } 53 54 protected void setUp() throws Exception 55 { 56 if (server != null) 57 return; 58 59 server = new Main(); 60 server.setPort(0); 61 server.setBindAddress("localhost"); 62 server.setClientSocketFactory(ClientSocketFactory .class.getName()); 63 server.setServerSocketFactory(ServerSocketFactory .class.getName()); 64 server.start(); 65 serverPort = server.getPort(); 66 } 67 68 public void testAccess() throws Exception 69 { 70 Properties env = new Properties (); 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 ctx = new InitialContext (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 [] args) throws Exception 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 , Serializable 94 { 95 static final long serialVersionUID = -3951228824124738736L; 96 97 static boolean created; 98 99 public Socket createSocket(String host, int port) throws IOException 100 { 101 Socket clientSocket = new Socket (host, port); 102 System.out.println("createSocket -> " + clientSocket); 103 created = true; 104 return clientSocket; 105 } 106 } 107 108 public static class ServerSocketFactory implements RMIServerSocketFactory , Serializable 109 { 110 static final long serialVersionUID = -2632886892871952872L; 111 112 static boolean created; 113 114 public ServerSocket createServerSocket(int port) throws IOException 115 { 116 ServerSocket serverSocket = new ServerSocket (port); 117 System.out.println("createServerSocket -> " + serverSocket); 118 created = true; 119 return serverSocket; 120 } 121 } 122 } 123 | Popular Tags |