<commit_before>package org.testng.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.testng.ClassMethodMap;
import org.testng.ITestClass;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestRunner;
import org.testng.internal.thread.ThreadUtil;
import org.testng.xml.XmlSuite;

/**
 * This class implements Runnable and will invoke the ITestMethod passed in its
 * constructor on its run() method.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class TestMethodWorker implements Runnable {
  private ITestNGMethod[] m_testMethods;
  private IInvoker m_invoker = null;
  private Map<String, String> m_parameters = null;
  private XmlSuite m_suite = null;
  private Map<ITestClass, ITestClass> m_invokedBeforeClassMethods = null;
  private Map<ITestClass, ITestClass> m_invokedAfterClassMethods = null;
  private ITestNGMethod[] m_allTestMethods;
  private List<ITestResult> m_testResults = new ArrayList<ITestResult>();
  private ConfigurationGroupMethods m_groupMethods = null;
  private ClassMethodMap m_classMethodMap = null;
  
  public TestMethodWorker(IInvoker invoker, 
                          ITestNGMethod[] testMethods,
                          XmlSuite suite,
                          Map<String, String> parameters,
                          Map<ITestClass, ITestClass> invokedBeforeClassMethods,
                          Map<ITestClass, ITestClass> invokedAfterClassMethods,
                          ITestNGMethod[] allTestMethods,
                          ConfigurationGroupMethods groupMethods,
                          ClassMethodMap classMethodMap)
  {
    m_invoker = invoker;
    m_testMethods = testMethods;
    m_suite = suite;
    m_parameters = parameters;
    m_invokedBeforeClassMethods = invokedBeforeClassMethods;
    m_invokedAfterClassMethods = invokedAfterClassMethods;
    m_allTestMethods = allTestMethods;
    m_groupMethods = groupMethods;
    m_classMethodMap = classMethodMap;
  }
  
  /**
   * Retrieves the maximum specified timeout of all ITestNGMethods to
   * be run.
   * 
   * @return the max timeout or 0 if no timeout was specified
   */
  public long getMaxTimeOut() {
    long result = 0;
    for (ITestNGMethod tm : m_testMethods) {
      if (tm.getTimeOut() > result) {
        result = tm.getTimeOut();
      }
    }
    
    return result;
  }
  
  @Override
  public String toString() {
    return "[Worker on thread:" + Thread.currentThread().getId() + " " + m_testMethods[0] + "]";
  }
  
  /**
   * Run all the ITestNGMethods passed in through the constructor.
   * 
   * @see java.lang.Runnable#run()
   */
  public void run() {
    // Using an index here because we need to tell the invoker
    // the index of the current method
    for (int indexMethod = 0; indexMethod < m_testMethods.length; indexMethod++) {
      ITestNGMethod tm = m_testMethods[indexMethod];
  
      //
      // Invoke the before class methods if not done already
      //
      ITestClass testClass = tm.getTestClass();
      
      boolean invokeBefore= false;
      synchronized(m_invokedBeforeClassMethods) {
        if (! m_invokedBeforeClassMethods.containsKey(testClass)) {  
          m_invokedBeforeClassMethods.put(testClass, testClass);
          invokeBefore= true;
        }
      }

      if(invokeBefore) {
        m_invoker.invokeConfigurations(testClass,
            testClass.getBeforeClassMethods(),
            m_suite,
            m_parameters,
            null /* instance */);
      }

      //
      // Invoke test method
      //
      
      // Potential bug here:  we look up the method index of tm among all
      // the test methods (not very efficient) but if this method appears
      // several times and these methods are run in parallel, the results
      // are unpredictable...  Need to think about this more (and make it
      // more efficient)
      List<ITestResult> testResults = m_invoker.invokeTestMethods(tm, 
          m_suite, 
          m_parameters, 
          m_allTestMethods, 
          indexOf(tm, m_allTestMethods), 
          m_groupMethods);
      
      if (testResults != null) {
        m_testResults.addAll(testResults);        
      }
      
      //
      // Invoke after class methods if this test method is the last one
      // on this class
      //<commit_msg>fixed a possible synch problem with the invocation of @BeforeClass<commit_after>package org.testng.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.testng.ClassMethodMap;
import org.testng.ITestClass;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestRunner;
import org.testng.internal.thread.ThreadUtil;
import org.testng.xml.XmlSuite;

/**
 * This class implements Runnable and will invoke the ITestMethod passed in its
 * constructor on its run() method.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class TestMethodWorker implements Runnable {
  private ITestNGMethod[] m_testMethods;
  private IInvoker m_invoker = null;
  private Map<String, String> m_parameters = null;
  private XmlSuite m_suite = null;
  private Map<ITestClass, ITestClass> m_invokedBeforeClassMethods = null;
  private Map<ITestClass, ITestClass> m_invokedAfterClassMethods = null;
  private ITestNGMethod[] m_allTestMethods;
  private List<ITestResult> m_testResults = new ArrayList<ITestResult>();
  private ConfigurationGroupMethods m_groupMethods = null;
  private ClassMethodMap m_classMethodMap = null;
  
  public TestMethodWorker(IInvoker invoker, 
                          ITestNGMethod[] testMethods,
                          XmlSuite suite,
                          Map<String, String> parameters,
                          Map<ITestClass, ITestClass> invokedBeforeClassMethods,
                          Map<ITestClass, ITestClass> invokedAfterClassMethods,
                          ITestNGMethod[] allTestMethods,
                          ConfigurationGroupMethods groupMethods,
                          ClassMethodMap classMethodMap)
  {
    m_invoker = invoker;
    m_testMethods = testMethods;
    m_suite = suite;
    m_parameters = parameters;
    m_invokedBeforeClassMethods = invokedBeforeClassMethods;
    m_invokedAfterClassMethods = invokedAfterClassMethods;
    m_allTestMethods = allTestMethods;
    m_groupMethods = groupMethods;
    m_classMethodMap = classMethodMap;
  }
  
  /**
   * Retrieves the maximum specified timeout of all ITestNGMethods to
   * be run.
   * 
   * @return the max timeout or 0 if no timeout was specified
   */
  public long getMaxTimeOut() {
    long result = 0;
    for (ITestNGMethod tm : m_testMethods) {
      if (tm.getTimeOut() > result) {
        result = tm.getTimeOut();
      }
    }
    
    return result;
  }
  
  @Override
  public String toString() {
    return "[Worker on thread:" + Thread.currentThread().getId() + " " + m_testMethods[0] + "]";
  }
  
  /**
   * Run all the ITestNGMethods passed in through the constructor.
   * 
   * @see java.lang.Runnable#run()
   */
  public void run() {
    // Using an index here because we need to tell the invoker
    // the index of the current method
    for (int indexMethod = 0; indexMethod < m_testMethods.length; indexMethod++) {
      ITestNGMethod tm = m_testMethods[indexMethod];
  
      //
      // Invoke the before class methods if not done already
      //
      ITestClass testClass = tm.getTestClass();

      // the whole invocation must be synchronized as other threads must
      // get a full initialized test object (not the same for @After)
      synchronized(m_invokedBeforeClassMethods) {
        if (! m_invokedBeforeClassMethods.containsKey(testClass)) {  
          m_invokedBeforeClassMethods.put(testClass, testClass);
          m_invoker.invokeConfigurations(testClass,
              testClass.getBeforeClassMethods(),
              m_suite,
              m_parameters,
              null /* instance */);
        }
      }

      //
      // Invoke test method
      //
      
      // Potential bug here:  we look up the method index of tm among all
      // the test methods (not very efficient) but if this method appears
      // several times and these methods are run in parallel, the results
      // are unpredictable...  Need to think about this more (and make it
      // more efficient)
      List<ITestResult> testResults = m_invoker.invokeTestMethods(tm, 
          m_suite, 
          m_parameters, 
          m_allTestMethods, 
          indexOf(tm, m_allTestMethods), 
          m_groupMethods);
      
      if (testResults != null) {
        m_testResults.addAll(testResults);        
      }
      
      //
      // Invoke after class methods if this test method is the last one
      // on this class
      //
{'fraction_non_alphanumeric': 0.08028121102165778, 'fraction_numerical': 0.0009071323279283366, 'mean_word_length': 2.505564387917329, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1210628', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2460, 'n_words': 678}