Class CommonTestUtils


  • public class CommonTestUtils
    extends Object
    This class contains reusable utility methods for unit tests.
    • Constructor Detail

      • CommonTestUtils

        public CommonTestUtils()
    • Method Detail

      • createCopySerializable

        public static <T extends Serializable> T createCopySerializable​(T original)
                                                                 throws IOException
        Creates a copy of an object via Java Serialization.
        Parameters:
        original - The original object.
        Returns:
        The copied object.
        Throws:
        IOException
      • createTempFile

        public static String createTempFile​(String contents)
                                     throws IOException
        Creates a temporary file that contains the given string. The file is written with the platform's default encoding.

        The temp file is automatically deleted on JVM exit.

        Parameters:
        contents - The contents to be written to the file.
        Returns:
        The temp file URI.
        Throws:
        IOException
      • blockForeverNonInterruptibly

        public static void blockForeverNonInterruptibly()
        Permanently blocks the current thread. The thread cannot be woken up via Thread.interrupt().
      • setEnv

        public static void setEnv​(Map<String,​String> newenv,
                                  boolean clearExisting)
      • containsCause

        public static boolean containsCause​(Throwable throwable,
                                            Class<? extends Throwable> cause)
        Checks whether the given throwable contains the given cause as a cause. The cause is not checked on equality but on type equality.
        Parameters:
        throwable - Throwable to check for the cause
        cause - Cause to look for
        Returns:
        True if the given Throwable contains the given cause (type equality); otherwise false
      • assertThrows

        public static void assertThrows​(String msg,
                                        Class<? extends Exception> expected,
                                        Callable<?> code)
        Checks whether an exception with a message occurs when running a piece of code.
      • waitUtil

        public static void waitUtil​(java.util.function.Supplier<Boolean> condition,
                                    java.time.Duration timeout,
                                    java.time.Duration pause,
                                    String errorMsg)
                             throws TimeoutException,
                                    InterruptedException
        Wait util the given condition is met or timeout.
        Parameters:
        condition - the condition to wait for.
        timeout - the maximum time to wait for the condition to become true.
        pause - delay between condition checks.
        errorMsg - the error message to include in the TimeoutException if the condition was not met before timeout.
        Throws:
        TimeoutException - if the condition is not met before timeout.
        InterruptedException - if the thread is interrupted.
      • waitUntilIgnoringExceptions

        public static void waitUntilIgnoringExceptions​(java.util.function.Supplier<Boolean> condition,
                                                       java.time.Duration timeout,
                                                       java.time.Duration pause,
                                                       String errorMsg)
                                                throws TimeoutException,
                                                       InterruptedException
        Wait until the given condition is met or timeout, ignoring any exceptions thrown by the condition.
        Parameters:
        condition - the condition to wait for.
        timeout - the maximum time to wait for the condition to become true.
        pause - delay between condition checks.
        errorMsg - the error message to include in the TimeoutException if the condition was not met before timeout.
        Throws:
        TimeoutException - if the condition is not met before timeout.
        InterruptedException - if the thread is interrupted.
      • waitUtil

        public static void waitUtil​(java.util.function.Supplier<Boolean> condition,
                                    java.time.Duration timeout,
                                    String errorMsg)
                             throws TimeoutException,
                                    InterruptedException
        Wait util the given condition is met or timeout.
        Parameters:
        condition - the condition to wait for.
        timeout - the maximum time to wait for the condition to become true.
        errorMsg - the error message to include in the TimeoutException if the condition was not met before timeout.
        Throws:
        TimeoutException - if the condition is not met before timeout.
        InterruptedException - if the thread is interrupted.