001: /*
002: This source file is part of Smyle, a database library.
003: For up-to-date information, see http://www.drjava.de/smyle
004: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: For full license text, see doc/license/lgpl.txt in this distribution
021: */
022:
023: package drjava.smyle.tests;
024:
025: import java.io.*;
026: import junit.framework.*;
027: import drjava.smyle.*;
028: import drjava.smyle.core.*;
029: import drjava.util.*;
030: import org.artsProject.mcop.*;
031:
032: public class FileSystemDiskTest extends TestCase {
033: public FileSystemDiskTest(String name) {
034: super (name);
035: }
036:
037: final File dir = new File("temp/fsdtest");
038:
039: FileSystemDisk disk;
040:
041: public void setUp() throws Exception {
042: clearDirectory(dir);
043: rotate();
044: assert(dir+" exists", dir.isDirectory());
045: }
046:
047: public void tearDown() throws Exception {
048: if (disk != null)
049: disk.release();
050: }
051:
052: void clearDirectory(File dir) {
053: File[] files = dir.listFiles();
054: if (files != null)
055: for (int i = 0; i < files.length; i++) {
056: if (files[i].isDirectory())
057: clearDirectory(files[i]);
058: else if (!files[i].delete())
059: throw new SmyleIOException("Couldn't delete "
060: + files[i]);
061: }
062: }
063:
064: void rotate() throws Exception {
065: if (disk != null)
066: disk.release();
067: disk = new FileSystemDisk(dir, false);
068: }
069:
070: long createMasterFile() throws Exception {
071: Buffer b = new Buffer();
072: b.writeByte((byte) 123);
073: return disk.saveMaster(b);
074: }
075:
076: long createFile() throws Exception {
077: Disk.NewFile file = disk.createFile();
078: OutputStream out = file.getOutputStream();
079: out.close();
080: return file.getId();
081: }
082:
083: void checkMasterFile(long id) throws Exception {
084: assertEquals("Master file length", 1, disk.getFileLength(id));
085: InputStream in = disk.readFile(id);
086: assertEquals(123, in.read());
087: in.close();
088: }
089:
090: public void testDeleteEverythingDiscardsMaster() throws Exception {
091: createMasterFile();
092: disk.deleteEverything();
093: assertEquals(0, disk.getMasterFile());
094: }
095:
096: public void testMasterFile() throws Exception {
097: disk.deleteEverything();
098:
099: assertEquals("No master file in empty dir", 0, disk.getMasterFile());
100:
101: long id = createMasterFile();
102: assertEquals("Id of first file", 1, id);
103:
104: // check file on disk
105:
106: assert("Files must have an extension", FileSystemDisk.EXT.length() != 0);
107: File f = new File(dir, "m"+id+FileSystemDisk.EXT);
108: assertEquals("File size on disk", 1, f.length());
109:
110: assertEquals("Master file", id, disk.getMasterFile());
111: checkMasterFile(id);
112: }
113:
114: public void testRetrieveMasterAfterRotate() throws Exception {
115: long id = createMasterFile();
116: rotate();
117: assertEquals("Master file ID", id, disk.getMasterFile());
118: checkMasterFile(id);
119: }
120:
121: public void testExceptions() throws Exception {
122: disk.deleteEverything();
123:
124: try {
125: // Non existent file
126: disk.getFileLength(123);
127: fail("should have thrown FileNotFoundException");
128: } catch (SmyleIOException e) {
129: // ok
130: }
131:
132: try {
133: // Non existent file
134: disk.readFile(123);
135: fail("should have thrown FileNotFoundException");
136: } catch (FileNotFoundException e) {
137: // ok
138: }
139: }
140:
141: public void testForeignFilesAreNotTouched() throws Exception {
142: File f = new File(dir, "foreignFile.txt");
143: File d = new File(dir, "foreignDir");
144: new FileOutputStream(f).close();
145: d.mkdir();
146:
147: disk.deleteEverything();
148:
149: assertTrue("File is untouched", f.isFile());
150: assertTrue("Directory is untouched", d.isDirectory());
151: }
152:
153: public void testTwoMasterFiles() throws Exception {
154: disk.deleteEverything();
155:
156: long master1 = createMasterFile();
157: long master2 = createMasterFile();
158: assertTrue(master1 != master2);
159:
160: assertEquals(master2, disk.getMasterFile());
161:
162: rotate();
163: assertEquals(master2, disk.getMasterFile());
164: }
165:
166: public void testNewFileIdsAfterRotate() throws Exception {
167: long file1 = createMasterFile();
168: rotate();
169: long file2 = createMasterFile();
170: assertTrue(file2 != file1);
171: }
172:
173: public void testNoDiskId() throws Exception {
174: tearDown();
175: disk = new FileSystemDisk(dir, 0, 1, false);
176: assertEquals(1, createFile());
177: assertEquals(2, createFile());
178: }
179:
180: public void testDiskId() throws Exception {
181: tearDown();
182: disk = new FileSystemDisk(dir, 1234, 10000, false);
183: assertEquals(11234, createFile());
184: assertEquals(21234, createFile());
185: }
186:
187: public void testHugeId() throws Exception {
188: tearDown();
189: disk = new FileSystemDisk(dir, 1234, 10000, false);
190: disk.setIdCounter(0x100000000L);
191: assertEquals(0x100000001L * 10000 + 1234, createFile());
192: }
193:
194: public void testDiskIsSynchronized() throws Exception {
195: // include package private methods to check for calls from inner classes
196: SynchronisationTester
197: .assertPackageAndPublicMethodsAreSynchronized(disk
198: .getClass());
199: }
200:
201: /** We just assume this works across processes too (no tricks with
202: static variables or global syncing) */
203: public void testDiskInUse() throws Exception {
204: disk = null;
205: try {
206: rotate();
207: fail("No exception");
208: } catch (DiskInUseException e) {
209: // ok
210: }
211: }
212:
213: public void testDiskInUseFlagClearedOnFinalization()
214: throws Exception {
215: disk = null;
216: System.gc();
217: System.runFinalization();
218: rotate();
219: }
220:
221: public void testNumberOfFiles() throws Exception {
222: // TODO
223: }
224:
225: public void testDeleteEverythingClosesOpenFiles() throws Exception {
226: disk.createFile();
227: disk.deleteEverything();
228: }
229:
230: public void testReleasesCloseOpenFiles() throws Exception {
231: disk.createFile();
232: rotate();
233: disk.deleteEverything();
234: }
235: }
|