Package de.xima.fc.testhelper
Class TestHelper
- java.lang.Object
- 
- de.xima.fc.testhelper.TestHelper
 
- 
 public final class TestHelper extends Object Helper with static utility functions for writing tests.
- 
- 
Constructor SummaryConstructors Constructor Description TestHelper()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static javax.mail.internet.InternetAddress[]addr(String... addresses)Parses the given strings intoInternetAddressinstances.static <T> T[]array(T... items)Shortcut for creating an array of items.static voidassertBlank(String actual)Asserts that the given string is blank.static voidassertContains(Iterable<?> expected, Iterable<?> actual)Asserts actual items contain the expected items.static voidassertContains(String expectedContent, String value)Asserts that the given string contains the expected content.static voidassertContainsEntries(Map<Object,Object> expected, Object actual)Asserts the given value is a map entry and that the expected map contains a key-value pair with the same key and value.static voidassertContainsValuesAtKeys(Set<?> expectedKeys, Set<?> expectedValues, Object actual)Asserts that the given value is aMapand that it contains the expected keys and values.static voidassertEmpty(Iterable<?> actual)Asserts that the given iterable is empty.static voidassertEmpty(String actual)Asserts that the given string is empty.static voidassertEndsWith(String expectedSuffix, String actual)Asserts that the given string ends with the expected suffix.static voidassertEqualsAsJson(Object expected, Object actual)Serializes both objects to JSON, then structurally compares those JSON values.static voidassertFullyConformingHtml(String html)Asserts the given HTML is W3C conform, without any errors, warnings or fatal errors.static voidassertFullyConformingHtml(String html, de.xima.nu_validator.wrapper.bridge.api.INuValidator validator)Asserts the given HTML is W3C conform, without any errors, warnings or fatal errors.static voidassertFullyConformingHtml(String html, de.xima.nu_validator.wrapper.bridge.api.INuValidator validator, Consumer<de.xima.nu_validator.wrapper.common.api.INuValidatorOptionsBuilder> configurator)Asserts the given HTML is W3C conform, without any errors, warnings or fatal errors.static voidassertFullyConformingHtml(String html, Consumer<de.xima.nu_validator.wrapper.common.api.INuValidatorOptionsBuilder> configurator)Asserts the given HTML is W3C conform, without any errors, warnings or fatal errors.static <T> TassertHasOneItem(Iterable<T> actual)Asserts that the given iterable has exactly one item, and returns that item.static voidassertHasSize(int expectedSize, Collection<?> actualCollection)Asserts the given collection has the expected size.static voidassertInRange(com.google.common.collect.Range<Double> expected, double actual)Asserts that the actual numerical value is within the expected range.static voidassertInRange(com.google.common.collect.Range<Float> expected, float actual)Asserts that the actual numerical value is within the expected range.static voidassertInRange(com.google.common.collect.Range<Integer> expected, int actual)Asserts that the actual numerical value is within the expected range.static voidassertInRange(com.google.common.collect.Range<Long> expected, long actual)Asserts that the actual numerical value is within the expected range.static <T> Class<? extends T>assertIsAssignableFrom(Class<T> expectedType, Class<?> actualType)Asserts that the actual type is assignable to the expected type.static <T> TassertIsInstance(Class<T> clazz, Object item)Deprecated.UseAssertions.assertInstanceOf(Class, Object).static <T extends Throwable>
 TassertIsOrHasCause(Class<T> expectedType, Throwable actual)Asserts that the given throwable is of the given type or has a throwable of that type somewhere in its chain of causes.static <T extends Throwable>
 TassertIsOrHasCause(Throwable actual, Class<T> expectedType)Deprecated.static voidassertLengthGreaterThan(int expectedMinLength, CharSequence value)Asserts that the given string's length is greater than the expected minimum length.static voidassertLinesEqual(String expected, String actual)Splits both strings into lines, trims them, and compares them line by line.static voidassertNoneEquals(Iterable<?> unexpected, Object actual)Asserts that the actual item is not equal to any of the unexpected items.static voidassertNotBlank(String actual)Asserts that the given string is blank.static voidassertNotContains(Iterable<?> unexpected, Iterable<?> actual)Asserts that the actual items do not contain the unexpected items.static voidassertSameItems(Object expected, Object actual)Asserts that both given values are collections with the same items, regardless of the order.static voidassertSetEquals(Iterable<?> expected, Iterable<?> actual)Asserts that both iterables contain the same items, regardless of the order.static voidassertStartsWith(String expectedPrefix, String actual)Asserts that the given string starts with the expected prefix.static voidassertStreamEquals(byte[] expected, InputStream actual)Asserts that the data from the given input stream equals the expected data.static voidassertStreamEquals(InputStream expected, InputStream actual)Asserts that the data from the given input stream equals the expected data.static voidassertStreamEquals(Reader expected, Reader actual)Asserts that the data from the given reader equals the expected data.static byte[]classpathResourceBinary(String resourcePath)Reads a resource from the class path as binary data.static StringclasspathResourcePlainText(String resourcePath)Reads a resource from the class path as a plain text string, using UTF-8 encoding.static FilecreateTmpDir()Creates a temporary directory.static FilecreateTmpFile(byte[] content, String fileName, String extension)Creates a file in the temp folder with the specified contentstatic FilecreateTmpFile(String content, String fileName, String extension)Creates a file in the temp folder with the specified contentstatic FilecreateTmpTextFile(String content)Creates a file in the temp folder with the specified contentstatic <T> List<T>list(T... items)Shortcut for creating a list of items.static <T> List<T[]>listOfArrays(T[]... items)Creates a list of arrays from the given arrays.static List<de.xima.mailserver.api.IMailMessage>mailsSorted(de.xima.mailserver.api.IMailInbox mailbox)Gets all mails from the given inbox, sorted by recipient.static <K,V>
 Map<K,V>map(Object... items)Shortcut for creating a map with arbitrary key-value pairs.static IUsermockUser(String name)Deprecated.static <K,V>
 Map<K,V>orderedMap(Object... items)Same asmap(Object...), but uses aLinkedHashMapimplementation that preserves the order of the key-value pairs.static intrandomFreePort()Finds a random port that is not in use by the current operating system.static BufferedImagereadBufferedImage(byte[] data, String formatName)Reads a buffered image from the given byte array using the given format name.static <T> Set<T>set(T... items)Shortcut for creating a set of items.static Stringstr(byte[] bytes)Decodes the given byte array to a string using UTF-8 encoding.static <K,V>
 Map<K,V>submap(Map<K,V> map, Iterable<?> keys)Creates a map with the data from the given map, but limited to the given keys.static <K,V>
 Map<K,V>submap(Map<K,V> map, K... keys)Creates a map with the data from the given map, but limited to the given keys.
 
