This project has retired. For details please refer to its Attic page.
TestWritableUtils xref
View Javadoc

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.giraph.utils;
19  
20  import org.apache.hadoop.io.FloatWritable;
21  import org.apache.hadoop.io.LongWritable;
22  import org.apache.hadoop.io.Writable;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.io.DataInputStream;
29  import java.io.DataOutputStream;
30  import java.io.IOException;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * Test case for WritableUtils.
36   */
37  public class TestWritableUtils {
38  
39    /**
40     * Tests readList and writeList functions in writable utils.
41     * @throws IOException
42     */
43    @Test
44    public void testListSerialization() throws IOException {
45      List<Writable> list = new ArrayList<>();
46      list.add(new LongWritable(1));
47      list.add(new LongWritable(2));
48      list.add(null);
49      list.add(new FloatWritable(3));
50      list.add(new FloatWritable(4));
51      list.add(new LongWritable(5));
52      list.add(new LongWritable(6));
53  
54      ByteArrayOutputStream bos = new ByteArrayOutputStream();
55      DataOutputStream dos = new DataOutputStream(bos);
56      WritableUtils.writeList(list, dos);
57      dos.close();
58  
59      byte[] data = bos.toByteArray();
60  
61      DataInputStream input =
62          new DataInputStream(new ByteArrayInputStream(data));
63  
64      List<Writable> result = (List<Writable>) WritableUtils.readList(input);
65  
66      Assert.assertEquals(list, result);
67  
68    }
69  
70    @Test
71    public void testIntArray() throws IOException {
72      int[] array = new int[] {1, 2, 3, 4, 5};
73      ByteArrayOutputStream bos = new ByteArrayOutputStream();
74      DataOutputStream dos = new DataOutputStream(bos);
75      WritableUtils.writeIntArray(array, dos);
76      dos.close();
77  
78      byte[] data = bos.toByteArray();
79  
80      DataInputStream input =
81          new DataInputStream(new ByteArrayInputStream(data));
82  
83      int[] result = WritableUtils.readIntArray(input);
84  
85      Assert.assertArrayEquals(array, result);
86    }
87  
88    @Test
89    public void testLongArray() throws IOException {
90      long[] array = new long[] {1, 2, 3, 4, 5};
91      ByteArrayOutputStream bos = new ByteArrayOutputStream();
92      DataOutputStream dos = new DataOutputStream(bos);
93      WritableUtils.writeLongArray(dos, array);
94      dos.close();
95  
96      byte[] data = bos.toByteArray();
97  
98      DataInputStream input =
99          new DataInputStream(new ByteArrayInputStream(data));
100 
101     long[] result = WritableUtils.readLongArray(input);
102 
103     Assert.assertArrayEquals(array, result);
104   }
105 
106 
107 
108 }