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.server.core.partition.impl.btree.jdbm;
21  
22  
23  import org.junit.Test;
24  import org.junit.Before;
25  import org.apache.directory.shared.ldap.util.ArrayUtils;
26  
27  import static junit.framework.Assert.*;
28  
29  import java.util.Random;
30  import java.io.IOException;
31  
32  
33  /**
34   * Test case for the BTreeRedirect serialization code.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev$
38   */
39  public class BTreeRedirectMarshallerTest
40  {
41      byte[] bites = new byte[BTreeRedirectMarshaller.SIZE];
42      BTreeRedirectMarshaller marshaller = new BTreeRedirectMarshaller();
43  
44  
45      @Before
46      public void setup()
47      {
48          bites[0] = 1;
49  
50          for ( int ii = 8; ii < BTreeRedirectMarshaller.SIZE; ii++ )
51          {
52              bites[ii] = 0;
53          }
54      }
55  
56  
57      @Test
58      public void testZero() throws IOException
59      {
60          assertEquals( 0, marshaller.deserialize( bites ).getRecId() );
61          assertTrue( ArrayUtils.isEquals( bites, marshaller.serialize( new BTreeRedirect( 0 ) ) ) );
62      }
63  
64  
65      @Test
66      public void testOne() throws IOException
67      {
68          bites[8] = 1;
69          assertEquals( 1, marshaller.deserialize( bites ).getRecId() );
70          assertTrue( ArrayUtils.isEquals( bites, marshaller.serialize( new BTreeRedirect( 1 ) ) ) );
71      }
72  
73  
74      @Test
75      public void testNegativeOne() throws IOException
76      {
77          for ( int ii = 1; ii < BTreeRedirectMarshaller.SIZE; ii++ )
78          {
79              bites[ii] =  ( byte ) 0xFF;
80          }
81  
82          assertEquals( -1, marshaller.deserialize( bites ).getRecId() );
83          assertTrue( ArrayUtils.isEquals( bites, marshaller.serialize( new BTreeRedirect( -1 ) ) ) );
84      }
85  
86  
87      @Test
88      public void testLongMinValue() throws IOException
89      {
90          bites[1] = ( byte ) 0x80;
91          assertEquals( Long.MIN_VALUE, marshaller.deserialize( bites ).getRecId() );
92          assertTrue( ArrayUtils.isEquals( bites, marshaller.serialize( new BTreeRedirect( Long.MIN_VALUE ) ) ) );
93      }
94  
95  
96      @Test
97      public void testLongMaxValue() throws IOException
98      {
99          bites[1] = ( byte ) 0x7F;
100 
101         for ( int ii = 2; ii < BTreeRedirectMarshaller.SIZE; ii++ )
102         {
103             bites[ii] =  ( byte ) 0xFF;
104         }
105 
106         assertEquals( Long.MAX_VALUE, marshaller.deserialize( bites ).getRecId() );
107         assertTrue( ArrayUtils.isEquals( bites, marshaller.serialize( new BTreeRedirect( Long.MAX_VALUE ) ) ) );
108     }
109 
110 
111     @Test
112     public void testRoundTripTests() throws IOException
113     {
114         Random random = new Random();
115         for ( int ii = 0; ii < 100; ii++ )
116         {
117             long orig = random.nextLong();
118             bites = marshaller.serialize( new BTreeRedirect( orig ) );
119             assertEquals( orig, marshaller.deserialize( bites ).getRecId() );
120         }
121     }
122 
123 
124     @Test
125     public void testMiscellaneous()
126     {
127         assertNotNull( new BTreeRedirect( 1 ).toString() );
128         assertFalse( BTreeRedirectMarshaller.isRedirect( null ) );
129 
130         try
131         {
132             marshaller.deserialize( null );
133             fail( "Should not get here." );
134         }
135         catch ( IOException e )
136         {
137         }
138 
139 
140         try
141         {
142             marshaller.deserialize( "bogus".getBytes() );
143             fail( "Should not get here." );
144         }
145         catch ( IOException e )
146         {
147         }
148     }
149 }