1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38
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();
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 }