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 org.apache.thrift.partial;
21 
22 import java.nio.ByteBuffer;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 
31 /** Helpers for creating test data related to partial deserialization. */
32 public class PartialThriftTestData {
33 
34   public final byte[] BYTES = new byte[] {1, 2, 3};
35 
createSmallStruct(int id)36   public SmallStruct createSmallStruct(int id) {
37     return new SmallStruct()
38         .setByteField((byte) id)
39         .setI16Field((short) id)
40         .setI32Field(id)
41         .setI64Field(id)
42         .setDoubleField(id)
43         .setStringField(Integer.toString(id))
44         .setEnumField(TstEnum.E_ONE);
45   }
46 
createTestStruct(int id, int numItems)47   public TestStruct createTestStruct(int id, int numItems) {
48 
49     TestStruct ts =
50         new TestStruct()
51             .setByteField((byte) id)
52             .setI16Field((short) id)
53             .setI32Field(id)
54             .setI64Field(id)
55             .setDoubleField(id)
56             .setStringField(Integer.toString(id))
57             .setEnumField(TstEnum.E_ONE)
58             .setBinaryField(BYTES)
59             .setStructField(createSmallStruct(id));
60 
61     initListFields(ts, id, numItems);
62     initSetFields(ts, id, numItems);
63     initMapFields(ts, id, numItems);
64 
65     return ts;
66   }
67 
initListFields(TestStruct ts, int id, int numItems)68   public void initListFields(TestStruct ts, int id, int numItems) {
69     List<Byte> byteList = new ArrayList<>(numItems);
70     List<Short> i16List = new ArrayList<>(numItems);
71     List<Integer> i32List = new ArrayList<>(numItems);
72     List<Long> i64List = new ArrayList<>(numItems);
73     List<Double> doubleList = new ArrayList<>(numItems);
74     List<String> stringList = new ArrayList<>(numItems);
75     List<TstEnum> enumList = new ArrayList<>(numItems);
76 
77     List<List<Integer>> listList = new ArrayList<>(numItems);
78     List<Set<Integer>> setList = new ArrayList<>(numItems);
79     List<Map<String, Integer>> mapList = new ArrayList<>(numItems);
80     List<SmallStruct> structList = new ArrayList<>(numItems);
81     List<ByteBuffer> binaryList = new ArrayList<>(numItems);
82 
83     for (int i = 0; i < numItems; i++) {
84       byteList.add((byte) i);
85       i16List.add((short) i);
86       i32List.add(i);
87       i64List.add((long) i);
88       doubleList.add((double) i);
89       stringList.add(Integer.toString(i));
90       enumList.add(TstEnum.E_ONE);
91       structList.add(createSmallStruct(i));
92       binaryList.add(ByteBuffer.wrap(BYTES));
93 
94       List<Integer> listItem = new ArrayList<>(numItems);
95       listList.add(listItem);
96 
97       Set<Integer> setItem = new HashSet<>();
98       setList.add(setItem);
99 
100       Map<String, Integer> mapItem = new HashMap<>();
101       mapList.add(mapItem);
102 
103       for (int j = 0; j < numItems; j++) {
104         listItem.add(j);
105         setItem.add(j);
106         mapItem.put(Integer.toString(j), j);
107       }
108     }
109 
110     ts.setByteList(byteList)
111         .setI16List(i16List)
112         .setI32List(i32List)
113         .setI64List(i64List)
114         .setDoubleList(doubleList)
115         .setStringList(stringList)
116         .setEnumList(enumList)
117         .setListList(listList)
118         .setSetList(setList)
119         .setMapList(mapList)
120         .setStructList(structList)
121         .setBinaryList(binaryList);
122   }
123 
initSetFields(TestStruct ts, int id, int numItems)124   public void initSetFields(TestStruct ts, int id, int numItems) {
125     Set<Byte> byteSet = new HashSet<>();
126     Set<Short> i16Set = new HashSet<>();
127     Set<Integer> i32Set = new HashSet<>();
128     Set<Long> i64Set = new HashSet<>();
129     Set<Double> doubleSet = new HashSet<>();
130     Set<String> stringSet = new HashSet<>();
131     Set<TstEnum> enumSet = new HashSet<>();
132 
133     Set<List<Integer>> listSet = new HashSet<>();
134     Set<Set<Integer>> setSet = new HashSet<>();
135     Set<Map<String, Integer>> mapSet = new HashSet<>();
136     Set<SmallStruct> structSet = new HashSet<>();
137     Set<ByteBuffer> binarySet = new HashSet<>();
138 
139     for (int i = 0; i < numItems; i++) {
140       byteSet.add((byte) i);
141       i16Set.add((short) i);
142       i32Set.add(i);
143       i64Set.add((long) i);
144       doubleSet.add((double) i);
145       stringSet.add(Integer.toString(i));
146       enumSet.add(TstEnum.E_ONE);
147       structSet.add(createSmallStruct(i));
148       binarySet.add(ByteBuffer.wrap(BYTES));
149 
150       List<Integer> listItem = new ArrayList<>(numItems);
151       Set<Integer> setItem = new HashSet<>();
152       Map<String, Integer> mapItem = new HashMap<>();
153 
154       for (int j = 0; j < numItems; j++) {
155         setItem.add(j);
156         listItem.add(j);
157         mapItem.put(Integer.toString(j), j);
158       }
159 
160       listSet.add(listItem);
161       setSet.add(setItem);
162       mapSet.add(mapItem);
163     }
164 
165     ts.setByteSet(byteSet)
166         .setI16Set(i16Set)
167         .setI32Set(i32Set)
168         .setI64Set(i64Set)
169         .setDoubleSet(doubleSet)
170         .setStringSet(stringSet)
171         .setEnumSet(enumSet)
172         .setListSet(listSet)
173         .setSetSet(setSet)
174         .setMapSet(mapSet)
175         .setStructSet(structSet)
176         .setBinarySet(binarySet);
177   }
178 
initMapFields(TestStruct ts, int id, int numItems)179   public void initMapFields(TestStruct ts, int id, int numItems) {
180     Map<Byte, Byte> byteMap = new HashMap<>();
181     Map<Short, Short> i16Map = new HashMap<>();
182     Map<Integer, Integer> i32Map = new HashMap<>();
183     Map<Long, Long> i64Map = new HashMap<>();
184     Map<Double, Double> doubleMap = new HashMap<>();
185     Map<String, String> stringMap = new HashMap<>();
186     Map<TstEnum, TstEnum> enumMap = new HashMap<>();
187 
188     Map<Integer, List<Integer>> listMap = new HashMap<>();
189     Map<Integer, Set<Integer>> setMap = new HashMap<>();
190     Map<Integer, Map<Integer, Integer>> mapMap = new HashMap<>();
191     Map<SmallStruct, SmallStruct> structMap = new HashMap<>();
192     Map<Integer, ByteBuffer> binaryMap = new HashMap<>();
193 
194     for (int i = 0; i < numItems; i++) {
195       byteMap.put((byte) i, (byte) i);
196       i16Map.put((short) i, (short) i);
197       i32Map.put(i, i);
198       i64Map.put((long) i, (long) i);
199       doubleMap.put((double) i, (double) i);
200       stringMap.put(Integer.toString(i), Integer.toString(i));
201       enumMap.put(TstEnum.E_ONE, TstEnum.E_ONE);
202       structMap.put(createSmallStruct(i), createSmallStruct(i));
203       binaryMap.put(i, ByteBuffer.wrap(BYTES));
204 
205       List<Integer> listItem = new ArrayList<>(numItems);
206       listMap.put(i, listItem);
207 
208       Set<Integer> setItem = new HashSet<>();
209       setMap.put(i, setItem);
210 
211       Map<Integer, Integer> mapItem = new HashMap<>();
212       mapMap.put(i, mapItem);
213 
214       for (int j = 0; j < numItems; j++) {
215         listItem.add(j);
216         setItem.add(j);
217         mapItem.put(j, j);
218       }
219     }
220 
221     ts.setByteMap(byteMap)
222         .setI16Map(i16Map)
223         .setI32Map(i32Map)
224         .setI64Map(i64Map)
225         .setDoubleMap(doubleMap)
226         .setStringMap(stringMap)
227         .setEnumMap(enumMap)
228         .setListMap(listMap)
229         .setSetMap(setMap)
230         .setMapMap(mapMap)
231         .setStructMap(structMap)
232         .setBinaryMap(binaryMap);
233   }
234 
allFieldsOfTestStruct()235   public List<String> allFieldsOfTestStruct() {
236     return new ArrayList<>(
237         Arrays.asList(
238             "byteField",
239             "i16Field",
240             "i32Field",
241             "i64Field",
242             "doubleField",
243             "stringField",
244             "structField.byteField",
245             "structField.i16Field",
246             "structField.i32Field",
247             "structField.i64Field",
248             "structField.doubleField",
249             "structField.stringField",
250             "structField.enumField",
251             "enumField",
252             "binaryField",
253             "byteList",
254             "i16List",
255             "i32List",
256             "i64List",
257             "doubleList",
258             "stringList",
259             "enumList",
260             "listList",
261             "setList",
262             "mapList",
263             "structList.byteField",
264             "structList.i16Field",
265             "structList.i32Field",
266             "structList.i64Field",
267             "structList.doubleField",
268             "structList.stringField",
269             "structList.enumField",
270             "binaryList",
271             "byteSet",
272             "i16Set",
273             "i32Set",
274             "i64Set",
275             "doubleSet",
276             "stringSet",
277             "enumSet",
278             "listSet",
279             "setSet",
280             "mapSet",
281             "structSet.byteField",
282             "structSet.i16Field",
283             "structSet.i32Field",
284             "structSet.i64Field",
285             "structSet.doubleField",
286             "structSet.stringField",
287             "structSet.enumField",
288             "binarySet",
289             "byteMap",
290             "i16Map",
291             "i32Map",
292             "i64Map",
293             "doubleMap",
294             "stringMap",
295             "enumMap",
296             "listMap",
297             "setMap",
298             "mapMap",
299             "structMap.byteField",
300             "structMap.i16Field",
301             "structMap.i32Field",
302             "structMap.i64Field",
303             "structMap.doubleField",
304             "structMap.stringField",
305             "structMap.enumField",
306             "binaryMap"));
307   }
308 }
309