1 20 package org.apache.mina.transport; 21 22 import java.net.InetSocketAddress ; 23 24 import junit.framework.Assert; 25 import junit.framework.TestCase; 26 27 import org.apache.mina.common.ConnectFuture; 28 import org.apache.mina.common.IoAcceptor; 29 import org.apache.mina.common.IoConnector; 30 import org.apache.mina.common.IoHandlerAdapter; 31 import org.apache.mina.common.IoSession; 32 import org.apache.mina.common.RuntimeIOException; 33 import org.apache.mina.util.AvailablePortFinder; 34 35 41 public abstract class AbstractConnectorTest extends TestCase { 42 43 protected abstract IoAcceptor createAcceptor(); 44 45 protected abstract IoConnector createConnector(); 46 47 public void testConnectFutureSuccessTiming() throws Exception { 48 int port = AvailablePortFinder.getNextAvailable(1025); 49 IoAcceptor acceptor = createAcceptor(); 50 acceptor.bind(new InetSocketAddress (port), new IoHandlerAdapter()); 51 52 try { 53 final StringBuffer buf = new StringBuffer (); 54 IoConnector connector = createConnector(); 55 ConnectFuture future = connector.connect(new InetSocketAddress ( 56 "localhost", port), new IoHandlerAdapter() { 57 public void sessionCreated(IoSession session) { 58 buf.append("1"); 59 } 60 61 public void sessionOpened(IoSession session) { 62 buf.append("2"); 63 } 64 65 public void exceptionCaught(IoSession session, Throwable cause) { 66 buf.append("X"); 67 } 68 }); 69 70 future.join(); 71 buf.append("3"); 72 future.getSession().close(); 73 Assert.assertEquals("123", buf.toString()); 74 } finally { 75 acceptor.unbind(new InetSocketAddress (port)); 76 } 77 } 78 79 public void testConnectFutureFailureTiming() throws Exception { 80 int port = AvailablePortFinder.getNextAvailable(1025); 81 final StringBuffer buf = new StringBuffer (); 82 83 IoConnector connector = createConnector(); 84 ConnectFuture future = connector.connect(new InetSocketAddress ( 85 "localhost", port), new IoHandlerAdapter() { 86 public void sessionCreated(IoSession session) { 87 buf.append("X"); 88 } 89 90 public void sessionOpened(IoSession session) { 91 buf.append("Y"); 92 } 93 94 public void exceptionCaught(IoSession session, Throwable cause) { 95 buf.append("Z"); 96 } 97 }); 98 99 future.join(); 100 buf.append("1"); 101 try { 102 future.getSession().close(); 103 fail(); 104 } catch (RuntimeIOException e) { 105 } 107 Assert.assertEquals("1", buf.toString()); 108 } 109 } 110 | Popular Tags |