blob: 686920c7aced528d1134b569b77ce1f108b8a563 [file] [log] [blame]
Mark Slee95771002006-06-07 06:53:25 +00001package com.facebook.thrift.test;
2
Mark Slee78f58e22006-09-02 04:17:07 +00003// Generated code
4import thrift.test.*;
5
Mark Slee95771002006-06-07 06:53:25 +00006import com.facebook.thrift.transport.TSocket;
7import com.facebook.thrift.transport.TTransportException;
8import com.facebook.thrift.protocol.TBinaryProtocol;
Mark Slee95771002006-06-07 06:53:25 +00009
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.ArrayList;
13
14/**
15 * Test Java client for thrift. Essentially just a copy of the C++ version,
16 * this makes a variety of requests to enable testing for both performance and
17 * correctness of the output.
18 *
19 * @author Mark Slee <mcslee@facebook.com>
20 */
21public class TestClient {
22 public static void main(String [] args) {
23 try {
24 String host = "localhost";
25 int port = 9090;
26 int numTests = 1;
27
28 if (args.length > 0) {
29 host = args[0];
30 }
31 if (args.length > 1) {
32 port = Integer.valueOf(args[1]);
33 }
34 if (args.length > 2) {
35 numTests = Integer.valueOf(args[2]);
36 }
37
38 TSocket tSocket =
39 new TSocket(host, port);
40 TBinaryProtocol binaryProtocol =
41 new TBinaryProtocol();
Mark Slee78f58e22006-09-02 04:17:07 +000042 ThriftTest.Client testClient =
43 new ThriftTest.Client(tSocket, binaryProtocol);
Mark Slee95771002006-06-07 06:53:25 +000044
45 for (int test = 0; test < numTests; ++test) {
46
47 /**
48 * CONNECT TEST
49 */
50 System.out.println("Test #" + (test+1) + ", " +
51 "connect " + host + ":" + port);
52 try {
53 tSocket.open();
54 } catch (TTransportException ttx) {
55 System.out.println("Connect failed: " + ttx.getMessage());
56 continue;
57 }
58
59 long start = System.currentTimeMillis();
60
61 /**
62 * VOID TEST
63 */
64 System.out.print("testVoid()");
65 testClient.testVoid();
66 System.out.print(" = void\n");
67
68 /**
69 * STRING TEST
70 */
71 System.out.print("testString(\"Test\")");
Mark Slee78f58e22006-09-02 04:17:07 +000072 String s = testClient.testString("Test");
73 System.out.print(" = \"" + s + "\"\n");
74
Mark Slee95771002006-06-07 06:53:25 +000075 /**
76 * BYTE TEST
77 */
78 System.out.print("testByte(1)");
Mark Slee78f58e22006-09-02 04:17:07 +000079 byte i8 = testClient.testByte((byte)1);
80 System.out.print(" = " + i8 + "\n");
Mark Slee6e536442006-06-30 18:28:50 +000081
Mark Slee95771002006-06-07 06:53:25 +000082 /**
83 * I32 TEST
84 */
85 System.out.print("testI32(-1)");
Mark Slee78f58e22006-09-02 04:17:07 +000086 int i32 = testClient.testI32(-1);
87 System.out.print(" = " + i32 + "\n");
Mark Slee95771002006-06-07 06:53:25 +000088
89 /**
Mark Slee95771002006-06-07 06:53:25 +000090 * I64 TEST
91 */
92 System.out.print("testI64(-34359738368)");
Mark Slee78f58e22006-09-02 04:17:07 +000093 long i64 = testClient.testI64(-34359738368L);
94 System.out.print(" = " + i64 + "\n");
Mark Slee95771002006-06-07 06:53:25 +000095
96 /**
97 * STRUCT TEST
98 */
Mark Slee6e536442006-06-30 18:28:50 +000099 System.out.print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee95771002006-06-07 06:53:25 +0000100 Xtruct out = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000101 out.string_thing = "Zero";
102 out.byte_thing = (byte) 1;
103 out.i32_thing = -3;
104 out.i64_thing = -5;
Mark Slee95771002006-06-07 06:53:25 +0000105 Xtruct in = testClient.testStruct(out);
106 System.out.print(" = {" +
Mark Slee78f58e22006-09-02 04:17:07 +0000107 "\"" + in.string_thing + "\", " +
108 in.byte_thing + ", " +
109 in.i32_thing + ", " +
110 in.i64_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000111
112 /**
113 * NESTED STRUCT TEST
114 */
Mark Slee6e536442006-06-30 18:28:50 +0000115 System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Slee95771002006-06-07 06:53:25 +0000116 Xtruct2 out2 = new Xtruct2();
Mark Slee78f58e22006-09-02 04:17:07 +0000117 out2.byte_thing = (short)1;
Mark Slee95771002006-06-07 06:53:25 +0000118 out2.struct_thing = out;
Mark Slee78f58e22006-09-02 04:17:07 +0000119 out2.i32_thing = 5;
Mark Slee95771002006-06-07 06:53:25 +0000120 Xtruct2 in2 = testClient.testNest(out2);
121 in = in2.struct_thing;
122 System.out.print(" = {" +
Mark Slee78f58e22006-09-02 04:17:07 +0000123 in2.byte_thing + ", {" +
124 "\"" + in.string_thing + "\", " +
125 in.byte_thing + ", " +
126 in.i32_thing + ", " +
127 in.i64_thing + "}, " +
128 in2.i32_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000129
130 /**
131 * MAP TEST
132 */
Mark Slee78f58e22006-09-02 04:17:07 +0000133 HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000134 for (int i = 0; i < 5; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000135 mapout.put(i, i-10);
Mark Slee95771002006-06-07 06:53:25 +0000136 }
137 System.out.print("testMap({");
138 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000139 for (int key : mapout.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000140 if (first) {
141 first = false;
142 } else {
143 System.out.print(", ");
144 }
Mark Slee78f58e22006-09-02 04:17:07 +0000145 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000146 }
147 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000148 HashMap<Integer,Integer> mapin = testClient.testMap(mapout);
Mark Slee95771002006-06-07 06:53:25 +0000149 System.out.print(" = {");
150 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000151 for (int key : mapin.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000152 if (first) {
153 first = false;
154 } else {
155 System.out.print(", ");
156 }
Mark Slee78f58e22006-09-02 04:17:07 +0000157 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000158 }
159 System.out.print("}\n");
160
161 /**
162 * SET TEST
163 */
Mark Slee78f58e22006-09-02 04:17:07 +0000164 HashSet<Integer> setout = new HashSet<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000165 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000166 setout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000167 }
168 System.out.print("testSet({");
169 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000170 for (int elem : setout) {
Mark Slee95771002006-06-07 06:53:25 +0000171 if (first) {
172 first = false;
173 } else {
174 System.out.print(", ");
175 }
Mark Slee78f58e22006-09-02 04:17:07 +0000176 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000177 }
178 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000179 HashSet<Integer> setin = testClient.testSet(setout);
Mark Slee95771002006-06-07 06:53:25 +0000180 System.out.print(" = {");
181 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000182 for (int elem : setin) {
Mark Slee95771002006-06-07 06:53:25 +0000183 if (first) {
184 first = false;
185 } else {
186 System.out.print(", ");
187 }
Mark Slee78f58e22006-09-02 04:17:07 +0000188 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000189 }
190 System.out.print("}\n");
191
192 /**
193 * LIST TEST
194 */
Mark Slee78f58e22006-09-02 04:17:07 +0000195 ArrayList<Integer> listout = new ArrayList<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000196 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000197 listout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000198 }
199 System.out.print("testList({");
200 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000201 for (int elem : listout) {
Mark Slee95771002006-06-07 06:53:25 +0000202 if (first) {
203 first = false;
204 } else {
205 System.out.print(", ");
206 }
Mark Slee78f58e22006-09-02 04:17:07 +0000207 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000208 }
209 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000210 ArrayList<Integer> listin = testClient.testList(listout);
Mark Slee95771002006-06-07 06:53:25 +0000211 System.out.print(" = {");
212 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000213 for (int elem : listin) {
Mark Slee95771002006-06-07 06:53:25 +0000214 if (first) {
215 first = false;
216 } else {
217 System.out.print(", ");
218 }
Mark Slee78f58e22006-09-02 04:17:07 +0000219 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000220 }
221 System.out.print("}\n");
222
223 /**
224 * ENUM TEST
225 */
226 System.out.print("testEnum(ONE)");
Mark Slee78f58e22006-09-02 04:17:07 +0000227 int ret = testClient.testEnum(Numberz.ONE);
228 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000229
230 System.out.print("testEnum(TWO)");
231 ret = testClient.testEnum(Numberz.TWO);
Mark Slee78f58e22006-09-02 04:17:07 +0000232 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000233
234 System.out.print("testEnum(THREE)");
235 ret = testClient.testEnum(Numberz.THREE);
Mark Slee78f58e22006-09-02 04:17:07 +0000236 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000237
238 System.out.print("testEnum(FIVE)");
239 ret = testClient.testEnum(Numberz.FIVE);
Mark Slee78f58e22006-09-02 04:17:07 +0000240 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000241
242 System.out.print("testEnum(EIGHT)");
243 ret = testClient.testEnum(Numberz.EIGHT);
Mark Slee78f58e22006-09-02 04:17:07 +0000244 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000245
246 /**
247 * TYPEDEF TEST
248 */
249 System.out.print("testTypedef(309858235082523)");
Mark Slee78f58e22006-09-02 04:17:07 +0000250 long uid = testClient.testTypedef(309858235082523L);
251 System.out.print(" = " + uid + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000252
253 /**
254 * NESTED MAP TEST
255 */
256 System.out.print("testMapMap(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000257 HashMap<Integer,HashMap<Integer,Integer>> mm =
258 testClient.testMapMap(1);
Mark Slee95771002006-06-07 06:53:25 +0000259 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000260 for (int key : mm.keySet()) {
261 System.out.print(key + " => {");
262 HashMap<Integer,Integer> m2 = mm.get(key);
263 for (int k2 : m2.keySet()) {
264 System.out.print(k2 + " => " + m2.get(k2) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000265 }
266 System.out.print("}, ");
267 }
268 System.out.print("}\n");
269
270 /**
271 * INSANITY TEST
272 */
273 Insanity insane = new Insanity();
Mark Slee78f58e22006-09-02 04:17:07 +0000274 insane.userMap.put(Numberz.FIVE, (long)5000);
Mark Slee95771002006-06-07 06:53:25 +0000275 Xtruct truck = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000276 truck.string_thing = "Truck";
277 truck.byte_thing = (byte)8;
278 truck.i32_thing = 8;
279 truck.i64_thing = 8;
Mark Slee95771002006-06-07 06:53:25 +0000280 insane.xtructs.add(truck);
281 System.out.print("testInsanity()");
Mark Slee78f58e22006-09-02 04:17:07 +0000282 HashMap<Long,HashMap<Integer,Insanity>> whoa =
Mark Slee95771002006-06-07 06:53:25 +0000283 testClient.testInsanity(insane);
284 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000285 for (long key : whoa.keySet()) {
286 HashMap<Integer,Insanity> val = whoa.get(key);
287 System.out.print(key + " => {");
Mark Slee95771002006-06-07 06:53:25 +0000288
Mark Slee78f58e22006-09-02 04:17:07 +0000289 for (int k2 : val.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000290 Insanity v2 = val.get(k2);
Mark Slee78f58e22006-09-02 04:17:07 +0000291 System.out.print(k2 + " => {");
292 HashMap<Integer, Long> userMap = v2.userMap;
Mark Slee95771002006-06-07 06:53:25 +0000293 System.out.print("{");
Mark Slee78f58e22006-09-02 04:17:07 +0000294 for (int k3 : userMap.keySet()) {
295 System.out.print(k3 + " => " +
296 userMap.get(k3) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000297 }
298 System.out.print("}, ");
299
300 ArrayList<Xtruct> xtructs = v2.xtructs;
301 System.out.print("{");
302 for (Xtruct x : xtructs) {
303 System.out.print("{" +
Mark Slee78f58e22006-09-02 04:17:07 +0000304 "\"" + x.string_thing + "\", " +
305 x.byte_thing + ", " +
306 x.i32_thing + ", "+
307 x.i64_thing + "}, ");
Mark Slee95771002006-06-07 06:53:25 +0000308 }
309 System.out.print("}");
310
311 System.out.print("}, ");
312 }
313 System.out.print("}, ");
314 }
315 System.out.print("}\n");
316
317 long stop = System.currentTimeMillis();
318 System.out.println("Total time: " + (stop-start) + "ms");
319
320 tSocket.close();
321 }
322
323 } catch (Exception x) {
324 x.printStackTrace();
325 }
326 }
327}