KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > btree > BTreePointerTestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.common.btree;
22
23 import com.db4o.db4ounit.common.foundation.IntArrays4;
24 import com.db4o.foundation.Iterator4;
25 import com.db4o.inside.btree.*;
26
27 import db4ounit.Assert;
28
29 /**
30  * @exclude
31  */

32 public class BTreePointerTestCase extends BTreeTestCaseBase {
33     
34     public static void main(String JavaDoc[] args) {
35         new BTreePointerTestCase().runSolo();
36     }
37
38     private final int[] keys = new int[] {
39             -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 7, 9
40     };
41     
42     protected void db4oSetupAfterStore() throws Exception JavaDoc {
43         super.db4oSetupAfterStore();
44         add(keys);
45         commit();
46     }
47     
48     public void testLastPointer(){
49         BTreePointer pointer = _btree.lastPointer(trans());
50         assertPointerKey(9, pointer);
51     }
52     
53     public void testPrevious(){
54         BTreePointer pointer = getPointerForKey(3);
55         BTreePointer previousPointer = pointer.previous();
56         assertPointerKey(2, previousPointer);
57     }
58
59     public void testNextOperatesInReadMode() {
60         BTreePointer pointer = _btree.firstPointer(trans());
61         assertReadModePointerIteration(keys, pointer);
62     }
63     
64     public void testSearchOperatesInReadMode() {
65         final BTreePointer pointer = getPointerForKey(3);
66         assertReadModePointerIteration(
67                 new int[] { 3, 4, 7, 9 },
68                 pointer);
69     }
70
71     private BTreePointer getPointerForKey(final int key) {
72         final BTreeRange range = search(key);
73         final Iterator4 pointers = range.pointers();
74         Assert.isTrue(pointers.moveNext());
75         final BTreePointer pointer = (BTreePointer) pointers.current();
76         return pointer;
77     }
78
79     private void assertReadModePointerIteration(final int[] expectedKeys, BTreePointer pointer) {
80         Object JavaDoc[] expected = IntArrays4.toObjectArray(expectedKeys);
81         for (int i = 0; i < expected.length; i++) {
82             Assert.isNotNull(pointer, "Expected '" + expected[i] + "'");
83             Assert.areNotSame(_btree.root(), pointer.node());
84             assertInReadMode(pointer.node());
85             Assert.areEqual(expected[i], pointer.key());
86             assertInReadMode(pointer.node());
87             pointer = pointer.next();
88         }
89     }
90
91     private void assertInReadMode(BTreeNode node) {
92         Assert.isFalse(node.canWrite());
93     }
94     
95     protected BTree newBTree() {
96         return newBTreeWithNoNodeCaching();
97     }
98
99     private BTree newBTreeWithNoNodeCaching() {
100         return BTreeAssert.createIntKeyBTree(stream(), 0, 0, BTREE_NODE_SIZE);
101     }
102
103 }
104
Popular Tags