XStreamFileLoggerTail extends AbstractTextFileLoggerTail { @Override public void initialize(Path path, boolean readonly) throws IOException { super.initialize(path, StandardCharsets.UTF_8, readonly); }  @Override void initialize(Path path, boolean readonly); static boolean exist(Path path);  }
@Test void writeread() throws IOException { this.tail.initialize(new File(this.tmpDir, "log").toPath(), false); this.tail.error("error0"); this.tail.error("error1"); assertEquals("error0", this.tail.getLogEvent(0).getMessage()); assertEquals("error1", this.tail.getLogEvent(1).getMessage()); }
@Test void readonly() throws IOException, ComponentLifecycleException { this.tail.initialize(new File(this.tmpDir, "log").toPath(), false); this.tail.error("error0"); this.tail.error("error1"); this.tail.dispose(); assertNull(this.tail.logStore); assertTrue(this.tail.open()); assertNotNull(this.tail.logStore); this.tail.close(true); assertNull(this.tail.logStore); this.tail.initialize(new File(this.tmpDir, "log").toPath(), true); assertEquals("error0", this.tail.getLogEvent(0).getMessage()); assertEquals("error1", this.tail.getLogEvent(1).getMessage()); assertEquals(2, this.tail.getLogEvents(0, -1).stream().count()); assertNull(this.tail.logStore); this.tail.error("error2"); assertNull(this.tail.getLogEvent(2)); }
@Test void getFirstLogEvent() throws IOException { this.tail.initialize(new File(this.tmpDir, "log").toPath(), false); assertNull(this.tail.getFirstLogEvent()); assertNull(this.tail.getFirstLogEvent(LogLevel.ERROR)); this.tail.info("info0"); this.tail.info("info1"); this.tail.warn("warn0"); this.tail.warn("warn1"); this.tail.error("error0"); this.tail.error("error1"); assertEquals("info0", this.tail.getFirstLogEvent().getMessage()); assertEquals("info0", this.tail.getFirstLogEvent(LogLevel.TRACE).getMessage()); assertEquals("info0", this.tail.getFirstLogEvent(LogLevel.INFO).getMessage()); assertEquals("warn0", this.tail.getFirstLogEvent(LogLevel.WARN).getMessage()); assertEquals("error0", this.tail.getFirstLogEvent(LogLevel.ERROR).getMessage()); }
@Test void getLastLogEvent() throws IOException { this.tail.initialize(new File(this.tmpDir, "log").toPath(), false); assertNull(this.tail.getLastLogEvent()); assertNull(this.tail.getLastLogEvent(LogLevel.ERROR)); this.tail.error("error0"); this.tail.error("error1"); this.tail.warn("warn0"); this.tail.warn("warn1"); this.tail.info("info0"); this.tail.info("info1"); assertEquals("info1", this.tail.getLastLogEvent().getMessage()); assertEquals("info1", this.tail.getLastLogEvent(LogLevel.TRACE).getMessage()); assertEquals("info1", this.tail.getLastLogEvent(LogLevel.INFO).getMessage()); assertEquals("warn1", this.tail.getLastLogEvent(LogLevel.WARN).getMessage()); assertEquals("error1", this.tail.getLastLogEvent(LogLevel.ERROR).getMessage()); }
@Test void getLogEvents() throws IOException { this.tail.initialize(new File(this.tmpDir, "log").toPath(), false); assertEquals(0, this.tail.getLogEvents(null).stream().count()); assertEquals(0, this.tail.getLogEvents(LogLevel.DEBUG).stream().count()); this.tail.info("info0"); this.tail.warn("warn0"); this.tail.error("error0"); this.tail.info("info1"); this.tail.warn("warn1"); this.tail.error("error1"); this.tail.info("info2"); this.tail.warn("warn2"); this.tail.error("error2"); assertEquals(9, this.tail.getLogEvents(null).stream().count()); assertEquals(9, this.tail.getLogEvents(LogLevel.DEBUG).stream().count()); assertEquals(9, this.tail.getLogEvents(LogLevel.INFO).stream().count()); assertEquals(6, this.tail.getLogEvents(LogLevel.WARN).stream().count()); assertEquals(3, this.tail.getLogEvents(LogLevel.ERROR).stream().count()); assertEquals(9, this.tail.getLogEvents(0, -1).stream().count()); assertEquals(6, this.tail.getLogEvents(3, -1).stream().count()); assertEquals(3, this.tail.getLogEvents(0, 3).stream().count()); assertEquals(3, this.tail.getLogEvents(3, 3).stream().count()); assertEquals(6, this.tail.getLogEvents(3, 42).stream().count()); assertEquals(9, this.tail.getLogEvents(-1, -1).stream().count()); }
@Test void getDeleteLog() throws IOException { this.tail.initialize(new File(this.tmpDir, "log").toPath(), false); this.tail.info("info"); this.tail.close(); this.tail.initialize(new File(this.tmpDir, "log").toPath(), true); assertEquals("info", this.tail.getLogEvent(0).getMessage()); this.tail.logFile.delete(); assertNull(this.tail.getLogEvent(0)); }
@Test void getModifiedLog() throws IOException { this.tail.initialize(new File(this.tmpDir, "log").toPath(), false); this.tail.info("info"); this.tail.close(); this.tail.initialize(new File(this.tmpDir, "log").toPath(), true); assertEquals("info", this.tail.getLogEvent(0).getMessage()); try (FileOutputStream fw = new FileOutputStream(this.tail.logFile, false)) { fw.flush(); } assertNull(this.tail.getLogEvent(0)); }
@Test void logStoreDeletedWhileReading() throws IOException { this.tail.initialize(new File(this.tmpDir, "log").toPath(), false); this.tail.info("info0"); this.tail.info("info1"); this.tail.info("info2"); assertEquals("info0", this.tail.getLogEvent(0).getMessage()); this.tail.flush(); this.tail.logFile.delete(); assertNull(this.tail.getLogEvent(1)); this.tail.error("info3"); }