Home | History | Annotate | Download | only in internal
      1 package org.testng.internal;
      2 
      3 import org.testng.ITestClass;
      4 import org.testng.ITestNGMethod;
      5 import org.testng.annotations.ITestAnnotation;
      6 import org.testng.internal.annotations.AnnotationHelper;
      7 import org.testng.internal.annotations.IAnnotationFinder;
      8 import org.testng.xml.XmlClass;
      9 import org.testng.xml.XmlInclude;
     10 import org.testng.xml.XmlTest;
     11 
     12 import java.io.Serializable;
     13 import java.lang.reflect.Method;
     14 import java.util.Collections;
     15 import java.util.Comparator;
     16 import java.util.List;
     17 
     18 
     19 /**
     20  * This class represents a test method.
     21  *
     22  * @author Cedric Beust, May 3, 2004
     23  * @author <a href = "mailto:the_mindstorm&#64;evolva.ro">Alexandru Popescu</a>
     24  */
     25 public class TestNGMethod extends BaseTestMethod implements Serializable {
     26   /**
     27    *
     28    */
     29   private static final long serialVersionUID = -1742868891986775307L;
     30   private int m_threadPoolSize = 0;
     31   private int m_invocationCount = 1;
     32   private int m_totalInvocationCount = m_invocationCount;
     33   private int m_successPercentage = 100;
     34 
     35   /**
     36    * Constructs a <code>TestNGMethod</code>
     37    *
     38    * @param method
     39    * @param finder
     40    */
     41   public TestNGMethod(Method method, IAnnotationFinder finder, XmlTest xmlTest, Object instance) {
     42     this(method, finder, true, xmlTest, instance);
     43   }
     44 
     45   private TestNGMethod(Method method, IAnnotationFinder finder, boolean initialize,
     46       XmlTest xmlTest, Object instance) {
     47     super(method.getName(), method, finder, instance);
     48 
     49     if(initialize) {
     50       init(xmlTest);
     51     }
     52   }
     53 
     54   /**
     55    * {@inheritDoc}
     56    */
     57   @Override
     58   public int getInvocationCount() {
     59     return m_invocationCount;
     60   }
     61 
     62   /**
     63    * {@inheritDoc}
     64    */
     65   @Override
     66   public int getTotalInvocationCount() {
     67     return m_totalInvocationCount;
     68   }
     69 
     70   /**
     71    * {@inheritDoc}
     72    */
     73   @Override
     74   public int getSuccessPercentage() {
     75     return m_successPercentage;
     76   }
     77 
     78   /**
     79    * {@inheritDoc}
     80    */
     81   @Override
     82   public boolean isTest() {
     83     return true;
     84   }
     85 
     86   private void ppp(String s) {
     87     System.out.println("[TestNGMethod] " + s);
     88   }
     89 
     90   private void init(XmlTest xmlTest) {
     91     setXmlTest(xmlTest);
     92     setInvocationNumbers(xmlTest.getInvocationNumbers(
     93         m_method.getDeclaringClass().getName() + "." + m_method.getName()));
     94     {
     95       ITestAnnotation testAnnotation =
     96           AnnotationHelper.findTest(getAnnotationFinder(), m_method.getMethod());
     97 
     98       if (testAnnotation == null) {
     99         // Try on the class
    100         testAnnotation = AnnotationHelper.findTest(getAnnotationFinder(), m_method.getDeclaringClass());
    101       }
    102 
    103       if (null != testAnnotation) {
    104         setTimeOut(testAnnotation.getTimeOut());
    105         m_successPercentage = testAnnotation.getSuccessPercentage();
    106 
    107         setInvocationCount(testAnnotation.getInvocationCount());
    108         m_totalInvocationCount = testAnnotation.getInvocationCount();
    109         setThreadPoolSize(testAnnotation.getThreadPoolSize());
    110         setAlwaysRun(testAnnotation.getAlwaysRun());
    111         setDescription(findDescription(testAnnotation, xmlTest));
    112         setEnabled(testAnnotation.getEnabled());
    113         setRetryAnalyzer(testAnnotation.getRetryAnalyzer());
    114         setSkipFailedInvocations(testAnnotation.skipFailedInvocations());
    115         setInvocationTimeOut(testAnnotation.invocationTimeOut());
    116         setIgnoreMissingDependencies(testAnnotation.ignoreMissingDependencies());
    117         setPriority(testAnnotation.getPriority());
    118       }
    119 
    120       // Groups
    121       {
    122         initGroups(ITestAnnotation.class);
    123       }
    124     }
    125   }
    126 
    127   private String findDescription(ITestAnnotation testAnnotation, XmlTest xmlTest) {
    128     String result = testAnnotation.getDescription();
    129     if (result == null) {
    130       List<XmlClass> classes = xmlTest.getXmlClasses();
    131       for (XmlClass c : classes) {
    132         if (c.getName().equals(m_method.getMethod().getDeclaringClass().getName())) {
    133           for (XmlInclude include : c.getIncludedMethods()) {
    134             if (include.getName().equals(m_method.getName())) {
    135               result = include.getDescription();
    136               if (result != null) {
    137                 break;
    138               }
    139             }
    140           }
    141         }
    142       }
    143     }
    144     return result;
    145   }
    146 
    147   /**
    148    * {@inheritDoc}
    149    */
    150   @Override
    151   public int getThreadPoolSize() {
    152     return m_threadPoolSize;
    153   }
    154 
    155   /**
    156    * Sets the number of threads on which this method should be invoked.
    157    */
    158   @Override
    159   public void setThreadPoolSize(int threadPoolSize) {
    160     m_threadPoolSize = threadPoolSize;
    161   }
    162 
    163   /**
    164    * Sets the number of invocations for this method.
    165    */
    166   @Override
    167   public void setInvocationCount(int counter) {
    168     m_invocationCount= counter;
    169   }
    170 
    171   /**
    172    * Clones the current <code>TestNGMethod</code> and its @BeforeMethod and @AfterMethod methods.
    173    * @see org.testng.internal.BaseTestMethod#clone()
    174    */
    175   @Override
    176   public BaseTestMethod clone() {
    177     TestNGMethod clone= new TestNGMethod(getMethod(), getAnnotationFinder(), false, getXmlTest(),
    178         getInstance());
    179     ITestClass tc= getTestClass();
    180     NoOpTestClass testClass= new NoOpTestClass(tc);
    181     testClass.setBeforeTestMethods(clone(tc.getBeforeTestMethods()));
    182     testClass.setAfterTestMethod(clone(tc.getAfterTestMethods()));
    183     clone.m_testClass= testClass;
    184     clone.setDate(getDate());
    185     clone.setGroups(getGroups());
    186     clone.setGroupsDependedUpon(getGroupsDependedUpon(), Collections.<String>emptyList());
    187     clone.setMethodsDependedUpon(getMethodsDependedUpon());
    188     clone.setAlwaysRun(isAlwaysRun());
    189     clone.m_beforeGroups= getBeforeGroups();
    190     clone.m_afterGroups= getAfterGroups();
    191     clone.m_currentInvocationCount= m_currentInvocationCount;
    192     clone.setMissingGroup(getMissingGroup());
    193     clone.setThreadPoolSize(getThreadPoolSize());
    194     clone.setDescription(getDescription());
    195     clone.setEnabled(getEnabled());
    196     clone.setParameterInvocationCount(getParameterInvocationCount());
    197     clone.setInvocationCount(getInvocationCount());
    198     clone.m_totalInvocationCount = getTotalInvocationCount();
    199     clone.m_successPercentage = getSuccessPercentage();
    200     clone.setTimeOut(getTimeOut());
    201     clone.setRetryAnalyzer(getRetryAnalyzer());
    202     clone.setSkipFailedInvocations(skipFailedInvocations());
    203     clone.setInvocationNumbers(getInvocationNumbers());
    204     clone.setPriority(getPriority());
    205 
    206     return clone;
    207   }
    208 
    209   private ITestNGMethod[] clone(ITestNGMethod[] sources) {
    210     ITestNGMethod[] clones= new ITestNGMethod[sources.length];
    211     for(int i= 0; i < sources.length; i++) {
    212       clones[i]= sources[i].clone();
    213     }
    214 
    215     return clones;
    216   }
    217 
    218   /** Sorts ITestNGMethod by Class name. */
    219   public static final Comparator<ITestNGMethod> SORT_BY_CLASS =
    220     new Comparator<ITestNGMethod>() {
    221 
    222     @Override
    223     public int compare(ITestNGMethod o1, ITestNGMethod o2) {
    224       String c1 = o1.getTestClass().getName();
    225       String c2 = o2.getTestClass().getName();
    226       return c1.compareTo(c2);
    227     }
    228   };
    229 }
    230