package org.ba.karlsruhe.filehandler;
import java.io.File;
public class FileHandlerTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
FileHandler handler = new FileHandler("testfile");
String exampleText = "This is a test line";
String exampleText2 = "Yet another line";
handler.appendLine(exampleText);
handler.appendLine(exampleText2);
// verify written line (cache should still be valid)
String[] fileContent = handler.getContent();
if(fileContent.length != 2)
System.out.println("Wrong number of lines returned");
if(fileContent.length != handler.getLineCount())
System.out.println("getLineCount() returned from line number count");
if(!fileContent[0].equals(exampleText))
System.out.println("Written line did not match read line");
if(!handler.getContentAtLine(0).equals(exampleText))
System.out.println("getContentAtLine() returned wrong line");
// change file
String blubTest = "this is some blub ;-)";
handler.appendLine(blubTest);
// ensure the line has been written
handler.read();
if(!handler.getContentAtLine(2).equals(blubTest))
System.out.println("Line was not written out.");
handler.removeLines("Blub", true); //nothing should happen
handler.read(); // ensure a refreshed cache
if(handler.getLineCount() != 3)
System.out.println("removeLines() does not respect case-sensitivity");
handler.removeLines("Blub", false);
handler.read();
if(handler.getLineCount() != 2)
System.out.println("removeLines() does not respect insensitive case");
fileContent = handler.getContent();
if(!fileContent[0].equals(exampleText) || !fileContent[1].equals(exampleText2))
System.out.println("huh, maybe the unit test has some false statements in it");
File testFile = new File("testfile");
testFile.delete();
handler = null;
/**
* Open another handler
*/
File file = new File("testfile2");
handler = new FileHandler(file);
// check write-behind mode
handler.setWriteThrough(false);
// do some benchmarking
long startTime = System.currentTimeMillis();
for(int i=0;i<500;i++)
handler.appendLine("This is a test line");
handler.write();
long endTime = System.currentTimeMillis();
long elapsedWriteBehind = endTime - startTime;
System.out.println("Elapsed time in write-behind mode: " + elapsedWriteBehind + " ms");
// check if the lines have been written out
handler.read();
if(handler.getLineCount() != 500)
System.out.println("Wrong line count in write-behind mode");
handler.setWriteThrough(true);
handler.removeAllLines();
if(handler.getLineCount() != 0)
System.out.println("removeAllLines() does not work");
// do some benchmarking with write-through
startTime = System.currentTimeMillis();
for(int i=0;i<500;i++)
handler.appendLine("This is a test line");
endTime = System.currentTimeMillis();
long elapsedWriteThrough = endTime - startTime;
System.out.println("Elapsed time in write-through mode: " + elapsedWriteThrough + " ms");
if(elapsedWriteBehind > elapsedWriteThrough)
System.out.println("Write-behind took longer than write-through, this should not happen");
file.delete();
} catch(FileHandlerException e) {
System.out.println("An exception occured: " + e.getMessage());
}
}
}