blob: 3f64ef7fb76dd68a87422ae148e6a0873f135f65 [file] [log] [blame]
David Reiss3b15ebc2008-03-02 06:29:19 +00001package com.facebook.thrift.test;
2
3// Generated code
4import thrift.test.*;
5
6import com.facebook.thrift.TSerializer;
7import com.facebook.thrift.TDeserializer;
8import com.facebook.thrift.protocol.TBinaryProtocol;
9import com.facebook.thrift.protocol.TSimpleJSONProtocol;
10
11import java.util.Map;
12import java.util.HashMap;
13import java.util.Set;
14import java.util.HashSet;
15import java.util.List;
16import java.util.ArrayList;
17
18import java.io.ByteArrayInputStream;
19import java.io.ByteArrayOutputStream;
20import java.io.ObjectInputStream;
21import java.io.ObjectOutputStream;
22
23/**
24 *
25 * @author David Reiss <dreiss@facebook.com>
26 */
27public class IdentityTest {
28 public static Object deepCopy(Object oldObj) throws Exception {
29 ObjectOutputStream oos = null;
30 ObjectInputStream ois = null;
31 try {
32 ByteArrayOutputStream bos =
33 new ByteArrayOutputStream();
34 oos = new ObjectOutputStream(bos);
35 oos.writeObject(oldObj);
36 oos.flush();
37 ByteArrayInputStream bis =
38 new ByteArrayInputStream(bos.toByteArray());
39 ois = new ObjectInputStream(bis);
40 return ois.readObject();
41 } finally {
42 oos.close();
43 ois.close();
44 }
45 }
46
47 public static void main(String[] args) throws Exception {
48 TSerializer binarySerializer = new TSerializer(new TBinaryProtocol.Factory());
49 TDeserializer binaryDeserializer = new TDeserializer(new TBinaryProtocol.Factory());
50
51 OneOfEach ooe = new OneOfEach();
52 ooe.im_true = true;
53 ooe.im_false = false;
54 ooe.a_bite = (byte)0xd6;
55 ooe.integer16 = 27000;
56 ooe.integer32 = 1<<24;
57 ooe.integer64 = (long)6000 * 1000 * 1000;
58 ooe.double_precision = Math.PI;
59 ooe.some_characters = "JSON THIS! \"\u0001";
60 ooe.base64 = new byte[]{1,2,3,(byte)255};
61
62 Nesting n = new Nesting();
63 n.my_ooe = (OneOfEach)deepCopy(ooe);
64 n.my_ooe.integer16 = 16;
65 n.my_ooe.integer32 = 32;
66 n.my_ooe.integer64 = 64;
67 n.my_ooe.double_precision = (Math.sqrt(5)+1)/2;
68 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
69 n.my_ooe.zomg_unicode = "\u04c0\u216e\u039d\u0020\u041d\u03bf\u217f"+
70 "\u043e\u0261\u0433\u0430\u03c1\u210e\u0020"+
71 "\u0391\u0074\u0074\u03b1\u217d\u03ba\u01c3"+
72 "\u203c";
73 n.my_bonk = new Bonk();
74 n.my_bonk.type = 31337;
75 n.my_bonk.message = "I am a bonk... xor!";
76
77 HolyMoley hm = new HolyMoley();
78 hm.big = new ArrayList<OneOfEach>();
79 hm.contain = new HashSet<List<String>>();
80 hm.bonks = new HashMap<String,List<Bonk>>();
81
82 hm.big.add((OneOfEach)deepCopy(ooe));
83 hm.big.add((OneOfEach)deepCopy(n.my_ooe));
84 hm.big.get(0).a_bite = 0x22;
85 hm.big.get(1).a_bite = 0x33;
86
87 List<String> stage1 = new ArrayList<String>();
88 stage1.add("and a one");
89 stage1.add("and a two");
90 hm.contain.add(stage1);
91 stage1 = new ArrayList<String>();
92 stage1.add("then a one, two");
93 stage1.add("three!");
94 stage1.add("FOUR!!");
95 hm.contain.add(stage1);
96 stage1 = new ArrayList<String>();
97 hm.contain.add(stage1);
98
99 List<Bonk> stage2 = new ArrayList<Bonk>();
100 hm.bonks.put("nothing", stage2);
101 stage2.add(new Bonk());
102 stage2.get(0).type = 1;
103 stage2.get(0).message = "Wait.";
104 stage2.add(new Bonk());
105 stage2.get(1).type = 2;
106 stage2.get(1).message = "What?";
107 hm.bonks.put("something", stage2);
108 stage2 = new ArrayList<Bonk>();
109 stage2.add(new Bonk());
110 stage2.get(0).type = 3;
111 stage2.get(0).message = "quoth";
112 stage2.add(new Bonk());
113 stage2.get(1).type = 4;
114 stage2.get(1).message = "the raven";
115 stage2.add(new Bonk());
116 stage2.get(2).type = 5;
117 stage2.get(2).message = "nevermore";
118 hm.bonks.put("poe", stage2);
119
120 OneOfEach ooe2 = new OneOfEach();
121 binaryDeserializer.deserialize(
122 ooe2,
123 binarySerializer.serialize(ooe));
124
125 if (!ooe.equals(ooe2)) {
126 throw new RuntimeException("Failure: ooe (equals)");
127 }
128 if (ooe.hashCode() != ooe2.hashCode()) {
129 throw new RuntimeException("Failure: ooe (hash)");
130 }
131
132
133 Nesting n2 = new Nesting();
134 binaryDeserializer.deserialize(
135 n2,
136 binarySerializer.serialize(n));
137
138 if (!n.equals(n2)) {
139 throw new RuntimeException("Failure: n (equals)");
140 }
141 if (n.hashCode() != n2.hashCode()) {
142 throw new RuntimeException("Failure: n (hash)");
143 }
144
145 HolyMoley hm2 = new HolyMoley();
146 binaryDeserializer.deserialize(
147 hm2,
148 binarySerializer.serialize(hm));
149
150 if (!hm.equals(hm2)) {
151 throw new RuntimeException("Failure: hm (equals)");
152 }
153 if (hm.hashCode() != hm2.hashCode()) {
154 throw new RuntimeException("Failure: hm (hash)");
155 }
156
157 }
158}