blob: cbcd8021f640c419d2b1b8682955a2309800ae2f [file] [log] [blame]
Mark Slee95771002006-06-07 06:53:25 +00001package com.facebook.thrift.test;
2
3import ThriftTest.*;
4import com.facebook.thrift.types.*;
5import com.facebook.thrift.transport.TSocket;
6import com.facebook.thrift.transport.TTransportException;
7import com.facebook.thrift.protocol.TBinaryProtocol;
8import com.facebook.thrift.protocol.TString;
9
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();
42 ThriftTestClient testClient =
43 new ThriftTestClient(tSocket, binaryProtocol);
44
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\")");
72 TString s = testClient.testString(new TString("Test"));
73 System.out.print(" = \"" + s.value + "\"\n");
74
75 /**
76 * BYTE TEST
77 */
78 System.out.print("testByte(1)");
79 UInt8 u8 = testClient.testByte(new UInt8((short)1));
80 System.out.print(" = " + u8.get() + "\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)");
86 Int32 i32 = testClient.testI32(new Int32(-1));
87 System.out.print(" = " + i32.get() + "\n");
88
89 /**
Mark Slee95771002006-06-07 06:53:25 +000090 * I64 TEST
91 */
92 System.out.print("testI64(-34359738368)");
93 Int64 i64 = testClient.testI64(new Int64(-34359738368L));
94 System.out.print(" = " + i64.get() + "\n");
95
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();
101 out.string_thing.value = "Zero";
102 out.byte_thing.set((short)1);
Mark Slee95771002006-06-07 06:53:25 +0000103 out.i32_thing.set(-3);
Mark Slee95771002006-06-07 06:53:25 +0000104 out.i64_thing.set(-5);
105 Xtruct in = testClient.testStruct(out);
106 System.out.print(" = {" +
107 "\"" + in.string_thing.value + "\", " +
108 in.byte_thing.get() + ", " +
Mark Slee95771002006-06-07 06:53:25 +0000109 in.i32_thing.get() + ", " +
Mark Slee95771002006-06-07 06:53:25 +0000110 in.i64_thing.get() + "}\n");
111
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();
117 out2.byte_thing.set((short)1);
118 out2.struct_thing = out;
119 out2.i32_thing.set(5);
120 Xtruct2 in2 = testClient.testNest(out2);
121 in = in2.struct_thing;
122 System.out.print(" = {" +
123 in2.byte_thing.get() + ", {" +
124 "\"" + in.string_thing.value + "\", " +
125 in.byte_thing.get() + ", " +
Mark Slee95771002006-06-07 06:53:25 +0000126 in.i32_thing.get() + ", " +
Mark Slee95771002006-06-07 06:53:25 +0000127 in.i64_thing.get() + "}, " +
128 in2.i32_thing.get() + "}\n");
129
130 /**
131 * MAP TEST
132 */
133 HashMap<Int32,Int32> mapout = new HashMap<Int32,Int32>();
134 for (int i = 0; i < 5; ++i) {
135 mapout.put(new Int32(i), new Int32(i-10));
136 }
137 System.out.print("testMap({");
138 boolean first = true;
139 for (Int32 key : mapout.keySet()) {
140 if (first) {
141 first = false;
142 } else {
143 System.out.print(", ");
144 }
145 System.out.print(key.get() + " => " + mapout.get(key).get());
146 }
147 System.out.print("})");
148 HashMap<Int32,Int32> mapin = testClient.testMap(mapout);
149 System.out.print(" = {");
150 first = true;
151 for (Int32 key : mapin.keySet()) {
152 if (first) {
153 first = false;
154 } else {
155 System.out.print(", ");
156 }
157 System.out.print(key.get() + " => " + mapout.get(key).get());
158 }
159 System.out.print("}\n");
160
161 /**
162 * SET TEST
163 */
164 HashSet<Int32> setout = new HashSet<Int32>();
165 for (int i = -2; i < 3; ++i) {
166 setout.add(new Int32(i));
167 }
168 System.out.print("testSet({");
169 first = true;
170 for (Int32 elem : setout) {
171 if (first) {
172 first = false;
173 } else {
174 System.out.print(", ");
175 }
176 System.out.print(elem.get());
177 }
178 System.out.print("})");
179 HashSet<Int32> setin = testClient.testSet(setout);
180 System.out.print(" = {");
181 first = true;
182 for (Int32 elem : setin) {
183 if (first) {
184 first = false;
185 } else {
186 System.out.print(", ");
187 }
188 System.out.print(elem.get());
189 }
190 System.out.print("}\n");
191
192 /**
193 * LIST TEST
194 */
195 ArrayList<Int32> listout = new ArrayList<Int32>();
196 for (int i = -2; i < 3; ++i) {
197 listout.add(new Int32(i));
198 }
199 System.out.print("testList({");
200 first = true;
201 for (Int32 elem : listout) {
202 if (first) {
203 first = false;
204 } else {
205 System.out.print(", ");
206 }
207 System.out.print(elem.get());
208 }
209 System.out.print("})");
210 ArrayList<Int32> listin = testClient.testList(listout);
211 System.out.print(" = {");
212 first = true;
213 for (Int32 elem : listin) {
214 if (first) {
215 first = false;
216 } else {
217 System.out.print(", ");
218 }
219 System.out.print(elem.get());
220 }
221 System.out.print("}\n");
222
223 /**
224 * ENUM TEST
225 */
226 System.out.print("testEnum(ONE)");
227 Int32 ret = testClient.testEnum(Numberz.ONE);
228 System.out.print(" = " + ret.get() + "\n");
229
230 System.out.print("testEnum(TWO)");
231 ret = testClient.testEnum(Numberz.TWO);
232 System.out.print(" = " + ret.get() + "\n");
233
234 System.out.print("testEnum(THREE)");
235 ret = testClient.testEnum(Numberz.THREE);
236 System.out.print(" = " + ret.get() + "\n");
237
238 System.out.print("testEnum(FIVE)");
239 ret = testClient.testEnum(Numberz.FIVE);
240 System.out.print(" = " + ret.get() + "\n");
241
242 System.out.print("testEnum(EIGHT)");
243 ret = testClient.testEnum(Numberz.EIGHT);
244 System.out.print(" = " + ret.get() + "\n");
245
246 /**
247 * TYPEDEF TEST
248 */
249 System.out.print("testTypedef(309858235082523)");
Mark Slee6e536442006-06-30 18:28:50 +0000250 Int64 uid = testClient.testTypedef(new Int64(309858235082523L));
251 System.out.print(" = " + uid.get() + "\n");
Mark Slee95771002006-06-07 06:53:25 +0000252
253 /**
254 * NESTED MAP TEST
255 */
256 System.out.print("testMapMap(1)");
257 HashMap<Int32,HashMap<Int32,Int32>> mm =
258 testClient.testMapMap(new Int32(1));
259 System.out.print(" = {");
260 for (Int32 key : mm.keySet()) {
261 System.out.print(key.get() + " => {");
262 HashMap<Int32,Int32> m2 = mm.get(key);
263 for (Int32 k2 : m2.keySet()) {
264 System.out.print(k2.get() + " => " + m2.get(k2).get() + ", ");
265 }
266 System.out.print("}, ");
267 }
268 System.out.print("}\n");
269
270 /**
271 * INSANITY TEST
272 */
273 Insanity insane = new Insanity();
Mark Slee6e536442006-06-30 18:28:50 +0000274 insane.userMap.put(Numberz.FIVE, new Int64(5000));
Mark Slee95771002006-06-07 06:53:25 +0000275 Xtruct truck = new Xtruct();
276 truck.string_thing.value = "Truck";
277 truck.byte_thing.set((short)8);
Mark Slee95771002006-06-07 06:53:25 +0000278 truck.i32_thing.set(8);
Mark Slee95771002006-06-07 06:53:25 +0000279 truck.i64_thing.set(8);
280 insane.xtructs.add(truck);
281 System.out.print("testInsanity()");
Mark Slee6e536442006-06-30 18:28:50 +0000282 HashMap<Int64,HashMap<Int32,Insanity>> whoa =
Mark Slee95771002006-06-07 06:53:25 +0000283 testClient.testInsanity(insane);
284 System.out.print(" = {");
Mark Slee6e536442006-06-30 18:28:50 +0000285 for (Int64 key : whoa.keySet()) {
Mark Slee95771002006-06-07 06:53:25 +0000286 HashMap<Int32,Insanity> val = whoa.get(key);
Mark Slee6e536442006-06-30 18:28:50 +0000287 System.out.print(key.get() + " => {");
Mark Slee95771002006-06-07 06:53:25 +0000288
289 for (Int32 k2 : val.keySet()) {
290 Insanity v2 = val.get(k2);
291 System.out.print(k2.get() + " => {");
Mark Slee6e536442006-06-30 18:28:50 +0000292 HashMap<Int32, Int64> userMap = v2.userMap;
Mark Slee95771002006-06-07 06:53:25 +0000293 System.out.print("{");
294 for (Int32 k3 : userMap.keySet()) {
295 System.out.print(k3.get() + " => " +
Mark Slee6e536442006-06-30 18:28:50 +0000296 userMap.get(k3).get() + ", ");
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("{" +
304 "\"" + x.string_thing.value + "\", " +
305 x.byte_thing.get() + ", " +
Mark Slee95771002006-06-07 06:53:25 +0000306 x.i32_thing.get() + ", "+
Mark Slee95771002006-06-07 06:53:25 +0000307 x.i64_thing.get() + "}, ");
308 }
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}