001: /*
002: * $Id: TestTransactionManager.java,v 1.4 2003/08/07 06:53:58 boisvert Exp $
003: *
004: * Unit tests for TransactionManager class
005: *
006: * Simple db toolkit
007: * Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Library General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Library General Public License for more details.
018: *
019: * You should have received a copy of the GNU Library General Public License
020: * along with this library; if not, write to the Free Software Foundation,
021: * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
022: */
023: package jdbm.recman;
024:
025: import junit.framework.*;
026: import java.io.*;
027:
028: /**
029: * This class contains all Unit tests for {@link TransactionManager}.
030: */
031: public class TestTransactionManager extends TestCase {
032:
033: public TestTransactionManager(String name) {
034: super (name);
035: }
036:
037: public void setUp() {
038: TestRecordFile.deleteTestFile();
039: }
040:
041: public void tearDown() {
042: TestRecordFile.deleteTestFile();
043: }
044:
045: /**
046: * Test constructor. Oops - can only be done indirectly :-)
047: */
048: public void testCtor() throws Exception {
049: RecordFile file = new RecordFile(TestRecordFile.testFileName);
050:
051: file.forceClose();
052: }
053:
054: /**
055: * Test recovery
056: */
057: public void XtestRecovery() throws Exception {
058: RecordFile file1 = new RecordFile(TestRecordFile.testFileName);
059:
060: // Do three transactions.
061: for (int i = 0; i < 3; i++) {
062: BlockIo node = file1.get(i);
063: node.setDirty();
064: file1.release(node);
065: file1.commit();
066: }
067: assertDataSizeEquals("len1", 0);
068: assertLogSizeNotZero("len1");
069:
070: file1.forceClose();
071:
072: // Leave the old record file in flux, and open it again.
073: // The second instance should start recovery.
074: RecordFile file2 = new RecordFile(TestRecordFile.testFileName);
075:
076: assertDataSizeEquals("len2", 3 * RecordFile.BLOCK_SIZE);
077: assertLogSizeEquals("len2", 8);
078:
079: file2.forceClose();
080:
081: // assure we can recover this log file
082: RecordFile file3 = new RecordFile(TestRecordFile.testFileName);
083:
084: file3.forceClose();
085: }
086:
087: /**
088: * Test background synching
089: */
090: public void XtestSynching() throws Exception {
091: RecordFile file1 = new RecordFile(TestRecordFile.testFileName);
092:
093: // Do enough transactions to fill the first slot
094: int txnCount = TransactionManager.DEFAULT_TXNS_IN_LOG + 5;
095: for (int i = 0; i < txnCount; i++) {
096: BlockIo node = file1.get(i);
097: node.setDirty();
098: file1.release(node);
099: file1.commit();
100: }
101: file1.forceClose();
102:
103: // The data file now has the first slotfull
104: assertDataSizeEquals("len1",
105: TransactionManager.DEFAULT_TXNS_IN_LOG
106: * RecordFile.BLOCK_SIZE);
107: assertLogSizeNotZero("len1");
108:
109: // Leave the old record file in flux, and open it again.
110: // The second instance should start recovery.
111: RecordFile file2 = new RecordFile(TestRecordFile.testFileName);
112:
113: assertDataSizeEquals("len2", txnCount * RecordFile.BLOCK_SIZE);
114: assertLogSizeEquals("len2", 8);
115:
116: file2.forceClose();
117: }
118:
119: // Helpers
120:
121: void assertDataSizeEquals(String msg, long size) {
122: assertEquals(msg + " data size", size, new File(
123: TestRecordFile.testFileName + RecordFile.extension)
124: .length());
125: }
126:
127: void assertLogSizeEquals(String msg, long size) {
128: assertEquals(msg + " log size", size, new File(
129: TestRecordFile.testFileName
130: + TransactionManager.extension).length());
131: }
132:
133: void assertLogSizeNotZero(String msg) {
134: assertTrue(msg + " log size", new File(
135: TestRecordFile.testFileName
136: + TransactionManager.extension).length() != 0);
137: }
138:
139: /**
140: * Runs all tests in this class
141: */
142: public static void main(String[] args) {
143: junit.textui.TestRunner.run(new TestSuite(
144: TestTransactionManager.class));
145: }
146: }
|