blob: 4177765b82e82c837577a543d4e77fffec6a3903 [file] [log] [blame]
David Reissea5e75a2008-03-07 20:12:20 +00001package com.facebook.thrift.test;
2
3// Generated code
4import thrift.test.*;
5
6import com.facebook.thrift.transport.TMemoryBuffer;
7import com.facebook.thrift.protocol.TJSONProtocol;
8
9import java.util.Map;
10import java.util.HashMap;
11import java.util.Set;
12import java.util.HashSet;
13import java.util.List;
14import java.util.ArrayList;
15
16/**
17 * Tests for the Java implementation of TJSONProtocol. Mirrors the C++ version
18 *
19 * @author Chad Walters <chad@powerset.com>
20 */
21public class JSONProtoTest {
22
23 private static final byte[] kUnicodeBytes = {
24 (byte)0xd3, (byte)0x80, (byte)0xe2, (byte)0x85, (byte)0xae, (byte)0xce,
25 (byte)0x9d, (byte)0x20, (byte)0xd0, (byte)0x9d, (byte)0xce, (byte)0xbf,
26 (byte)0xe2, (byte)0x85, (byte)0xbf, (byte)0xd0, (byte)0xbe, (byte)0xc9,
27 (byte)0xa1, (byte)0xd0, (byte)0xb3, (byte)0xd0, (byte)0xb0, (byte)0xcf,
28 (byte)0x81, (byte)0xe2, (byte)0x84, (byte)0x8e, (byte)0x20, (byte)0xce,
29 (byte)0x91, (byte)0x74, (byte)0x74, (byte)0xce, (byte)0xb1, (byte)0xe2,
30 (byte)0x85, (byte)0xbd, (byte)0xce, (byte)0xba, (byte)0x83, (byte)0xe2,
31 (byte)0x80, (byte)0xbc
32 };
33
34 public static void main(String [] args) throws Exception {
35 try {
36 System.out.println("In JSON Proto test");
37
38 OneOfEach ooe = new OneOfEach();
39 ooe.im_true = true;
40 ooe.im_false = false;
41 ooe.a_bite = (byte)0xd6;
42 ooe.integer16 = 27000;
43 ooe.integer32 = 1<<24;
44 ooe.integer64 = (long)6000 * 1000 * 1000;
45 ooe.double_precision = Math.PI;
46 ooe.some_characters = "JSON THIS! \"\1";
47 ooe.zomg_unicode = new String(kUnicodeBytes, "UTF-8");
48
49
50 Nesting n = new Nesting(new Bonk(), new OneOfEach());
51 n.my_ooe.integer16 = 16;
52 n.my_ooe.integer32 = 32;
53 n.my_ooe.integer64 = 64;
54 n.my_ooe.double_precision = (Math.sqrt(5)+1)/2;
55 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
56 n.my_ooe.zomg_unicode = new String(kUnicodeBytes, "UTF-8");
57 n.my_bonk.type = 31337;
58 n.my_bonk.message = "I am a bonk... xor!";
59
60 HolyMoley hm = new HolyMoley();
61
62 hm.big = new ArrayList<OneOfEach>();
63 hm.big.add(ooe);
64 hm.big.add(n.my_ooe);
65 hm.big.get(0).a_bite = (byte)0x22;
66 hm.big.get(1).a_bite = (byte)0x23;
67
68 hm.contain = new HashSet<List<String>>();
69 ArrayList<String> stage1 = new ArrayList<String>(2);
70 stage1.add("and a one");
71 stage1.add("and a two");
72 hm.contain.add(stage1);
73 stage1 = new ArrayList<String>(3);
74 stage1.add("then a one, two");
75 stage1.add("three!");
76 stage1.add("FOUR!!");
77 hm.contain.add(stage1);
78 stage1 = new ArrayList<String>(0);
79 hm.contain.add(stage1);
80
81 ArrayList<Bonk> stage2 = new ArrayList<Bonk>();
82 hm.bonks = new HashMap<String, List<Bonk>>();
83 hm.bonks.put("nothing", stage2);
84 Bonk b = new Bonk();
85 b.type = 1;
86 b.message = "Wait.";
87 stage2.add(b);
88 b = new Bonk();
89 b.type = 2;
90 b.message = "What?";
91 stage2.add(b);
92 stage2 = new ArrayList<Bonk>();
93 hm.bonks.put("something", stage2);
94 b = new Bonk();
95 b.type = 3;
96 b.message = "quoth";
97 b = new Bonk();
98 b.type = 4;
99 b.message = "the raven";
100 b = new Bonk();
101 b.type = 5;
102 b.message = "nevermore";
103 hm.bonks.put("poe", stage2);
104
105 TMemoryBuffer buffer = new TMemoryBuffer(1024);
106 TJSONProtocol proto = new TJSONProtocol(buffer);
107
108 System.out.println("Writing ooe");
109 ooe.write(proto);
110 System.out.println("Reading ooe");
111 OneOfEach ooe2 = new OneOfEach();
112 ooe2.read(proto);
113
114 System.out.println("Comparing ooe");
115 if (!ooe.equals(ooe2)) {
116 throw new RuntimeException("ooe != ooe2");
117 }
118
119 System.out.println("Writing hm");
120 hm.write(proto);
121
122 System.out.println("Reading hm");
123 HolyMoley hm2 = new HolyMoley();
124 hm2.read(proto);
125
126 System.out.println("Comparing hm");
127 if (!hm.equals(hm2)) {
128 throw new RuntimeException("hm != hm2");
129 }
130
131 hm2.big.get(0).a_bite = (byte)0xFF;
132 if (hm.equals(hm2)) {
133 throw new RuntimeException("hm should not equal hm2");
134 }
135
136 Base64 base = new Base64();
137 base.a = 123;
138 base.b1 = "1".getBytes("UTF-8");
139 base.b2 = "12".getBytes("UTF-8");
140 base.b3 = "123".getBytes("UTF-8");
141 base.b4 = "1234".getBytes("UTF-8");
142 base.b5 = "12345".getBytes("UTF-8");
143 base.b6 = "123456".getBytes("UTF-8");
144
145 System.out.println("Writing base");
146 base.write(proto);
147
148 System.out.println("Reading base");
149 Base64 base2 = new Base64();
150 base2.read(proto);
151
152 System.out.println("Comparing base");
153 if (!base.equals(base2)) {
154 throw new RuntimeException("base != base2");
155 }
156
157 } catch (Exception ex) {
158 ex.printStackTrace();
159 throw ex;
160 }
161 }
162
163}