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.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.junit.After;
28  
29  import java.util.Comparator;
30  import java.io.File;
31  import java.io.IOException;
32  
33  import jdbm.RecordManager;
34  import jdbm.btree.BTree;
35  import jdbm.helper.IntegerSerializer;
36  import jdbm.helper.IntegerComparator;
37  import jdbm.helper.TupleBrowser;
38  import jdbm.helper.Tuple;
39  import jdbm.recman.BaseRecordManager;
40  import static junit.framework.Assert.assertTrue;
41  import static junit.framework.Assert.assertEquals;
42  
43  
44  /**
45   * A test case to confirm the JDBM browser issue.
46   *
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   * @version $$Rev$$
49   */
50  public class JdbmBrowserBugTest
51  {
52      Comparator<Integer> comparator;
53      BTree bt;
54      private static final String TEST_OUTPUT_PATH = "test.output.path";
55      private static final Logger LOG = LoggerFactory.getLogger( JdbmBrowserBugTest.class.getSimpleName() );
56      private File dbFile = null;
57      private RecordManager recman = null;
58  
59      @Before
60      public void createTree() throws Exception
61      {
62          comparator = new Comparator<Integer>()
63          {
64            public int compare( Integer i1, Integer i2 )
65            {
66                return i1.compareTo( i2 );
67            }
68          };
69  
70          File tmpDir = null;
71          if ( System.getProperty( TEST_OUTPUT_PATH, null ) != null )
72          {
73              tmpDir = new File( System.getProperty( TEST_OUTPUT_PATH ) );
74          }
75  
76          dbFile = File.createTempFile( getClass().getSimpleName(), "db", tmpDir );
77          recman = new BaseRecordManager( dbFile.getAbsolutePath() );
78          bt = BTree.createInstance( recman, new IntegerComparator(), new IntegerSerializer(), new IntegerSerializer() );
79          LOG.debug( "created new BTree" );
80      }
81  
82  
83      @After
84      public void cleanup() throws IOException
85      {
86          recman.close();
87          recman = null;
88          bt = null;
89          
90          if ( dbFile.exists() )
91          {
92              String fileToDelete = dbFile.getAbsolutePath();
93              new File( fileToDelete ).delete();
94              new File( fileToDelete + ".db" ).delete();
95              new File( fileToDelete + ".lg" ).delete();
96  
97              dbFile.delete();
98          }
99          
100         dbFile = null;
101     }
102 
103 
104     @Test
105     public void testDirectionChange() throws Exception
106     {
107         bt.insert( 3, 3, true );
108         bt.insert( 5, 3, true );
109         bt.insert( 7, 3, true );
110         bt.insert( 12, 3, true );
111         bt.insert( 0, 3, true );
112         bt.insert( 30, 3, true );
113         bt.insert( 25, 3, true );
114 
115         Tuple tuple = new Tuple();
116         TupleBrowser browser = bt.browse( null );
117         assertTrue( browser.getPrevious( tuple ) );
118         //noinspection AssertEqualsBetweenInconvertibleTypes
119         assertEquals( 30, tuple.getKey() );
120 
121         assertTrue( browser.getPrevious( tuple ) );
122         //noinspection AssertEqualsBetweenInconvertibleTypes
123         assertEquals( 25, tuple.getKey() );
124 
125         assertTrue( browser.getNext( tuple ) );
126         //noinspection AssertEqualsBetweenInconvertibleTypes
127         assertEquals( "If this works the jdbm bug is gone: will start to return " +
128             "30 instead as expected for correct operation", 25, tuple.getKey() );
129     }
130 }