A simple testing assertion which looks up for a line within a file until a pre defined time frame ends.
package com;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;
import org.apache.log4j.Logger;
/**
*
* TODO Description go here.
*
* @author Varra
* @version 1.0
*
*/
public class TailAssertor
{
private static final long sec = 1000L;
private static final Logger log = Logger.getLogger(TailAssertor.class);
private static long sampleInterval = 5000;
/**
* Asserts a new line with tail.
*
* @param logfile
* @param expectedLine
* @param durationInSec
*/
public static void assertNewLinesWithTail(final File logfile, final String expectedLine, int durationInSec)
{
long filePointer = logfile.length();// The file pointer keeps track of
// where we are in the file
RandomAccessFile file = null;
try
{
file = new RandomAccessFile(logfile, "r");
for (final Date startTime = new Date(); new Date().getTime() - startTime.getTime() < durationInSec * sec;)
{
long fileLength = logfile.length();
if (fileLength < filePointer)
{
// file has been cleared
file = new RandomAccessFile(logfile, "r");
filePointer = 0;
}
if (fileLength > filePointer)
{
file.seek(filePointer);// There is new data to read
for (String line = file.readLine(); line != null;)
{
if (line.contains(expectedLine))
{
return;
}
line = file.readLine();
}
filePointer = file.getFilePointer();
}
Thread.sleep(sampleInterval);// Sleep for the specified interval
}
}
catch (Exception e)
{
log.error(e);
}
finally
{
closeFile(file);
}
throw new AssertionError("the requested line wasn't found within the required time frame!");
}
/**
* CLoses the file.
*
* @param file
*/
private static void closeFile(RandomAccessFile file)
{
if (file != null)
{
try
{
file.close();
}
catch (IOException e)
{
log.error(e);
}
}
}
}
package com;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;
import org.apache.log4j.Logger;
/**
*
* TODO Description go here.
*
* @author Varra
* @version 1.0
*
*/
public class TailAssertor
{
private static final long sec = 1000L;
private static final Logger log = Logger.getLogger(TailAssertor.class);
private static long sampleInterval = 5000;
/**
* Asserts a new line with tail.
*
* @param logfile
* @param expectedLine
* @param durationInSec
*/
public static void assertNewLinesWithTail(final File logfile, final String expectedLine, int durationInSec)
{
long filePointer = logfile.length();// The file pointer keeps track of
// where we are in the file
RandomAccessFile file = null;
try
{
file = new RandomAccessFile(logfile, "r");
for (final Date startTime = new Date(); new Date().getTime() - startTime.getTime() < durationInSec * sec;)
{
long fileLength = logfile.length();
if (fileLength < filePointer)
{
// file has been cleared
file = new RandomAccessFile(logfile, "r");
filePointer = 0;
}
if (fileLength > filePointer)
{
file.seek(filePointer);// There is new data to read
for (String line = file.readLine(); line != null;)
{
if (line.contains(expectedLine))
{
return;
}
line = file.readLine();
}
filePointer = file.getFilePointer();
}
Thread.sleep(sampleInterval);// Sleep for the specified interval
}
}
catch (Exception e)
{
log.error(e);
}
finally
{
closeFile(file);
}
throw new AssertionError("the requested line wasn't found within the required time frame!");
}
/**
* CLoses the file.
*
* @param file
*/
private static void closeFile(RandomAccessFile file)
{
if (file != null)
{
try
{
file.close();
}
catch (IOException e)
{
log.error(e);
}
}
}
}
No comments:
Post a Comment