Class TestUtil

java.lang.Object
de.tomatengames.util.TestUtil

public class TestUtil extends Object
Provides methods that are helpful for JUnit testing.
Since:
1.0
  • Method Details

    • assertFileExists

      public static void assertFileExists(boolean expectedExists, Path basePath, String path)
      Asserts that the specified file exists if and only if expectedExists is true.
      Parameters:
      expectedExists - If the file must exist. If false, the file must not exist.
      basePath - The base path.
      path - The path to the file, relative to the base path.
    • assertFileExecutable

      public static void assertFileExecutable(Path basePath, String path)
      Asserts that the specified file is executable. On some systems, e.g. Windows, this test never fails.
      Parameters:
      basePath - The base path.
      path - The path to the file, relative to the base path.
    • assertTextFile

      public static void assertTextFile(String expectedText, Path basePath, String path) throws IOException
      Asserts that the specified text file exists and contains the specified text.
      Parameters:
      expectedText - The expected text content of the file. If null, the text file must not exist.
      basePath - The base path.
      path - The path to the text file, relative to the base path.
      Throws:
      IOException - If an I/O error occurs.
    • assertBinaryFile

      public static void assertBinaryFile(String expectedHex, Path basePath, String path) throws IOException
      Asserts that the binary file exists and matches the hexadecimal string.
      Parameters:
      expectedHex - A hex string that represents the expected bytes. Whitespace characters are ignored. If null, the binary file must not exist.
      basePath - The base path.
      path - The path to the binary file, relative to the base path.
      Throws:
      IOException - If an I/O error occurs.
    • assertBinaryFile

      public static void assertBinaryFile(byte[] expectedBytes, Path basePath, String path) throws IOException
      Asserts that the binary file exists and matches the specified byte array.
      Parameters:
      expectedBytes - The expected bytes of the file. If null, the binary file must not exist.
      basePath - The base path.
      path - The path to the binary file, relative to the base path.
      Throws:
      IOException - If an I/O error occurs.
      Since:
      1.2
    • assertOutputStreamThrows

      public static ByteArrayOutputStream assertOutputStreamThrows(Class<?> expectedType, RefConsumerWithThrows<OutputStream, Throwable> action)
      Runs the specified action on a new OutputStream and asserts that a Throwable of the specified type is thrown.
      Parameters:
      expectedType - The expected type of the Throwable thrown by the action. Not null.
      action - The action that should output bytes to the output stream. Not null.
      Returns:
      The output stream in which the action has written. Not null.
      Since:
      1.4
    • assertOutputStream

      public static void assertOutputStream(String expectedHex, ByteArrayOutputStream outputStream)
      Asserts that the bytes written to the output stream match the expected hexadecimal string.

      This method can be chained with assertOutputStreamThrows(Class, RefConsumerWithThrows) to check the data written to an output stream if an exception occurred.

      assertOutputStream("...", assertOutputStreamThrows(IOException.class,
          out -> out.write(...)))
      
      Parameters:
      expectedHex - A hex string that represents the expected bytes. Whitespace characters are ignored. Must not be null.
      outputStream - The output stream that should be checked. Must not be null.
      Since:
      1.4
    • assertOutputStream

      public static void assertOutputStream(String expectedHex, RefConsumerWithThrows<OutputStream, IOException> action) throws IOException
      Runs the specified action on a new OutputStream and asserts that the bytes written to the output stream match the expected hexadecimal string.
      Parameters:
      expectedHex - A hex string that represents the expected bytes. Whitespace characters are ignored. Must not be null.
      action - The action that should output bytes to the output stream.
      Throws:
      IOException - If an I/O error occurs while writing to the stream.
      Since:
      1.2
    • assertOutputStream

      public static void assertOutputStream(byte[] expectedBytes, RefConsumerWithThrows<OutputStream, IOException> action) throws IOException
      Runs the specified action on a new OutputStream and asserts that the bytes written to the output stream match the expected bytes.
      Parameters:
      expectedBytes - The bytes expected to be written to the stream. Must not be null.
      action - The action that should output bytes to the output stream.
      Throws:
      IOException - If an I/O error occurs while writing to the stream.
      Since:
      1.2
    • assertFileTree

      public static void assertFileTree(Path expected, Path actual, Predicate<? super Path> filter) throws IOException
      Asserts that both specified files or directories are recursively equal. If both paths represent files, the content of these files must be equal. If both paths are directories, they must have the an equal file list. The inner files and directories must also be equal. Links are followed.
      Parameters:
      expected - The expected file tree.
      actual - The actual file tree.
      filter - A filter that allows to exclude some files from the test. Files and directories that do not match this filter can appear arbitrarily in file lists. Directories that do not match this filter are not checked recursively. Must not be null.
      Throws:
      IOException - If an I/O error occurs.
    • assertFileTree

      public static void assertFileTree(Path expected, Path actual) throws IOException
      Asserts that both specified files or directories are recursively equal. If both paths represent files, the content of these files must be equal. If both paths are directories, they must have the an equal file list. The inner files and directories must also be equal. Links are followed.

      Files and directories that start with a "." are ignored. They can appear arbitrarily in file lists and they are not checked recursively.

      Parameters:
      expected - The expected file tree.
      actual - The actual file tree.
      Throws:
      IOException - If an I/O error occurs.
    • assertThrowsWithMessage

      public static <T extends Throwable> T assertThrowsWithMessage(Class<T> expectedType, String expectedMessage, VoidConsumerWithThrows<?> executor)
      Asserts that the given executor throws an exception of the expected type with the expected message.
      Type Parameters:
      T - The type of the exception that is expected.
      Parameters:
      expectedType - The type of the exception that is expected. Not null.
      expectedMessage - The message that is expected for the exception.
      executor - The executor that is used to execute the code that should throw an exception. Not null.
      Returns:
      The exception that was thrown by the executor. Not null.
      Throws:
      AssertionError - If the executor does not throw an exception of the expected type with the expected message.
      Since:
      1.8