View Javadoc
1   package com.github.valfirst.slf4jtest;
2   
3   import java.util.Arrays;
4   import java.util.List;
5   import java.util.Optional;
6   import java.util.function.Predicate;
7   import java.util.function.Supplier;
8   import org.assertj.core.api.AbstractAssert;
9   import org.slf4j.event.Level;
10  
11  abstract class AbstractTestLoggerAssert<C extends AbstractAssert<C, TestLogger>>
12          extends AbstractAssert<C, TestLogger> {
13      protected Supplier<List<LoggingEvent>> loggingEventsSupplier;
14  
15      protected AbstractTestLoggerAssert(TestLogger testLogger, Class clazz) {
16          super(testLogger, clazz);
17          loggingEventsSupplier = testLogger::getLoggingEvents;
18      }
19  
20      protected long getLogCount(Level level, Predicate<LoggingEvent> predicate) {
21          return loggingEventsSupplier.get().stream()
22                  .filter(event -> level.equals(event.getLevel()) && predicate.test(event))
23                  .count();
24      }
25  
26      protected static Predicate<LoggingEvent> logWithMessage(String message, Object... arguments) {
27          return logWithMessage(message, Optional.empty(), arguments);
28      }
29  
30      protected static Predicate<LoggingEvent> logWithMessage(
31              String message,
32              @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<Throwable> maybeThrowable,
33              Object... arguments) {
34          return event ->
35                  message.equals(event.getMessage())
36                          && event.getArguments().equals(Arrays.asList(arguments))
37                          && event.getThrowable().equals(maybeThrowable);
38      }
39  
40      protected static LoggingEvent event(Level level, String message, Object... arguments) {
41          switch (level) {
42              case WARN:
43                  return LoggingEvent.warn(message, arguments);
44              case ERROR:
45                  return LoggingEvent.error(message, arguments);
46              case INFO:
47                  return LoggingEvent.info(message, arguments);
48              case DEBUG:
49                  return LoggingEvent.debug(message, arguments);
50              case TRACE:
51                  return LoggingEvent.trace(message, arguments);
52              default:
53                  throw new IllegalStateException("Unmatched level " + level + " provided");
54          }
55      }
56  
57      protected static LoggingEvent event(
58              Throwable throwable, Level level, String message, Object... arguments) {
59          switch (level) {
60              case WARN:
61                  return LoggingEvent.warn(throwable, message, arguments);
62              case ERROR:
63                  return LoggingEvent.error(throwable, message, arguments);
64              case INFO:
65                  return LoggingEvent.info(throwable, message, arguments);
66              case DEBUG:
67                  return LoggingEvent.debug(throwable, message, arguments);
68              case TRACE:
69                  return LoggingEvent.trace(throwable, message, arguments);
70              default:
71                  throw new IllegalStateException("Unmatched level " + level + " provided");
72          }
73      }
74  }