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;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.UUID;
29  
30  import org.apache.commons.io.FileUtils;
31  import org.apache.directory.mavibot.btree.serializer.IntSerializer;
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Rule;
35  import org.junit.Test;
36  import org.junit.rules.TemporaryFolder;
37  
38  /**
39   * Tests for KeyCursor of a persisted sub-Btree.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class PersistedSubBtreeKeyCursorTest
44  {
45      private BTree<Integer, Integer> btree = null;
46  
47      private RecordManager recordManager = null;
48  
49      @Rule
50      public TemporaryFolder tempFolder = new TemporaryFolder();
51  
52      private File dataDir = null;
53  
54  
55      @Before
56      public void createBTree() throws IOException
57      {
58          dataDir = tempFolder.newFolder( UUID.randomUUID().toString() );
59  
60          // Now, try to reload the file back
61          recordManager = new RecordManager( dataDir.getAbsolutePath() );
62  
63          try
64          {
65              PersistedBTreeConfiguration<Integer, Integer> configuration = new PersistedBTreeConfiguration<Integer, Integer>();
66              configuration.setAllowDuplicates( false );
67              configuration.setKeySerializer( IntSerializer.INSTANCE );
68              configuration.setValueSerializer( IntSerializer.INSTANCE );
69              configuration.setName( "sub-btree" );
70              configuration.setBtreeType( BTreeTypeEnum.PERSISTED_SUB );
71  
72              btree = BTreeFactory.createPersistedBTree( configuration );
73  
74              recordManager.manage( btree, RecordManager.INTERNAL_BTREE );
75          }
76          catch ( Exception e )
77          {
78              throw new RuntimeException( e );
79          }
80      }
81  
82  
83      @After
84      public void cleanup() throws IOException
85      {
86          dataDir = new File( System.getProperty( "java.io.tmpdir" ) + "/recordman" );
87  
88          btree.close();
89  
90          if ( dataDir.exists() )
91          {
92              FileUtils.deleteDirectory( dataDir );
93          }
94          
95          recordManager.close();
96          assertTrue( recordManager.isContextOk() );
97      }
98  
99      @Test
100     public void testBrowseKeys() throws Exception
101     {
102         for( int i=0; i< 10; i++ )
103         {
104             // only the keys are stored, values are ignored
105             btree.insert( i, i );
106         }
107         
108         KeyCursor<Integer> cursor = btree.browseKeys();
109         
110         for( int i=0; i< 10; i++ )
111         {
112             assertTrue( cursor.hasNext() );
113             assertEquals( String.valueOf( i ), String.valueOf( cursor.next() ) );
114         }
115         
116         assertFalse( cursor.hasNext() );
117 
118         cursor.afterLast();
119         
120         for( int i=9; i>= 0; i-- )
121         {
122             assertTrue( cursor.hasPrev() );
123             assertEquals( String.valueOf( i ), String.valueOf( cursor.prev() ) );
124         }
125 
126         assertFalse( cursor.hasPrev() );
127         cursor.close();
128     }
129 }