Module iu.util.test
Package edu.iu.test

Class IuTestLogger

java.lang.Object
edu.iu.test.IuTestLogger

public final class IuTestLogger extends Object
Asserts that calls to Logger.log(LogRecord) must be expected by the test case being evaluated, and that all expected log events must occur in order.

This mechanism is tied in automatically when depending on iu-java-test for unit testing. Any use of Logger.log(LogRecord) will cause the test to fail unless the log event is explicitly expected to be the next log event. Log event expectations require:

  • Logger name to match exactly
  • Log level to match exactly
  • Log message to match a regular expression
  • Thrown exception class to match exactly, or for an exception to not be thrown
  • Thrown exception to meet additional criteria if defined by Predicate

Platform-level loggers are exempt, and will be logged normally as configured by java.logging before the test framework is configured, i.e., INFO level and higher logged to console. The following logger names are considered platform loggers.

  • org.junit
  • org.mockito
  • org.apiguardian
  • net.bytebuddy
  • org.objenesis
  • org.opentest4j
  • Any logger name for which IuObject.isPlatformName(String) returns true
  • Any logger name prefixed by the comma-separated test property value iu.util.test.platformLoggers
  • Method Details

    • isPlatformLogger

      static boolean isPlatformLogger(String loggerName)
      Determines if a logger name is related to a platform logger, and so should omitted from test expectations.
      Parameters:
      loggerName - logger name
      Returns:
      true if name is associated with a platform logger
    • init

      static void init()
      Initialization hook.
    • startTest

      static void startTest(String name)
      Test start hook.
      Parameters:
      name - test name
    • finishTest

      static void finishTest(String name)
      Test finish hook.
      Parameters:
      name - test name
    • destroy

      static void destroy()
      Test destroy hook.
    • allow

      public static void allow(String loggerName, Level level)
      Allows a log messages from a logger.

      Messages may be logged zero or more times, and will be exempt from expectation checks.

      Parameters:
      loggerName - Logger name prefix
      level - maximum log level to allow
    • allow

      public static void allow(String loggerName, Level level, String message)
      Allows a log message with or without an exception.

      The message may be logged zero or more times, and will be exempt from expectation checks.

      Parameters:
      loggerName - Logger name prefix
      level - maximum log level to allow
      message - regular expression to match against the message
    • allow

      public static void allow(String loggerName, Level level, String message, Class<? extends Throwable> thrownClass)
      Allows a log message with a thrown exception.

      The message may be logged zero or more times, and will be exempt from expectation checks.

      Parameters:
      loggerName - Logger name prefix
      level - maximum log level to allow
      message - regular expression to match against the message
      thrownClass - Expected exception class, must match exactly
    • allow

      public static <T extends Throwable> void allow(String loggerName, Level level, String message, Class<T> thrownClass, Predicate<T> thrownTest)
      Allows a log message with a thrown exception.

      The message may be logged zero or more times, and will be exempt from expectation checks.

      Type Parameters:
      T - Thrown exception type
      Parameters:
      loggerName - Logger name prefix
      level - maximum log level to allow
      message - regular expression to match against the message
      thrownClass - Expected exception class, must match exactly
      thrownTest - Expected exception test
    • expect

      public static void expect(String loggerName, Level level, String message)
      Expects a log message with no thrown exception.
      Parameters:
      loggerName - Logger name, must match exactly
      level - level, must match exactly
      message - regular expression to match against the message
    • expect

      public static void expect(String loggerName, Level level, String message, Class<? extends Throwable> thrownClass)
      Expects a log message with a thrown exception.
      Parameters:
      loggerName - Logger name, must match exactly
      level - level, must match exactly
      message - regular expression to match against the message
      thrownClass - Expected exception class, must match exactly
    • expect

      public static <T extends Throwable> void expect(String loggerName, Level level, String message, Class<T> thrownClass, Predicate<T> thrownTest)
      Expects a log message with a thrown exception.
      Type Parameters:
      T - Thrown exception type
      Parameters:
      loggerName - Logger name, must match exactly
      level - level, must match exactly
      message - regular expression to match against the message
      thrownClass - Expected exception class, must match exactly
      thrownTest - Expected exception test
    • assertExpectedMessages

      public static void assertExpectedMessages()
      Asserts that all expected messages declared so far have been logged.
    • clearUnexpected

      static void clearUnexpected(Throwable unexpected)
      Clears an unexpected log message from the pending queue.
      Parameters:
      unexpected - error related to an unexpected log message