KickJava   Java API By Example, From Geeks To Geeks.

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


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.Transaction;
24 import com.db4o.inside.btree.BTree;
25 import com.db4o.inside.btree.BTreePointer;
26 import com.db4o.inside.btree.BTreeRange;
27
28 import db4ounit.Assert;
29 import db4ounit.extensions.AbstractDb4oTestCase;
30 import db4ounit.extensions.fixtures.OptOutCS;
31
32 public abstract class BTreeTestCaseBase extends AbstractDb4oTestCase implements
33         OptOutCS {
34
35     protected static final int BTREE_NODE_SIZE = 4;
36
37     protected BTree _btree;
38
39     protected void db4oSetupAfterStore() throws Exception JavaDoc {
40         _btree = newBTree();
41     }
42
43     protected BTree newBTree() {
44         return BTreeAssert.createIntKeyBTree(stream(), 0, BTREE_NODE_SIZE);
45     }
46
47     protected BTreeRange range(int lower, int upper) {
48         final BTreeRange lowerRange = search(lower);
49         final BTreeRange upperRange = search(upper);
50         return lowerRange.extendToLastOf(upperRange);
51     }
52
53     protected BTreeRange search(int key) {
54         return search(trans(), key);
55     }
56
57     protected void add(int[] keys) {
58         for (int i = 0; i < keys.length; ++i) {
59             add(keys[i]);
60         }
61     }
62
63     protected BTreeRange search(Transaction trans, int key) {
64         return _btree.search(trans, new Integer JavaDoc(key));
65     }
66
67     protected void commit(Transaction trans) {
68         _btree.commit(trans);
69     }
70
71     protected void commit() {
72         commit(trans());
73     }
74
75     protected void remove(Transaction transaction, int[] keys) {
76         for (int i = 0; i < keys.length; i++) {
77             remove(transaction, keys[i]);
78         }
79     }
80
81     protected void add(Transaction transaction, int[] keys) {
82         for (int i = 0; i < keys.length; i++) {
83             add(transaction, keys[i]);
84         }
85     }
86
87     protected void assertEmpty(Transaction transaction) {
88         BTreeAssert.assertEmpty(transaction, _btree);
89     }
90
91     protected void add(Transaction transaction, int element) {
92         _btree.add(transaction, new Integer JavaDoc(element));
93     }
94
95     protected void remove(final int element) {
96         remove(trans(), element);
97     }
98
99     protected void remove(final Transaction trans, final int element) {
100         _btree.remove(trans, new Integer JavaDoc(element));
101     }
102
103     protected void add(int element) {
104         add(trans(), element);
105     }
106
107     private int size() {
108         return _btree.size(trans());
109     }
110
111     protected void assertSize(int expected) {
112         Assert.areEqual(expected, size());
113     }
114
115     protected void assertSingleElement(final int element) {
116         assertSingleElement(trans(), element);
117     }
118
119     protected void assertSingleElement(final Transaction trans,
120             final int element) {
121         BTreeAssert.assertSingleElement(trans, _btree, new Integer JavaDoc(element));
122     }
123
124     protected void assertPointerKey(int key, BTreePointer pointer) {
125         Assert.areEqual(new Integer JavaDoc(key), pointer.key());
126     }
127 }
128
Popular Tags