- 
- 
- 
Method Detail- 
addrpublic static javax.mail.internet.InternetAddress[] addr(String... addresses) Parses the given strings intoInternetAddressinstances. The strings may contain multiple addresses separated by commas. The addresses are sorted by their string representation.- Parameters:
- addresses- Addresses to parse.
- Returns:
- An array of InternetAddressinstances.
 
 - 
array@SafeVarargs public static <T> T[] array(T... items) Shortcut for creating an array of items.- Type Parameters:
- T- Type of the items.
- Parameters:
- items- Items to be added to the array.
- Returns:
- An array with the items.
 
 - 
assertBlankpublic static void assertBlank(String actual) Asserts that the given string is blank.- Parameters:
- actual- The string to check.
 
 - 
assertContainspublic static void assertContains(Iterable<?> expected, Iterable<?> actual) Asserts actual items contain the expected items. It may contain other items as well.- Parameters:
- expected- Expected items that should be contained in the actual items.
- actual- Actual items that should contain the expected items.
 
 - 
assertContainspublic static void assertContains(String expectedContent, String value) Asserts that the given string contains the expected content.- Parameters:
- expectedContent- The expected content.
- value- The string to check.
 
 - 
assertContainsEntriespublic static void assertContainsEntries(Map<Object,Object> expected, Object actual) Asserts the given value is a map entry and that the expected map contains a key-value pair with the same key and value.- Parameters:
- expected- Map with entries.
- actual- Entry to check.
 
 - 
