1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 package org.apache.thrift;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.nio.ByteBuffer;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Set;
32 
33 import junit.framework.TestCase;
34 
35 import org.apache.thrift.protocol.TBinaryProtocol;
36 import org.apache.thrift.protocol.TProtocol;
37 import org.apache.thrift.protocol.TTupleProtocol;
38 import org.apache.thrift.transport.TMemoryBuffer;
39 
40 import thrift.test.ComparableUnion;
41 import thrift.test.Empty;
42 import thrift.test.RandomStuff;
43 import thrift.test.SomeEnum;
44 import thrift.test.StructWithAUnion;
45 import thrift.test.TestUnion;
46 import thrift.test.TestUnionMinusStringField;
47 
48 public class TestTUnion extends TestCase {
49 
testBasic()50   public void testBasic() throws Exception {
51     TestUnion union = new TestUnion();
52 
53     assertFalse(union.isSet());
54     assertFalse(union.isSetI32_field());
55     assertNull(union.getFieldValue());
56 
57     union = new TestUnion(TestUnion._Fields.I32_FIELD, 25);
58 
59     assertEquals(Integer.valueOf(25), union.getFieldValue());
60 
61     assertEquals(Integer.valueOf(25), union.getFieldValue(TestUnion._Fields.I32_FIELD));
62 
63     assertTrue(union.isSetI32_field());
64 
65     try {
66       union.getFieldValue(TestUnion._Fields.STRING_FIELD);
67       fail("should have thrown an exception");
68     } catch (IllegalArgumentException e) {
69       // cool!
70     }
71 
72     union = new TestUnion();
73 
74     // should not throw an exception here
75     union.hashCode();
76 
77     union.setI32_field(1);
78     assertEquals(1, union.getI32_field());
79     union.hashCode();
80 
81     assertFalse(union.isSetString_field());
82 
83     try {
84       union.getString_field();
85       fail("should have thrown an exception");
86     } catch (Exception e) {
87       // sweet
88     }
89 
90     union = TestUnion.i32_field(1);
91 
92     assertFalse(union.equals((TestUnion)null));
93 
94     union = TestUnion.enum_field(SomeEnum.ONE);
95     union.hashCode();
96 
97     union = new TestUnion();
98     // should not throw an exception
99     union.toString();
100   }
101 
testCompareTo()102   public void testCompareTo() throws Exception {
103     ComparableUnion cu = ComparableUnion.string_field("a");
104     ComparableUnion cu2 = ComparableUnion.string_field("b");
105 
106     assertTrue(cu.compareTo(cu) == 0);
107     assertTrue(cu2.compareTo(cu2) == 0);
108 
109     assertTrue(cu.compareTo(cu2) < 0);
110     assertTrue(cu2.compareTo(cu) > 0);
111 
112     cu2 = ComparableUnion.binary_field(ByteBuffer.wrap(new byte[]{2}));
113 
114     assertTrue(cu.compareTo(cu2) < 0);
115     assertTrue(cu2.compareTo(cu) > 0);
116 
117     cu = ComparableUnion.binary_field(ByteBuffer.wrap(new byte[]{1}));
118 
119     assertTrue(cu.compareTo(cu2) < 0);
120     assertTrue(cu2.compareTo(cu) > 0);
121 
122     TestUnion union1 = new TestUnion(TestUnion._Fields.STRUCT_LIST, new ArrayList<RandomStuff>());
123     TestUnion union2 = new TestUnion(TestUnion._Fields.STRUCT_LIST, new ArrayList<RandomStuff>());
124     assertTrue(union1.compareTo(union2) == 0);
125 
126     TestUnion union3 = new TestUnion(TestUnion._Fields.I32_SET, new HashSet<Integer>());
127     Set<Integer> i32_set = new HashSet<Integer>();
128     i32_set.add(1);
129     TestUnion union4 = new TestUnion(TestUnion._Fields.I32_SET, i32_set);
130     assertTrue(union3.compareTo(union4) < 0);
131 
132     Map<Integer, Integer> i32_map = new HashMap<Integer, Integer>();
133     i32_map.put(1,1);
134     TestUnion union5 = new TestUnion(TestUnion._Fields.I32_MAP, i32_map);
135     TestUnion union6 = new TestUnion(TestUnion._Fields.I32_MAP, new HashMap<Integer, Integer>());
136     assertTrue(union5.compareTo(union6) > 0);
137   }
138 
testEquality()139   public void testEquality() throws Exception {
140     TestUnion union = new TestUnion(TestUnion._Fields.I32_FIELD, 25);
141 
142     TestUnion otherUnion = new TestUnion(TestUnion._Fields.STRING_FIELD, "blah!!!");
143 
144     assertFalse(union.equals(otherUnion));
145 
146     otherUnion = new TestUnion(TestUnion._Fields.I32_FIELD, 400);
147 
148     assertFalse(union.equals(otherUnion));
149 
150     otherUnion = new TestUnion(TestUnion._Fields.OTHER_I32_FIELD, 25);
151 
152     assertFalse(union.equals(otherUnion));
153   }
154 
testSerialization()155   public void testSerialization() throws Exception {
156     TestUnion union = new TestUnion(TestUnion._Fields.I32_FIELD, 25);
157     union.setI32_set(Collections.singleton(42));
158 
159     TMemoryBuffer buf = new TMemoryBuffer(0);
160     TProtocol proto = new TBinaryProtocol(buf);
161 
162     union.write(proto);
163 
164     TestUnion u2 = new TestUnion();
165 
166     u2.read(proto);
167 
168     assertEquals(u2, union);
169 
170     StructWithAUnion swau = new StructWithAUnion(u2);
171 
172     buf = new TMemoryBuffer(0);
173     proto = new TBinaryProtocol(buf);
174 
175     swau.write(proto);
176 
177     StructWithAUnion swau2 = new StructWithAUnion();
178     assertFalse(swau2.equals(swau));
179     swau2.read(proto);
180     assertEquals(swau2, swau);
181 
182     // this should NOT throw an exception.
183     buf = new TMemoryBuffer(0);
184     proto = new TBinaryProtocol(buf);
185 
186     swau.write(proto);
187     new Empty().read(proto);
188   }
189 
testTupleProtocolSerialization()190   public void testTupleProtocolSerialization () throws Exception {
191     TestUnion union = new TestUnion(TestUnion._Fields.I32_FIELD, 25);
192     union.setI32_set(Collections.singleton(42));
193 
194     TMemoryBuffer buf = new TMemoryBuffer(0);
195     TProtocol proto = new TTupleProtocol(buf);
196 
197     union.write(proto);
198 
199     TestUnion u2 = new TestUnion();
200 
201     u2.read(proto);
202 
203     assertEquals(u2, union);
204 
205     StructWithAUnion swau = new StructWithAUnion(u2);
206 
207     buf = new TMemoryBuffer(0);
208     proto = new TBinaryProtocol(buf);
209 
210     swau.write(proto);
211 
212     StructWithAUnion swau2 = new StructWithAUnion();
213     assertFalse(swau2.equals(swau));
214     swau2.read(proto);
215     assertEquals(swau2, swau);
216 
217     // this should NOT throw an exception.
218     buf = new TMemoryBuffer(0);
219     proto = new TTupleProtocol(buf);
220 
221     swau.write(proto);
222     new Empty().read(proto);
223   }
224 
testSkip()225   public void testSkip() throws Exception {
226     TestUnion tu = TestUnion.string_field("string");
227     byte[] tuSerialized = new TSerializer().serialize(tu);
228     TestUnionMinusStringField tums = new TestUnionMinusStringField();
229     new TDeserializer().deserialize(tums, tuSerialized);
230     assertNull(tums.getSetField());
231     assertNull(tums.getFieldValue());
232   }
233 
testDeepCopy()234   public void testDeepCopy() throws Exception {
235     byte[] bytes = {1, 2, 3};
236     ByteBuffer value = ByteBuffer.wrap(bytes);
237     ComparableUnion cu = ComparableUnion.binary_field(value);
238     ComparableUnion copy = cu.deepCopy();
239     assertEquals(cu, copy);
240     assertNotSame(cu.bufferForBinary_field().array(), copy.bufferForBinary_field().array());
241   }
242 
testToString()243   public void testToString() throws Exception {
244     byte[] bytes = {1, 2, 3};
245     ByteBuffer value = ByteBuffer.wrap(bytes);
246     ComparableUnion cu = ComparableUnion.binary_field(value);
247     String expectedString = "<ComparableUnion binary_field:01 02 03>";
248     assertEquals(expectedString, cu.toString());
249   }
250 
testJavaSerializable()251   public void testJavaSerializable() throws Exception {
252     ByteArrayOutputStream baos = new ByteArrayOutputStream();
253     ObjectOutputStream oos = new ObjectOutputStream(baos);
254 
255     TestUnion tu = TestUnion.string_field("string");
256 
257     // Serialize tu the Java way...
258     oos.writeObject(tu);
259     byte[] serialized = baos.toByteArray();
260 
261     // Attempt to deserialize it
262     ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
263     ObjectInputStream ois = new ObjectInputStream(bais);
264     TestUnion tu2 = (TestUnion) ois.readObject();
265 
266     assertEquals(tu, tu2);
267   }
268 }
269