forked from apache/thrift
/
UnionTest.java
136 lines (98 loc) · 3.46 KB
/
UnionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.apache.thrift.test;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TMemoryBuffer;
import thrift.test.Empty;
import thrift.test.StructWithAUnion;
import thrift.test.TestUnion;
public class UnionTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
testBasic();
testEquality();
testSerialization();
}
public static void testBasic() throws Exception {
TestUnion union = new TestUnion();
if (union.isSet()) {
throw new RuntimeException("new union with default constructor counts as set!");
}
if (union.getFieldValue() != null) {
throw new RuntimeException("unset union didn't return null for value");
}
union = new TestUnion(TestUnion.I32_FIELD, 25);
if ((Integer)union.getFieldValue() != 25) {
throw new RuntimeException("set i32 field didn't come out as planned");
}
if ((Integer)union.getFieldValue(TestUnion.I32_FIELD) != 25) {
throw new RuntimeException("set i32 field didn't come out of TBase getFieldValue");
}
try {
union.getFieldValue(TestUnion.STRING_FIELD);
throw new RuntimeException("was expecting an exception around wrong set field");
} catch (IllegalArgumentException e) {
// cool!
}
System.out.println(union);
union = new TestUnion();
union.setI32_field(1);
if (union.getI32_field() != 1) {
throw new RuntimeException("didn't get the right value for i32 field!");
}
try {
union.getString_field();
throw new RuntimeException("should have gotten an exception");
} catch (Exception e) {
// sweet
}
union = TestUnion.i32_field(1);
if (union.equals((TestUnion)null)) {
throw new RuntimeException("uh oh, union.equals(null)!");
}
}
public static void testEquality() throws Exception {
TestUnion union = new TestUnion(TestUnion.I32_FIELD, 25);
TestUnion otherUnion = new TestUnion(TestUnion.STRING_FIELD, "blah!!!");
if (union.equals(otherUnion)) {
throw new RuntimeException("shouldn't be equal");
}
otherUnion = new TestUnion(TestUnion.I32_FIELD, 400);
if (union.equals(otherUnion)) {
throw new RuntimeException("shouldn't be equal");
}
otherUnion = new TestUnion(TestUnion.OTHER_I32_FIELD, 25);
if (union.equals(otherUnion)) {
throw new RuntimeException("shouldn't be equal");
}
}
public static void testSerialization() throws Exception {
TestUnion union = new TestUnion(TestUnion.I32_FIELD, 25);
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = new TBinaryProtocol(buf);
union.write(proto);
TestUnion u2 = new TestUnion();
u2.read(proto);
if (!u2.equals(union)) {
throw new RuntimeException("serialization fails!");
}
StructWithAUnion swau = new StructWithAUnion(u2);
buf = new TMemoryBuffer(0);
proto = new TBinaryProtocol(buf);
swau.write(proto);
StructWithAUnion swau2 = new StructWithAUnion();
if (swau2.equals(swau)) {
throw new RuntimeException("objects match before they are supposed to!");
}
swau2.read(proto);
if (!swau2.equals(swau)) {
throw new RuntimeException("objects don't match when they are supposed to!");
}
// this should NOT throw an exception.
buf = new TMemoryBuffer(0);
proto = new TBinaryProtocol(buf);
swau.write(proto);
new Empty().read(proto);
}
}