blob: 74fbfefca38bb1825dd378689f6bb0e324ab5ec2 [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 /**
Mark Sleec98d0502006-09-06 02:42:25 +000097 * DOUBLE TEST
98 */
99 System.out.print("testDouble(5.325098235)");
100 double dub = testClient.testDouble(5.325098235);
101 System.out.print(" = " + dub + "\n");
102
103 /**
Mark Slee95771002006-06-07 06:53:25 +0000104 * STRUCT TEST
105 */
Mark Slee6e536442006-06-30 18:28:50 +0000106 System.out.print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee95771002006-06-07 06:53:25 +0000107 Xtruct out = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000108 out.string_thing = "Zero";
109 out.byte_thing = (byte) 1;
110 out.i32_thing = -3;
111 out.i64_thing = -5;
Mark Slee95771002006-06-07 06:53:25 +0000112 Xtruct in = testClient.testStruct(out);
113 System.out.print(" = {" +
Mark Slee78f58e22006-09-02 04:17:07 +0000114 "\"" + in.string_thing + "\", " +
115 in.byte_thing + ", " +
116 in.i32_thing + ", " +
117 in.i64_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000118
119 /**
120 * NESTED STRUCT TEST
121 */
Mark Slee6e536442006-06-30 18:28:50 +0000122 System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Mark Slee95771002006-06-07 06:53:25 +0000123 Xtruct2 out2 = new Xtruct2();
Mark Slee78f58e22006-09-02 04:17:07 +0000124 out2.byte_thing = (short)1;
Mark Slee95771002006-06-07 06:53:25 +0000125 out2.struct_thing = out;
Mark Slee78f58e22006-09-02 04:17:07 +0000126 out2.i32_thing = 5;
Mark Slee95771002006-06-07 06:53:25 +0000127 Xtruct2 in2 = testClient.testNest(out2);
128 in = in2.struct_thing;
129 System.out.print(" = {" +
Mark Slee78f58e22006-09-02 04:17:07 +0000130 in2.byte_thing + ", {" +
131 "\"" + in.string_thing + "\", " +
132 in.byte_thing + ", " +
133 in.i32_thing + ", " +
134 in.i64_thing + "}, " +
135 in2.i32_thing + "}\n");
Mark Slee95771002006-06-07 06:53:25 +0000136
137 /**
138 * MAP TEST
139 */
Mark Slee78f58e22006-09-02 04:17:07 +0000140 HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000141 for (int i = 0; i < 5; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000142 mapout.put(i, i-10);
Mark Slee95771002006-06-07 06:53:25 +0000143 }
144 System.out.print("testMap({");
145 boolean first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000146 for (int key : mapout.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000147 if (first) {
148 first = false;
149 } else {
150 System.out.print(", ");
151 }
Mark Slee78f58e22006-09-02 04:17:07 +0000152 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000153 }
154 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000155 HashMap<Integer,Integer> mapin = testClient.testMap(mapout);
Mark Slee95771002006-06-07 06:53:25 +0000156 System.out.print(" = {");
157 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000158 for (int key : mapin.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000159 if (first) {
160 first = false;
161 } else {
162 System.out.print(", ");
163 }
Mark Slee78f58e22006-09-02 04:17:07 +0000164 System.out.print(key + " => " + mapout.get(key));
Mark Slee95771002006-06-07 06:53:25 +0000165 }
166 System.out.print("}\n");
167
168 /**
169 * SET TEST
170 */
Mark Slee78f58e22006-09-02 04:17:07 +0000171 HashSet<Integer> setout = new HashSet<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000172 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000173 setout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000174 }
175 System.out.print("testSet({");
176 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000177 for (int elem : setout) {
Mark Slee95771002006-06-07 06:53:25 +0000178 if (first) {
179 first = false;
180 } else {
181 System.out.print(", ");
182 }
Mark Slee78f58e22006-09-02 04:17:07 +0000183 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000184 }
185 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000186 HashSet<Integer> setin = testClient.testSet(setout);
Mark Slee95771002006-06-07 06:53:25 +0000187 System.out.print(" = {");
188 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000189 for (int elem : setin) {
Mark Slee95771002006-06-07 06:53:25 +0000190 if (first) {
191 first = false;
192 } else {
193 System.out.print(", ");
194 }
Mark Slee78f58e22006-09-02 04:17:07 +0000195 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000196 }
197 System.out.print("}\n");
198
199 /**
200 * LIST TEST
201 */
Mark Slee78f58e22006-09-02 04:17:07 +0000202 ArrayList<Integer> listout = new ArrayList<Integer>();
Mark Slee95771002006-06-07 06:53:25 +0000203 for (int i = -2; i < 3; ++i) {
Mark Slee78f58e22006-09-02 04:17:07 +0000204 listout.add(i);
Mark Slee95771002006-06-07 06:53:25 +0000205 }
206 System.out.print("testList({");
207 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000208 for (int elem : listout) {
Mark Slee95771002006-06-07 06:53:25 +0000209 if (first) {
210 first = false;
211 } else {
212 System.out.print(", ");
213 }
Mark Slee78f58e22006-09-02 04:17:07 +0000214 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000215 }
216 System.out.print("})");
Mark Slee78f58e22006-09-02 04:17:07 +0000217 ArrayList<Integer> listin = testClient.testList(listout);
Mark Slee95771002006-06-07 06:53:25 +0000218 System.out.print(" = {");
219 first = true;
Mark Slee78f58e22006-09-02 04:17:07 +0000220 for (int elem : listin) {
Mark Slee95771002006-06-07 06:53:25 +0000221 if (first) {
222 first = false;
223 } else {
224 System.out.print(", ");
225 }
Mark Slee78f58e22006-09-02 04:17:07 +0000226 System.out.print(elem);
Mark Slee95771002006-06-07 06:53:25 +0000227 }
228 System.out.print("}\n");
229
230 /**
231 * ENUM TEST
232 */
233 System.out.print("testEnum(ONE)");
Mark Slee78f58e22006-09-02 04:17:07 +0000234 int ret = testClient.testEnum(Numberz.ONE);
235 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000236
237 System.out.print("testEnum(TWO)");
238 ret = testClient.testEnum(Numberz.TWO);
Mark Slee78f58e22006-09-02 04:17:07 +0000239 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000240
241 System.out.print("testEnum(THREE)");
242 ret = testClient.testEnum(Numberz.THREE);
Mark Slee78f58e22006-09-02 04:17:07 +0000243 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000244
245 System.out.print("testEnum(FIVE)");
246 ret = testClient.testEnum(Numberz.FIVE);
Mark Slee78f58e22006-09-02 04:17:07 +0000247 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000248
249 System.out.print("testEnum(EIGHT)");
250 ret = testClient.testEnum(Numberz.EIGHT);
Mark Slee78f58e22006-09-02 04:17:07 +0000251 System.out.print(" = " + ret + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000252
253 /**
254 * TYPEDEF TEST
255 */
256 System.out.print("testTypedef(309858235082523)");
Mark Slee78f58e22006-09-02 04:17:07 +0000257 long uid = testClient.testTypedef(309858235082523L);
258 System.out.print(" = " + uid + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000259
260 /**
261 * NESTED MAP TEST
262 */
263 System.out.print("testMapMap(1)");
Mark Slee78f58e22006-09-02 04:17:07 +0000264 HashMap<Integer,HashMap<Integer,Integer>> mm =
265 testClient.testMapMap(1);
Mark Slee95771002006-06-07 06:53:25 +0000266 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000267 for (int key : mm.keySet()) {
268 System.out.print(key + " => {");
269 HashMap<Integer,Integer> m2 = mm.get(key);
270 for (int k2 : m2.keySet()) {
271 System.out.print(k2 + " => " + m2.get(k2) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000272 }
273 System.out.print("}, ");
274 }
275 System.out.print("}\n");
276
277 /**
278 * INSANITY TEST
279 */
280 Insanity insane = new Insanity();
Mark Slee78f58e22006-09-02 04:17:07 +0000281 insane.userMap.put(Numberz.FIVE, (long)5000);
Mark Slee95771002006-06-07 06:53:25 +0000282 Xtruct truck = new Xtruct();
Mark Slee78f58e22006-09-02 04:17:07 +0000283 truck.string_thing = "Truck";
284 truck.byte_thing = (byte)8;
285 truck.i32_thing = 8;
286 truck.i64_thing = 8;
Mark Slee95771002006-06-07 06:53:25 +0000287 insane.xtructs.add(truck);
288 System.out.print("testInsanity()");
Mark Slee78f58e22006-09-02 04:17:07 +0000289 HashMap<Long,HashMap<Integer,Insanity>> whoa =
Mark Slee95771002006-06-07 06:53:25 +0000290 testClient.testInsanity(insane);
291 System.out.print(" = {");
Mark Slee78f58e22006-09-02 04:17:07 +0000292 for (long key : whoa.keySet()) {
293 HashMap<Integer,Insanity> val = whoa.get(key);
294 System.out.print(key + " => {");
Mark Slee95771002006-06-07 06:53:25 +0000295
Mark Slee78f58e22006-09-02 04:17:07 +0000296 for (int k2 : val.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000297 Insanity v2 = val.get(k2);
Mark Slee78f58e22006-09-02 04:17:07 +0000298 System.out.print(k2 + " => {");
299 HashMap<Integer, Long> userMap = v2.userMap;
Mark Slee95771002006-06-07 06:53:25 +0000300 System.out.print("{");
Mark Slee78f58e22006-09-02 04:17:07 +0000301 for (int k3 : userMap.keySet()) {
302 System.out.print(k3 + " => " +
303 userMap.get(k3) + ", ");
Mark Slee95771002006-06-07 06:53:25 +0000304 }
305 System.out.print("}, ");
306
307 ArrayList<Xtruct> xtructs = v2.xtructs;
308 System.out.print("{");
309 for (Xtruct x : xtructs) {
310 System.out.print("{" +
Mark Slee78f58e22006-09-02 04:17:07 +0000311 "\"" + x.string_thing + "\", " +
312 x.byte_thing + ", " +
313 x.i32_thing + ", "+
314 x.i64_thing + "}, ");
Mark Slee95771002006-06-07 06:53:25 +0000315 }
316 System.out.print("}");
317
318 System.out.print("}, ");
319 }
320 System.out.print("}, ");
321 }
322 System.out.print("}\n");
323
324 long stop = System.currentTimeMillis();
325 System.out.println("Total time: " + (stop-start) + "ms");
326
327 tSocket.close();
328 }
329
330 } catch (Exception x) {
331 x.printStackTrace();
332 }
333 }
334}