001: /*
002: * $Id: TestRecordFile.java,v 1.4 2001/11/10 18:36:50 boisvert Exp $
003: *
004: * Unit tests for RecordFile 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.File;
027: import java.io.IOException;
028:
029: /**
030: * This class contains all Unit tests for {@link RecordFile}.
031: */
032: public class TestRecordFile extends TestCase {
033:
034: public final static String testFileName = "test";
035:
036: public TestRecordFile(String name) {
037: super (name);
038: }
039:
040: public static void deleteFile(String filename) {
041: File file = new File(filename);
042:
043: if (file.exists()) {
044: try {
045: file.delete();
046: } catch (Exception except) {
047: except.printStackTrace();
048: }
049: if (file.exists()) {
050: System.out.println("WARNING: Cannot delete file: "
051: + file);
052: }
053: }
054: }
055:
056: public static void deleteTestFile() {
057: System.gc();
058:
059: deleteFile(testFileName);
060:
061: deleteFile(testFileName + RecordFile.extension);
062:
063: deleteFile(testFileName + TransactionManager.extension);
064: }
065:
066: public void setUp() {
067: deleteTestFile();
068: }
069:
070: public void tearDown() {
071: deleteTestFile();
072: }
073:
074: /**
075: * Test constructor
076: */
077: public void testCtor() throws Exception {
078: RecordFile file = new RecordFile(testFileName);
079: file.close();
080: }
081:
082: /**
083: * Test addition of record 0
084: */
085: public void testAddZero() throws Exception {
086: RecordFile file = new RecordFile(testFileName);
087: byte[] data = file.get(0).getData();
088: data[14] = (byte) 'b';
089: file.release(0, true);
090: file.close();
091: file = new RecordFile(testFileName);
092: data = file.get(0).getData();
093: assertEquals((byte) 'b', data[14]);
094: file.release(0, false);
095: file.close();
096: }
097:
098: /**
099: * Test addition of a number of records, with holes.
100: */
101: public void testWithHoles() throws Exception {
102: RecordFile file = new RecordFile(testFileName);
103:
104: // Write recid 0, byte 0 with 'b'
105: byte[] data = file.get(0).getData();
106: data[0] = (byte) 'b';
107: file.release(0, true);
108:
109: // Write recid 10, byte 10 with 'c'
110: data = file.get(10).getData();
111: data[10] = (byte) 'c';
112: file.release(10, true);
113:
114: // Write recid 5, byte 5 with 'e' but don't mark as dirty
115: data = file.get(5).getData();
116: data[5] = (byte) 'e';
117: file.release(5, false);
118:
119: file.close();
120:
121: file = new RecordFile(testFileName);
122: data = file.get(0).getData();
123: assertEquals("0 = b", (byte) 'b', data[0]);
124: file.release(0, false);
125:
126: data = file.get(5).getData();
127: assertEquals("5 = 0", 0, data[5]);
128: file.release(5, false);
129:
130: data = file.get(10).getData();
131: assertEquals("10 = c", (byte) 'c', data[10]);
132: file.release(10, false);
133:
134: file.close();
135: }
136:
137: /**
138: * Test wrong release
139: */
140: public void testWrongRelease() throws Exception {
141: RecordFile file = new RecordFile(testFileName);
142:
143: // Write recid 0, byte 0 with 'b'
144: byte[] data = file.get(0).getData();
145: data[0] = (byte) 'b';
146: try {
147: file.release(1, true);
148: fail("expected exception");
149: } catch (IOException except) {
150: // ignore
151: }
152: file.release(0, false);
153:
154: file.close();
155:
156: // @alex retry to open the file
157: /*
158: file = new RecordFile( testFileName );
159: PageManager pm = new PageManager( file );
160: pm.close();
161: file.close();
162: */
163: }
164:
165: /**
166: * Runs all tests in this class
167: */
168: public static void main(String[] args) {
169: junit.textui.TestRunner
170: .run(new TestSuite(TestRecordFile.class));
171: }
172: }
|