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  package org.apache.directory.server.core.partition.impl.btree.jdbm;
20  
21  import static junit.framework.Assert.assertEquals;
22  import static junit.framework.Assert.assertFalse;
23  import static junit.framework.Assert.assertTrue;
24  
25  import java.util.Comparator;
26  
27  import org.apache.directory.server.core.avltree.AvlTree;
28  import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
29  import org.apache.directory.server.xdbm.Tuple;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  /**
34   * 
35   * Test case for KeyTupleAvlCursor.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$, $Date$
39   */
40  public class KeyTupleAvlCursorTest
41  {
42  
43      AvlTree<Integer> tree;
44      Comparator<Integer> comparator;
45      KeyTupleAvlCursor<Integer, Integer> cursor;
46      
47      private static final Integer KEY = new Integer( 1 );
48      
49      @Before
50      public void createTree()
51      {
52        comparator = new Comparator<Integer>() 
53        {
54  
55            public int compare( Integer i1, Integer i2 )
56            {
57                return i1.compareTo( i2 );
58            }
59          
60          };
61          
62        tree = new AvlTree<Integer>( comparator );  
63        
64        cursor = new KeyTupleAvlCursor<Integer, Integer>( tree, KEY );
65      }
66      
67      
68      @Test( expected = InvalidCursorPositionException.class )
69      public void testEmptyCursor() throws Exception
70      {
71          assertFalse( cursor.next() );
72          assertFalse( cursor.available() );
73          
74          assertTrue( cursor.isElementReused() );
75          assertFalse( cursor.isClosed() );
76          
77          assertFalse( cursor.first() );
78          assertFalse( cursor.last() );
79          
80          cursor.get(); // should throw InvalidCursorPositionException
81      }
82      
83      
84      @Test
85      public void testNonEmptyCursor() throws Exception
86      {
87          tree.insert( 3 );
88          tree.insert( 5 );
89          tree.insert( 7 );
90          tree.insert( 12 );
91          tree.insert( 0 );
92          tree.insert( 30 );
93          tree.insert( 25 );
94          
95          cursor.before( new Tuple<Integer, Integer>( null, 3) );
96          assertTrue( cursor.next() );
97          assertEquals( 3, ( int ) cursor.get().getValue() );
98          
99          cursor.after( new Tuple<Integer, Integer>( null, 34 ) );
100         assertFalse( cursor.next() );
101 
102         cursor.after( new Tuple<Integer, Integer>( null, 13 ) );
103         assertTrue( cursor.next() );
104         assertEquals( 25, ( int ) cursor.get().getValue() );
105         
106         cursor.beforeFirst();
107         assertFalse( cursor.previous() );
108         assertTrue( cursor.next() );
109         assertEquals( 0, ( int ) cursor.get().getValue() );
110         
111         cursor.afterLast();
112         assertFalse( cursor.next() );
113         
114         assertTrue( cursor.first() );
115         assertTrue( cursor.available() );
116         assertEquals( 0, ( int ) cursor.get().getValue() );
117         
118         assertTrue( cursor.last() );
119         assertTrue( cursor.available() );
120         assertEquals( 30, ( int ) cursor.get().getValue() );
121         
122         assertTrue( cursor.previous() );
123         assertEquals( 25, ( int ) cursor.get().getValue() );
124 
125         assertTrue( cursor.next() );
126         assertEquals( 30, ( int ) cursor.get().getValue() );
127 
128     }
129 }