This project has retired. For details please refer to its
Attic page.
KryoSimpleTests xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.writable.kryo;
19
20 import static junit.framework.TestCase.assertTrue;
21 import static org.junit.Assert.assertEquals;
22
23 import java.util.List;
24 import java.util.Set;
25 import java.util.Map;
26 import java.util.Random;
27 import java.util.HashSet;
28 import java.util.HashMap;
29 import java.util.ArrayList;
30
31 import org.apache.commons.lang3.RandomStringUtils;
32 import org.apache.giraph.utils.WritableUtils;
33 import org.junit.Test;
34
35
36
37
38 public class KryoSimpleTests {
39 interface BasicProperties {
40 boolean getBoolProp();
41 void setBoolProp(boolean val);
42 byte getByteProp();
43 void setByteProp(byte val);
44 short getShortProp();
45 void setShortProp(short val);
46 char getCharProp();
47 void setCharProp(char val);
48 int getIntProp();
49 void setIntProp(int val);
50 long getLongProp();
51 void setLongProp(long val);
52 float getFloatProp();
53 void setFloatProp(float val);
54 double getDoubleProp() ;
55 void setDoubleProp(double val);
56 String getStrProp() ;
57 void setStrProp(String strProp);
58 List getListProp();
59 void setListProp(List val);
60 Set getSetProp();
61 void setSetProp(Set val);
62 Map getMapProp() ;
63 void setMapProp(Map val);
64 }
65 public static class TestClass implements BasicProperties{
66 boolean boolProp;
67 byte byteProp;
68 short shortProp;
69 char charProp;
70 int intProp;
71 long longProp;
72 float floatProp;
73 double doubleProp;
74 String strProp;
75 List listProp;
76 Set setProp;
77 Map mapProp;
78
79 public TestClass() {
80 }
81
82 public boolean getBoolProp() {
83 return boolProp;
84 }
85 public void setBoolProp(boolean val) {
86 boolProp = val;
87 }
88 public byte getByteProp() {
89 return byteProp;
90 }
91 public void setByteProp(byte val) {
92 byteProp = val;
93 }
94 public short getShortProp() {
95 return shortProp;
96 }
97 public void setShortProp(short val) {
98 shortProp = val;
99 }
100 public char getCharProp() {
101 return charProp;
102 }
103 public void setCharProp(char val) {
104 charProp = val;
105 }
106 public int getIntProp() {
107 return intProp;
108 }
109 public void setIntProp(int val) {
110 intProp = val;
111 }
112 public long getLongProp() {
113 return longProp;
114 }
115 public void setLongProp(long val) {
116 longProp = val;
117 }
118 public float getFloatProp() {
119 return floatProp;
120 }
121 public void setFloatProp(float val) {
122 floatProp = val;
123 }
124 public double getDoubleProp() {
125 return doubleProp;
126 }
127 public void setDoubleProp(double val) {
128 doubleProp = val;
129 }
130 public String getStrProp() {
131 return strProp;
132 }
133 public void setStrProp(String strProp) {
134 this.strProp = strProp;
135 }
136 public List getListProp() {
137 return this.listProp;
138 }
139 public void setListProp(List val) {
140 this.listProp = val;
141 }
142 public Set getSetProp() {
143 return setProp;
144 }
145 public void setSetProp(Set val) {
146 this.setProp = val;
147 }
148 public Map getMapProp() {
149 return mapProp;
150 }
151 public void setMapProp(Map val) {
152 this.mapProp = val;
153 }
154
155 }
156
157 public static class TestClassFromWritable extends KryoSimpleWritable implements BasicProperties {
158 boolean boolProp;
159 byte byteProp;
160 short shortProp;
161 char charProp;
162 int intProp;
163 long longProp;
164 float floatProp;
165 double doubleProp;
166 String strProp;
167 List listProp;
168 Set setProp;
169 Map mapProp;
170
171 public TestClassFromWritable() {
172 }
173 public boolean getBoolProp() {
174 return boolProp;
175 }
176 public void setBoolProp(boolean val) {
177 boolProp = val;
178 }
179 public byte getByteProp() {
180 return byteProp;
181 }
182 public void setByteProp(byte val) {
183 byteProp = val;
184 }
185 public short getShortProp() {
186 return shortProp;
187 }
188 public void setShortProp(short val) {
189 shortProp = val;
190 }
191 public char getCharProp() {
192 return charProp;
193 }
194 public void setCharProp(char val) {
195 charProp = val;
196 }
197 public int getIntProp() {
198 return intProp;
199 }
200 public void setIntProp(int val) {
201 intProp = val;
202 }
203 public long getLongProp() {
204 return longProp;
205 }
206 public void setLongProp(long val) {
207 longProp = val;
208 }
209 public float getFloatProp() {
210 return floatProp;
211 }
212 public void setFloatProp(float val) {
213 floatProp = val;
214 }
215 public double getDoubleProp() {
216 return doubleProp;
217 }
218 public void setDoubleProp(double val) {
219 doubleProp = val;
220 }
221 public String getStrProp() {
222 return strProp;
223 }
224 public void setStrProp(String strProp) {
225 this.strProp = strProp;
226 }
227 public List getListProp() {
228 return this.listProp;
229 }
230 public void setListProp(List val) {
231 this.listProp = val;
232 }
233 public Set getSetProp() {
234 return setProp;
235 }
236 public void setSetProp(Set val) {
237 this.setProp = val;
238 }
239 public Map getMapProp() {
240 return mapProp;
241 }
242 public void setMapProp(Map val) {
243 this.mapProp = val;
244 }
245
246 }
247
248 public static class TestClassFromKryoWritable extends KryoWritable implements BasicProperties {
249 boolean boolProp;
250 byte byteProp;
251 short shortProp;
252 char charProp;
253 int intProp;
254 long longProp;
255 float floatProp;
256 double doubleProp;
257 String strProp;
258 List listProp;
259 Set setProp;
260 Map mapProp;
261
262 public TestClassFromKryoWritable() {
263 }
264 public boolean getBoolProp() {
265 return boolProp;
266 }
267 public void setBoolProp(boolean val) {
268 boolProp = val;
269 }
270 public byte getByteProp() {
271 return byteProp;
272 }
273 public void setByteProp(byte val) {
274 byteProp = val;
275 }
276 public short getShortProp() {
277 return shortProp;
278 }
279 public void setShortProp(short val) {
280 shortProp = val;
281 }
282 public char getCharProp() {
283 return charProp;
284 }
285 public void setCharProp(char val) {
286 charProp = val;
287 }
288 public int getIntProp() {
289 return intProp;
290 }
291 public void setIntProp(int val) {
292 intProp = val;
293 }
294 public long getLongProp() {
295 return longProp;
296 }
297 public void setLongProp(long val) {
298 longProp = val;
299 }
300 public float getFloatProp() {
301 return floatProp;
302 }
303 public void setFloatProp(float val) {
304 floatProp = val;
305 }
306 public double getDoubleProp() {
307 return doubleProp;
308 }
309 public void setDoubleProp(double val) {
310 doubleProp = val;
311 }
312 public String getStrProp() {
313 return strProp;
314 }
315 public void setStrProp(String strProp) {
316 this.strProp = strProp;
317 }
318 public List getListProp() {
319 return this.listProp;
320 }
321 public void setListProp(List val) {
322 this.listProp = val;
323 }
324 public Set getSetProp() {
325 return setProp;
326 }
327 public void setSetProp(Set val) {
328 this.setProp = val;
329 }
330 public Map getMapProp() {
331 return mapProp;
332 }
333 public void setMapProp(Map val) {
334 this.mapProp = val;
335 }
336 }
337
338 static int collectionSize = 100000;
339 private static BasicProperties populateTestClass(BasicProperties testClass) {
340 Random rand = new Random();
341 BasicProperties reference = new TestClass();
342 reference.setBoolProp(rand.nextBoolean());
343 byte [] byteArr = new byte[1];
344 rand.nextBytes(byteArr);
345 reference.setByteProp(byteArr[0]);
346 reference.setCharProp((char)rand.nextInt((int)Character.MAX_VALUE));
347 reference.setIntProp(rand.nextInt(Integer.MAX_VALUE));
348 reference.setShortProp((short)rand.nextInt((int)Short.MAX_VALUE));
349 reference.setLongProp(rand.nextLong());
350 reference.setFloatProp(rand.nextFloat());
351 reference.setDoubleProp(rand.nextDouble());
352 reference.setStrProp(RandomStringUtils.randomAlphabetic(10));
353
354 List<Integer> list = new ArrayList<>();
355 for (int i=0; i < collectionSize ; i++) {
356 list.add(rand.nextInt(Integer.MAX_VALUE));
357 }
358 reference.setListProp(list);
359
360 Set<Integer> set = new HashSet<>();
361 for (int i=0; i < collectionSize; i++) {
362 set.add(rand.nextInt(Integer.MAX_VALUE));
363 }
364 reference.setSetProp(set);
365
366 Map<Integer, Integer> map = new HashMap<>();
367 for (int i=0; i < collectionSize; i++) {
368 map.put(i, rand.nextInt(Integer.MAX_VALUE));
369 }
370 reference.setMapProp(map);
371 popuateTestFrom(reference, testClass);
372 return reference;
373 }
374
375 private static void popuateTestFrom(BasicProperties from, BasicProperties to) {
376 to.setStrProp(from.getStrProp());
377 to.setDoubleProp(from.getDoubleProp());
378 to.setFloatProp(from.getFloatProp());
379 to.setLongProp(from.getLongProp());
380 to.setIntProp(from.getIntProp());
381 to.setCharProp(from.getCharProp());
382 to.setByteProp(from.getByteProp());
383 to.setBoolProp(from.getBoolProp());
384 to.setShortProp(from.getShortProp());
385 to.setMapProp(from.getMapProp());
386 to.setSetProp(from.getSetProp());
387 to.setListProp(from.getListProp());
388 }
389
390 private static void verifyTestClass(BasicProperties testClass, BasicProperties referenceClass) {
391 assertEquals(testClass.getBoolProp(), referenceClass.getBoolProp());
392 assertEquals(testClass.getByteProp(), referenceClass.getByteProp());
393 assertEquals(testClass.getCharProp(), referenceClass.getCharProp());
394 assertEquals(testClass.getDoubleProp(), referenceClass.getDoubleProp(), 0.0001);
395 assertEquals(testClass.getFloatProp(), referenceClass.getFloatProp(), 0.0001);
396 assertEquals(testClass.getIntProp(), referenceClass.getIntProp());
397 assertEquals(testClass.getLongProp(), referenceClass.getLongProp());
398 assertEquals(testClass.getShortProp(), referenceClass.getShortProp());
399 assertEquals(testClass.getStrProp(), referenceClass.getStrProp());
400 assertTrue(testClass.getListProp().equals(referenceClass.getListProp()));
401 assertTrue(testClass.getSetProp().equals(referenceClass.getSetProp()));
402 assertTrue(testClass.getMapProp().equals(referenceClass.getMapProp()));
403 }
404
405 @Test
406 public void testClassFromWritable() {
407 TestClassFromWritable writableClass = new TestClassFromWritable();
408 TestClassFromWritable res = new TestClassFromWritable();
409 BasicProperties reference = populateTestClass(writableClass);
410 WritableUtils.copyInto(
411 writableClass, res, true);
412 verifyTestClass(res,reference);
413 }
414
415 @Test
416 public void testKryoSimpleWrapper() {
417 TestClass testClass = new TestClass();
418 BasicProperties reference = populateTestClass(testClass);
419 TestClass res = WritableUtils.createCopy(new KryoSimpleWrapper<>(testClass)).get();
420 verifyTestClass(res, reference);
421 }
422 }