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 drjava.smyle.*;
027: import drjava.smyle.core.*;
028: import org.artsProject.mcop.*;
029: import junit.framework.*;
030: import drjava.util.*;
031:
032: public class ChunkManagerTest extends TestCase {
033: public ChunkManagerTest(String name) {
034: super (name);
035: }
036:
037: MemoryDisk disk;
038: DefaultChunkManager cm;
039: ChunkRef chunk1, chunk2;
040:
041: public void setUp() throws Exception {
042: disk = new MemoryDisk();
043: rotate();
044: }
045:
046: void rotate() {
047: cm = new DefaultChunkManager(disk, DiskStore.VERSION);
048: }
049:
050: Buffer data1() {
051: Buffer b = new Buffer();
052: for (int i = 0; i < 64; i++)
053: b.writeLong(123);
054: return b;
055: }
056:
057: Buffer data2() {
058: Buffer b = new Buffer();
059: for (int i = 0; i < 64; i++)
060: b.writeLong(432);
061: return b;
062: }
063:
064: public void testWriteReadMasterChunk() throws Exception {
065: ChunkRef chunk = cm.createMasterChunk(data1());
066:
067: assertNotNull(chunk);
068: assertEquals(chunk, cm.getMasterChunk());
069: assertEquals(data1().toString(), cm.readChunk(chunk).toString());
070: assertEquals(3, disk.numberOfFiles());
071: }
072:
073: public void testWriteReadMasterChunkTwice() throws Exception {
074: chunk1 = cm.createMasterChunk(data1());
075: chunk2 = cm.createMasterChunk(data2());
076:
077: assertEquals(chunk2, cm.getMasterChunk());
078: assertEquals(data1().toString(), cm.readChunk(chunk1)
079: .toString());
080: assertEquals(data2().toString(), cm.readChunk(chunk2)
081: .toString());
082:
083: assertEquals(6, disk.numberOfFiles());
084: }
085:
086: public void testWriteReadMasterChunkRotate() throws Exception {
087: ChunkRef chunk = cm.createMasterChunk(data1());
088: rotate();
089: assertEquals(data1().toString(), cm.readChunk(
090: cm.getMasterChunk()).toString());
091: }
092:
093: void testNormalAndMasterChunk(boolean rotate) throws Exception {
094: chunk1 = cm.createChunk(data1());
095: chunk2 = cm.createMasterChunk(data2());
096:
097: if (rotate)
098: rotate();
099: assertEquals(chunk2, cm.getMasterChunk());
100: assertEquals(data1().toString(), cm.readChunk(chunk1)
101: .toString());
102: assertEquals(data2().toString(), cm.readChunk(chunk2)
103: .toString());
104:
105: // asserts that both chunks were combined in one file
106: assertEquals(3, disk.numberOfFiles());
107: }
108:
109: public void testNormalAndMasterChunk() throws Exception {
110: testNormalAndMasterChunk(false);
111: }
112:
113: public void testNormalAndMasterChunkRotate() throws Exception {
114: testNormalAndMasterChunk(true);
115: }
116:
117: public void testExactNaturalFileSize() throws Exception {
118: cm.setNaturalFileSize(4 * 64 * 2);
119: chunk1 = cm.createChunk(data1());
120: chunk2 = cm.createMasterChunk(data2());
121: assertEquals(data1().toString(), cm.readChunk(chunk1)
122: .toString());
123: assertEquals(data2().toString(), cm.readChunk(chunk2)
124: .toString());
125: assertEquals(3, disk.numberOfFiles());
126: }
127:
128: public void testNaturalFileSize() throws Exception {
129: cm.setNaturalFileSize(4 * 64 + 32);
130: chunk1 = cm.createChunk(data1());
131: chunk2 = cm.createMasterChunk(data2());
132: assertEquals(data1().toString(), cm.readChunk(chunk1)
133: .toString());
134: assertEquals(data2().toString(), cm.readChunk(chunk2)
135: .toString());
136: assertEquals(4, disk.numberOfFiles());
137: }
138:
139: public void testNoMasterChunk() throws Exception {
140: ChunkRef chunk = cm.getMasterChunk();
141: assertNotNull(chunk);
142: assertEquals(0, chunk.index);
143: }
144:
145: public void testChunkManagerIsSynchronized() throws Exception {
146: SynchronisationTester.assertPublicMethodsAreSynchronized(cm
147: .getClass());
148: }
149: }
|