001: /*
002:
003: * $Id: TestRecordManager.java,v 1.6 2003/09/21 15:49:02 boisvert Exp $
004:
005: *
006:
007: * Unit tests for RecordManager class
008:
009: *
010:
011: * Simple db toolkit
012:
013: * Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
014:
015: *
016:
017: * This library is free software; you can redistribute it and/or
018:
019: * modify it under the terms of the GNU Library General Public License
020:
021: * as published by the Free Software Foundation; either version 2
022:
023: * of the License, or (at your option) any later version.
024:
025: *
026:
027: * This library is distributed in the hope that it will be useful,
028:
029: * but WITHOUT ANY WARRANTY; without even the implied warranty of
030:
031: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
032:
033: * Library General Public License for more details.
034:
035: *
036:
037: * You should have received a copy of the GNU Library General Public License
038:
039: * along with this library; if not, write to the Free Software Foundation,
040:
041: * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
042:
043: */
044:
045: package jdbm.recman;
046:
047: import jdbm.RecordManager;
048:
049: import jdbm.RecordManagerFactory;
050:
051: import junit.framework.*;
052:
053: /**
054:
055: * This class contains all Unit tests for {@link RecordManager}.
056:
057: */
058:
059: public class TestRecordManager extends TestCase {
060:
061: public TestRecordManager(String name) {
062:
063: super (name);
064:
065: }
066:
067: public void setUp() {
068:
069: TestRecordFile.deleteTestFile();
070:
071: }
072:
073: public void tearDown() {
074:
075: TestRecordFile.deleteTestFile();
076:
077: }
078:
079: /**
080:
081: * Test constructor
082:
083: */
084:
085: public void testCtor()
086:
087: throws Exception
088:
089: {
090:
091: RecordManager recman;
092:
093: recman = RecordManagerFactory
094: .createRecordManager(TestRecordFile.testFileName);
095:
096: recman.close();
097:
098: }
099:
100: /**
101:
102: * Test basics
103:
104: */
105:
106: public void testBasics()
107:
108: throws Exception
109:
110: {
111:
112: RecordManager recman;
113:
114: recman = new BaseRecordManager(TestRecordFile.testFileName);
115:
116: // insert a 10,000 byte record.
117:
118: byte[] data = TestUtil.makeRecord(10000, (byte) 1);
119:
120: long rowid = recman.insert(data);
121:
122: assertTrue("check data1",
123:
124: TestUtil.checkRecord((byte[]) recman.fetch(rowid), 10000,
125: (byte) 1));
126:
127: // update it as a 20,000 byte record.
128:
129: data = TestUtil.makeRecord(20000, (byte) 2);
130:
131: recman.update(rowid, data);
132:
133: assertTrue("check data2",
134:
135: TestUtil.checkRecord((byte[]) recman.fetch(rowid), 20000,
136: (byte) 2));
137:
138: // insert a third record.
139:
140: data = TestUtil.makeRecord(20, (byte) 3);
141:
142: long rowid2 = recman.insert(data);
143:
144: assertTrue("check data3",
145:
146: TestUtil.checkRecord((byte[]) recman.fetch(rowid2), 20,
147: (byte) 3));
148:
149: // now, grow the first record again
150:
151: data = TestUtil.makeRecord(30000, (byte) 4);
152:
153: recman.update(rowid, data);
154:
155: assertTrue("check data4",
156:
157: TestUtil.checkRecord((byte[]) recman.fetch(rowid), 30000,
158: (byte) 4));
159:
160: // delete the record
161:
162: recman.delete(rowid);
163:
164: // close the file
165:
166: recman.close();
167:
168: }
169:
170: /**
171:
172: * Test delete and immediate reuse. This attempts to reproduce
173:
174: * a bug in the stress test involving 0 record lengths.
175:
176: */
177:
178: public void testDeleteAndReuse()
179:
180: throws Exception
181:
182: {
183:
184: RecordManager recman;
185:
186: recman = new BaseRecordManager(TestRecordFile.testFileName);
187:
188: // insert a 1500 byte record.
189:
190: byte[] data = TestUtil.makeRecord(1500, (byte) 1);
191:
192: long rowid = recman.insert(data);
193:
194: assertTrue("check data1",
195:
196: TestUtil.checkRecord((byte[]) recman.fetch(rowid), 1500,
197: (byte) 1));
198:
199: // delete the record
200:
201: recman.delete(rowid);
202:
203: // insert a 0 byte record. Should have the same rowid.
204:
205: data = TestUtil.makeRecord(0, (byte) 2);
206:
207: long rowid2 = recman.insert(data);
208:
209: assertEquals("old and new rowid", rowid, rowid2);
210:
211: assertTrue("check data2",
212:
213: TestUtil.checkRecord((byte[]) recman.fetch(rowid2), 0,
214: (byte) 2));
215:
216: // now make the record a bit bigger
217:
218: data = TestUtil.makeRecord(10000, (byte) 3);
219:
220: recman.update(rowid, data);
221:
222: assertTrue("check data3",
223:
224: TestUtil.checkRecord((byte[]) recman.fetch(rowid), 10000,
225: (byte) 3));
226:
227: // .. and again
228:
229: data = TestUtil.makeRecord(30000, (byte) 4);
230:
231: recman.update(rowid, data);
232:
233: assertTrue("check data3",
234:
235: TestUtil.checkRecord((byte[]) recman.fetch(rowid), 30000,
236: (byte) 4));
237:
238: // close the file
239:
240: recman.close();
241:
242: }
243:
244: /**
245:
246: * Test rollback sanity. Attemts to add a new record, rollback and
247:
248: * add the same record. We should obtain the same record id for both
249:
250: * operations.
251:
252: */
253:
254: public void testRollback()
255:
256: throws Exception
257:
258: {
259:
260: RecordManager recman;
261:
262: // Note: We start out with an empty file
263:
264: recman = new BaseRecordManager(TestRecordFile.testFileName);
265:
266: // insert a 150000 byte record.
267:
268: byte[] data1 = TestUtil.makeRecord(150000, (byte) 1);
269:
270: long rowid1 = recman.insert(data1);
271:
272: assertTrue("check data1",
273:
274: TestUtil.checkRecord((byte[]) recman.fetch(rowid1), 150000,
275: (byte) 1));
276:
277: // rollback transaction, should revert to previous state
278:
279: recman.rollback();
280:
281: // insert same 150000 byte record.
282:
283: byte[] data2 = TestUtil.makeRecord(150000, (byte) 1);
284:
285: long rowid2 = recman.insert(data2);
286:
287: assertTrue("check data2",
288:
289: TestUtil.checkRecord((byte[]) recman.fetch(rowid2), 150000,
290: (byte) 1));
291:
292: assertEquals("old and new rowid", rowid1, rowid2);
293:
294: recman.commit();
295:
296: // insert a 150000 byte record.
297:
298: data1 = TestUtil.makeRecord(150000, (byte) 2);
299:
300: rowid1 = recman.insert(data1);
301:
302: assertTrue("check data1",
303:
304: TestUtil.checkRecord((byte[]) recman.fetch(rowid1), 150000,
305: (byte) 2));
306:
307: // rollback transaction, should revert to previous state
308:
309: recman.rollback();
310:
311: // insert same 150000 byte record.
312:
313: data2 = TestUtil.makeRecord(150000, (byte) 2);
314:
315: rowid2 = recman.insert(data2);
316:
317: assertTrue("check data2",
318:
319: TestUtil.checkRecord((byte[]) recman.fetch(rowid2), 150000,
320: (byte) 2));
321:
322: assertEquals("old and new rowid", rowid1, rowid2);
323:
324: // close the file
325:
326: recman.close();
327:
328: }
329:
330: /**
331:
332: * Runs all tests in this class
333:
334: */
335:
336: public static void main(String[] args) {
337:
338: junit.textui.TestRunner.run(new TestSuite(
339: TestRecordManager.class));
340:
341: }
342:
343: }
|