Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 1 | package com.facebook.thrift.test; |
| 2 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3 | // Generated code |
| 4 | import thrift.test.*; |
| 5 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 6 | import com.facebook.thrift.transport.TSocket; |
| 7 | import com.facebook.thrift.transport.TTransportException; |
| 8 | import com.facebook.thrift.protocol.TBinaryProtocol; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 9 | |
| 10 | import java.util.HashMap; |
| 11 | import java.util.HashSet; |
| 12 | import 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 | */ |
| 21 | public 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 Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 42 | ThriftTest.Client testClient = |
| 43 | new ThriftTest.Client(tSocket, binaryProtocol); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 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\")"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 72 | String s = testClient.testString("Test"); |
| 73 | System.out.print(" = \"" + s + "\"\n"); |
| 74 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 75 | /** |
| 76 | * BYTE TEST |
| 77 | */ |
| 78 | System.out.print("testByte(1)"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 79 | byte i8 = testClient.testByte((byte)1); |
| 80 | System.out.print(" = " + i8 + "\n"); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 81 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 82 | /** |
| 83 | * I32 TEST |
| 84 | */ |
| 85 | System.out.print("testI32(-1)"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 86 | int i32 = testClient.testI32(-1); |
| 87 | System.out.print(" = " + i32 + "\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 90 | * I64 TEST |
| 91 | */ |
| 92 | System.out.print("testI64(-34359738368)"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 93 | long i64 = testClient.testI64(-34359738368L); |
| 94 | System.out.print(" = " + i64 + "\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame^] | 97 | * 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 Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 104 | * STRUCT TEST |
| 105 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 106 | System.out.print("testStruct({\"Zero\", 1, -3, -5})"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 107 | Xtruct out = new Xtruct(); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 108 | out.string_thing = "Zero"; |
| 109 | out.byte_thing = (byte) 1; |
| 110 | out.i32_thing = -3; |
| 111 | out.i64_thing = -5; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 112 | Xtruct in = testClient.testStruct(out); |
| 113 | System.out.print(" = {" + |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 114 | "\"" + in.string_thing + "\", " + |
| 115 | in.byte_thing + ", " + |
| 116 | in.i32_thing + ", " + |
| 117 | in.i64_thing + "}\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * NESTED STRUCT TEST |
| 121 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 122 | System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 123 | Xtruct2 out2 = new Xtruct2(); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 124 | out2.byte_thing = (short)1; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 125 | out2.struct_thing = out; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 126 | out2.i32_thing = 5; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 127 | Xtruct2 in2 = testClient.testNest(out2); |
| 128 | in = in2.struct_thing; |
| 129 | System.out.print(" = {" + |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 130 | 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 Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * MAP TEST |
| 139 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 140 | HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>(); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 141 | for (int i = 0; i < 5; ++i) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 142 | mapout.put(i, i-10); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 143 | } |
| 144 | System.out.print("testMap({"); |
| 145 | boolean first = true; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 146 | for (int key : mapout.keySet()) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 147 | if (first) { |
| 148 | first = false; |
| 149 | } else { |
| 150 | System.out.print(", "); |
| 151 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 152 | System.out.print(key + " => " + mapout.get(key)); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 153 | } |
| 154 | System.out.print("})"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 155 | HashMap<Integer,Integer> mapin = testClient.testMap(mapout); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 156 | System.out.print(" = {"); |
| 157 | first = true; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 158 | for (int key : mapin.keySet()) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 159 | if (first) { |
| 160 | first = false; |
| 161 | } else { |
| 162 | System.out.print(", "); |
| 163 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 164 | System.out.print(key + " => " + mapout.get(key)); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 165 | } |
| 166 | System.out.print("}\n"); |
| 167 | |
| 168 | /** |
| 169 | * SET TEST |
| 170 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 171 | HashSet<Integer> setout = new HashSet<Integer>(); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 172 | for (int i = -2; i < 3; ++i) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 173 | setout.add(i); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 174 | } |
| 175 | System.out.print("testSet({"); |
| 176 | first = true; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 177 | for (int elem : setout) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 178 | if (first) { |
| 179 | first = false; |
| 180 | } else { |
| 181 | System.out.print(", "); |
| 182 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 183 | System.out.print(elem); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 184 | } |
| 185 | System.out.print("})"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 186 | HashSet<Integer> setin = testClient.testSet(setout); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 187 | System.out.print(" = {"); |
| 188 | first = true; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 189 | for (int elem : setin) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 190 | if (first) { |
| 191 | first = false; |
| 192 | } else { |
| 193 | System.out.print(", "); |
| 194 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 195 | System.out.print(elem); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 196 | } |
| 197 | System.out.print("}\n"); |
| 198 | |
| 199 | /** |
| 200 | * LIST TEST |
| 201 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 202 | ArrayList<Integer> listout = new ArrayList<Integer>(); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 203 | for (int i = -2; i < 3; ++i) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 204 | listout.add(i); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 205 | } |
| 206 | System.out.print("testList({"); |
| 207 | first = true; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 208 | for (int elem : listout) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 209 | if (first) { |
| 210 | first = false; |
| 211 | } else { |
| 212 | System.out.print(", "); |
| 213 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 214 | System.out.print(elem); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 215 | } |
| 216 | System.out.print("})"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 217 | ArrayList<Integer> listin = testClient.testList(listout); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 218 | System.out.print(" = {"); |
| 219 | first = true; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 220 | for (int elem : listin) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 221 | if (first) { |
| 222 | first = false; |
| 223 | } else { |
| 224 | System.out.print(", "); |
| 225 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 226 | System.out.print(elem); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 227 | } |
| 228 | System.out.print("}\n"); |
| 229 | |
| 230 | /** |
| 231 | * ENUM TEST |
| 232 | */ |
| 233 | System.out.print("testEnum(ONE)"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 234 | int ret = testClient.testEnum(Numberz.ONE); |
| 235 | System.out.print(" = " + ret + "\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 236 | |
| 237 | System.out.print("testEnum(TWO)"); |
| 238 | ret = testClient.testEnum(Numberz.TWO); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 239 | System.out.print(" = " + ret + "\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 240 | |
| 241 | System.out.print("testEnum(THREE)"); |
| 242 | ret = testClient.testEnum(Numberz.THREE); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 243 | System.out.print(" = " + ret + "\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 244 | |
| 245 | System.out.print("testEnum(FIVE)"); |
| 246 | ret = testClient.testEnum(Numberz.FIVE); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 247 | System.out.print(" = " + ret + "\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 248 | |
| 249 | System.out.print("testEnum(EIGHT)"); |
| 250 | ret = testClient.testEnum(Numberz.EIGHT); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 251 | System.out.print(" = " + ret + "\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * TYPEDEF TEST |
| 255 | */ |
| 256 | System.out.print("testTypedef(309858235082523)"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 257 | long uid = testClient.testTypedef(309858235082523L); |
| 258 | System.out.print(" = " + uid + "\n"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 259 | |
| 260 | /** |
| 261 | * NESTED MAP TEST |
| 262 | */ |
| 263 | System.out.print("testMapMap(1)"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 264 | HashMap<Integer,HashMap<Integer,Integer>> mm = |
| 265 | testClient.testMapMap(1); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 266 | System.out.print(" = {"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 267 | 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 Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 272 | } |
| 273 | System.out.print("}, "); |
| 274 | } |
| 275 | System.out.print("}\n"); |
| 276 | |
| 277 | /** |
| 278 | * INSANITY TEST |
| 279 | */ |
| 280 | Insanity insane = new Insanity(); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 281 | insane.userMap.put(Numberz.FIVE, (long)5000); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 282 | Xtruct truck = new Xtruct(); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 283 | truck.string_thing = "Truck"; |
| 284 | truck.byte_thing = (byte)8; |
| 285 | truck.i32_thing = 8; |
| 286 | truck.i64_thing = 8; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 287 | insane.xtructs.add(truck); |
| 288 | System.out.print("testInsanity()"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 289 | HashMap<Long,HashMap<Integer,Insanity>> whoa = |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 290 | testClient.testInsanity(insane); |
| 291 | System.out.print(" = {"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 292 | for (long key : whoa.keySet()) { |
| 293 | HashMap<Integer,Insanity> val = whoa.get(key); |
| 294 | System.out.print(key + " => {"); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 295 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 296 | for (int k2 : val.keySet()) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 297 | Insanity v2 = val.get(k2); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 298 | System.out.print(k2 + " => {"); |
| 299 | HashMap<Integer, Long> userMap = v2.userMap; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 300 | System.out.print("{"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 301 | for (int k3 : userMap.keySet()) { |
| 302 | System.out.print(k3 + " => " + |
| 303 | userMap.get(k3) + ", "); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 304 | } |
| 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 Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 311 | "\"" + x.string_thing + "\", " + |
| 312 | x.byte_thing + ", " + |
| 313 | x.i32_thing + ", "+ |
| 314 | x.i64_thing + "}, "); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 315 | } |
| 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 | } |