blob: 70cf826b7a013e469df12091cb2b952878418ccb [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");
81
82 /**
83 * U32 TEST
84 */
85 System.out.print("testU32(1)");
86 UInt32 u32 = testClient.testU32(new UInt32(1));
87 System.out.print(" = " + u32.get() + "\n");
88
89 /**
90 * I32 TEST
91 */
92 System.out.print("testI32(-1)");
93 Int32 i32 = testClient.testI32(new Int32(-1));
94 System.out.print(" = " + i32.get() + "\n");
95
96 /**
97 * U64 TEST
98 */
99 System.out.print("testU64(34359738368)");
100 UInt64 u64 = testClient.testU64(new UInt64(34359738368L));
101 System.out.print(" = " + u64.toLong() + "\n");
102
103 /**
104 * I64 TEST
105 */
106 System.out.print("testI64(-34359738368)");
107 Int64 i64 = testClient.testI64(new Int64(-34359738368L));
108 System.out.print(" = " + i64.get() + "\n");
109
110 /**
111 * STRUCT TEST
112 */
113 System.out.print("testStruct({\"Zero\", 1, 2, -3, 4, -5})");
114 Xtruct out = new Xtruct();
115 out.string_thing.value = "Zero";
116 out.byte_thing.set((short)1);
117 out.u32_thing.set(2);
118 out.i32_thing.set(-3);
119 out.u64_thing.set(4);
120 out.i64_thing.set(-5);
121 Xtruct in = testClient.testStruct(out);
122 System.out.print(" = {" +
123 "\"" + in.string_thing.value + "\", " +
124 in.byte_thing.get() + ", " +
125 in.u32_thing.get() + ", " +
126 in.i32_thing.get() + ", " +
127 in.u64_thing.toLong() + ", " +
128 in.i64_thing.get() + "}\n");
129
130 /**
131 * NESTED STRUCT TEST
132 */
133 System.out.print("testNest({1, {\"Zero\", 1, 2, -3, 4, -5}), 5}");
134 Xtruct2 out2 = new Xtruct2();
135 out2.byte_thing.set((short)1);
136 out2.struct_thing = out;
137 out2.i32_thing.set(5);
138 Xtruct2 in2 = testClient.testNest(out2);
139 in = in2.struct_thing;
140 System.out.print(" = {" +
141 in2.byte_thing.get() + ", {" +
142 "\"" + in.string_thing.value + "\", " +
143 in.byte_thing.get() + ", " +
144 in.u32_thing.get() + ", " +
145 in.i32_thing.get() + ", " +
146 in.u64_thing.toLong() + ", " +
147 in.i64_thing.get() + "}, " +
148 in2.i32_thing.get() + "}\n");
149
150 /**
151 * MAP TEST
152 */
153 HashMap<Int32,Int32> mapout = new HashMap<Int32,Int32>();
154 for (int i = 0; i < 5; ++i) {
155 mapout.put(new Int32(i), new Int32(i-10));
156 }
157 System.out.print("testMap({");
158 boolean first = true;
159 for (Int32 key : mapout.keySet()) {
160 if (first) {
161 first = false;
162 } else {
163 System.out.print(", ");
164 }
165 System.out.print(key.get() + " => " + mapout.get(key).get());
166 }
167 System.out.print("})");
168 HashMap<Int32,Int32> mapin = testClient.testMap(mapout);
169 System.out.print(" = {");
170 first = true;
171 for (Int32 key : mapin.keySet()) {
172 if (first) {
173 first = false;
174 } else {
175 System.out.print(", ");
176 }
177 System.out.print(key.get() + " => " + mapout.get(key).get());
178 }
179 System.out.print("}\n");
180
181 /**
182 * SET TEST
183 */
184 HashSet<Int32> setout = new HashSet<Int32>();
185 for (int i = -2; i < 3; ++i) {
186 setout.add(new Int32(i));
187 }
188 System.out.print("testSet({");
189 first = true;
190 for (Int32 elem : setout) {
191 if (first) {
192 first = false;
193 } else {
194 System.out.print(", ");
195 }
196 System.out.print(elem.get());
197 }
198 System.out.print("})");
199 HashSet<Int32> setin = testClient.testSet(setout);
200 System.out.print(" = {");
201 first = true;
202 for (Int32 elem : setin) {
203 if (first) {
204 first = false;
205 } else {
206 System.out.print(", ");
207 }
208 System.out.print(elem.get());
209 }
210 System.out.print("}\n");
211
212 /**
213 * LIST TEST
214 */
215 ArrayList<Int32> listout = new ArrayList<Int32>();
216 for (int i = -2; i < 3; ++i) {
217 listout.add(new Int32(i));
218 }
219 System.out.print("testList({");
220 first = true;
221 for (Int32 elem : listout) {
222 if (first) {
223 first = false;
224 } else {
225 System.out.print(", ");
226 }
227 System.out.print(elem.get());
228 }
229 System.out.print("})");
230 ArrayList<Int32> listin = testClient.testList(listout);
231 System.out.print(" = {");
232 first = true;
233 for (Int32 elem : listin) {
234 if (first) {
235 first = false;
236 } else {
237 System.out.print(", ");
238 }
239 System.out.print(elem.get());
240 }
241 System.out.print("}\n");
242
243 /**
244 * ENUM TEST
245 */
246 System.out.print("testEnum(ONE)");
247 Int32 ret = testClient.testEnum(Numberz.ONE);
248 System.out.print(" = " + ret.get() + "\n");
249
250 System.out.print("testEnum(TWO)");
251 ret = testClient.testEnum(Numberz.TWO);
252 System.out.print(" = " + ret.get() + "\n");
253
254 System.out.print("testEnum(THREE)");
255 ret = testClient.testEnum(Numberz.THREE);
256 System.out.print(" = " + ret.get() + "\n");
257
258 System.out.print("testEnum(FIVE)");
259 ret = testClient.testEnum(Numberz.FIVE);
260 System.out.print(" = " + ret.get() + "\n");
261
262 System.out.print("testEnum(EIGHT)");
263 ret = testClient.testEnum(Numberz.EIGHT);
264 System.out.print(" = " + ret.get() + "\n");
265
266 /**
267 * TYPEDEF TEST
268 */
269 System.out.print("testTypedef(309858235082523)");
270 UInt64 uid = testClient.testTypedef(new UInt64(309858235082523L));
271 System.out.print(" = " + uid.toLong() + "\n");
272
273 /**
274 * NESTED MAP TEST
275 */
276 System.out.print("testMapMap(1)");
277 HashMap<Int32,HashMap<Int32,Int32>> mm =
278 testClient.testMapMap(new Int32(1));
279 System.out.print(" = {");
280 for (Int32 key : mm.keySet()) {
281 System.out.print(key.get() + " => {");
282 HashMap<Int32,Int32> m2 = mm.get(key);
283 for (Int32 k2 : m2.keySet()) {
284 System.out.print(k2.get() + " => " + m2.get(k2).get() + ", ");
285 }
286 System.out.print("}, ");
287 }
288 System.out.print("}\n");
289
290 /**
291 * INSANITY TEST
292 */
293 Insanity insane = new Insanity();
294 insane.userMap.put(Numberz.FIVE, new UInt64(5000));
295 Xtruct truck = new Xtruct();
296 truck.string_thing.value = "Truck";
297 truck.byte_thing.set((short)8);
298 truck.u32_thing.set(8);
299 truck.i32_thing.set(8);
300 truck.u64_thing.set(8);
301 truck.i64_thing.set(8);
302 insane.xtructs.add(truck);
303 System.out.print("testInsanity()");
304 HashMap<UInt64,HashMap<Int32,Insanity>> whoa =
305 testClient.testInsanity(insane);
306 System.out.print(" = {");
307 for (UInt64 key : whoa.keySet()) {
308 HashMap<Int32,Insanity> val = whoa.get(key);
309 System.out.print(key.toLong() + " => {");
310
311 for (Int32 k2 : val.keySet()) {
312 Insanity v2 = val.get(k2);
313 System.out.print(k2.get() + " => {");
314 HashMap<Int32, UInt64> userMap = v2.userMap;
315 System.out.print("{");
316 for (Int32 k3 : userMap.keySet()) {
317 System.out.print(k3.get() + " => " +
318 userMap.get(k3).toLong() + ", ");
319 }
320 System.out.print("}, ");
321
322 ArrayList<Xtruct> xtructs = v2.xtructs;
323 System.out.print("{");
324 for (Xtruct x : xtructs) {
325 System.out.print("{" +
326 "\"" + x.string_thing.value + "\", " +
327 x.byte_thing.get() + ", " +
328 x.u32_thing.get() + ", "+
329 x.i32_thing.get() + ", "+
330 x.u64_thing.toLong() + ", "+
331 x.i64_thing.get() + "}, ");
332 }
333 System.out.print("}");
334
335 System.out.print("}, ");
336 }
337 System.out.print("}, ");
338 }
339 System.out.print("}\n");
340
341 long stop = System.currentTimeMillis();
342 System.out.println("Total time: " + (stop-start) + "ms");
343
344 tSocket.close();
345 }
346
347 } catch (Exception x) {
348 x.printStackTrace();
349 }
350 }
351}