Package com.reallifedeveloper.tools.test
Class TestUtil
java.lang.Object
com.reallifedeveloper.tools.test.TestUtil
Miscellaneous utility methods that are useful when testing.
- Author:
- RealLifeDeveloper
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The date format used byparseDate(String)
("yyyy-MM-dd").static final String
The date+time format used byparseDateTime(String)
("yyyy-MM-dd HH:mm:ss"). -
Method Summary
Modifier and TypeMethodDescriptionstatic int
Gives a port number on the local machine that no server process is listening to.static @Nullable Object
getFieldValue
(Object obj, String fieldName) Gives the value of an object's field, which may be private.static void
injectField
(Object obj, String fieldName, Object value) Injects a value into an object's field, which may be private.static Date
Parses a date string on the form "yyyy-MM-dd" and returns the correspondingjava.util.Date
object.static Date
parseDateTime
(String dateTime) Parses a date and time string on the form "yyyy-MM-dd HH:mm:ss" and returns the corresondingjava.util.Date
object.static String
readResource
(String resourceName) Reads a string from a classpath resource, which is assumed to be UTF-8 encoded text.static void
writeToFile
(String s, String filename, Charset charset) Writes a string to a file using the given character encoding.
-
Field Details
-
DATE_FORMAT
The date format used byparseDate(String)
("yyyy-MM-dd").- See Also:
-
DATE_TIME_FORMAT
The date+time format used byparseDateTime(String)
("yyyy-MM-dd HH:mm:ss").- See Also:
-
-
Method Details
-
findFreePort
Gives a port number on the local machine that no server process is listening to.- Returns:
- a free port number
- Throws:
IOException
- if an I/O error occurs when trying to open a socket
-
parseDate
Parses a date string on the form "yyyy-MM-dd" and returns the correspondingjava.util.Date
object.- Parameters:
date
- the date string to parse, should be on the form "yyyy-MM-dd"- Returns:
- the
java.util.Date
corresponding todate
- Throws:
IllegalArgumentException
- ifdate
cannot be parsed
-
parseDateTime
Parses a date and time string on the form "yyyy-MM-dd HH:mm:ss" and returns the corresondingjava.util.Date
object.- Parameters:
dateTime
- the date+time string to parse, should be on the form "yyyy-MM-dd HH:mm:ss"- Returns:
- the
java.util.Date
corresponding todateTime
- Throws:
IllegalArgumentException
- ifdateTime
cannot be parsed
-
writeToFile
Writes a string to a file using the given character encoding.- Parameters:
s
- the string to writefilename
- the name of the file to write tocharset
- the character set to use, e.g.,java.nio.charset.StandardCharsets.UTF_8
- Throws:
IOException
- if writing to the file failed
-
readResource
Reads a string from a classpath resource, which is assumed to be UTF-8 encoded text.- Parameters:
resourceName
- the name of the classpath resource to read- Returns:
- a string representation of the classpath resource
resourceName
- Throws:
IOException
- if reading the resource failed
-
injectField
Injects a value into an object's field, which may be private.- Parameters:
obj
- the object in which to inject the valuefieldName
- the name of the fieldvalue
- the value to inject- Throws:
IllegalArgumentException
- ifobj
orfieldName
isnull
IllegalStateException
- if reflecction failure
-
getFieldValue
Gives the value of an object's field, which may be private.- Parameters:
obj
- the object containing the fieldfieldName
- the name of the field- Returns:
- the value of the field
fieldName
in the objectobj
- Throws:
IllegalArgumentException
- ifobj
orfieldName
isnull
IllegalStateException
- if reflection failure
-