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,
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.directory.mavibot.btree.serializer;
21  
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.IOException;
27  import java.util.Arrays;
28  
29  import org.apache.directory.mavibot.btree.serializer.BufferHandler;
30  import org.apache.directory.mavibot.btree.serializer.ByteArraySerializer;
31  import org.junit.Test;
32  
33  
34  /**
35   * Test the BytesSerializer class
36   * 
37   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
38   */
39  public class ByteArraySerializerTest
40  {
41      private static ByteArraySerializer serializer = new ByteArraySerializer();
42  
43  
44      @Test
45      public void testBytesSerializer() throws IOException
46      {
47          byte[] value = null;
48          byte[] result = serializer.serialize( value );
49  
50          assertEquals( 4, result.length );
51          assertEquals( ( byte ) 0xFF, result[0] );
52          assertEquals( ( byte ) 0xFF, result[1] );
53          assertEquals( ( byte ) 0xFF, result[2] );
54          assertEquals( ( byte ) 0xFF, result[3] );
55  
56          assertEquals( value, serializer.deserialize( new BufferHandler( result ) ) );
57  
58          // ------------------------------------------------------------------
59          value = new byte[]
60              {};
61          result = serializer.serialize( value );
62  
63          assertEquals( 4, result.length );
64          assertEquals( ( byte ) 0x00, result[0] );
65          assertEquals( ( byte ) 0x00, result[1] );
66          assertEquals( ( byte ) 0x00, result[2] );
67          assertEquals( ( byte ) 0x00, result[3] );
68  
69          assertTrue( Arrays.equals( value, serializer.deserialize( new BufferHandler( result ) ) ) );
70  
71          // ------------------------------------------------------------------
72          value = "test".getBytes();
73          result = serializer.serialize( value );
74  
75          assertEquals( 8, result.length );
76          assertEquals( ( byte ) 0x00, result[0] );
77          assertEquals( ( byte ) 0x00, result[1] );
78          assertEquals( ( byte ) 0x00, result[2] );
79          assertEquals( ( byte ) 0x04, result[3] );
80          assertEquals( 't', result[4] );
81          assertEquals( 'e', result[5] );
82          assertEquals( 's', result[6] );
83          assertEquals( 't', result[7] );
84  
85          assertTrue( Arrays.equals( value, serializer.deserialize( new BufferHandler( result ) ) ) );
86      }
87  }