]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | ||
20 | package thrift | |
21 | ||
22 | import ( | |
23 | "context" | |
24 | "errors" | |
25 | "fmt" | |
26 | "testing" | |
27 | ) | |
28 | ||
29 | type ProtocolFactory interface { | |
30 | GetProtocol(t TTransport) TProtocol | |
31 | } | |
32 | ||
33 | func compareStructs(m, m1 MyTestStruct) (bool, error) { | |
34 | switch { | |
35 | case m.On != m1.On: | |
36 | return false, errors.New("Boolean not equal") | |
37 | case m.B != m1.B: | |
38 | return false, errors.New("Byte not equal") | |
39 | case m.Int16 != m1.Int16: | |
40 | return false, errors.New("Int16 not equal") | |
41 | case m.Int32 != m1.Int32: | |
42 | return false, errors.New("Int32 not equal") | |
43 | case m.Int64 != m1.Int64: | |
44 | return false, errors.New("Int64 not equal") | |
45 | case m.D != m1.D: | |
46 | return false, errors.New("Double not equal") | |
47 | case m.St != m1.St: | |
48 | return false, errors.New("String not equal") | |
49 | ||
50 | case len(m.Bin) != len(m1.Bin): | |
51 | return false, errors.New("Binary size not equal") | |
52 | case len(m.Bin) == len(m1.Bin): | |
53 | for i := range m.Bin { | |
54 | if m.Bin[i] != m1.Bin[i] { | |
55 | return false, errors.New("Binary not equal") | |
56 | } | |
57 | } | |
58 | case len(m.StringMap) != len(m1.StringMap): | |
59 | return false, errors.New("StringMap size not equal") | |
60 | case len(m.StringList) != len(m1.StringList): | |
61 | return false, errors.New("StringList size not equal") | |
62 | case len(m.StringSet) != len(m1.StringSet): | |
63 | return false, errors.New("StringSet size not equal") | |
64 | ||
65 | case m.E != m1.E: | |
66 | return false, errors.New("MyTestEnum not equal") | |
67 | ||
68 | default: | |
69 | return true, nil | |
70 | ||
71 | } | |
72 | return true, nil | |
73 | } | |
74 | ||
75 | func ProtocolTest1(test *testing.T, pf ProtocolFactory) (bool, error) { | |
76 | t := NewTSerializer() | |
77 | t.Protocol = pf.GetProtocol(t.Transport) | |
78 | var m = MyTestStruct{} | |
79 | m.On = true | |
80 | m.B = int8(0) | |
81 | m.Int16 = 1 | |
82 | m.Int32 = 2 | |
83 | m.Int64 = 3 | |
84 | m.D = 4.1 | |
85 | m.St = "Test" | |
86 | m.Bin = make([]byte, 10) | |
87 | m.StringMap = make(map[string]string, 5) | |
88 | m.StringList = make([]string, 5) | |
89 | m.StringSet = make(map[string]struct{}, 5) | |
90 | m.E = 2 | |
91 | ||
92 | s, err := t.WriteString(context.Background(), &m) | |
93 | if err != nil { | |
94 | return false, errors.New(fmt.Sprintf("Unable to Serialize struct\n\t %s", err)) | |
95 | } | |
96 | ||
97 | t1 := NewTDeserializer() | |
98 | t1.Protocol = pf.GetProtocol(t1.Transport) | |
99 | var m1 = MyTestStruct{} | |
100 | if err = t1.ReadString(&m1, s); err != nil { | |
101 | return false, errors.New(fmt.Sprintf("Unable to Deserialize struct\n\t %s", err)) | |
102 | ||
103 | } | |
104 | ||
105 | return compareStructs(m, m1) | |
106 | ||
107 | } | |
108 | ||
109 | func ProtocolTest2(test *testing.T, pf ProtocolFactory) (bool, error) { | |
110 | t := NewTSerializer() | |
111 | t.Protocol = pf.GetProtocol(t.Transport) | |
112 | var m = MyTestStruct{} | |
113 | m.On = false | |
114 | m.B = int8(0) | |
115 | m.Int16 = 1 | |
116 | m.Int32 = 2 | |
117 | m.Int64 = 3 | |
118 | m.D = 4.1 | |
119 | m.St = "Test" | |
120 | m.Bin = make([]byte, 10) | |
121 | m.StringMap = make(map[string]string, 5) | |
122 | m.StringList = make([]string, 5) | |
123 | m.StringSet = make(map[string]struct{}, 5) | |
124 | m.E = 2 | |
125 | ||
126 | s, err := t.WriteString(context.Background(), &m) | |
127 | if err != nil { | |
128 | return false, errors.New(fmt.Sprintf("Unable to Serialize struct\n\t %s", err)) | |
129 | ||
130 | } | |
131 | ||
132 | t1 := NewTDeserializer() | |
133 | t1.Protocol = pf.GetProtocol(t1.Transport) | |
134 | var m1 = MyTestStruct{} | |
135 | if err = t1.ReadString(&m1, s); err != nil { | |
136 | return false, errors.New(fmt.Sprintf("Unable to Deserialize struct\n\t %s", err)) | |
137 | ||
138 | } | |
139 | ||
140 | return compareStructs(m, m1) | |
141 | ||
142 | } | |
143 | ||
144 | func TestSerializer(t *testing.T) { | |
145 | ||
146 | var protocol_factories map[string]ProtocolFactory | |
147 | protocol_factories = make(map[string]ProtocolFactory) | |
148 | protocol_factories["Binary"] = NewTBinaryProtocolFactoryDefault() | |
149 | protocol_factories["Compact"] = NewTCompactProtocolFactory() | |
150 | //protocol_factories["SimpleJSON"] = NewTSimpleJSONProtocolFactory() - write only, can't be read back by design | |
151 | protocol_factories["JSON"] = NewTJSONProtocolFactory() | |
152 | ||
153 | var tests map[string]func(*testing.T, ProtocolFactory) (bool, error) | |
154 | tests = make(map[string]func(*testing.T, ProtocolFactory) (bool, error)) | |
155 | tests["Test 1"] = ProtocolTest1 | |
156 | tests["Test 2"] = ProtocolTest2 | |
157 | //tests["Test 3"] = ProtocolTest3 // Example of how to add additional tests | |
158 | ||
159 | for name, pf := range protocol_factories { | |
160 | ||
161 | for test, f := range tests { | |
162 | ||
163 | if s, err := f(t, pf); !s || err != nil { | |
164 | t.Errorf("%s Failed for %s protocol\n\t %s", test, name, err) | |
165 | } | |
166 | ||
167 | } | |
168 | } | |
169 | ||
170 | } |