Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
package org.apache.thrift.test;
19
 
20
import org.apache.thrift.protocol.TBinaryProtocol;
21
import org.apache.thrift.protocol.TProtocol;
22
import org.apache.thrift.transport.TMemoryBuffer;
23
 
24
import thrift.test.Empty;
25
import thrift.test.StructWithAUnion;
26
import thrift.test.TestUnion;
27
import thrift.test.SomeEnum;
28
import thrift.test.ComparableUnion;
29
 
30
public class UnionTest {
31
 
32
  /**
33
   * @param args
34
   */
35
  public static void main(String[] args) throws Exception {
36
    testBasic();
37
    testEquality();
38
    testSerialization();
39
    testCompareTo();
40
  }
41
 
42
  public static void testBasic() throws Exception {
43
    TestUnion union = new TestUnion();
44
 
45
    if (union.isSet()) {
46
      throw new RuntimeException("new union with default constructor counts as set!");
47
    }
48
 
49
    if (union.getFieldValue() != null) {
50
      throw new RuntimeException("unset union didn't return null for value");
51
    }
52
 
53
    union = new TestUnion(TestUnion._Fields.I32_FIELD, 25);
54
 
55
    if ((Integer)union.getFieldValue() != 25) {
56
      throw new RuntimeException("set i32 field didn't come out as planned");
57
    }
58
 
59
    if ((Integer)union.getFieldValue(TestUnion._Fields.I32_FIELD) != 25) {
60
      throw new RuntimeException("set i32 field didn't come out of TBase getFieldValue");
61
    }
62
 
63
    try {
64
      union.getFieldValue(TestUnion._Fields.STRING_FIELD);
65
      throw new RuntimeException("was expecting an exception around wrong set field");
66
    } catch (IllegalArgumentException e) {
67
      // cool!
68
    }
69
 
70
    System.out.println(union);
71
 
72
    union = new TestUnion();
73
    union.setI32_field(1);
74
    if (union.getI32_field() != 1) {
75
      throw new RuntimeException("didn't get the right value for i32 field!");
76
    }
77
    union.hashCode();
78
 
79
    try {
80
      union.getString_field();
81
      throw new RuntimeException("should have gotten an exception");
82
    } catch (Exception e) {
83
      // sweet
84
    }
85
 
86
    union = TestUnion.i32_field(1);
87
 
88
    if (union.equals((TestUnion)null)) {
89
      throw new RuntimeException("uh oh, union.equals(null)!");
90
    }
91
 
92
    union = TestUnion.enum_field(SomeEnum.ONE);
93
    union.hashCode();
94
  }
95
 
96
 
97
  public static void testEquality() throws Exception {
98
    TestUnion union = new TestUnion(TestUnion._Fields.I32_FIELD, 25);
99
 
100
    TestUnion otherUnion = new TestUnion(TestUnion._Fields.STRING_FIELD, "blah!!!");
101
 
102
    if (union.equals(otherUnion)) {
103
      throw new RuntimeException("shouldn't be equal");
104
    }
105
 
106
    otherUnion = new TestUnion(TestUnion._Fields.I32_FIELD, 400);
107
 
108
    if (union.equals(otherUnion)) {
109
      throw new RuntimeException("shouldn't be equal");
110
    }
111
 
112
    otherUnion = new TestUnion(TestUnion._Fields.OTHER_I32_FIELD, 25);
113
 
114
    if (union.equals(otherUnion)) {
115
      throw new RuntimeException("shouldn't be equal");
116
    }
117
  }
118
 
119
 
120
  public static void testSerialization() throws Exception {
121
    TestUnion union = new TestUnion(TestUnion._Fields.I32_FIELD, 25);
122
 
123
    TMemoryBuffer buf = new TMemoryBuffer(0);
124
    TProtocol proto = new TBinaryProtocol(buf);
125
 
126
    union.write(proto);
127
 
128
    TestUnion u2 = new TestUnion();
129
 
130
    u2.read(proto);
131
 
132
    if (!u2.equals(union)) {
133
      throw new RuntimeException("serialization fails!");
134
    }
135
 
136
    StructWithAUnion swau = new StructWithAUnion(u2);
137
 
138
    buf = new TMemoryBuffer(0);
139
    proto = new TBinaryProtocol(buf);
140
 
141
    swau.write(proto);
142
 
143
    StructWithAUnion swau2 = new StructWithAUnion();
144
    if (swau2.equals(swau)) {
145
      throw new RuntimeException("objects match before they are supposed to!");
146
    }
147
    swau2.read(proto);
148
    if (!swau2.equals(swau)) {
149
      throw new RuntimeException("objects don't match when they are supposed to!");
150
    }
151
 
152
    // this should NOT throw an exception.
153
    buf = new TMemoryBuffer(0);
154
    proto = new TBinaryProtocol(buf);
155
 
156
    swau.write(proto);
157
    new Empty().read(proto);
158
  }
159
 
160
  public static void testCompareTo() throws Exception {
161
    ComparableUnion cu = ComparableUnion.string_field("a");
162
    ComparableUnion cu2 = ComparableUnion.string_field("b");
163
 
164
    if (cu.compareTo(cu2) != -1) {
165
      throw new RuntimeException("a was supposed to be < b, but was " + cu.compareTo(cu2));
166
    }
167
 
168
    if (cu2.compareTo(cu) != 1) {
169
      throw new RuntimeException("b was supposed to be > a, but was " + cu2.compareTo(cu));
170
    }
171
 
172
    cu2 = ComparableUnion.binary_field(new byte[]{2});
173
 
174
    if (cu.compareTo(cu2) != -1) {
175
      throw new RuntimeException("a was supposed to be < b, but was " + cu.compareTo(cu2));
176
    }
177
 
178
    if (cu2.compareTo(cu) != 1) {
179
      throw new RuntimeException("b was supposed to be > a, but was " + cu2.compareTo(cu));
180
    }
181
 
182
    cu = ComparableUnion.binary_field(new byte[]{1});
183
 
184
    if (cu.compareTo(cu2) != -1) {
185
      throw new RuntimeException("a was supposed to be < b, but was " + cu.compareTo(cu2));
186
    }
187
 
188
    if (cu2.compareTo(cu) != 1) {
189
      throw new RuntimeException("b was supposed to be > a, but was " + cu2.compareTo(cu));
190
    }
191
  }
192
}