Package generic.test

Class TestUtils


  • public class TestUtils
    extends java.lang.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 Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object[] args​(java.lang.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.
      static java.lang.Class<?>[] argTypes​(java.lang.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.
      static java.lang.String createStackTraceForAllThreads()
      Returns a string which is a printout of a stack trace for each thread running in the current JVM
      static java.util.List<java.lang.Object> getAllInstanceFields​(java.lang.Object ownerInstance)
      Gets all fields of the given object.
      static java.lang.Object getInstanceField​(java.lang.String fieldName, java.lang.Object ownerInstance)
      Gets the instance field by the given name on the given object instance.
      static <T> T getInstanceFieldByClassType​(java.lang.Class<T> classType, java.lang.Object ownerInstance)
      Get the first field object contained within object ownerInstance which has the type classType.
      static java.lang.Object invokeConstructor​(java.lang.Class<?> containingClass, java.lang.Class<?>[] parameterTypes, java.lang.Object[] args)
      Uses reflection to execute the constructor for the given class with the given parameters.
      static java.lang.Object invokeInstanceMethod​(java.lang.String methodName, java.lang.Object ownerInstance)
      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.
      static java.lang.Object invokeInstanceMethod​(java.lang.String methodName, java.lang.Object ownerInstance, java.lang.Class<?>[] parameterTypes, java.lang.Object[] args)
      Uses reflection to execute the method denoted by the given method name.
      static java.lang.Object invokeInstanceMethod​(java.lang.String methodName, java.lang.Object ownerInstance, java.lang.Class<?> parameterType, java.lang.Object arg)
      Uses reflection to execute the method denoted by the given method name.
      static java.lang.Object invokeInstanceMethod​(java.lang.String methodName, java.lang.Object ownerInstance, java.lang.Object... args)
      Uses reflection to execute the method denoted by the given method name.
      static java.lang.Object invokeInstanceMethod​(java.lang.String methodName, java.lang.Object ownerInstance, java.util.List<java.lang.Class<?>> parameterTypes, java.util.List<java.lang.Object> args)
      Uses reflection to execute the method denoted by the given method name.
      static java.lang.reflect.Field locateFieldByTypeOnClass​(java.lang.Class<?> classType, java.lang.Class<?> containingClass)
      Get the first field specification contained within containingClass which has the type classType.
      static void setInstanceField​(java.lang.String fieldName, java.lang.Object ownerInstance, java.lang.Object value)
      Sets the instance field by the given name on the given object instance.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • createStackTraceForAllThreads

        public static java.lang.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​(java.lang.String fieldName,
                                            java.lang.Object ownerInstance,
                                            java.lang.Object value)
                                     throws java.lang.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:
        java.lang.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:
        Field.set(Object, Object)
      • getInstanceField

        public static java.lang.Object getInstanceField​(java.lang.String fieldName,
                                                        java.lang.Object ownerInstance)
                                                 throws java.lang.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:
        java.lang.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:
        Field.get(java.lang.Object)
      • getAllInstanceFields

        public static java.util.List<java.lang.Object> getAllInstanceFields​(java.lang.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 java.lang.Object invokeInstanceMethod​(java.lang.String methodName,
                                                            java.lang.Object ownerInstance,
                                                            java.lang.Class<?>[] parameterTypes,
                                                            java.lang.Object[] args)
                                                     throws java.lang.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:
        java.lang.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:
        Method.invoke(java.lang.Object, java.lang.Object[])
      • invokeInstanceMethod

        public static java.lang.Object invokeInstanceMethod​(java.lang.String methodName,
                                                            java.lang.Object ownerInstance,
                                                            java.util.List<java.lang.Class<?>> parameterTypes,
                                                            java.util.List<java.lang.Object> args)
                                                     throws java.lang.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:
        java.lang.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 java.lang.Object invokeInstanceMethod​(java.lang.String methodName,
                                                            java.lang.Object ownerInstance,
                                                            java.lang.Class<?> parameterType,
                                                            java.lang.Object arg)
                                                     throws java.lang.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:
        java.lang.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 java.lang.Object invokeInstanceMethod​(java.lang.String methodName,
                                                            java.lang.Object ownerInstance,
                                                            java.lang.Object... args)
                                                     throws java.lang.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:
        java.lang.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 java.lang.Object invokeInstanceMethod​(java.lang.String methodName,
                                                            java.lang.Object ownerInstance)
                                                     throws java.lang.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:
        java.lang.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:
        Method.invoke(java.lang.Object, java.lang.Object[]), invokeInstanceMethod(String, Object, Class[], Object[])
      • invokeConstructor

        public static java.lang.Object invokeConstructor​(java.lang.Class<?> containingClass,
                                                         java.lang.Class<?>[] parameterTypes,
                                                         java.lang.Object[] args)
                                                  throws java.lang.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:
        java.lang.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 java.lang.Class<?>[] argTypes​(java.lang.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 java.lang.Object[] args​(java.lang.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​(java.lang.Class<T> classType,
                                                        java.lang.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 java.lang.reflect.Field locateFieldByTypeOnClass​(java.lang.Class<?> classType,
                                                                       java.lang.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