/ Published in: Java

A simple testing assertion which looks up for a line within a file until a pre defined time frame ends (loosely based upon http://www.informit.com/guides/content.aspx?g=java&seqNum=226).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public class TailAssertor { private static final long sec = 1000L; private static final Log log = LogService.getLog(TailAssertor.class.getName()); private static long sampleInterval = 5000; long filePointer = logfile.length();// The file pointer keeps track of where we are in the file try { long fileLength = logfile.length(); if (fileLength < filePointer) {// file has been cleared filePointer = 0; } if (fileLength > filePointer) { file.seek(filePointer);// There is new data to read if (line.contains(expectedLine)) { return; } line = file.readLine(); } filePointer = file.getFilePointer(); } } log.error(e); } finally { closeFile(file); } throw new AssertionError("the requested line wasn't found within the required time frame!"); } if (file != null) { try { file.close(); log.error(e); } } } }
Comments
