Class TestTools


  • public final class TestTools
    extends java.lang.Object
    Utilities to aid in producing JUnit tests.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertAfter​(int delay, int duration, java.lang.Runnable assertions)
      Runs the specified runnable until it completes successfully or until the specified time expires.
      static void assertAfter​(int duration, java.lang.Runnable assertions)
      Runs the specified runnable until it completes successfully or until the specified time expires.
      static java.util.List<java.io.File> createTestFiles​(java.util.List<java.lang.String> paths)
      Creates a directory tree of test files.
      static java.util.List<java.io.File> createTestFiles​(java.util.List<java.lang.String> paths, int minSize, int maxSize)
      Creates a directory tree of test files.
      static void delay​(int ms)
      Suspends the current thread for a specified number of millis.
      static int findAvailablePort​(int defaultPort)  
      static long now()
      Returns the current time in millis since epoch.
      static void print​(java.lang.String msg)  
      static void tweakBytes​(java.util.Random random, byte[] data, int count)
      Tweaks the given number of bytes in a byte array.
      static void tweakBytes​(java.util.Random random, byte[] data, int count, int start, int end)
      Tweaks the given number of bytes in the specified range of a byte array.
      static void writeRandomFile​(java.io.File file, int minSize, int maxSize)
      Writes random binary content into the specified file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • print

        public static void print​(java.lang.String msg)
      • delay

        public static void delay​(int ms)
        Suspends the current thread for a specified number of millis.
        Parameters:
        ms - number of millis
      • now

        public static long now()
        Returns the current time in millis since epoch.
        Returns:
        current time
      • assertAfter

        public static void assertAfter​(int delay,
                                       int duration,
                                       java.lang.Runnable assertions)
        Runs the specified runnable until it completes successfully or until the specified time expires. If the latter occurs, the first encountered assertion on the last attempt will be re-thrown. Errors other than assertion errors will be propagated immediately.

        Assertions attempts will not be closer than 10 millis apart and no further than 50 millis.

        Parameters:
        delay - number of millis to delay before the first attempt
        duration - number of milliseconds beyond the current time
        assertions - test assertions runnable
      • assertAfter

        public static void assertAfter​(int duration,
                                       java.lang.Runnable assertions)
        Runs the specified runnable until it completes successfully or until the specified time expires. If the latter occurs, the first encountered assertion on the last attempt will be re-thrown. Errors other than assertion errors will be propagated immediately.

        Assertions attempts will not be closer than 10 millis apart and no further than 50 millis.

        Parameters:
        duration - number of milliseconds beyond the current time
        assertions - test assertions runnable
      • createTestFiles

        public static java.util.List<java.io.File> createTestFiles​(java.util.List<java.lang.String> paths)
                                                            throws java.io.IOException
        Creates a directory tree of test files. To signify creating a directory file path should end with '/'.
        Parameters:
        paths - list of file paths
        Returns:
        list of created files
        Throws:
        java.io.IOException - if there is an issue
      • createTestFiles

        public static java.util.List<java.io.File> createTestFiles​(java.util.List<java.lang.String> paths,
                                                                   int minSize,
                                                                   int maxSize)
                                                            throws java.io.IOException
        Creates a directory tree of test files. To signify creating a directory file path should end with '/'.
        Parameters:
        paths - list of file paths
        minSize - minimum file size in bytes
        maxSize - maximum file size in bytes
        Returns:
        list of created files
        Throws:
        java.io.IOException - if there is an issue
      • writeRandomFile

        public static void writeRandomFile​(java.io.File file,
                                           int minSize,
                                           int maxSize)
                                    throws java.io.IOException
        Writes random binary content into the specified file. The number of bytes will be random between the given minimum and maximum.
        Parameters:
        file - file to write data to
        minSize - minimum number of bytes to write
        maxSize - maximum number of bytes to write
        Throws:
        java.io.IOException - if there is an issue
      • tweakBytes

        public static void tweakBytes​(java.util.Random random,
                                      byte[] data,
                                      int count)
        Tweaks the given number of bytes in a byte array.
        Parameters:
        random - random number generator
        data - byte array to be tweaked
        count - number of bytes to tweak
      • tweakBytes

        public static void tweakBytes​(java.util.Random random,
                                      byte[] data,
                                      int count,
                                      int start,
                                      int end)
        Tweaks the given number of bytes in the specified range of a byte array.
        Parameters:
        random - random number generator
        data - byte array to be tweaked
        count - number of bytes to tweak
        start - index at beginning of range (inclusive)
        end - index at end of range (exclusive)
      • findAvailablePort

        public static int findAvailablePort​(int defaultPort)