1 package org.testng.internal;
2 
3 import org.testng.IMethodSelector;
4 import org.testng.IMethodSelectorContext;
5 import org.testng.ITestNGMethod;
6 import org.testng.collections.Lists;
7 
8 import java.io.Serializable;
9 import java.util.Collections;
10 import java.util.List;
11 
12 /**
13  * This class contains all the information needed to determine
14  * what methods should be run.  It gets invoked by the TestRunner
15  * and then goes through its list of method selectors to decide what methods
16  * need to be run.
17  *
18  * @author <a href="mailto:cedric@beust.com">Cedric Beust</a>
19  */
20 public class RunInfo implements Serializable {
21   private static final long serialVersionUID = -9085221672822562888L;
22   transient private List<MethodSelectorDescriptor>
23     m_methodSelectors = Lists.newArrayList();
24 
addMethodSelector(IMethodSelector selector, int priority)25   public void addMethodSelector(IMethodSelector selector, int priority) {
26     Utils.log("RunInfo", 3, "Adding method selector: " + selector + " priority: " + priority);
27     MethodSelectorDescriptor md = new MethodSelectorDescriptor(selector, priority);
28     m_methodSelectors.add(md);
29   }
30 
31   /**
32    * @return true as soon as we fond a Method Selector that returns
33    * true for the method "tm".
34    */
includeMethod(ITestNGMethod tm, boolean isTestMethod)35   public boolean includeMethod(ITestNGMethod tm, boolean isTestMethod) {
36     Collections.sort(m_methodSelectors);
37     boolean foundNegative = false;
38     IMethodSelectorContext context = new DefaultMethodSelectorContext();
39 
40     boolean result = false;
41     for (MethodSelectorDescriptor mds : m_methodSelectors) {
42       // If we found any negative priority, we break as soon as we encounter
43       // a selector with a positive priority
44       if (! foundNegative) {
45         foundNegative = mds.getPriority() < 0;
46       }
47       if (foundNegative && mds.getPriority() >= 0) {
48         break;
49       }
50 
51       // Proceeed normally
52       IMethodSelector md = mds.getMethodSelector();
53       result = md.includeMethod(context, tm, isTestMethod);
54       if (context.isStopped()) {
55         return result;
56       }
57 
58       // This selector returned false, move on to the next
59     }
60 
61     return result;
62   }
63 
ppp(String s)64   public static void ppp(String s) {
65     System.out.println("[RunInfo] " + s);
66   }
67 
setTestMethods(List<ITestNGMethod> testMethods)68   public void setTestMethods(List<ITestNGMethod> testMethods) {
69     for (MethodSelectorDescriptor mds : m_methodSelectors) {
70       mds.setTestMethods(testMethods);
71     }
72   }
73 }
74