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 StringThe date format used byparseDate(String)("yyyy-MM-dd").static final StringThe date+time format used byparseDateTime(String)("yyyy-MM-dd HH:mm:ss"). -
Method Summary
Modifier and TypeMethodDescriptionstatic intGives a port number on the local machine that no server process is listening to.static @Nullable ObjectgetFieldValue(Object obj, String fieldName) Gives the value of an object's field, which may be private.static voidinjectField(Object obj, String fieldName, @Nullable Object value) Injects a value into an object's field, which may be private.static DateParses a date string on the form "yyyy-MM-dd" and returns the correspondingjava.util.Dateobject.static DateparseDateTime(String dateTime) Parses a date and time string on the form "yyyy-MM-dd HH:mm:ss" and returns the corresondingjava.util.Dateobject.static StringreadResource(String resourceName) Reads a string from a classpath resource, which is assumed to be UTF-8 encoded text.static ZonedDateTimeutcNow()Gives the current date and time in the UTC time zone.static voidwriteToFile(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.Dateobject.- Parameters:
date- the date string to parse, should be on the form "yyyy-MM-dd"- Returns:
- the
java.util.Datecorresponding todate - Throws:
IllegalArgumentException- ifdatecannot be parsed
-
parseDateTime
Parses a date and time string on the form "yyyy-MM-dd HH:mm:ss" and returns the corresondingjava.util.Dateobject.- Parameters:
dateTime- the date+time string to parse, should be on the form "yyyy-MM-dd HH:mm:ss"- Returns:
- the
java.util.Datecorresponding todateTime - Throws:
IllegalArgumentException- ifdateTimecannot be parsed
-
utcNow
Gives the current date and time in the UTC time zone.- Returns:
- the current UTC date and time
-
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, may benull- Throws:
IllegalArgumentException- ifobjorfieldNameisnullIllegalStateException- 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
fieldNamein the objectobj - Throws:
IllegalArgumentException- ifobjorfieldNameisnullIllegalStateException- if reflection failure
-