blob: a5b19cd137e0c728889c450ff35ccb6ab13751b2 [file] [log] [blame]
David Reiss603d5042008-12-02 02:06:31 +00001
2package com.facebook.thrift.test;
3
4import com.facebook.thrift.TDeserializer;
5import com.facebook.thrift.TSerializer;
6import com.facebook.thrift.protocol.TBinaryProtocol;
7import thrift.test.*;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.HashSet;
11import java.util.List;
12
13public class DeepCopyTest {
14
15 private static final byte[] kUnicodeBytes = {
16 (byte)0xd3, (byte)0x80, (byte)0xe2, (byte)0x85, (byte)0xae, (byte)0xce,
17 (byte)0x9d, (byte)0x20, (byte)0xd0, (byte)0x9d, (byte)0xce, (byte)0xbf,
18 (byte)0xe2, (byte)0x85, (byte)0xbf, (byte)0xd0, (byte)0xbe, (byte)0xc9,
19 (byte)0xa1, (byte)0xd0, (byte)0xb3, (byte)0xd0, (byte)0xb0, (byte)0xcf,
20 (byte)0x81, (byte)0xe2, (byte)0x84, (byte)0x8e, (byte)0x20, (byte)0xce,
21 (byte)0x91, (byte)0x74, (byte)0x74, (byte)0xce, (byte)0xb1, (byte)0xe2,
22 (byte)0x85, (byte)0xbd, (byte)0xce, (byte)0xba, (byte)0x83, (byte)0xe2,
23 (byte)0x80, (byte)0xbc
24 };
25
26 public static void main(String[] args) throws Exception {
27 TSerializer binarySerializer = new TSerializer(new TBinaryProtocol.Factory());
28 TDeserializer binaryDeserializer = new TDeserializer(new TBinaryProtocol.Factory());
29
30 OneOfEach ooe = new OneOfEach();
31 ooe.im_true = true;
32 ooe.im_false = false;
33 ooe.a_bite = (byte) 0xd6;
34 ooe.integer16 = 27000;
35 ooe.integer32 = 1 << 24;
36 ooe.integer64 = (long) 6000 * 1000 * 1000;
37 ooe.double_precision = Math.PI;
38 ooe.some_characters = "JSON THIS! \"\1";
39 ooe.zomg_unicode = new String(kUnicodeBytes, "UTF-8");
40
41 Nesting n = new Nesting(new Bonk(), new OneOfEach());
42 n.my_ooe.integer16 = 16;
43 n.my_ooe.integer32 = 32;
44 n.my_ooe.integer64 = 64;
45 n.my_ooe.double_precision = (Math.sqrt(5) + 1) / 2;
46 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
47 n.my_ooe.zomg_unicode = new String(kUnicodeBytes, "UTF-8");
48 n.my_bonk.type = 31337;
49 n.my_bonk.message = "I am a bonk... xor!";
50
51 HolyMoley hm = new HolyMoley();
52
53 hm.big = new ArrayList<OneOfEach>();
54 hm.big.add(ooe);
55 hm.big.add(n.my_ooe);
56 hm.big.get(0).a_bite = (byte) 0x22;
57 hm.big.get(1).a_bite = (byte) 0x23;
58
59 hm.contain = new HashSet<List<String>>();
60 ArrayList<String> stage1 = new ArrayList<String>(2);
61 stage1.add("and a one");
62 stage1.add("and a two");
63 hm.contain.add(stage1);
64 stage1 = new ArrayList<String>(3);
65 stage1.add("then a one, two");
66 stage1.add("three!");
67 stage1.add("FOUR!!");
68 hm.contain.add(stage1);
69 stage1 = new ArrayList<String>(0);
70 hm.contain.add(stage1);
71
72 ArrayList<Bonk> stage2 = new ArrayList<Bonk>();
73 hm.bonks = new HashMap<String, List<Bonk>>();
74 hm.bonks.put("nothing", stage2);
75 Bonk b = new Bonk();
76 b.type = 1;
77 b.message = "Wait.";
78 stage2.add(b);
79 b = new Bonk();
80 b.type = 2;
81 b.message = "What?";
82 stage2.add(b);
83 stage2 = new ArrayList<Bonk>();
84 hm.bonks.put("something", stage2);
85 b = new Bonk();
86 b.type = 3;
87 b.message = "quoth";
88 b = new Bonk();
89 b.type = 4;
90 b.message = "the raven";
91 b = new Bonk();
92 b.type = 5;
93 b.message = "nevermore";
94 hm.bonks.put("poe", stage2);
95
96
97 byte[] binaryCopy = binarySerializer.serialize(hm);
98 HolyMoley hmCopy = new HolyMoley();
99 binaryDeserializer.deserialize(hmCopy, binaryCopy);
100 HolyMoley hmCopy2 = new HolyMoley(hm);
101
102 if (!hm.equals(hmCopy))
103 throw new RuntimeException("copy constructor modified the original object!");
104 if (!hmCopy.equals(hmCopy2))
105 throw new RuntimeException("copy constructor generated incorrect copy");
106
107 hmCopy2.bonks.get("nothing").get(1).message = "What else?";
108
109 if (hm.equals(hmCopy2))
110 throw new RuntimeException("A deep copy was not done!");
111
112 //System.out.println("DeepCopyTest passed!");
113 }
114}