blob: ac11ffae48f2ccac60907a7ebcf8ecd860036181 [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");
David Reiss5455f002009-01-05 21:02:48 +000040 ooe.base64 = "string to bytes".getBytes();
David Reiss603d5042008-12-02 02:06:31 +000041
42 Nesting n = new Nesting(new Bonk(), new OneOfEach());
43 n.my_ooe.integer16 = 16;
44 n.my_ooe.integer32 = 32;
45 n.my_ooe.integer64 = 64;
46 n.my_ooe.double_precision = (Math.sqrt(5) + 1) / 2;
47 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
48 n.my_ooe.zomg_unicode = new String(kUnicodeBytes, "UTF-8");
49 n.my_bonk.type = 31337;
50 n.my_bonk.message = "I am a bonk... xor!";
51
52 HolyMoley hm = new HolyMoley();
53
54 hm.big = new ArrayList<OneOfEach>();
55 hm.big.add(ooe);
56 hm.big.add(n.my_ooe);
57 hm.big.get(0).a_bite = (byte) 0x22;
58 hm.big.get(1).a_bite = (byte) 0x23;
59
60 hm.contain = new HashSet<List<String>>();
61 ArrayList<String> stage1 = new ArrayList<String>(2);
62 stage1.add("and a one");
63 stage1.add("and a two");
64 hm.contain.add(stage1);
65 stage1 = new ArrayList<String>(3);
66 stage1.add("then a one, two");
67 stage1.add("three!");
68 stage1.add("FOUR!!");
69 hm.contain.add(stage1);
70 stage1 = new ArrayList<String>(0);
71 hm.contain.add(stage1);
72
73 ArrayList<Bonk> stage2 = new ArrayList<Bonk>();
74 hm.bonks = new HashMap<String, List<Bonk>>();
75 hm.bonks.put("nothing", stage2);
76 Bonk b = new Bonk();
77 b.type = 1;
78 b.message = "Wait.";
79 stage2.add(b);
80 b = new Bonk();
81 b.type = 2;
82 b.message = "What?";
83 stage2.add(b);
84 stage2 = new ArrayList<Bonk>();
85 hm.bonks.put("something", stage2);
86 b = new Bonk();
87 b.type = 3;
88 b.message = "quoth";
89 b = new Bonk();
90 b.type = 4;
91 b.message = "the raven";
92 b = new Bonk();
93 b.type = 5;
94 b.message = "nevermore";
95 hm.bonks.put("poe", stage2);
96
97
98 byte[] binaryCopy = binarySerializer.serialize(hm);
99 HolyMoley hmCopy = new HolyMoley();
100 binaryDeserializer.deserialize(hmCopy, binaryCopy);
101 HolyMoley hmCopy2 = new HolyMoley(hm);
102
103 if (!hm.equals(hmCopy))
104 throw new RuntimeException("copy constructor modified the original object!");
105 if (!hmCopy.equals(hmCopy2))
106 throw new RuntimeException("copy constructor generated incorrect copy");
107
David Reiss5455f002009-01-05 21:02:48 +0000108 hm.big.get(0).base64[0]++; // change binary value in original object
109 if (hm.equals(hmCopy2)) // make sure the change didn't propagate to the copied object
110 throw new RuntimeException("Binary field not copied correctly!");
111 hm.big.get(0).base64[0]--; // undo change
112
David Reiss603d5042008-12-02 02:06:31 +0000113 hmCopy2.bonks.get("nothing").get(1).message = "What else?";
114
115 if (hm.equals(hmCopy2))
116 throw new RuntimeException("A deep copy was not done!");
117
David Reiss603d5042008-12-02 02:06:31 +0000118 }
119}