001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.btree;
022:
023: import com.db4o.internal.*;
024: import com.db4o.internal.btree.*;
025:
026: import db4ounit.Assert;
027:
028: public class BTreeAddRemoveTestCase extends BTreeTestCaseBase {
029:
030: public void testFirstPointerMultiTransactional() {
031: int count = BTREE_NODE_SIZE + 1;
032: for (int i = 0; i < count; i++) {
033: add(count + i + 1);
034: }
035: int smallest = count + 1;
036: Transaction trans = newTransaction();
037: for (int i = 0; i < count; i++) {
038: add(trans, i);
039: }
040: final BTreePointer firstPointer = _btree.firstPointer(trans());
041: assertPointerKey(smallest, firstPointer);
042: }
043:
044: public void testSingleRemoveAdd() {
045:
046: final int element = 1;
047: add(element);
048: assertSize(1);
049:
050: remove(element);
051: assertSize(0);
052:
053: add(element);
054:
055: assertSingleElement(element);
056: }
057:
058: public void testSearchingRemoved() {
059: final int[] keys = new int[] { 3, 4, 7, 9 };
060: add(keys);
061: remove(4);
062: final BTreeRange result = search(4);
063: Assert.isTrue(result.isEmpty());
064:
065: final BTreeRange range = result.greater();
066: BTreeAssert.assertRange(new int[] { 7, 9 }, range);
067: }
068:
069: public void testMultipleRemoveAdds() {
070:
071: final int element = 1;
072:
073: add(element);
074: remove(element);
075: remove(element);
076: add(element);
077:
078: assertSingleElement(element);
079: }
080:
081: public void testMultiTransactionCancelledRemoval() {
082: final int element = 1;
083: add(element);
084: commit();
085:
086: final Transaction trans1 = newTransaction();
087: final Transaction trans2 = newTransaction();
088:
089: remove(trans1, element);
090: assertSingleElement(trans2, element);
091: add(trans1, element);
092: assertSingleElement(trans1, element);
093: assertSingleElement(trans2, element);
094:
095: trans1.commit();
096: assertSingleElement(element);
097: }
098:
099: 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,
106: 66, 89, 10 };
107: add(systemTrans(), assorted);
108: assertKeys(keys);
109:
110: remove(systemTrans(), assorted);
111: assertKeys(keys);
112:
113: BTreeAssert.assertRange(new int[] { 7, 9 }, search(trans(), 4)
114: .greater());
115: }
116:
117: private void assertKeys(final int[] keys) {
118: BTreeAssert.assertKeys(trans(), _btree, keys);
119: }
120:
121: public void testAddRemoveInDifferentTransactions() {
122:
123: final int element = 1;
124:
125: add(trans(), element);
126: add(systemTrans(), element);
127:
128: remove(systemTrans(), element);
129: remove(trans(), element);
130:
131: assertEmpty(systemTrans());
132: assertEmpty(trans());
133:
134: _btree.commit(systemTrans());
135: _btree.commit(trans());
136:
137: assertEmpty(systemTrans());
138: assertEmpty(trans());
139: }
140:
141: public void testRemoveCommitInDifferentTransactions() {
142:
143: final int element = 1;
144:
145: add(trans(), element);
146: _btree.commit(trans());
147:
148: remove(systemTrans(), element);
149: remove(trans(), element);
150:
151: assertEmpty(systemTrans());
152: assertEmpty(trans());
153:
154: _btree.commit(systemTrans());
155: _btree.commit(trans());
156:
157: assertEmpty(systemTrans());
158: assertEmpty(trans());
159: }
160:
161: public void testRemoveAddInDifferentTransactions() {
162: final int element = 1;
163:
164: add(element);
165:
166: db().commit();
167:
168: remove(trans(), element);
169: remove(systemTrans(), element);
170:
171: assertEmpty(systemTrans());
172: assertEmpty(trans());
173:
174: add(trans(), element);
175: assertSingleElement(trans(), element);
176:
177: add(systemTrans(), element);
178: assertSingleElement(systemTrans(), element);
179: }
180:
181: public void testAddAddRollbackCommmitInDifferentTransactions() {
182: final int element = 1;
183:
184: add(trans(), element);
185: add(systemTrans(), element);
186: db().rollback();
187:
188: assertSingleElement(systemTrans(), element);
189:
190: db().commit();
191:
192: assertSingleElement(trans(), element);
193: assertSingleElement(systemTrans(), element);
194: }
195:
196: public void testMultipleConcurrentRemoves() {
197: int count = 100;
198: for (int i = 0; i < count; i++) {
199: add(trans(), i);
200: }
201: db().commit();
202: Transaction secondTransaction = newTransaction();
203: for (int i = 1; i < count; i++) {
204: if (i % 2 == 0) {
205: remove(trans(), i);
206: } else {
207: remove(secondTransaction, i);
208: }
209: }
210: secondTransaction.commit();
211: db().commit();
212: assertSize(1);
213: }
214:
215: public static void main(String[] args) {
216: new BTreeAddRemoveTestCase().runSolo();
217: }
218: }
|