View Javadoc

1   package org.apache.directmemory.serialization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.junit.BeforeClass;
23  import org.junit.Test;
24  
25  import java.io.IOException;
26  import java.util.Random;
27  
28  import static org.junit.Assert.*;
29  
30  public class StandardSerializerTest
31  {
32  
33      private static Serializer serializer;
34  
35      private static final Random r = new Random();
36  
37      @BeforeClass
38      public static void init()
39      {
40          serializer = new StandardSerializer();
41      }
42  
43      @Test
44      public void validateBooleanSerialization()
45          throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
46      {
47          byte[] payload = serializer.serialize( true );
48          boolean res = serializer.deserialize( payload, Boolean.class );
49          assertEquals( true, res );
50      }
51  
52      @Test
53      public void validateBooleanArraySerialization()
54          throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
55      {
56          byte[] payload = serializer.serialize( new boolean[]{ true } );
57          boolean[] res = serializer.deserialize( payload, boolean[].class );
58          assertEquals( 1, res.length );
59          assertTrue( res[0] );
60      }
61  
62      @Test
63      public void validateByteSerialization()
64          throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
65      {
66          byte[] payload = serializer.serialize( (byte) 127 );
67          byte res = serializer.deserialize( payload, Byte.class );
68          assertEquals( (byte) 127, res );
69      }
70  
71      @Test
72      public void validateByteArraySerialization()
73          throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
74      {
75          byte[] value = new byte[1024];
76          r.nextBytes( value );
77          byte[] payload = serializer.serialize( value );
78          byte[] res = serializer.deserialize( payload, byte[].class );
79          assertArrayEquals( value, res );
80      }
81  
82      @Test
83      public void validateCharacterSerialization()
84          throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
85      {
86          byte[] payload = serializer.serialize( 'z' );
87          char res = serializer.deserialize( payload, Character.class );
88          assertEquals( 'z', res );
89      }
90  
91      @Test
92      public void validateCharacterArraySerialization()
93          throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
94      {
95          char[] value = new char[]{ 'a', 'z', 'x', ' ', '-', '-' };
96          byte[] payload = serializer.serialize( value );
97          char[] res = serializer.deserialize( payload, char[].class );
98          assertArrayEquals( value, res );
99      }
100 
101     @Test
102     public void validateDoubleSerialization()
103         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
104     {
105         byte[] payload = serializer.serialize( 1.2d );
106         double res = serializer.deserialize( payload, Double.class );
107         assertEquals( 1.2d, res, 0.0d );
108     }
109 
110     @Test
111     public void validateDoubleArraySerialization()
112         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
113     {
114         double[] value = new double[]{ 1.1d, 3.1d, 0.1d };
115         byte[] payload = serializer.serialize( value );
116         double[] res = serializer.deserialize( payload, double[].class );
117         assertArrayEquals( value, res, 0.0d );
118     }
119 
120     @Test
121     public void validateFloatSerialization()
122         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
123     {
124         byte[] payload = serializer.serialize( 1.2f );
125         float res = serializer.deserialize( payload, Float.class );
126         assertEquals( 1.2f, res, 0.0d );
127     }
128 
129     @Test
130     public void validateFloatArraySerialization()
131         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
132     {
133         float[] value = new float[]{ 1.1f, 4.05f, 55.5f };
134         byte[] payload = serializer.serialize( value );
135         float[] res = serializer.deserialize( payload, float[].class );
136         assertArrayEquals( value, res, 0.0f );
137     }
138 
139     @Test
140     public void validateIntegerSerialization()
141         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
142     {
143         byte[] payload = serializer.serialize( 1 );
144         int res = serializer.deserialize( payload, Integer.class );
145         assertEquals( 1, res );
146     }
147 
148     @Test
149     public void validateIntegerArraySerialization()
150         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
151     {
152         int[] value = new int[]{ 3, 1, -1 };
153         byte[] payload = serializer.serialize( value );
154         int[] res = serializer.deserialize( payload, int[].class );
155         assertArrayEquals( value, res );
156     }
157 
158     @Test
159     public void validateLongSerialization()
160         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
161     {
162         byte[] payload = serializer.serialize( 1L );
163         long res = serializer.deserialize( payload, Long.class );
164         assertEquals( 1, res );
165     }
166 
167     @Test
168     public void validateLongArraySerialization()
169         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
170     {
171         long[] value = new long[]{ 1l, 3l, 1212121212121l };
172         byte[] payload = serializer.serialize( value );
173         long[] res = serializer.deserialize( payload, long[].class );
174         assertArrayEquals( value, res );
175     }
176 
177     @Test
178     public void validateShortSerialization()
179         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
180     {
181         byte[] payload = serializer.serialize( (short) 1234 );
182         short res = serializer.deserialize( payload, Short.class );
183         assertEquals( 1234, res );
184     }
185 
186     @Test
187     public void validateShortArraySerialization()
188         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
189     {
190         short[] value = new short[]{ 1, -1, 4, 32767 };
191         byte[] payload = serializer.serialize( value );
192         short[] res = serializer.deserialize( payload, short[].class );
193         assertArrayEquals( value, res );
194     }
195 
196     @Test
197     public void validateStringSerialization()
198         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
199     {
200         String value = "a sample string to serialize";
201         byte[] payload = serializer.serialize( value );
202         String res = serializer.deserialize( payload, String.class );
203         assertEquals( value, res );
204     }
205 
206     @Test
207     public void validateStringArraySerialization()
208         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
209     {
210         String[] value = new String[]{ "String1", "", "String2" };
211         byte[] payload = serializer.serialize( value );
212         String[] res = serializer.deserialize( payload, String[].class );
213         assertArrayEquals( value, res );
214     }
215 }