KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
25
26 import db4ounit.Assert;
27
28 public class BTreeAddRemoveTestCase extends BTreeTestCaseBase {
29     
30     public void testFirstPointerMultiTransactional(){
31         int count = BTREE_NODE_SIZE + 1;
32         for (int i = 0; i < count; i++) {
33             add(count + i + 1);
34         }
35         int smallest = count + 1;
36         Transaction trans = newTransaction();
37         for (int i = 0; i < count; i++) {
38             add(trans, i);
39         }
40         final BTreePointer firstPointer = _btree.firstPointer(trans());
41         assertPointerKey(smallest, firstPointer);
42     }
43     
44     public void testSingleRemoveAdd() {
45         
46         final int element = 1;
47         add(element);
48         assertSize(1);
49         
50         remove(element);
51         assertSize(0);
52         
53         add(element);
54         
55         assertSingleElement(element);
56     }
57     
58     public void testSearchingRemoved() {
59         final int[] keys = new int[] { 3, 4, 7, 9 };
60         add(keys);
61         remove(4);
62         final BTreeRange result = search(4);
63         Assert.isTrue(result.isEmpty());
64         
65         final BTreeRange range = result.greater();
66         BTreeAssert.assertRange(new int[] { 7, 9 }, range);
67     }
68
69     public void testMultipleRemoveAdds() {
70         
71         final int element = 1;
72         
73         add(element);
74         remove(element);
75         remove(element);
76         add(element);
77         
78         assertSingleElement(element);
79     }
80     
81     public void testMultiTransactionCancelledRemoval() {
82         final int element = 1;
83         add(element);
84         commit();
85         
86         final Transaction trans1 = newTransaction();
87         final Transaction trans2 = newTransaction();
88         
89         remove(trans1, element);
90         assertSingleElement(trans2, element);
91         add(trans1, element);
92         assertSingleElement(trans1, element);
93         assertSingleElement(trans2, element);
94         
95         trans1.commit();
96         assertSingleElement(element);
97     }
98     
99     public void testMultiTransactionSearch() {
100         
101         final int[] keys = new int[] { 3, 4, 7, 9 };
102         add(trans(), keys);
103         commit(trans());
104         
105         final int[] assorted = new int[] { 1, 2, 11, 13, 21, 52, 51, 66, 89, 10 };
106         add(systemTrans(), assorted);
107         assertKeys(keys);
108         
109         remove(systemTrans(), assorted);
110         assertKeys(keys);
111         
112         BTreeAssert.assertRange(new int[] { 7, 9 }, search(trans(), 4).greater());
113     }
114
115     private void assertKeys(final int[] keys) {
116         BTreeAssert.assertKeys(trans(), _btree, keys);
117     }
118
119     public void testAddRemoveInDifferentTransactions() {
120         
121         final int element = 1;
122         
123         add(trans(), element);
124         add(systemTrans(), element);
125         
126         remove(systemTrans(), element);
127         remove(trans(), element);
128         
129         assertEmpty(systemTrans());
130         assertEmpty(trans());
131     }
132     
133     public void testRemoveAddInDifferentTransactions() {
134         final int element = 1;
135         
136         add(element);
137         
138         db().commit();
139         
140         remove(trans(), element);
141         remove(systemTrans(), element);
142         
143         assertEmpty(systemTrans());
144         assertEmpty(trans());
145         
146         add(trans(), element);
147         assertSingleElement(trans(), element);
148         
149         add(systemTrans(), element);
150         assertSingleElement(systemTrans(), element);
151     }
152     
153     public static void main(String JavaDoc[] args) {
154         new BTreeAddRemoveTestCase().runSolo();
155     }
156 }
157
Popular Tags