Class TestUtil
java.lang.Object
de.tomatengames.util.TestUtil
Provides methods that are helpful for JUnit testing.
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidassertBinaryFile(byte[] expectedBytes, Path basePath, String path) Asserts that the binary file exists and matches the specified byte array.static voidassertBinaryFile(String expectedHex, Path basePath, String path) Asserts that the binary file exists and matches the hexadecimal string.static voidassertFileExecutable(Path basePath, String path) Asserts that the specified file is executable.static voidassertFileExists(boolean expectedExists, Path basePath, String path) Asserts that the specified file exists if and only ifexpectedExistsistrue.static voidassertFileTree(Path expected, Path actual) Asserts that both specified files or directories are recursively equal.static voidassertFileTree(Path expected, Path actual, Predicate<? super Path> filter) Asserts that both specified files or directories are recursively equal.static voidassertOutputStream(byte[] expectedBytes, RefConsumerWithThrows<OutputStream, IOException> action) Runs the specified action on a newOutputStreamand asserts that the bytes written to the output stream match the expected bytes.static voidassertOutputStream(String expectedHex, RefConsumerWithThrows<OutputStream, IOException> action) Runs the specified action on a newOutputStreamand asserts that the bytes written to the output stream match the expected hexadecimal string.static voidassertOutputStream(String expectedHex, ByteArrayOutputStream outputStream) Asserts that the bytes written to the output stream match the expected hexadecimal string.static ByteArrayOutputStreamassertOutputStreamThrows(Class<?> expectedType, RefConsumerWithThrows<OutputStream, Throwable> action) Runs the specified action on a newOutputStreamand asserts that aThrowableof the specified type is thrown.static voidassertTextFile(String expectedText, Path basePath, String path) Asserts that the specified text file exists and contains the specified text.static <T extends Throwable>
TassertThrowsWithMessage(Class<T> expectedType, String expectedMessage, VoidConsumerWithThrows<?> executor) Asserts that the given executor throws an exception of the expected type with the expected message.
-
Method Details
-
assertFileExists
Asserts that the specified file exists if and only ifexpectedExistsistrue.- Parameters:
expectedExists- If the file must exist. Iffalse, the file must not exist.basePath- The base path.path- The path to the file, relative to the base path.
-
assertFileExecutable
-
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. Ifnull, 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. Ifnull, 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. Ifnull, 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 newOutputStreamand asserts that aThrowableof the specified type is thrown.- Parameters:
expectedType- The expected type of theThrowablethrown by the action. Notnull.action- The action that should output bytes to the output stream. Notnull.- Returns:
- The output stream in which the action has written. Not
null. - Since:
- 1.4
-
assertOutputStream
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 benull.outputStream- The output stream that should be checked. Must not benull.- Since:
- 1.4
-
assertOutputStream
public static void assertOutputStream(String expectedHex, RefConsumerWithThrows<OutputStream, IOException> action) throws IOException Runs the specified action on a newOutputStreamand 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 benull.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 newOutputStreamand 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 benull.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 benull.- Throws:
IOException- If an I/O error occurs.
-
assertFileTree
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
-