Package generic.test

Class TestUtils

java.lang.Object
generic.test.TestUtils

public class TestUtils extends Object
Actually, not. At least not soon...all the *TestCase classes now can be split apart into static-style utility methods, and instance-type test harness/scaffold methods, but they will need to live at their respective layer, not all here in Base. Future home of utility methods (many methods of TestCase can be put here).

A primary motivating factor for creating this class is to gain access to some of the myriad functionality in TestCase without loading its static data.

  • Method Details

    • createStackTraceForAllThreads

      public static String createStackTraceForAllThreads()
      Returns a string which is a printout of a stack trace for each thread running in the current JVM
      Returns:
      the stack trace string
    • setInstanceField

      public static void setInstanceField(String fieldName, Object ownerInstance, Object value) throws RuntimeException
      Sets the instance field by the given name on the given object instance.

      Note: if the field is static, then the ownerInstance field can be the class of the object that contains the variable.

      Parameters:
      fieldName - The name of the field to retrieve.
      ownerInstance - The object instance from which to get the variable instance.
      value - The value to use when setting the given field
      Throws:
      RuntimeException - if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
      See Also:
    • getInstanceField

      public static Object getInstanceField(String fieldName, Object ownerInstance) throws RuntimeException
      Gets the instance field by the given name on the given object instance. The value is a primitive wrapper if it is a primitive type.

      Note: if the field is static, then the ownerInstance field can be the class of the object that contains the variable.

      Parameters:
      fieldName - The name of the field to retrieve.
      ownerInstance - The object instance from which to get the variable instance.
      Returns:
      The field instance.
      Throws:
      RuntimeException - if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
      Since:
      Tracker Id 267
      See Also:
    • getAllInstanceFields

      public static List<Object> getAllInstanceFields(Object ownerInstance)
      Gets all fields of the given object. Only objects on the immediate instance are returned.
      Parameters:
      ownerInstance - the object from which to get fields
      Returns:
      the fields
    • invokeInstanceMethod

      public static Object invokeInstanceMethod(String methodName, Object ownerInstance, Class<?>[] parameterTypes, Object[] args) throws RuntimeException
      Uses reflection to execute the method denoted by the given method name. If any value is returned from the method execution, then it will be returned from this method. Otherwise, null is returned.

      Note: if the method is static, then the ownerInstance field can be the class of the object that contains the method.

      Parameters:
      methodName - The name of the method to execute.
      ownerInstance - The object instance of which the method will be executed.
      parameterTypes - The parameter types that the method takes.
      args - The parameter values that should be passed to the method. This value can be null or zero length if there are no parameters to pass
      Returns:
      The return value as returned from executing the method.
      Throws:
      RuntimeException - if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
      Since:
      Tracker Id 267
      See Also:
    • invokeInstanceMethod

      public static Object invokeInstanceMethod(String methodName, Object ownerInstance, List<Class<?>> parameterTypes, List<Object> args) throws RuntimeException
      Uses reflection to execute the method denoted by the given method name. If any value is returned from the method execution, then it will be returned from this method. Otherwise, null is returned.

      Note: if the method is static, then the ownerInstance field can be the class of the object that contains the method.

      This method is just a convenience for calling invokeInstanceMethod(String, Object, Class[], Object[]). As the following example shows, this method's uses is a bit cleaner:

              // The call below is equivalent to calling:  System.out.println("Hi")
              invokeInstanceMethod("println", System.out, Arrays.asList(String.class), Arrays.asList("Hi"));
              
       
      Parameters:
      methodName - The name of the method to execute.
      ownerInstance - The object instance of which the method will be executed.
      parameterTypes - The parameter types that the method takes.
      args - The parameter values that should be passed to the method. This value can be null or zero length if there are no parameters to pass
      Returns:
      The return value as returned from executing the method.
      Throws:
      RuntimeException - if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
    • invokeInstanceMethod

      public static Object invokeInstanceMethod(String methodName, Object ownerInstance, Class<?> parameterType, Object arg) throws RuntimeException
      Uses reflection to execute the method denoted by the given method name. If any value is returned from the method execution, then it will be returned from this method. Otherwise, null is returned.

      Note: if the method is static, then the ownerInstance field can be the class of the object that contains the method.

      If the method you are calling takes no parameters, then call invokeInstanceMethod(String, Object) instead.

      This method is just a convenience for calling invokeInstanceMethod(String, Object, Class[], Object[]) when the method only takes a single parameter, so that you don't have the ugliness of creating arrays as the parameters for this method.

      As an example:

              // The call below is equivalent to calling:  System.out.println("Hi")
              invokeInstanceMethod("println", System.out, String.class, "Hi");
              
       
      Parameters:
      methodName - The name of the method to execute.
      ownerInstance - The object instance of which the method will be executed.
      parameterType - The parameter types that the method takes.
      arg - The parameter value that should be passed to the method.
      Returns:
      The return value as returned from executing the method.
      Throws:
      RuntimeException - if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
    • invokeInstanceMethod

      public static Object invokeInstanceMethod(String methodName, Object ownerInstance, Object... args) throws RuntimeException
      Uses reflection to execute the method denoted by the given method name. If any value is returned from the method execution, then it will be returned from this method. Otherwise, null is returned.

      Note: if the method is static, then the ownerInstance field can be the class of the object that contains the method.

      Warning: The exact class of each arg will be used as the class type of the parameter for the method being called. If the method you are calling takes parameters that do not match exactly the class of the args you wish to use, then call invokeInstanceMethod(String, Object, List, List) instead so that you can specify the parameter types explicitly.

      If the method you are calling takes no parameters, then call invokeInstanceMethod(String, Object) instead.

      This method is just a convenience for calling invokeInstanceMethod(String, Object, Class[], Object[]) when the method only takes a single parameter, so that you don't have the ugliness of creating arrays as the parameters for this method.

      As an example:

              // The call below is equivalent to calling:  System.out.println("Hi")
              invokeInstanceMethod("println", System.out, "Hi");
       
              // This call is equivalent to the one above
              invokeInstanceMethod("println", System.out, Arrays.asList(String.class), Arrays.asList("Hi"));
              
              
       
      Parameters:
      methodName - The name of the method to execute.
      ownerInstance - The object instance of which the method will be executed.
      args - The parameter value that should be passed to the method.
      Returns:
      The return value as returned from executing the method.
      Throws:
      RuntimeException - if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
    • invokeInstanceMethod

      public static Object invokeInstanceMethod(String methodName, Object ownerInstance) throws RuntimeException
      This method is just a "pass through" method for invokeInstanceMethod(String, Object, Class[], Object[]) so that callers do not need to pass null to that method when the underlying instance method does not have any parameters.
      Parameters:
      methodName - The name of the method to execute.
      ownerInstance - The object instance of which the method will be executed.
      Returns:
      The return value as returned from executing the method.
      Throws:
      RuntimeException - if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
      See Also:
    • invokeConstructor

      public static Object invokeConstructor(Class<?> containingClass, Class<?>[] parameterTypes, Object[] args) throws RuntimeException
      Uses reflection to execute the constructor for the given class with the given parameters. The new instance of the given class will be returned.

      Parameters:
      containingClass - The class that contains the desired constructor.
      parameterTypes - The parameter types that the constructor takes. This value can be null or zero length if there are no parameters to pass
      args - The parameter values that should be passed to the constructor. This value can be null or zero length if there are no parameters to pass
      Returns:
      The new class instance
      Throws:
      RuntimeException - if there is a problem accessing the constructor using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
    • argTypes

      public static Class<?>[] argTypes(Class<?>... classes)
      A convenience method that can be statically imported to use with the class, allowing you to avoid your own ugly manual array creation.
      Parameters:
      classes - the classes
      Returns:
      the classes array
    • args

      public static Object[] args(Object... objects)
      A convenience method that can be statically imported to use with the class, allowing you to avoid your own ugly manual array creation.
      Parameters:
      objects - the objects
      Returns:
      the objects array
    • getInstanceFieldByClassType

      public static <T> T getInstanceFieldByClassType(Class<T> classType, Object ownerInstance)
      Get the first field object contained within object ownerInstance which has the type classType. This method is only really useful if it is known that only a single field of classType exists within the ownerInstance.
      Type Parameters:
      T - the type
      Parameters:
      classType - the class type of the desired field
      ownerInstance - the object instance that owns the field
      Returns:
      field object of type classType or null
    • locateFieldByTypeOnClass

      public static Field locateFieldByTypeOnClass(Class<?> classType, Class<?> containingClass)
      Get the first field specification contained within containingClass which has the type classType. This method is only really useful if it is known that only a single field of classType exists within the containingClass hierarchy.
      Parameters:
      classType - the class
      containingClass - the class that contains a field of the given type
      Returns:
      field which corresponds to type classType or null