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  
21  package org.apache.directory.mavibot.btree.managed;
22  
23  
24  import static org.junit.Assert.assertEquals;
25  
26  import java.io.File;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.directory.mavibot.btree.Tuple;
31  import org.apache.directory.mavibot.btree.TupleCursor;
32  import org.apache.directory.mavibot.btree.serializer.IntSerializer;
33  import org.junit.Test;
34  
35  
36  /**
37   * Test cases for ManagedBTreeBuilder.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class ManagedBTreeBuilderTest
42  {
43  
44      @Test
45      public void testManagedBTreeBuilding() throws Exception
46      {
47          List<Tuple<Integer, Integer>> sortedTuple = new ArrayList<Tuple<Integer, Integer>>();
48          for ( int i = 1; i < 8; i++ )
49          {
50              Tuple<Integer, Integer> t = new Tuple<Integer, Integer>( i, i );
51              sortedTuple.add( t );
52          }
53  
54          File file = File.createTempFile( "managedbtreebuilder", ".data" );
55          file.deleteOnExit();
56          
57          RecordManager rm = new RecordManager( file.getAbsolutePath() );
58          
59          IntSerializer ser = new IntSerializer();
60          ManagedBTreeBuilder<Integer, Integer> bb = new ManagedBTreeBuilder<Integer, Integer>( rm, "master", 4, ser, ser );
61  
62          // contains 1, 2, 3, 4, 5, 6, 7
63          BTree<Integer, Integer> btree = bb.build( sortedTuple.iterator() );
64  
65          rm.close();
66          
67          rm = new RecordManager( file.getAbsolutePath() );
68          btree = rm.getManagedTree( "master" );
69          
70          assertEquals( 1, btree.rootPage.getNbElems() );
71  
72          assertEquals( 7, btree.rootPage.findRightMost().getKey().intValue() );
73  
74          assertEquals( 1, btree.rootPage.findLeftMost().getKey().intValue() );
75  
76          TupleCursor<Integer, Integer> cursor = btree.browse();
77          int i = 0;
78  
79          while ( cursor.hasNext() )
80          {
81              Tuple<Integer, Integer> expected = sortedTuple.get( i++ );
82              Tuple<Integer, Integer> actual = cursor.next();
83              assertEquals( expected.getKey(), actual.getKey() );
84              assertEquals( expected.getValue(), actual.getValue() );
85          }
86  
87          cursor.close();
88          btree.close();
89      }
90  
91  }