assertContainsValuesAtKeyspublic static void assertContainsValuesAtKeys(Set<?> expectedKeys, Set<?> expectedValues, Object actual) Asserts that the given value is aMapand that it contains the expected keys and values.- Parameters:
- expectedKeys- Expected keys.
- expectedValues- Expected values.
- actual- Map to check.
 
 - 
assertEmptypublic static void assertEmpty(Iterable<?> actual) Asserts that the given iterable is empty.- Parameters:
- actual- The actual value.
 
 - 
assertEmptypublic static void assertEmpty(String actual) Asserts that the given string is empty.- Parameters:
- actual- The actual value.
 
 - 
assertEndsWithpublic static void assertEndsWith(String expectedSuffix, String actual) Asserts that the given string ends with the expected suffix.- Parameters:
- expectedSuffix- The expected suffix.
- actual- The actual value.
 
 - 
assertEqualsAsJsonpublic static void assertEqualsAsJson(Object expected, Object actual) Serializes both objects to JSON, then structurally compares those JSON values. Useful when you have e.g. different map or list implementation with a bad equals implementation; or anything containing arrays (arrays do not implement equals in a way that compares their contents).- Parameters:
- expected- Expected value.
- actual- Actual value.
 
 - 
assertFullyConformingHtmlpublic static void assertFullyConformingHtml(String html) throws de.xima.nu_validator.wrapper.bridge.api.NuValidatorException Asserts the given HTML is W3C conform, without any errors, warnings or fatal errors.- Parameters:
- html- The HTML to validate.
- Throws:
- de.xima.nu_validator.wrapper.bridge.api.NuValidatorException- When the HTML could not be validated.
 
 - 
assertFullyConformingHtmlpublic static void assertFullyConformingHtml(String html, Consumer<de.xima.nu_validator.wrapper.common.api.INuValidatorOptionsBuilder> configurator) throws de.xima.nu_validator.wrapper.bridge.api.NuValidatorException Asserts the given HTML is W3C conform, without any errors, warnings or fatal errors.- Parameters:
- html- The HTML to validate.
- configurator- Configurator for the options to use for validation.
- Throws:
- de.xima.nu_validator.wrapper.bridge.api.NuValidatorException- When the HTML could not be validated.
 
 - 
assertFullyConformingHtmlpublic static void assertFullyConformingHtml(String html, de.xima.nu_validator.wrapper.bridge.api.INuValidator validator) throws de.xima.nu_validator.wrapper.bridge.api.NuValidatorException Asserts the given HTML is W3C conform, without any errors, warnings or fatal errors.- Parameters:
- html- The HTML to validate.
- validator- Validator to use for validation.
- Throws:
- de.xima.nu_validator.wrapper.bridge.api.NuValidatorException- When the HTML could not be validated.
 
 - 
assertFullyConformingHtmlpublic static void assertFullyConformingHtml(String html, de.xima.nu_validator.wrapper.bridge.api.INuValidator validator, Consumer<de.xima.nu_validator.wrapper.common.api.INuValidatorOptionsBuilder> configurator) throws de.xima.nu_validator.wrapper.bridge.api.NuValidatorException Asserts the given HTML is W3C conform, without any errors, warnings or fatal errors.- Parameters:
- html- The HTML to validate.
- validator- Validator to use for validation.
- configurator- Configurator for the options to use for validation.
- Throws:
- de.xima.nu_validator.wrapper.bridge.api.NuValidatorException- When the HTML could not be validated.
 
 - 
