import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import dijjer.io.store.DataStore;
import dijjer.io.store.Store;
import dijjer.util.VeryLongIntegerKey;
public class DataStoreTest extends TestCase {
public static final String VERSION = "$Id: DataStoreTest.java,v 1.17 2005/04/12 13:04:15 sanity Exp $";
private File _index;
private File _data;
private DataStore _ds;
public void setUp() throws Exception {
super.setUp();
_index = File.createTempFile("dijjer-index", ".dat");
_data = File.createTempFile("dijjer-data", ".dat");
_ds = new DataStore(_index, _data, 10);
}
public void tearDown() throws Exception {
if (_index.exists()) {
_index.delete();
}
if (_data.exists()) {
_data.delete();
}
super.tearDown();
}
public void testAddBlock() throws Exception {
List byteList = new ArrayList();
long iterations = _ds.getMaxBlocks();
addDataBlocks(iterations, byteList);
checkDataBlocks(byteList);
//next one should be written at the front of the file, so clear the list
byteList.clear();
addDataBlocks(1, byteList);
checkDataBlocks(byteList);
}
private void addDataBlocks(long iterations, List byteList) throws IOException {
for (int x = 0; x < iterations; x++) {
byte[] bytes = new byte[Store.DATA_BLOCK_SIZE];
byteList.add(bytes);
for (int y = 0; y < bytes.length; y++) {
bytes[y] = (byte) x;
}
_ds.addDataAsBlock(new VeryLongIntegerKey((long) 0, (long) x), bytes);
}
}
private void checkDataBlocks(List byteList) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(_data));
for (int x = 0; x < byteList.size(); x++) {
byte[] bytes = new byte[Store.DATA_BLOCK_SIZE];
for (int y = 0; y < bytes.length; y++) {
bytes[y] = (byte) in.read();
}
byte[] expected = (byte[]) byteList.get(x);
assertEquals(expected.length, bytes.length);
for (int i=0;i<expected.length;i++) {
assertEquals(expected[i], bytes[i]);
}
}
in.close();
}
public void testDataStore() throws Exception {
for (int x = 0; x < 15; x++) {
byte[] b = new byte[Store.DATA_BLOCK_SIZE];
for (int y = 0; y < b.length; y++) {
b[y] = (byte) x;
}
_ds.addDataAsBlock(new VeryLongIntegerKey((long) 0, (long) x), b);
}
_ds.getDataForBlock(new VeryLongIntegerKey((long) 0, (long) 7));
for (int x = 15; x < 20; x++) {
byte[] b = new byte[Store.DATA_BLOCK_SIZE];
for (int y = 0; y < b.length; y++) {
b[y] = (byte) x;
}
_ds.addDataAsBlock(new VeryLongIntegerKey((long) 0, (long) x), b);
}
}
}