1 package org.testng.internal; 2 3 4 import java.lang.reflect.Method; 5 import java.util.List; 6 import java.util.Set; 7 8 import org.testng.ITestMethodFinder; 9 import org.testng.ITestNGMethod; 10 import org.testng.annotations.IConfigurationAnnotation; 11 import org.testng.annotations.ITestAnnotation; 12 import org.testng.collections.Lists; 13 import org.testng.internal.annotations.AnnotationHelper; 14 import org.testng.internal.annotations.IAnnotationFinder; 15 import org.testng.xml.XmlTest; 16 17 /** 18 * The default strategy for finding test methods: look up 19 * annotations @Test in front of methods. 20 * 21 * @author Cedric Beust, May 3, 2004 22 * @author <a href='mailto:the_mindstorm (at) evolva.ro'>Alexandru Popescu</a> 23 */ 24 public class TestNGMethodFinder implements ITestMethodFinder { 25 private static final int BEFORE_SUITE = 1; 26 private static final int AFTER_SUITE = 2; 27 private static final int BEFORE_TEST = 3; 28 private static final int AFTER_TEST = 4; 29 private static final int BEFORE_CLASS = 5; 30 private static final int AFTER_CLASS = 6; 31 private static final int BEFORE_TEST_METHOD = 7; 32 private static final int AFTER_TEST_METHOD = 8; 33 private static final int BEFORE_GROUPS = 9; 34 private static final int AFTER_GROUPS = 10; 35 36 private RunInfo m_runInfo = null; 37 private IAnnotationFinder m_annotationFinder = null; 38 39 public TestNGMethodFinder(RunInfo runInfo, IAnnotationFinder annotationFinder) 40 { 41 m_runInfo = runInfo; 42 m_annotationFinder = annotationFinder; 43 } 44 45 @Override 46 public ITestNGMethod[] getTestMethods(Class<?> clazz, XmlTest xmlTest) { 47 return AnnotationHelper.findMethodsWithAnnotation( 48 clazz, ITestAnnotation.class, m_annotationFinder, xmlTest); 49 } 50 51 @Override 52 public ITestNGMethod[] getBeforeClassMethods(Class cls) { 53 return findConfiguration(cls, BEFORE_CLASS); 54 } 55 56 @Override 57 public ITestNGMethod[] getAfterClassMethods(Class cls) { 58 return findConfiguration(cls, AFTER_CLASS); 59 } 60 61 @Override 62 public ITestNGMethod[] getBeforeTestMethods(Class cls) { 63 return findConfiguration(cls, BEFORE_TEST_METHOD); 64 } 65 66 @Override 67 public ITestNGMethod[] getAfterTestMethods(Class cls) { 68 return findConfiguration(cls, AFTER_TEST_METHOD); 69 } 70 71 @Override 72 public ITestNGMethod[] getBeforeSuiteMethods(Class cls) { 73 return findConfiguration(cls, BEFORE_SUITE); 74 } 75 76 @Override 77 public ITestNGMethod[] getAfterSuiteMethods(Class cls) { 78 return findConfiguration(cls, AFTER_SUITE); 79 } 80 81 @Override 82 public ITestNGMethod[] getBeforeTestConfigurationMethods(Class clazz) { 83 return findConfiguration(clazz, BEFORE_TEST); 84 } 85 86 @Override 87 public ITestNGMethod[] getAfterTestConfigurationMethods(Class clazz) { 88 return findConfiguration(clazz, AFTER_TEST); 89 } 90 91 @Override 92 public ITestNGMethod[] getBeforeGroupsConfigurationMethods(Class clazz) { 93 return findConfiguration(clazz, BEFORE_GROUPS); 94 } 95 96 @Override 97 public ITestNGMethod[] getAfterGroupsConfigurationMethods(Class clazz) { 98 return findConfiguration(clazz, AFTER_GROUPS); 99 } 100 101 private ITestNGMethod[] findConfiguration(final Class clazz, final int configurationType) { 102 List<ITestNGMethod> vResult = Lists.newArrayList(); 103 104 Set<Method> methods = ClassHelper.getAvailableMethods(clazz); 105 106 for (Method m : methods) { 107 IConfigurationAnnotation configuration = AnnotationHelper.findConfiguration(m_annotationFinder, m); 108 109 if (null == configuration) { 110 continue; 111 } 112 113 boolean create = false; 114 boolean isBeforeSuite = false; 115 boolean isAfterSuite = false; 116 boolean isBeforeTest = false; 117 boolean isAfterTest = false; 118 boolean isBeforeClass = false; 119 boolean isAfterClass = false; 120 boolean isBeforeTestMethod = false; 121 boolean isAfterTestMethod = false; 122 String[] beforeGroups = null; 123 String[] afterGroups = null; 124 125 switch(configurationType) { 126 case BEFORE_SUITE: 127 create = configuration.getBeforeSuite(); 128 isBeforeSuite = true; 129 break; 130 case AFTER_SUITE: 131 create = configuration.getAfterSuite(); 132 isAfterSuite = true; 133 break; 134 case BEFORE_TEST: 135 create = configuration.getBeforeTest(); 136 isBeforeTest = true; 137 break; 138 case AFTER_TEST: 139 create = configuration.getAfterTest(); 140 isAfterTest = true; 141 break; 142 case BEFORE_CLASS: 143 create = configuration.getBeforeTestClass(); 144 isBeforeClass = true; 145 break; 146 case AFTER_CLASS: 147 create = configuration.getAfterTestClass(); 148 isAfterClass = true; 149 break; 150 case BEFORE_TEST_METHOD: 151 create = configuration.getBeforeTestMethod(); 152 isBeforeTestMethod = true; 153 break; 154 case AFTER_TEST_METHOD: 155 create = configuration.getAfterTestMethod(); 156 isAfterTestMethod = true; 157 break; 158 case BEFORE_GROUPS: 159 beforeGroups = configuration.getBeforeGroups(); 160 create = beforeGroups.length > 0; 161 isBeforeTestMethod = true; 162 break; 163 case AFTER_GROUPS: 164 afterGroups = configuration.getAfterGroups(); 165 create = afterGroups.length > 0; 166 isBeforeTestMethod = true; 167 break; 168 } 169 170 if(create) { 171 addConfigurationMethod(clazz, 172 vResult, 173 m, 174 isBeforeSuite, 175 isAfterSuite, 176 isBeforeTest, 177 isAfterTest, 178 isBeforeClass, 179 isAfterClass, 180 isBeforeTestMethod, 181 isAfterTestMethod, 182 beforeGroups, 183 afterGroups, 184 null); /* @@@ */ 185 } 186 } 187 188 List<ITestNGMethod> excludedMethods = Lists.newArrayList(); 189 boolean unique = configurationType == BEFORE_SUITE || configurationType == AFTER_SUITE; 190 ITestNGMethod[] tmResult = MethodHelper.collectAndOrderMethods(Lists.newArrayList(vResult), 191 false /* forTests */, 192 m_runInfo, 193 m_annotationFinder, 194 unique, 195 excludedMethods); 196 197 return tmResult; 198 } 199 200 private void addConfigurationMethod(Class<?> clazz, 201 List<ITestNGMethod> results, 202 Method method, 203 boolean isBeforeSuite, 204 boolean isAfterSuite, 205 boolean isBeforeTest, 206 boolean isAfterTest, 207 boolean isBeforeClass, 208 boolean isAfterClass, 209 boolean isBeforeTestMethod, 210 boolean isAfterTestMethod, 211 String[] beforeGroups, 212 String[] afterGroups, 213 Object instance) 214 { 215 if(method.getDeclaringClass().isAssignableFrom(clazz)) { 216 ITestNGMethod confMethod = new ConfigurationMethod(new ConstructorOrMethod(method), 217 m_annotationFinder, 218 isBeforeSuite, 219 isAfterSuite, 220 isBeforeTest, 221 isAfterTest, 222 isBeforeClass, 223 isAfterClass, 224 isBeforeTestMethod, 225 isAfterTestMethod, 226 beforeGroups, 227 afterGroups, 228 instance); 229 results.add(confMethod); 230 } 231 } 232 233 } 234