ParseLog { public static String pullLastLineRead(final File file, final String dataFileName) throws IOException { if (!file.exists()) { file.createNewFile(); return null; } try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");) { StringBuilder builder = new StringBuilder(); long length = file.length(); logger.debug("dataFileName: " + dataFileName); if (length == 0) { return null; } length--; randomAccessFile.seek(length); for (long seek = length; seek >= 0; --seek) { randomAccessFile.seek(seek); final char c = (char) randomAccessFile.read(); builder.append(c); if (c == '\n') { builder = builder.reverse(); logger.debug("builder.toString(): " + builder.toString()); if (builder.toString().contains(last + dataFileName + lineRead)) { final String[] parseString = builder.toString().split(last + dataFileName + lineRead); final String returnValue = parseString[1].replace("\r", ""); return returnValue.trim(); } builder = new StringBuilder(); } } } return null; }  static void main(final String[] args); static int countLines(final String filename); static String pullLastLineRead(final File file, final String dataFileName); static LogEntryObject pullOutLogValues(final String line, final String type); static void process(final String line, final String type, final LogType logFile); static Date parseDate(final String dateline, final String pattern, final boolean singleSplit); static String parseRemoteSystem(final String line); static String[] getPaths(final String logPath); static Properties getPropertiesValue(final String fileName); static Connection getDbConnection();  }
@Test public void testPullLastLineRead() { logger.debug("testPullLastLineRead: enter"); final File file = new File(testFile1); String returnValue = null; try { returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log"); logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue); } catch (final IOException e) { fail(); } assertEquals("52", returnValue); try { returnValue = ParseLog.pullLastLineRead(file, "debug.log"); logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue); } catch (final IOException e) { fail(); } assertEquals("17", returnValue); try { returnValue = ParseLog.pullLastLineRead(file, "error.log"); logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue); } catch (final IOException e) { fail(); } assertEquals("22", returnValue); logger.debug("testPullLastLineRead: exit"); }
@Test public void testPullLastLineReadNoFile() { logger.debug("testPullLastLineReadNoFile: enter"); final File file = new File("nonExistFile.txt"); try { assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest")); } catch (final IOException e) { fail(); } logger.debug("testPullLastLineReadNoFile: exit"); }
@Test public void testPullLastLineReadFail() { logger.debug("testPullLastLineReadFail: enter"); final File file = new File(testFile2); try { assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest")); } catch (final IOException e) { fail(); } logger.debug("testPullLastLineReadFail: exit"); }