assertHasOneItempublic static <T> T assertHasOneItem(Iterable<T> actual) Asserts that the given iterable has exactly one item, and returns that item.- Type Parameters:
- T- Type of the item.
- Parameters:
- actual- The actual iterable.
- Returns:
- The single item in the iterable.
 
 - 
assertHasSizepublic static void assertHasSize(int expectedSize, Collection<?> actualCollection)Asserts the given collection has the expected size.- Parameters:
- expectedSize- Expected size.
- actualCollection- Actual collection.
 
 - 
assertInRangepublic static void assertInRange(com.google.common.collect.Range<Double> expected, double actual) Asserts that the actual numerical value is within the expected range.- Parameters:
- expected- The expected range.
- actual- The actual value.
 
 - 
assertInRangepublic static void assertInRange(com.google.common.collect.Range<Float> expected, float actual) Asserts that the actual numerical value is within the expected range.- Parameters:
- expected- The expected range.
- actual- The actual value.
 
 - 
assertInRangepublic static void assertInRange(com.google.common.collect.Range<Integer> expected, int actual) Asserts that the actual numerical value is within the expected range.- Parameters:
- expected- The expected range.
- actual- The actual value.
 
 - 
assertInRangepublic static void assertInRange(com.google.common.collect.Range<Long> expected, long actual) Asserts that the actual numerical value is within the expected range.- Parameters:
- expected- The expected range.
- actual- The actual value.
 
 - 
assertIsAssignableFrompublic static <T> Class<? extends T> assertIsAssignableFrom(Class<T> expectedType, Class<?> actualType) Asserts that the actual type is assignable to the expected type.- Type Parameters:
- T- Expected type.
- Parameters:
- expectedType- Expected type.
- actualType- Actual type.
- Returns:
- The actual type cast to the expected type.
 
 - 
assertIsInstance@Deprecated public static <T> T assertIsInstance(Class<T> clazz, Object item) Deprecated.UseAssertions.assertInstanceOf(Class, Object).Checks that the given value is an instance of the expected class.- Type Parameters:
- T- Expected type.
- Parameters:
- clazz- Expected class.
- item- Instance to check.
- Returns:
- The instance cast to the expected class.
 
 - 
assertIsOrHasCause@Deprecated public static <T extends Throwable> T assertIsOrHasCause(Throwable actual, Class<T> expectedType) Deprecated.Asserts that the given throwable is of the given type or has a throwable of that type somewhere in its chain of causes.- Type Parameters:
- T- Expected type.
- Parameters:
- actual- The actual throwable.
- expectedType- The expected type.
- Returns:
- The throwable of the expected type.
 
 - 
assertIsOrHasCausepublic static <T extends Throwable> T assertIsOrHasCause(Class<T> expectedType, Throwable actual) Asserts that the given throwable is of the given type or has a throwable of that type somewhere in its chain of causes.- Type Parameters:
- T- Expected type.
- Parameters:
- actual- The actual throwable.
- expectedType- The expected type.
- Returns:
- The throwable of the expected type.
 
 - 
assertLengthGreaterThanpublic static void assertLengthGreaterThan(int expectedMinLength, CharSequence value)Asserts that the given string's length is greater than the expected minimum length.- Parameters:
- expectedMinLength- The expected minimum length.
- value- The string to check.
 
 - 
assertLinesEqualpublic static void assertLinesEqual(String expected, String actual) Splits both strings into lines, trims them, and compares them line by line. Useful when you have e.g. different line endings or leading/trailing whitespace. Line terminators are line feed (LF), carriage return (CR), or carriage return followed by line feed (CRLF).- Parameters:
- expected- Expected lines.
- actual- Actual lines.
 
 - 
assertNoneEqualspublic static void assertNoneEquals(Iterable<?> unexpected, Object actual) Asserts that the actual item is not equal to any of the unexpected items.- Parameters:
- unexpected- Unexpected items of which none should be equal to the actual item.
- actual- Actual item that should not be equal to any of the unexpected items.
 
 - 
