<commit_before>package org.testng.reporters;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.internal.MethodHelper;
import org.testng.internal.Utils;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

/**
 * This reporter is responsible for creating testng-failed.xml
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'><NAME></a>
 */
public class FailedReporter extends TestListenerAdapter implements IReporter {
  public static final String TESTNG_FAILED_XML = "testng-failed.xml";
  
  private XmlSuite m_xmlSuite;
  
  public FailedReporter() {
  }

  public FailedReporter(XmlSuite xmlSuite) {
    m_xmlSuite = xmlSuite;
  }

  public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
    for(int i= 0; i < xmlSuites.size(); i++) {
      generateFailureSuite(xmlSuites.get(i), suites.get(i), outputDirectory);
    }
  }

  protected void generateFailureSuite(XmlSuite xmlSuite, ISuite suite, String outputDir) {
    XmlSuite failedSuite = (XmlSuite) xmlSuite.clone();
    failedSuite.setName("Failed suite [" + xmlSuite.getName() + "]");
    m_xmlSuite= failedSuite;

    Map<String, ISuiteResult> results= suite.getResults();
    
    for(String name: results.keySet()) {
      ISuiteResult suiteResult= results.get(name);
      ITestContext testContext= suiteResult.getTestContext();
      generateXmlTest(testContext, testContext.getFailedTests().getAllResults(), testContext.getSkippedTests().getAllResults());
    }
   
    Utils.writeFile(outputDir, TESTNG_FAILED_XML, failedSuite.toXml());
    Utils.writeFile(suite.getOutputDirectory(), TESTNG_FAILED_XML, failedSuite.toXml());
  }

  /**
   * Do not rely on this method. The class is used as <code>IReporter</code>.
   * 
   * @see org.testng.TestListenerAdapter#onFinish(org.testng.ITestContext)
   * @deprecated this class is used now as IReporter
   */
  @Override
  public void onFinish(ITestContext context) {
    // Delete the previous file
//    File f = new File(context.getOutputDirectory(), getFileName(context));
//    f.delete();
    
    // Calculate the methods we need to rerun :  failed tests and
    // their dependents
//    List<ITestResult> failedTests = getFailedTests();
//    List<ITestResult> skippedTests = getSkippedTests();
  }
  
  private void generateXmlTest(ITestContext context, Collection<ITestResult> failedTests, Collection<ITestResult> skippedTests) {
    // Note:  we can have skipped tests and no failed tests
    // if a method depends on nonexistent groups
    if (skippedTests.size() > 0 || failedTests.size() > 0) {
      Map<ITestNGMethod, ITestNGMethod> methodsToReRun = new HashMap<ITestNGMethod, ITestNGMethod>();
      
      // Get the transitive closure of all the failed methods and the methods
      // they depend on
      Collection<ITestResult> tests = failedTests.isEmpty() ? skippedTests : failedTests;
      for (ITestResult failedTest : tests) {
        ITestNGMethod current = failedTest.getMethod();
        if (current.isTest()) {
          methodsToReRun.put(current, current);
          ITestNGMethod method = failedTest.getMethod();
          // Don't count configuration methods
          if (method.isTest()) {
            List<ITestNGMethod> methodsDependedUpon = MethodHelper.getMethodsDependedUpon(method, context.getAllTestMethods());
            
            for (ITestNGMethod m : methodsDependedUpon) {
              if (m.isTest()) {
                methodsToReRun.put(m, m);
              }
            }
          }
        }
      }
      
      //
      // Now we have all the right methods.  Go through the list of
      // all the methods that were run and only pick those that are
      // in the methodToReRun map.  Since the methods are already
      // sorted, we don't need to sort them again.
      //
      List<ITestNGMethod> result = new ArrayList<ITestNGMethod>();
      for (ITestNGMethod m : context.getAllTestMethods()) {
        if (null != methodsToReRun.get(m)) {
          result.add(m);
        }
      }
      
      createXmlTest(context, result);
    }
  }
  
  /**
   * Generate testng-failed.xml
   */
  private void createXmlTest(ITestContext context, List<ITestNGMethod> methods) {
    XmlTest xmlTest = new XmlTest(m_xmlSuite);
    xmlTest.setName(context.getName() + "(failed)");
    List<XmlClass> xmlClasses = createXmlClasses(methods);
    xmlTest.setXmlClasses(xmlClasses);
  }
    
  /**
   * @param methods The methods we want to represent
   * @return A list of XmlClass objects (each representing a <class> tag) based
   * on the parameter methods
   */<commit_msg>quick hack to fix TESTNG-40<commit_after>package org.testng.reporters;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.TestRunner;
import org.testng.internal.MethodHelper;
import org.testng.internal.Utils;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

/**
 * This reporter is responsible for creating testng-failed.xml
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'><NAME></a>
 */
public class FailedReporter extends TestListenerAdapter implements IReporter {
  public static final String TESTNG_FAILED_XML = "testng-failed.xml";
  
  private XmlSuite m_xmlSuite;
  
  public FailedReporter() {
  }

