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 <T> @Nullable List<T> asList(@Nullable T[] a) A null-safe version ofjava.util.Arrays.asListthat converts an array to a list, or returns null if the array is null.static <T> TcastToNonNull(@Nullable T x) Checks that a value that is declared as@Nullableactually is non-null.static 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.This also works with nested fields, so if
fieldNameis"a.b.c", the method does something like the following:Object temp1 = obj.a; Object temp2 = temp1.b; Object result = temp2.c; return result;
For nested fields, the method is "forgiving", in that if some intermediate field isnull, the method returnsnullinstead of throwing an exception. So in the example above, iftemp1.bisnull, the method would returnnullafter the second step.- Parameters:
obj- the object containing the fieldfieldName- the name of the field, may be nested, e.g.,"field1.nestedField"- Returns:
- the value of the field
fieldNamein the objectobj - Throws:
IllegalArgumentException- ifobjorfieldNameisnullIllegalStateException- if reflection failure
-
castToNonNull
public static <T> T castToNonNull(@Nullable T x) Checks that a value that is declared as@Nullableactually is non-null.- Type Parameters:
T- the type ofx- Parameters:
x- the value to check- Returns:
xif it is non-null- Throws:
IllegalStateException- ifxisnull- See Also:
-
asList
A null-safe version ofjava.util.Arrays.asListthat converts an array to a list, or returns null if the array is null.- Type Parameters:
T- the type of the array- Parameters:
a- the array- Returns:
- the array as a list, or
nullifaisnull
-