assertNotBlankpublic static void assertNotBlank(String actual) Asserts that the given string is blank.- Parameters:
- actual- The string to check.
 
 - 
assertNotContainspublic static void assertNotContains(Iterable<?> unexpected, Iterable<?> actual) Asserts that the actual items do not contain the unexpected items.- Parameters:
- unexpected- Unexpected items that should not be in the actual items.
- actual- Actual item that should not contain the unexpected items.
 
 - 
assertSameItemspublic static void assertSameItems(Object expected, Object actual) Asserts that both given values are collections with the same items, regardless of the order.- Parameters:
- expected- Expected collection.
- actual- Actual collection.
 
 - 
assertSetEqualspublic static void assertSetEquals(Iterable<?> expected, Iterable<?> actual) Asserts that both iterables contain the same items, regardless of the order.- Parameters:
- expected- Expected items.
- actual- Actual items.
 
 - 
assertStartsWithpublic static void assertStartsWith(String expectedPrefix, String actual) Asserts that the given string starts with the expected prefix.- Parameters:
- expectedPrefix- The expected prefix.
- actual- String to check.
 
 - 
assertStreamEqualspublic static void assertStreamEquals(byte[] expected, InputStream actual)Asserts that the data from the given input stream equals the expected data.- Parameters:
- expected- Expected data.
- actual- Input stream to check.
 
 - 
assertStreamEqualspublic static void assertStreamEquals(InputStream expected, InputStream actual) Asserts that the data from the given input stream equals the expected data.- Parameters:
- expected- Input stream with the expected data.
- actual- Input stream to check.
 
 - 
assertStreamEqualspublic static void assertStreamEquals(Reader expected, Reader actual) Asserts that the data from the given reader equals the expected data.- Parameters:
- expected- Reader with the expected data.
- actual- Reader to check.
 
 - 
classpathResourceBinarypublic static byte[] classpathResourceBinary(String resourcePath) Reads a resource from the class path as binary data.- Parameters:
- resourcePath- Path to the resource, with or without a leading slash.
- Returns:
- The content of the resource as binary data.
 
 - 
classpathResourcePlainTextpublic static String classpathResourcePlainText(String resourcePath) Reads a resource from the class path as a plain text string, using UTF-8 encoding.- Parameters:
- resourcePath- Path to the resource, with or without a leading slash.
- Returns:
- The content of the resource as a string.
 
 - 
createTmpDirpublic static File createTmpDir() throws IOException Creates a temporary directory. Make sure to delete it once you're done with it!- Returns:
- The temporary directory.
- Throws:
- IOException- When the directory could not be created.
 
 - 
createTmpFilepublic static File createTmpFile(byte[] content, String fileName, String extension) throws IOException Creates a file in the temp folder with the specified content- Parameters:
- content- the file content
- fileName- Name of the tmp file
- extension- Extension of the tmp file.
- Returns:
- the Fileinstance
- Throws:
- IOException- When the text file could not be created.
 
 - 
createTmpFilepublic static File createTmpFile(String content, String fileName, String extension) throws IOException Creates a file in the temp folder with the specified content- Parameters:
- content- the file content
- fileName- Name of the tmp file
- extension- Extension of the tmp file.
- Returns:
- the Fileinstance
- Throws:
- IOException- When the text file could not be created.
 
 - 
createTmpTextFilepublic static File createTmpTextFile(String content) throws IOException Creates a file in the temp folder with the specified content- Parameters:
- content- the file content
- Returns:
- the Fileinstance
- Throws:
- IOException- When the text file could not be created.
 
 - 
list@SafeVarargs public static <T> List<T> list(T... items) Shortcut for creating a list of items.- Type Parameters:
- T- Type of the items.
- Parameters:
- items- Items to be added to the list.
- Returns:
- A list of the items.
 
 - 
