Class Invoker

  • All Implemented Interfaces:
    IInvoker

    public class Invoker
    extends java.lang.Object
    implements IInvoker
    This class is responsible for invoking methods: - test methods - configuration methods - possibly in a separate thread and then for notifying the result listeners.
    • Field Detail

      • m_skipFailedInvocationCounts

        private final boolean m_skipFailedInvocationCounts
      • m_invokedMethodListeners

        private final java.util.Collection<IInvokedMethodListener> m_invokedMethodListeners
      • m_continueOnFailedConfiguration

        private final boolean m_continueOnFailedConfiguration
      • m_classListeners

        private final java.util.List<IClassListener> m_classListeners
      • m_dataproviderListeners

        private final java.util.Collection<IDataProviderListener> m_dataproviderListeners
      • m_beforegroupsFailures

        private final java.util.Map<java.lang.String,​java.lang.Boolean> m_beforegroupsFailures
        Group failures must be synced as the Invoker is accessed concurrently
      • m_classInvocationResults

        private final java.util.Map<java.lang.Class<?>,​java.util.Set<java.lang.Object>> m_classInvocationResults
        Class failures must be synced as the Invoker is accessed concurrently
      • m_methodInvocationResults

        private final java.util.Map<ITestNGMethod,​java.util.Set<java.lang.Object>> m_methodInvocationResults
        Test methods whose configuration methods have failed.
    • Method Detail

      • setClassInvocationFailure

        private void setClassInvocationFailure​(java.lang.Class<?> clazz,
                                               java.lang.Object instance)
      • setMethodInvocationFailure

        private void setMethodInvocationFailure​(ITestNGMethod method,
                                                java.lang.Object instance)
      • invokeConfigurations

        public void invokeConfigurations​(IClass testClass,
                                         ITestNGMethod[] allMethods,
                                         XmlSuite suite,
                                         java.util.Map<java.lang.String,​java.lang.String> params,
                                         java.lang.Object[] parameterValues,
                                         java.lang.Object instance)
        Invoke configuration methods if they belong to the same TestClass passed in parameter..

        TODO: Calculate ahead of time which methods should be invoked for each class. Might speed things up for users who invoke the same test class with different parameters in the same suite run. If instance is non-null, the configuration will be run on it. If it is null, the configuration methods will be run on all the instances retrieved from the ITestClass.

        Specified by:
        invokeConfigurations in interface IInvoker
        Parameters:
        testClass - the class whose configuration methods must be run
      • invokeConfigurations

        private void invokeConfigurations​(IClass testClass,
                                          ITestNGMethod currentTestMethod,
                                          ITestNGMethod[] allMethods,
                                          XmlSuite suite,
                                          java.util.Map<java.lang.String,​java.lang.String> params,
                                          java.lang.Object[] parameterValues,
                                          java.lang.Object instance,
                                          ITestResult testMethodResult)
      • computeInstance

        private static java.lang.Object computeInstance​(java.lang.Object instance,
                                                        java.lang.Object inst,
                                                        ITestNGMethod tm)
      • recordConfigurationInvocationFailed

        private void recordConfigurationInvocationFailed​(ITestNGMethod tm,
                                                         IClass testClass,
                                                         IConfigurationAnnotation annotation,
                                                         ITestNGMethod currentTestMethod,
                                                         java.lang.Object instance,
                                                         XmlSuite suite)
        Record internally the failure of a Configuration, so that we can determine later if @Test should be skipped.
      • classConfigurationFailed

        private boolean classConfigurationFailed​(java.lang.Class<?> cls,
                                                 java.lang.Object instance)
        Returns:
        true if this class or a parent class failed to initialize.
      • confInvocationPassed

        private boolean confInvocationPassed​(ITestNGMethod method,
                                             ITestNGMethod currentTestMethod,
                                             IClass testClass,
                                             java.lang.Object instance)
        Returns:
        true if this class has successfully run all its @Configuration method or false if at least one of these methods failed.
      • hasConfigFailure

        private boolean hasConfigFailure​(ITestNGMethod currentTestMethod)
      • getInvocationResults

        private java.util.Set<java.lang.Object> getInvocationResults​(IClass testClass)
      • invokeConfigurationMethod

        private void invokeConfigurationMethod​(java.lang.Object targetInstance,
                                               ITestNGMethod tm,
                                               java.lang.Object[] params,
                                               ITestResult testResult)
                                        throws java.lang.reflect.InvocationTargetException,
                                               java.lang.IllegalAccessException
        Effectively invokes a configuration method on all passed in instances.
        Throws:
        java.lang.reflect.InvocationTargetException
        java.lang.IllegalAccessException
      • throwConfigurationFailure

        private void throwConfigurationFailure​(ITestResult testResult,
                                               java.lang.Throwable ex)
      • noListenersPresent

        private boolean noListenersPresent()
      • setTestStatus

        private static void setTestStatus​(ITestResult result,
                                          int status)
      • invokeTestMethod

        ITestResult invokeTestMethod​(java.lang.Object instance,
                                     ITestNGMethod tm,
                                     java.lang.Object[] parameterValues,
                                     int parametersIndex,
                                     XmlSuite suite,
                                     java.util.Map<java.lang.String,​java.lang.String> params,
                                     ITestClass testClass,
                                     ITestNGMethod[] beforeMethods,
                                     ITestNGMethod[] afterMethods,
                                     ConfigurationGroupMethods groupMethods,
                                     Invoker.FailureContext failureContext)
        invokeTestMethods() eventually converge here to invoke a single @Test method.

        This method is responsible for actually invoking the method. It decides if the invocation must be done:

        • through an IHookable
        • directly (through reflection)
        • in a separate thread (in case it needs to timeout)

        This method is also responsible for invoking @BeforeGroup, @BeforeMethod, @AfterMethod, @AfterGroup if it is the case for the passed in @Test method.

      • invokeBeforeGroupsConfigurations

        private void invokeBeforeGroupsConfigurations​(ITestNGMethod currentTestMethod,
                                                      ConfigurationGroupMethods groupMethods,
                                                      XmlSuite suite,
                                                      java.util.Map<java.lang.String,​java.lang.String> params,
                                                      java.lang.Object instance)
        Filter all the beforeGroups methods and invoke only those that apply to the current test method
      • invokeAfterGroupsConfigurations

        private void invokeAfterGroupsConfigurations​(ITestNGMethod currentTestMethod,
                                                     ConfigurationGroupMethods groupMethods,
                                                     XmlSuite suite,
                                                     java.util.Map<java.lang.String,​java.lang.String> params,
                                                     java.lang.Object instance)
      • registerSkippedTestResult

        private ITestResult registerSkippedTestResult​(ITestNGMethod testMethod,
                                                      java.lang.Object instance,
                                                      long start,
                                                      java.lang.Throwable throwable)
      • computeTestStatusComparingTestResultAndStatusHolder

        private static int computeTestStatusComparingTestResultAndStatusHolder​(ITestResult testResult,
                                                                               Invoker.StatusHolder holder,
                                                                               boolean wasResultUnaltered)
      • isSkipExceptionAndSkip

        private boolean isSkipExceptionAndSkip​(java.lang.Throwable ite)
      • runWorkers

        private java.util.List<ITestResult> runWorkers​(ITestNGMethod testMethod,
                                                       java.util.List<IWorker<ITestNGMethod>> workers,
                                                       int threadPoolSize,
                                                       ConfigurationGroupMethods groupMethods,
                                                       XmlSuite suite,
                                                       java.util.Map<java.lang.String,​java.lang.String> parameters)
        To reduce thread contention and also to correctly handle thread-confinement this method invokes the @BeforeGroups and @AfterGroups corresponding to the current @Test method.
      • checkDependencies

        private java.lang.String checkDependencies​(ITestNGMethod testMethod,
                                                   ITestNGMethod[] allTestMethods)
        Checks to see of the test method has certain dependencies that prevents TestNG from executing it
        Parameters:
        testMethod - test method being checked for
        Returns:
        error message or null if dependencies have been run successfully
      • keepSameInstances

        private java.util.Set<ITestResult> keepSameInstances​(ITestNGMethod method,
                                                             java.util.Set<ITestResult> results)
        Returns:
        the test results that apply to one of the instances of the testMethod.
      • haveBeenRunSuccessfully

        private boolean haveBeenRunSuccessfully​(ITestNGMethod testMethod,
                                                ITestNGMethod[] methods)
        Returns:
        true if all the methods have been run successfully
      • handleException

        private void handleException​(java.lang.Throwable throwable,
                                     ITestNGMethod testMethod,
                                     ITestResult testResult,
                                     int failureCount)
        An exception was thrown by the test, determine if this method should be marked as a failure or as failure_but_within_successPercentage
      • runConfigurationListeners

        private void runConfigurationListeners​(ITestResult tr,
                                               boolean before)
      • runTestListeners

        void runTestListeners​(ITestResult tr)
      • log

        private void log​(int level,
                         java.lang.String s)