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 java.util.*;
028: import drjava.util.*;
029: import drjava.gjutil.*;
030: import org.artsProject.mcop.*;
031: import drjava.smyle.*;
032: import drjava.smyle.testtypes.*;
033: import drjava.smyle.core.*;
034:
035: public class DiskTest extends TestCase {
036: final Factory<Disk> diskFactory;
037: Disk disk;
038:
039: public DiskTest(String name, Factory<Disk> diskFactory) {
040: super (name);
041: this .diskFactory = diskFactory;
042: }
043:
044: public static Test suite(final Factory<Disk> diskFactory) {
045: return new FactoryTestSuite(DiskTest.class, new TestFactory() {
046: public TestCase createTest(String name) {
047: return new DiskTest(name, diskFactory);
048: }
049: });
050: }
051:
052: public static TestSuite suite() {
053: return new AggregatedTestSuite("Disk Tests", new Test[] {
054: // memory
055:
056: suite(new Factory<Disk>() {
057: public Disk produce() {
058: return new MemoryDisk();
059: }
060: }),
061:
062: // file system
063:
064: suite(new Factory<Disk>() {
065: public Disk produce() {
066: return new FileSystemDisk(new File(
067: "temp/disktest"), false);
068: }
069: }) });
070: }
071:
072: public void setUp() throws Exception {
073: createDisk(true);
074: }
075:
076: public void tearDown() throws Exception {
077: if (disk != null)
078: disk.release();
079: }
080:
081: void createDisk(boolean wipe) throws Exception {
082: disk = diskFactory.produce();
083: if (wipe)
084: disk.deleteEverything();
085: }
086:
087: void assertFileDoesntExist(long id) throws Exception {
088: try {
089: disk.readFile(id).close();
090: fail("File " + id + " should not exist");
091: } catch (FileNotFoundException e) {
092: // ok
093: }
094: }
095:
096: public void testDeleteEverythingBut() throws Exception {
097: Disk.NewFile file1 = disk.createFile();
098: file1.getOutputStream().close();
099: Disk.NewFile file2 = disk.createFile();
100: file2.getOutputStream().close();
101: Disk.NewFile file3 = disk.createFile();
102: file3.getOutputStream().close();
103: HashSet<FileRef> whitelist = new HashSet<FileRef>();
104: whitelist.add(new FileRef(file2.getId()));
105:
106: disk.deleteEverythingBut(whitelist);
107:
108: // assert file2 exists
109: disk.readFile(file2.getId()).close();
110:
111: // assert file1 and file3 have been deleted
112: assertFileDoesntExist(file1.getId());
113: assertFileDoesntExist(file3.getId());
114: }
115:
116: public void testTotalBytesWritten() throws Exception {
117: disk.setClusterSize(6);
118:
119: Buffer b = new Buffer();
120: b.writeLong(123);
121: DiskUtil.bufferToFile(disk, b);
122: assertEquals(6, disk.totalBytesWritten());
123:
124: b = new Buffer();
125: b.writeLongLong(123);
126: disk.saveMaster(b);
127: assertEquals(18, disk.totalBytesWritten());
128: }
129: }
|