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 Summary
Constructors Constructor Description TestHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static javax.mail.internet.InternetAddress[]
addr(String... addresses)
Parses the given strings intoInternetAddress
instances.static <T> T[]
array(T... items)
Shortcut for creating an array of items.static void
assertBlank(String actual)
Asserts that the given string is blank.static void
assertContains(Iterable<?> expected, Iterable<?> actual)
Asserts actual items contain the expected items.static void
assertContains(String expectedContent, String value)
Asserts that the given string contains the expected content.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.static void
assertContainsValuesAtKeys(Set<?> expectedKeys, Set<?> expectedValues, Object actual)
Asserts that the given value is aMap
and that it contains the expected keys and values.static void
assertEmpty(Iterable<?> actual)
Asserts that the given iterable is empty.static void
assertEmpty(String actual)
Asserts that the given string is empty.static void
assertEndsWith(String expectedSuffix, String actual)
Asserts that the given string ends with the expected suffix.static void
assertEqualsAsJson(Object expected, Object actual)
Serializes both objects to JSON, then structurally compares those JSON values.static void
assertFullyConformingHtml(String html)
Asserts the given HTML is W3C conform, without any errors, warnings or fatal errors.static void
assertFullyConformingHtml(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 void
assertFullyConformingHtml(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 void
assertFullyConformingHtml(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> T
assertHasOneItem(Iterable<T> actual)
Asserts that the given iterable has exactly one item, and returns that item.static void
assertHasSize(int expectedSize, Collection<?> actualCollection)
Asserts the given collection has the expected size.static void
assertInRange(com.google.common.collect.Range<Double> expected, double actual)
Asserts that the actual numerical value is within the expected range.static void
assertInRange(com.google.common.collect.Range<Float> expected, float actual)
Asserts that the actual numerical value is within the expected range.static void
assertInRange(com.google.common.collect.Range<Integer> expected, int actual)
Asserts that the actual numerical value is within the expected range.static void
assertInRange(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> T
assertIsInstance(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 void
assertLengthGreaterThan(int expectedMinLength, CharSequence value)
Asserts that the given string's length is greater than the expected minimum length.static void
assertLinesEqual(String expected, String actual)
Splits both strings into lines, trims them, and compares them line by line.static void
assertNoneEquals(Iterable<?> unexpected, Object actual)
Asserts that the actual item is not equal to any of the unexpected items.static void
assertNotBlank(String actual)
Asserts that the given string is blank.static void
assertNotContains(Iterable<?> unexpected, Iterable<?> actual)
Asserts that the actual items do not contain the unexpected items.static void
assertSameItems(Object expected, Object actual)
Asserts that both given values are collections with the same items, regardless of the order.static void
assertSetEquals(Iterable<?> expected, Iterable<?> actual)
Asserts that both iterables contain the same items, regardless of the order.static void
assertStartsWith(String expectedPrefix, String actual)
Asserts that the given string starts with the expected prefix.static void
assertStreamEquals(byte[] expected, InputStream actual)
Asserts that the data from the given input stream equals the expected data.static void
assertStreamEquals(InputStream expected, InputStream actual)
Asserts that the data from the given input stream equals the expected data.static void
assertStreamEquals(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 String
classpathResourcePlainText(String resourcePath)
Reads a resource from the class path as a plain text string, using UTF-8 encoding.static File
createTmpDir()
Creates a temporary directory.static File
createTmpFile(byte[] content, String fileName, String extension)
Creates a file in the temp folder with the specified contentstatic File
createTmpFile(String content, String fileName, String extension)
Creates a file in the temp folder with the specified contentstatic File
createTmpTextFile(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 IUser
mockUser(String name)
Deprecated.static <K,V>
Map<K,V>orderedMap(Object... items)
Same asmap(Object...)
, but uses aLinkedHashMap
implementation that preserves the order of the key-value pairs.static int
randomFreePort()
Finds a random port that is not in use by the current operating system.static BufferedImage
readBufferedImage(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 String
str(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
-
addr
public static javax.mail.internet.InternetAddress[] addr(String... addresses)
Parses the given strings intoInternetAddress
instances. 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
InternetAddress
instances.
-
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.
-
assertBlank
public static void assertBlank(String actual)
Asserts that the given string is blank.- Parameters:
actual
- The string to check.
-
assertContains
public 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.
-
assertContains
public 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.
-
assertContainsEntries
public 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.
-
assertContainsValuesAtKeys
public static void assertContainsValuesAtKeys(Set<?> expectedKeys, Set<?> expectedValues, Object actual)
Asserts that the given value is aMap
and that it contains the expected keys and values.- Parameters:
expectedKeys
- Expected keys.expectedValues
- Expected values.actual
- Map to check.
-
assertEmpty
public static void assertEmpty(Iterable<?> actual)
Asserts that the given iterable is empty.- Parameters:
actual
- The actual value.
-
assertEmpty
public static void assertEmpty(String actual)
Asserts that the given string is empty.- Parameters:
actual
- The actual value.
-
assertEndsWith
public 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.
-
assertEqualsAsJson
public 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.
-
assertFullyConformingHtml
public 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.
-
assertFullyConformingHtml
public 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.
-
assertFullyConformingHtml
public 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.
-
assertFullyConformingHtml
public 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.
-
assertHasOneItem
public 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.
-
assertHasSize
public static void assertHasSize(int expectedSize, Collection<?> actualCollection)
Asserts the given collection has the expected size.- Parameters:
expectedSize
- Expected size.actualCollection
- Actual collection.
-
assertInRange
public 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.
-
assertInRange
public 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.
-
assertInRange
public 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.
-
assertInRange
public 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.
-
assertIsAssignableFrom
public 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.
-
assertIsOrHasCause
public 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.
-
assertLengthGreaterThan
public 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.
-
assertLinesEqual
public 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.
-
assertNoneEquals
public 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.
-
assertNotBlank
public static void assertNotBlank(String actual)
Asserts that the given string is blank.- Parameters:
actual
- The string to check.
-
assertNotContains
public 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.
-
assertSameItems
public 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.
-
assertSetEquals
public 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.
-
assertStartsWith
public 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.
-
assertStreamEquals
public 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.
-
assertStreamEquals
public 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.
-
assertStreamEquals
public 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.
-
classpathResourceBinary
public 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.
-
classpathResourcePlainText
public 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.
-
createTmpDir
public 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.
-
createTmpFile
public 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 contentfileName
- Name of the tmp fileextension
- Extension of the tmp file.- Returns:
- the
File
instance - Throws:
IOException
- When the text file could not be created.
-
createTmpFile
public 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 contentfileName
- Name of the tmp fileextension
- Extension of the tmp file.- Returns:
- the
File
instance - Throws:
IOException
- When the text file could not be created.
-
createTmpTextFile
public 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
File
instance - 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.
-
mailsSorted
public 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.
-
map
public 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.
-
orderedMap
public static <K,V> Map<K,V> orderedMap(Object... items)
Same asmap(Object...)
, but uses aLinkedHashMap
implementation 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.
-
randomFreePort
public 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.
-
readBufferedImage
public 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.
-
str
public 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.
-
submap
public 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.
-
-