package org.nthx.pat.tests;
import org.nthx.pat.Pat;
import org.nthx.pat.Persisteble;
public class FileReadingWritingTest
extends PatTestCase
{
public void setUp()
{
}
public void testCreatingRootOnly()
{
Pat.unload();
cleanPrevaylersRepo();
assertEquals("Should be 0 files - clean repo", 0, getFilesCountInRepo());
TestRoot root = new TestRoot();
assertEquals("Should be 1 file - clean snapshot", 1, getFilesCountInRepo());
assertEquals("Should be 0 users", 0, root.getUsers().size());
}
public void testCreatingRootAndSnapshot()
{
Pat.unload();
cleanPrevaylersRepo();
assertEquals("Should be 0 files - clean repo!", 0, getFilesCountInRepo());
TestRoot root = new TestRoot();
assertEquals("Should be 1 file - fresh snapshot", 1, getFilesCountInRepo());
((Persisteble)root).takeSnapshot();
assertEquals("Should be still 1 file", 1, getFilesCountInRepo());
String firstSnapshotName = "000000000000000000000.snapshot";
assertEquals("Name should be: " + firstSnapshotName,
firstSnapshotName, getLastFileFromRepo());
Pat.unload();
TestRoot rootReloaded = new TestRoot();
if (rootReloaded == root)
throw new AssertionError("Roots' addresses should differ");
assertEquals("Should be 1 file - fresh snapshot", 1, getFilesCountInRepo());
assertEquals("Should be 0 users", 0, rootReloaded.getUsers().size());
}
public void testCreatingRootAndSnapshotAndChangesToRoot()
{
Pat.unload();
cleanPrevaylersRepo();
assertEquals("Should be 0 files - clean repo!", 0, getFilesCountInRepo());
TestRoot root = new TestRoot();
assertEquals("Should be 1 file - fresh snapshot", 1, getFilesCountInRepo());
((Persisteble)root).takeSnapshot();
assertEquals("Should be still 1 file", 1, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000000.snapshot",
"000000000000000000000.snapshot", getLastFileFromRepo());
root.newUser("user 1");
assertEquals("Should be 1 user", 1, root.getUsers().size());
assertEquals("Should be 2 files: snap and log", 2, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000001.commandLog",
"000000000000000000001.commandLog", getLastFileFromRepo());
Pat.unload();
TestRoot rootReloaded = new TestRoot();
if (rootReloaded == root)
throw new AssertionError("Roots' addresses should differ");
assertEquals("Should be 2 files", 2, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000001.commandLog",
"000000000000000000001.commandLog", getLastFileFromRepo());
assertEquals("Should be 1 user", 1, rootReloaded.getUsers().size());
}
public void testDiskChangesMadeSeveralTimes()
{
Pat.unload();
cleanPrevaylersRepo();
assertEquals("Should be 0 files - clean repo!", 0, getFilesCountInRepo());
TestRoot root = new TestRoot();
assertEquals("Should be 1 file - fresh snapshot", 1, getFilesCountInRepo());
((Persisteble)root).takeSnapshot();
assertEquals("Should be still 1 file", 1, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000000.snapshot",
"000000000000000000000.snapshot", getLastFileFromRepo());
root.newUser("user 1");
assertEquals("Should be 1 user", 1, root.getUsers().size());
assertEquals("Should be 2 files: snap and log", 2, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000001.commandLog",
"000000000000000000001.commandLog", getLastFileFromRepo());
Pat.unload();
root = new TestRoot();
assertEquals("Should be 2 files", 2, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000001.commandLog",
"000000000000000000001.commandLog", getLastFileFromRepo());
assertEquals("Should be 1 user", 1, root.getUsers().size());
root.newUser("user - 2");
assertEquals("Should be 2 user", 2, root.getUsers().size());
assertEquals("Should be 3 files: snap and 2xlog", 3, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000002.commandLog",
"000000000000000000002.commandLog", getLastFileFromRepo());
Pat.unload();
root = new TestRoot();
assertEquals("Should be 3 files", 3, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000002.commandLog",
"000000000000000000002.commandLog", getLastFileFromRepo());
root.newUser("user 3");
assertEquals("Should be 3 users", 3, root.getUsers().size());
assertEquals("Should be 4 files", 4, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000003.commandLog",
"000000000000000000003.commandLog", getLastFileFromRepo());
((Persisteble)root).takeSnapshot();
assertEquals("Should be 5 files: snap,3xlog,smap",
5, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000003.snapshot",
"000000000000000000003.snapshot", getLastFileFromRepo());
Pat.unload();
root = new TestRoot();
assertEquals("Should be 3 users", 3, root.getUsers().size());
assertEquals("Should be 5 files: snap,3xlog,snap",
5, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000003.snapshot",
"000000000000000000003.snapshot", getLastFileFromRepo());
((Persisteble)root).takeSnapshot();
assertEquals("Should be 5 files: snap,3xlog,snap",
5, getFilesCountInRepo());
assertEquals("Name should be: 000000000000000000003.snapshot",
"000000000000000000003.snapshot", getLastFileFromRepo());
Pat.unload();
root = new TestRoot();
assertEquals("Should be 3 users", 3, root.getUsers().size());
}
public FileReadingWritingTest(String arg)
{
super(arg);
}
public static void main(String[] args)
{
junit.textui.TestRunner.run(FileReadingWritingTest.class);
}
}