Menu

[r11]: / trunk / test / cz / eman / jsonrpc / TCPClientServerTest.java  Maximize  Restore  History

Download this file

109 lines (84 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
package cz.eman.jsonrpc;
import java.io.IOException;
import java.net.InetSocketAddress;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
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;
@Before
@Override
protected void setUp() throws Exception {
super.setUp();
new Thread(new Runnable() {
@Override
public void run() {
try {
new TcpJsonMultiServer(new WebService(), PORT).start();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}).start();
Thread.sleep(1000);
}
@After
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
// @Test
public void _testClientOneConnection() throws IOException {
WebServiceClientProxy proxy;
TcpJsonClient client = new TcpJsonClient(new InetSocketAddress("localhost", PORT));
proxy = new WebServiceClientProxy(client);
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!");
}
}
@Test
public void testClient() throws IOException {
WebServiceClientProxy proxy;
TcpJsonClient client = new TcpJsonClient(new InetSocketAddress("localhost", PORT), false);
proxy = new WebServiceClientProxy(client);
client.connect();
client.close();
client.close();
client.connect();
assertEquals(Integer.valueOf(3), proxy.add(1, 2));
client.close();
assertEquals(Integer.valueOf(3), proxy.add(1, 2));
client.close();
}
}