listOfArrays@SafeVarargs public static <T> List<T[]> listOfArrays(T[]... items) Creates a list of arrays from the given arrays. You could also uselist(Object...)but when that method is used with a single array, it will create a list of the array elements instead of a list of arrays.- Type Parameters:
- T- Type of the array elements.
- Parameters:
- items- Arrays to be added to the list.
- Returns:
- A list of arrays.
 
 - 
mailsSortedpublic static List<de.xima.mailserver.api.IMailMessage> mailsSorted(de.xima.mailserver.api.IMailInbox mailbox) throws de.xima.mailserver.api.MailServerException Gets all mails from the given inbox, sorted by recipient.- Parameters:
- mailbox- Mailbox to get the mails from.
- Returns:
- A list of all mails in the mailbox, sorted by recipient.
- Throws:
- de.xima.mailserver.api.MailServerException- When the mails could not be retrieved.
 
 - 
mappublic static <K,V> Map<K,V> map(Object... items) Shortcut for creating a map with arbitrary key-value pairs. The number of arguments must be even. In comparison withMap.of(), this method allows for more than 10 key-value pairs, but at the cost of giving up type safety. This usually isn't a problem in tests, but if you need type safety, you should useMap.of()instead.- Type Parameters:
- K- Type of the keys.
- V- Type of the values.
- Parameters:
- items- Key-value pairs to be added to the map.
- Returns:
- A map with the key-value pairs.
 
 - 
mockUser@Deprecated public static IUser mockUser(String name) Deprecated.- Parameters:
- name- Name of the user, unused.
- Returns:
- An anonymous virtual user.
 
 - 
orderedMappublic static <K,V> Map<K,V> orderedMap(Object... items) Same asmap(Object...), but uses aLinkedHashMapimplementation that preserves the order of the key-value pairs. Creates a map with arbitrary key-value pairs. The number of arguments must be even.- Type Parameters:
- K- Type of the keys.
- V- Type of the values.
- Parameters:
- items- Key-value pairs to be added to the map.
- Returns:
- A map with the key-value pairs.
 
 - 
randomFreePortpublic static int randomFreePort() Finds a random port that is not in use by the current operating system. Note that there is no guarantee that the port will still be free after this method returns, when some application binds to it in the meantime.- Returns:
- A random free port.
 
 - 
readBufferedImagepublic static BufferedImage readBufferedImage(byte[] data, String formatName) throws IOException Reads a buffered image from the given byte array using the given format name.- Parameters:
- data- Byte array with the image data.
- formatName- Format name of the image, e.g. "png" or "jpeg".
- Returns:
- The buffered image.
- Throws:
- IOException- When the image could not be read.
 
 - 
set@SafeVarargs public static <T> Set<T> set(T... items) Shortcut for creating a set of items.- Type Parameters:
- T- Type of the items.
- Parameters:
- items- Items to be added to the set.
- Returns:
- A set with the items.
 
 - 
strpublic static String str(byte[] bytes) Decodes the given byte array to a string using UTF-8 encoding.- Parameters:
- bytes- Byte array to decode.
- Returns:
- The decoded string.
 
 - 
submappublic static <K,V> Map<K,V> submap(Map<K,V> map, Iterable<?> keys) Creates a map with the data from the given map, but limited to the given keys.- Type Parameters:
- K- Type of the keys.
- V- Type of the values.
- Parameters:
- map- Map to get the data from.
- keys- Keys to include in the new map.
- Returns:
- A map with the data from the given map, but limited to the given keys.
 
 - 
submap@SafeVarargs public static <K,V> Map<K,V> submap(Map<K,V> map, K... keys) Creates a map with the data from the given map, but limited to the given keys.- Type Parameters:
- K- Type of the keys.
- V- Type of the values.
- Parameters:
- map- Map to get the data from.
- keys- Keys to include in the new map.
- Returns:
- A map with the data from the given map, but limited to the given keys.
 
 
- 
 
-