1 8 package org.codehaus.spice.netserve.sockets.impl; 9 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.OutputStream ; 13 import java.net.InetAddress ; 14 import java.net.ServerSocket ; 15 import java.net.Socket ; 16 import java.net.UnknownHostException ; 17 import junit.framework.TestCase; 18 import org.codehaus.spice.netserve.sockets.ServerSocketFactory; 19 import org.codehaus.spice.netserve.sockets.SocketFactory; 20 21 27 public class SocketTestCase 28 extends TestCase 29 { 30 private static final int PORT = 1977; 31 private static final InetAddress HOST = getLocalHost(); 32 private static final byte DATA = 23; 33 34 public SocketTestCase( final String name ) 35 { 36 super( name ); 37 } 38 39 public void testDefault() 40 throws Exception 41 { 42 final SocketFactory csf = new DefaultSocketFactory(); 43 final ServerSocketFactory ssf = new DefaultServerSocketFactory(); 44 45 doSocketPairTest( csf, ssf ); 46 } 47 48 private void doSocketPairTest( final SocketFactory csf, final ServerSocketFactory ssf ) throws IOException 49 { 50 try 51 { 52 csf.createSocket( HOST, PORT ); 53 final String message = "Should not be able to create " + 54 "client socket when server socket not initialized."; 55 fail( message ); 56 } 57 catch( IOException e ) 58 { 59 } 60 61 final ServerSocket ss1 = ssf.createServerSocket( PORT ); 62 readWriteTestsOnSingleSocket( ss1, csf ); 63 64 final ServerSocket ss2 = ssf.createServerSocket( PORT, 1 ); 65 readWriteTestsOnSingleSocket( ss2, csf ); 66 67 final ServerSocket ss3 = ssf.createServerSocket( PORT, 1, HOST ); 68 readWriteTestsOnSingleSocket( ss3, csf ); 69 } 70 71 private void readWriteTestsOnSingleSocket( final ServerSocket serverSocket, 72 final SocketFactory csf ) 73 throws IOException 74 { 75 final Socket client = csf.createSocket( HOST, PORT ); 76 final Socket server = serverSocket.accept(); 77 78 pushPullData( "Testing single socket conn", client, server ); 79 80 final Socket client2 = csf.createSocket( HOST, PORT, HOST, 0 ); 81 final Socket server2 = serverSocket.accept(); 82 pushPullData( "Testing single socket conn2", client2, server2 ); 83 84 serverSocket.close(); 85 } 86 87 private void pushPullData( final String prefix, 88 final Socket client, 89 final Socket server ) 90 throws IOException 91 { 92 pushData( prefix + ": c2s", client, server ); 93 pushData( prefix + ": s2c", server, client ); 94 95 client.close(); 96 server.close(); 97 } 98 99 102 private void pushData( final String prefix, 103 final Socket socket1, 104 final Socket socket2 ) throws IOException 105 { 106 final OutputStream cos = socket1.getOutputStream(); 107 cos.write( DATA ); 108 cos.flush(); 109 110 final InputStream sis = socket2.getInputStream(); 111 assertEquals( prefix + ": Write/read byte", DATA, sis.read() ); 112 } 113 114 private static InetAddress getLocalHost() 115 { 116 try 117 { 118 return InetAddress.getLocalHost(); 119 } 120 catch( UnknownHostException e ) 121 { 122 throw new IllegalStateException ( e.toString() ); 123 } 124 } 125 } 126 | Popular Tags |