package com.ikamobile.ikasoa.core.thrift;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.thrift.transport.TTransportException;
import org.junit.Test;

import junit.framework.TestCase;

/**
 * Socket连接池单元测试
 */
public class SocketPoolTest extends TestCase {

	private static final String HOST = "localhost";

	private static final int PORT = 38001;

	@Test
	public void testBuildThriftSocket() {
		ThriftSocketThreadTest tstt = new ThriftSocketThreadTest();
		tstt.start();
		ThriftSocket ts = SocketPool.buildThriftSocket(HOST, PORT);
		assertNotNull(ts);
		try {
			ts.open();
			Socket s = ts.getSocket();
			assertEquals(s.getInetAddress().getHostName(), HOST);
			assertEquals(s.getPort(), PORT);
		} catch (TTransportException e) {
			fail();
		} finally {
			ts.close();
		}
		tstt.interrupt();
	}

	private class ThriftSocketThreadTest extends Thread {
		@SuppressWarnings("resource")
		public void run() {
			try {
				new ServerSocket(PORT).accept();
			} catch (IOException e) {
			}
		}
	}

}

@@ -49,1054 +49,255 @@
 ort 
-java.io.IOException;%0D%0Aimport java.net.ServerSocket;%0D%0Aimport java.net.Socket;%0D%0A%0D%0Aimport org.apache.thrift.transport.TTransportException;%0D%0Aimport org.junit.Test;%0D%0A%0D%0Aimport junit.framework.TestCase;%0D%0A%0D%0A/**%0D%0A * Socket%E8%BF%9E%E6%8E%A5%E6%B1%A0%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%0D%0A */%0D%0Apublic class SocketPoolTest extends TestCase %7B%0D%0A%0D%0A%09private static final String HOST = %22localhost%22;%0D%0A%0D%0A%09private static final int PORT = 38001;%0D%0A%0D%0A%09@Test%0D%0A%09public void testBuildThriftSocket() %7B%0D%0A%09%09ThriftSocketThreadTest tstt = new ThriftSocketThreadTest();%0D%0A%09%09tstt.start();%0D%0A%09%09ThriftSocket ts = SocketPool.buildThriftSocket(HOST, PORT);%0D%0A%09%09assertNotNull(ts);%0D%0A%09%09try %7B%0D%0A%09%09%09ts.open();%0D%0A%09%09%09Socket s = ts.getSocket();%0D%0A%09%09%09assertEquals(s.getInetAddress().getHostName(), HOST);%0D%0A%09%09%09assertEquals(s.getPort(), PORT);%0D%0A%09%09%7D catch (TTransportException e) %7B%0D%0A%09%09%09fail();%0D%0A%09%09%7D finally %7B%0D%0A%09%09%09ts.close();%0D%0A%09%09%7D%0D%0A%09%09tstt.interrupt();%0D%0A%09%7D%0D%0A%0D%0A%09private class ThriftSocketThreadTest extends Thread %7B%0D%0A%09%09@SuppressWarnings(%22resource%22)%0D%0A%09%09public void run() %7B%0D%0A%09%09%09try %7B%0D%0A%09%09%09%09new ServerSocket(PORT).accept();%0D%0A%09%09%09%7D catch (IOException e) %7B%0D%0A%09%09%09%7D%0D%0A%09%09%7D
+org.junit.Test;%0D%0A%0D%0Aimport junit.framework.TestCase;%0D%0A%0D%0A/**%0D%0A * Socket%E8%BF%9E%E6%8E%A5%E6%B1%A0%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%0D%0A */%0D%0Apublic class SocketPoolTest extends TestCase %7B%0D%0A%0D%0A%09@Test%0D%0A%09public void testBuildThriftSocket() %7B%0D%0A%09%09assertNotNull(SocketPool.buildThriftSocket(%22localhost%22, 38001));
 %0D%0A%09%7D

update SocketPoolTest.java