Class TestUtil

java.lang.Object
com.reallifedeveloper.tools.test.TestUtil

public final class TestUtil extends Object
Miscellaneous utility methods that are useful when testing.
Author:
RealLifeDeveloper
  • Field Details

  • Method Details

    • findFreePort

      public static int findFreePort() throws IOException
      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

      public static Date parseDate(String date)
      Parses a date string on the form "yyyy-MM-dd" and returns the corresponding java.util.Date object.
      Parameters:
      date - the date string to parse, should be on the form "yyyy-MM-dd"
      Returns:
      the java.util.Date corresponding to date
      Throws:
      IllegalArgumentException - if date cannot be parsed
    • parseDateTime

      public static Date parseDateTime(String dateTime)
      Parses a date and time string on the form "yyyy-MM-dd HH:mm:ss" and returns the corresonding java.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 to dateTime
      Throws:
      IllegalArgumentException - if dateTime cannot be parsed
    • utcNow

      public static ZonedDateTime utcNow()
      Gives the current date and time in the UTC time zone.
      Returns:
      the current UTC date and time
    • writeToFile

      public static void writeToFile(String s, String filename, Charset charset) throws IOException
      Writes a string to a file using the given character encoding.
      Parameters:
      s - the string to write
      filename - the name of the file to write to
      charset - the character set to use, e.g., java.nio.charset.StandardCharsets.UTF_8
      Throws:
      IOException - if writing to the file failed
    • readResource

      public static String readResource(String resourceName) throws IOException
      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

      public static void injectField(Object obj, String fieldName, @Nullable Object value)
      Injects a value into an object's field, which may be private.
      Parameters:
      obj - the object in which to inject the value
      fieldName - the name of the field
      value - the value to inject, may be null
      Throws:
      IllegalArgumentException - if obj or fieldName is null
      IllegalStateException - if reflecction failure
    • getFieldValue

      public static @Nullable Object getFieldValue(Object obj, String fieldName)
      Gives the value of an object's field, which may be private.

      This also works with nested fields, so if fieldName is "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 is null, the method returns null instead of throwing an exception. So in the example above, if temp1.b is null, the method would return null after the second step.
      Parameters:
      obj - the object containing the field
      fieldName - the name of the field, may be nested, e.g., "field1.nestedField"
      Returns:
      the value of the field fieldName in the object obj
      Throws:
      IllegalArgumentException - if obj or fieldName is null
      IllegalStateException - if reflection failure
    • castToNonNull

      public static <T> T castToNonNull(@Nullable T x)
      Checks that a value that is declared as @Nullable actually is non-null.
      Type Parameters:
      T - the type of x
      Parameters:
      x - the value to check
      Returns:
      x if it is non-null
      Throws:
      IllegalStateException - if x is null
      See Also:
    • asList

      public static <T> @Nullable List<T> asList(@Nullable T[] a)
      A null-safe version of java.util.Arrays.asList that 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 null if a is null