  public FailedReporter(XmlSuite xmlSuite) {
    m_xmlSuite = xmlSuite;
  }

  public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
    for(int i= 0; i < xmlSuites.size(); i++) {
      generateFailureSuite(xmlSuites.get(i), suites.get(i), outputDirectory);
    }
  }

  protected void generateFailureSuite(XmlSuite xmlSuite, ISuite suite, String outputDir) {
    XmlSuite failedSuite = (XmlSuite) xmlSuite.clone();
    failedSuite.setName("Failed suite [" + xmlSuite.getName() + "]");
    m_xmlSuite= failedSuite;

    Map<String, ISuiteResult> results= suite.getResults();
    
    for(String name: results.keySet()) {
      ISuiteResult suiteResult= results.get(name);
      ITestContext testContext= suiteResult.getTestContext();
      generateXmlTest(testContext, testContext.getFailedTests().getAllResults(), testContext.getSkippedTests().getAllResults());
    }
   
    Utils.writeFile(outputDir, TESTNG_FAILED_XML, failedSuite.toXml());
    Utils.writeFile(suite.getOutputDirectory(), TESTNG_FAILED_XML, failedSuite.toXml());
  }

  /**
   * Do not rely on this method. The class is used as <code>IReporter</code>.
   * 
   * @see org.testng.TestListenerAdapter#onFinish(org.testng.ITestContext)
   * @deprecated this class is used now as IReporter
   */
  @Override
  public void onFinish(ITestContext context) {
    // Delete the previous file
//    File f = new File(context.getOutputDirectory(), getFileName(context));
//    f.delete();
    
    // Calculate the methods we need to rerun :  failed tests and
    // their dependents
//    List<ITestResult> failedTests = getFailedTests();
//    List<ITestResult> skippedTests = getSkippedTests();
  }
  
  private void generateXmlTest(ITestContext context, Collection<ITestResult> failedTests, Collection<ITestResult> skippedTests) {
    // Note:  we can have skipped tests and no failed tests
    // if a method depends on nonexistent groups
    if (skippedTests.size() > 0 || failedTests.size() > 0) {
      Map<ITestNGMethod, ITestNGMethod> methodsToReRun = new HashMap<ITestNGMethod, ITestNGMethod>();
      
      // Get the transitive closure of all the failed methods and the methods
      // they depend on
      Collection<ITestResult> tests = failedTests.isEmpty() ? skippedTests : failedTests;
      for (ITestResult failedTest : tests) {
        ITestNGMethod current = failedTest.getMethod();
        if (current.isTest()) {
          methodsToReRun.put(current, current);
          ITestNGMethod method = failedTest.getMethod();
          // Don't count configuration methods
          if (method.isTest()) {
            List<ITestNGMethod> methodsDependedUpon = MethodHelper.getMethodsDependedUpon(method, context.getAllTestMethods());
            
            for (ITestNGMethod m : methodsDependedUpon) {
              if (m.isTest()) {
                methodsToReRun.put(m, m);
              }
            }
          }
        }
      }
      
      // retrieve also the @Before/After Suite/Test methods
      // HACK for TESTNG-40
      if(context instanceof TestRunner) {
        TestRunner tr= (TestRunner) context;
        addMethods(methodsToReRun, tr.getBeforeSuiteMethods()); 
        addMethods(methodsToReRun, tr.getBeforeTestConfigurationMethods());
        addMethods(methodsToReRun, tr.getAfterTestConfigurationMethods());
        addMethods(methodsToReRun, tr.getAfterSuiteMethods());
      }
      
      //
      // Now we have all the right methods.  Go through the list of
      // all the methods that were run and only pick those that are
      // in the methodToReRun map.  Since the methods are already
      // sorted, we don't need to sort them again.
      //
      List<ITestNGMethod> result = new ArrayList<ITestNGMethod>();
      for (ITestNGMethod m : context.getAllTestMethods()) {
        if (null != methodsToReRun.get(m)) {
          result.add(m);
        }
      }
      
      createXmlTest(context, result);
    }
  }
  
  private void addMethods(Map<ITestNGMethod, ITestNGMethod> map, ITestNGMethod[] methods) {
    if(null == methods) {
      return;
    }
    for(ITestNGMethod tm: methods) {
      map.put(tm, tm);
    }
  }
  
  /**
   * Generate testng-failed.xml
   */
  private void createXmlTest(ITestContext context, List<ITestNGMethod> methods) {
    XmlTest xmlTest = new XmlTest(m_xmlSuite);
    xmlTest.setName(context.getName() + "(failed)");
    List<XmlClass> xmlClasses = createXmlClasses(methods);
    xmlTest.setXmlClasses(xmlClasses);
  }
    
  /**
   * @param methods The methods we want to represent
   * @return A list of XmlClass objects (each representing a <class> tag) based
   * on the parameter methods
   */
{'fraction_non_alphanumeric': 0.10797034291010195, 'fraction_numerical': 0.0009267840593141798, 'mean_word_length': 3.2736633663366335, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 46}, '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': '2396214', 'n_tokens_mistral': 3266, 'n_tokens_neox': 3208, 'n_words': 785}