Class JavaTest

java.lang.Object
org.openhab.core.test.java.JavaTest
Direct Known Subclasses:
JavaOSGiTest

@NonNullByDefault public class JavaTest extends Object
JavaTest is an abstract base class for tests which are not necessarily based on OSGi.
Author:
Simon Kaufmann - Initial contribution
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final int
     
    protected static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    assertLogMessage(Class<?> clazz, JavaTest.LogLevel logLevel, String message)
    Assert that a message was logged for a class with a given log level and message
    protected void
    Assert that not message was logged for a class
    protected static <T> T
    Useful for testing @NonNull annotated parameters
    protected void
    Set up an intercepting logger for a class with a log level threshold
    protected void
    Stop the logging interception
    protected boolean
    Wait until the condition is fulfilled or the timeout is reached.
    protected boolean
    waitFor(BooleanSupplier condition, long timeout, long sleepTime)
    Wait until the condition is fulfilled or the timeout is reached.
    protected void
    Wait until the assertion is fulfilled or the timeout is reached.
    protected void
    waitForAssert(Runnable assertion, long timeout, long sleepTime)
    Wait until the assertion is fulfilled or the timeout is reached.
    protected void
    waitForAssert(Runnable assertion, @Nullable Runnable beforeLastCall, long timeout, long sleepTime)
    Wait until the assertion is fulfilled or the timeout is reached.
    protected void
    waitForAssert(Runnable assertion, @Nullable Runnable beforeLastCall, @Nullable Runnable afterLastCall, long timeout, long sleepTime)
    Wait until the assertion is fulfilled or the timeout is reached.
    protected <T> T
    waitForAssert(Supplier<T> assertion)
    Wait until the assertion is fulfilled or the timeout is reached.
    protected <T> T
    waitForAssert(Supplier<T> assertion, long timeout, long sleepTime)
    Wait until the assertion is fulfilled or the timeout is reached.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • JavaTest

      public JavaTest()
  • Method Details

    • setupInterceptedLogger

      protected void setupInterceptedLogger(Class<?> clazz, JavaTest.LogLevel minLogLevel)
      Set up an intercepting logger for a class with a log level threshold
      Parameters:
      clazz - The Class to intercept the logs for
      minLogLevel - The minimum log level that should be recorded
    • stopInterceptedLogger

      protected void stopInterceptedLogger(Class<?> clazz)
      Stop the logging interception
      Parameters:
      clazz - The Class to stop intercepting logs for
    • assertNoLogMessage

      protected void assertNoLogMessage(Class<?> clazz)
      Assert that not message was logged for a class
      Parameters:
      clazz - The Class to check
    • assertLogMessage

      protected void assertLogMessage(Class<?> clazz, JavaTest.LogLevel logLevel, String message)
      Assert that a message was logged for a class with a given log level and message
      Parameters:
      clazz - The Class to check
      logLevel - The expected log level
      message - The expected message
    • waitFor

      protected boolean waitFor(BooleanSupplier condition)
      Wait until the condition is fulfilled or the timeout is reached.

      This method uses the default timing parameters.

      Parameters:
      condition - the condition to check
      Returns:
      true on success, false on timeout
    • waitFor

      protected boolean waitFor(BooleanSupplier condition, long timeout, long sleepTime)
      Wait until the condition is fulfilled or the timeout is reached.
      Parameters:
      condition - the condition to check
      timeout - timeout
      sleepTime - interval for checking the condition
      Returns:
      true on success, false on timeout
    • waitForAssert

      protected void waitForAssert(Runnable assertion)
      Wait until the assertion is fulfilled or the timeout is reached.

      This method uses the default timing parameters.

      Parameters:
      assertion - closure that must not have an argument
    • waitForAssert

      protected void waitForAssert(Runnable assertion, long timeout, long sleepTime)
      Wait until the assertion is fulfilled or the timeout is reached.
      Parameters:
      assertion - the logic to execute
      timeout - timeout
      sleepTime - interval for checking the condition
    • waitForAssert

      protected <T> T waitForAssert(Supplier<T> assertion)
      Wait until the assertion is fulfilled or the timeout is reached.

      This method uses the default timing parameters.

      Parameters:
      assertion - the logic to execute
      Returns:
      the return value of the supplied assertion object's function on success
    • waitForAssert

      protected <T> T waitForAssert(Supplier<T> assertion, long timeout, long sleepTime)
      Wait until the assertion is fulfilled or the timeout is reached.
      Parameters:
      assertion - the logic to execute
      timeout - timeout
      sleepTime - interval for checking the condition
      Returns:
      the return value of the supplied assertion object's function on success
    • waitForAssert

      protected void waitForAssert(Runnable assertion, @Nullable Runnable beforeLastCall, long timeout, long sleepTime)
      Wait until the assertion is fulfilled or the timeout is reached.
      Parameters:
      assertion - the logic to execute
      beforeLastCall - logic to execute in front of the last call to ${code assertion}
      sleepTime - interval for checking the condition
    • waitForAssert

      protected void waitForAssert(Runnable assertion, @Nullable Runnable beforeLastCall, @Nullable Runnable afterLastCall, long timeout, long sleepTime)
      Wait until the assertion is fulfilled or the timeout is reached.
      Parameters:
      assertion - the logic to execute
      beforeLastCall - logic to execute in front of the last call to ${code assertion}
      afterLastCall - logic to execute after the last call to ${code assertion}
      sleepTime - interval for checking the condition
    • giveNull

      protected static <T> T giveNull()
      Useful for testing @NonNull annotated parameters

      This method can be used if you want to check the behavior of a method if you supply null to a non-null marked argument. If you use null directly the compiler will raise an error. Using this method allows you to work around that compiler check by using a null value that is marked as non-null.

      Returns:
      null for testing purpose