package cz.eman.jsonrpc;
import java.io.IOException;
import java.net.Socket;
import junit.framework.TestCase;
import org.junit.Test;
import cz.eman.jsonrpc.client.TcpJsonClient;
import cz.eman.jsonrpc.client.example.WebServiceClientProxy;
import cz.eman.jsonrpc.server.example.B;
import cz.eman.jsonrpc.server.example.WebService;
import cz.eman.jsonrpc.server.tcp.TcpJsonMultiServer;
import cz.eman.jsonrpc.shared.exception.RpcErrorException;
public class TCPClientServerTest extends TestCase {
public static final int PORT = 33333;
@Override
protected void setUp() throws Exception {
super.setUp();
new Thread(new Runnable() {
@Override
public void run() {
try {
new TcpJsonMultiServer(new WebService(), PORT);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Thread.sleep(1000);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testClient() throws IOException {
WebServiceClientProxy proxy;
proxy = new WebServiceClientProxy(new TcpJsonClient(new Socket("localhost", PORT)));
WebService service = new WebService();
assertEquals(Integer.valueOf(3), proxy.add(1, 2));
assertEquals(Integer.valueOf(3), service.add(1, 2));
assertEquals(proxy.add(1, 2), service.add(1, 2));
B b = new B();
b.setA("ASDF");
b.setB("FDSA");
assertEquals(b, service.echo(b));
assertEquals(b, proxy.echo(b));
proxy.touch();
try {
proxy.addWithException(1, 2);
fail("should not reach here!");
} catch (RpcErrorException e) {
// should happen
} catch (Throwable e) {
fail("shoudl not reach here!");
}
try {
proxy.touchWithException();
fail("should not reach here!");
} catch (RpcErrorException e) {
// should happen
} catch (Throwable e) {
fail("shoudl not reach here!");
}
}
}