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