Home | History | Annotate | Download | only in testng
      1 package org.testng;
      2 
      3 import org.testng.collections.Lists;
      4 import org.testng.collections.Objects;
      5 import org.testng.internal.IResultListener2;
      6 
      7 import java.util.ArrayList;
      8 import java.util.Collections;
      9 import java.util.List;
     10 
     11 
     12 /**
     13  * A simple ITestListener adapter that stores all the tests
     14  * that were run.  You can retrieve these results with the
     15  * following methods:
     16  * getPassedTests()
     17  * getFailedTests()
     18  * getSkippedTests()
     19  *
     20  * If you extend this class in order to override any of these
     21  * methods, remember to call their super equivalent if you want
     22  * this list of tests to be maintained.
     23  *
     24  * @author Cedric Beust, Aug 6, 2004
     25  * @author <a href='mailto:the_mindstorm (at) evolva.ro'>Alexandru Popescu</a>
     26  */
     27 public class TestListenerAdapter implements IResultListener2 {
     28   private List<ITestNGMethod> m_allTestMethods =
     29       Collections.synchronizedList(Lists.<ITestNGMethod>newArrayList());
     30   private List<ITestResult> m_passedTests = Collections.synchronizedList(Lists.<ITestResult>newArrayList());
     31   private List<ITestResult> m_failedTests = Collections.synchronizedList(Lists.<ITestResult>newArrayList());
     32   private List<ITestResult> m_skippedTests = Collections.synchronizedList(Lists.<ITestResult>newArrayList());
     33   private List<ITestResult> m_failedButWSPerTests = Collections.synchronizedList(Lists.<ITestResult>newArrayList());
     34   private List<ITestContext> m_testContexts= Collections.synchronizedList(new ArrayList<ITestContext>());
     35   private List<ITestResult> m_failedConfs= Collections.synchronizedList(Lists.<ITestResult>newArrayList());
     36   private List<ITestResult> m_skippedConfs= Collections.synchronizedList(Lists.<ITestResult>newArrayList());
     37   private List<ITestResult> m_passedConfs= Collections.synchronizedList(Lists.<ITestResult>newArrayList());
     38 
     39   @Override
     40   public void onTestSuccess(ITestResult tr) {
     41     m_allTestMethods.add(tr.getMethod());
     42     m_passedTests.add(tr);
     43   }
     44 
     45   @Override
     46   public void onTestFailure(ITestResult tr) {
     47     m_allTestMethods.add(tr.getMethod());
     48     m_failedTests.add(tr);
     49   }
     50 
     51   @Override
     52   public void onTestSkipped(ITestResult tr) {
     53     m_allTestMethods.add(tr.getMethod());
     54     m_skippedTests.add(tr);
     55   }
     56 
     57   @Override
     58   public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
     59     m_allTestMethods.add(tr.getMethod());
     60     m_failedButWSPerTests.add(tr);
     61   }
     62 
     63   protected ITestNGMethod[] getAllTestMethods() {
     64     return m_allTestMethods.toArray(new ITestNGMethod[m_allTestMethods.size()]);
     65   }
     66 
     67   @Override
     68   public void onStart(ITestContext testContext) {
     69 	  m_testContexts.add(testContext);
     70   }
     71 
     72   @Override
     73   public void onFinish(ITestContext testContext) {
     74   }
     75 
     76   /**
     77    * @return Returns the failedButWithinSuccessPercentageTests.
     78    */
     79   public List<ITestResult> getFailedButWithinSuccessPercentageTests() {
     80     return m_failedButWSPerTests;
     81   }
     82   /**
     83    * @return Returns the failedTests.
     84    */
     85   public List<ITestResult> getFailedTests() {
     86     return m_failedTests;
     87   }
     88   /**
     89    * @return Returns the passedTests.
     90    */
     91   public List<ITestResult> getPassedTests() {
     92     return m_passedTests;
     93   }
     94   /**
     95    * @return Returns the skippedTests.
     96    */
     97   public List<ITestResult> getSkippedTests() {
     98     return m_skippedTests;
     99   }
    100 
    101   private static void ppp(String s) {
    102     System.out.println("[TestListenerAdapter] " + s);
    103   }
    104   /**
    105    * @param allTestMethods The allTestMethods to set.
    106    */
    107   public void setAllTestMethods(List<ITestNGMethod> allTestMethods) {
    108     m_allTestMethods = allTestMethods;
    109   }
    110   /**
    111    * @param failedButWithinSuccessPercentageTests The failedButWithinSuccessPercentageTests to set.
    112    */
    113   public void setFailedButWithinSuccessPercentageTests(
    114       List<ITestResult> failedButWithinSuccessPercentageTests) {
    115     m_failedButWSPerTests = failedButWithinSuccessPercentageTests;
    116   }
    117   /**
    118    * @param failedTests The failedTests to set.
    119    */
    120   public void setFailedTests(List<ITestResult> failedTests) {
    121     m_failedTests = failedTests;
    122   }
    123   /**
    124    * @param passedTests The passedTests to set.
    125    */
    126   public void setPassedTests(List<ITestResult> passedTests) {
    127     m_passedTests = passedTests;
    128   }
    129   /**
    130    * @param skippedTests The skippedTests to set.
    131    */
    132   public void setSkippedTests(List<ITestResult> skippedTests) {
    133     m_skippedTests = skippedTests;
    134   }
    135 
    136   @Override
    137   public void onTestStart(ITestResult result) {
    138   }
    139 
    140   public List<ITestContext> getTestContexts() {
    141     return m_testContexts;
    142   }
    143 
    144   public List<ITestResult> getConfigurationFailures() {
    145     return m_failedConfs;
    146   }
    147 
    148   /**
    149    * @see org.testng.IConfigurationListener#onConfigurationFailure(org.testng.ITestResult)
    150    */
    151   @Override
    152   public void onConfigurationFailure(ITestResult itr) {
    153     m_failedConfs.add(itr);
    154   }
    155 
    156   public List<ITestResult> getConfigurationSkips() {
    157     return m_skippedConfs;
    158   }
    159 
    160   @Override
    161   public void beforeConfiguration(ITestResult tr) {
    162   }
    163 
    164   /**
    165    * @see org.testng.IConfigurationListener#onConfigurationSkip(org.testng.ITestResult)
    166    */
    167   @Override
    168   public void onConfigurationSkip(ITestResult itr) {
    169     m_skippedConfs.add(itr);
    170   }
    171 
    172   /**
    173    * @see org.testng.IConfigurationListener#onConfigurationSuccess(org.testng.ITestResult)
    174    */
    175   @Override
    176   public void onConfigurationSuccess(ITestResult itr) {
    177     m_passedConfs.add(itr);
    178   }
    179 
    180   @Override
    181   public String toString() {
    182     return Objects.toStringHelper(getClass())
    183         .add("passed", getPassedTests().size())
    184         .add("failed", getFailedTests().size())
    185         .add("skipped", getSkippedTests().size())
    186         .toString();
    187   }
    188 }
    189