LogFilter { protected LineFilterResult acceptsLogEntry(LogLevelExtractor.Result result) { if (result == null) { return resultOfPreviousLine; } return acceptsLogEntry(result.getLogLevel(), result.getLogTimestamp()); } LogFilter(Level logLevel, LocalDateTime startDate, LocalDateTime endDate);  LogFilter(Level logLevel, boolean thisLevelOnly, LocalDateTime startDate,
      LocalDateTime endDate); void startNewFile(); LineFilterResult acceptsLine(String logLine); boolean acceptsFile(Path file);  }
@Test public void permittedLogLevelsCanFilterLines() { LogFilter logFilter = new LogFilter(Level.INFO, null, null); LocalDateTime now = LocalDateTime.now(); assertThat(logFilter.acceptsLogEntry(Level.INFO, now)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.WARN, now)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.DEBUG, now)).isEqualTo(LINE_REJECTED); assertThat(logFilter.acceptsLogEntry(Level.TRACE, now)).isEqualTo(LINE_REJECTED); }
@Test public void permittedOnlyLogLevels() { LogFilter logFilter = new LogFilter(Level.INFO, true, null, null); LocalDateTime now = LocalDateTime.now(); assertThat(logFilter.acceptsLogEntry(Level.INFO, now)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.WARN, now)).isEqualTo(LINE_REJECTED); assertThat(logFilter.acceptsLogEntry(Level.DEBUG, now)).isEqualTo(LINE_REJECTED); assertThat(logFilter.acceptsLogEntry(Level.TRACE, now)).isEqualTo(LINE_REJECTED); }
@Test public void permittedLogLevelsALL() { LogFilter logFilter = new LogFilter(Level.ALL, null, null); LocalDateTime now = LocalDateTime.now(); assertThat(logFilter.acceptsLogEntry(Level.INFO, now)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.WARN, now)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.DEBUG, now)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.TRACE, now)).isEqualTo(LINE_ACCEPTED); }
@Test public void startDateCanFilterLines() { LocalDateTime startDate = LocalDateTime.now().minusDays(2); LogFilter logFilter = new LogFilter(Level.ALL, startDate, null); assertThat(logFilter.acceptsLogEntry(Level.INFO, LocalDateTime.now())).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.INFO, startDate)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.DEBUG, startDate)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.INFO, LocalDateTime.now().minusDays(3))) .isEqualTo(LINE_REJECTED); }
@Test public void endDateCanFilterLines() { LocalDateTime endDate = LocalDateTime.now().minusDays(2); LogFilter logFilter = new LogFilter(Level.ALL, null, endDate); assertThat(logFilter.acceptsLogEntry(Level.INFO, LocalDateTime.now().minusDays(3))) .isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.INFO, endDate)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.DEBUG, endDate)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.INFO, LocalDateTime.now())) .isEqualTo(REMAINDER_OF_FILE_REJECTED); }
@Test public void filterWorksWithLevelBasedAndTimeBasedFiltering() { LocalDateTime startDate = LocalDateTime.now().minusDays(5); LocalDateTime endDate = LocalDateTime.now().minusDays(2); LogFilter logFilter = new LogFilter(Level.INFO, startDate, endDate); assertThat(logFilter.acceptsLogEntry(Level.ERROR, LocalDateTime.now().minusDays(6))) .isEqualTo(LINE_REJECTED); assertThat(logFilter.acceptsLogEntry(Level.INFO, LocalDateTime.now().minusDays(6))) .isEqualTo(LINE_REJECTED); assertThat(logFilter.acceptsLogEntry(null)).isEqualTo(LINE_REJECTED); assertThat(logFilter.acceptsLogEntry(Level.ERROR, LocalDateTime.now().minusDays(6))) .isEqualTo(LINE_REJECTED); assertThat(logFilter.acceptsLogEntry(null)).isEqualTo(LINE_REJECTED); assertThat(logFilter.acceptsLogEntry(Level.INFO, LocalDateTime.now().minusDays(4))) .isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(null)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.ERROR, LocalDateTime.now().minusDays(1))) .isEqualTo(REMAINDER_OF_FILE_REJECTED); assertThat(logFilter.acceptsLogEntry(null)).isEqualTo(REMAINDER_OF_FILE_REJECTED); assertThat(logFilter.acceptsLogEntry(Level.INFO, LocalDateTime.now().minusDays(1))) .isEqualTo(REMAINDER_OF_FILE_REJECTED); }
@Test public void firstLinesAreAcceptedIfParsableLineHasNotBeenSeenYet() { LogFilter logFilter = new LogFilter(Level.INFO, null, null); assertThat(logFilter.acceptsLogEntry(null)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(null)).isEqualTo(LINE_ACCEPTED); assertThat(logFilter.acceptsLogEntry(Level.DEBUG, LocalDateTime.now())) .isEqualTo(LINE_REJECTED); assertThat(logFilter.acceptsLogEntry(null)).isEqualTo(LINE_REJECTED); }