ConfigurationMethod.java revision c2f269c2c123407058d36416782b081a39c3f4b4
1package org.testng.internal; 2 3import org.testng.Assert; 4import org.testng.ITestNGMethod; 5import org.testng.annotations.IAnnotation; 6import org.testng.annotations.IConfigurationAnnotation; 7import org.testng.annotations.ITestAnnotation; 8import org.testng.collections.Lists; 9import org.testng.collections.Maps; 10import org.testng.internal.annotations.AnnotationHelper; 11import org.testng.internal.annotations.ConfigurationAnnotation; 12import org.testng.internal.annotations.IAfterClass; 13import org.testng.internal.annotations.IAfterGroups; 14import org.testng.internal.annotations.IAfterMethod; 15import org.testng.internal.annotations.IAfterSuite; 16import org.testng.internal.annotations.IAfterTest; 17import org.testng.internal.annotations.IAnnotationFinder; 18import org.testng.internal.annotations.IBeforeClass; 19import org.testng.internal.annotations.IBeforeGroups; 20import org.testng.internal.annotations.IBeforeMethod; 21import org.testng.internal.annotations.IBeforeSuite; 22import org.testng.internal.annotations.IBeforeTest; 23 24import java.lang.reflect.Method; 25import java.util.List; 26import java.util.Map; 27 28public class ConfigurationMethod extends BaseTestMethod { 29 /** 30 * 31 */ 32 private static final long serialVersionUID = -6537771498553619645L; 33 private final boolean m_isBeforeSuiteConfiguration; 34 private final boolean m_isAfterSuiteConfiguration; 35 36 private final boolean m_isBeforeTestConfiguration; 37 private final boolean m_isAfterTestConfiguration; 38 39 private final boolean m_isBeforeClassConfiguration; 40 private final boolean m_isAfterClassConfiguration; 41 42 private final boolean m_isBeforeMethodConfiguration; 43 private final boolean m_isAfterMethodConfiguration; 44 45 private boolean m_inheritGroupsFromTestClass = false; 46 47 private ConfigurationMethod(Method method, 48 IAnnotationFinder annotationFinder, 49 boolean isBeforeSuite, 50 boolean isAfterSuite, 51 boolean isBeforeTest, 52 boolean isAfterTest, 53 boolean isBeforeClass, 54 boolean isAfterClass, 55 boolean isBeforeMethod, 56 boolean isAfterMethod, 57 String[] beforeGroups, 58 String[] afterGroups, 59 boolean initialize) 60 { 61 super(method, annotationFinder); 62 if(initialize) { 63 init(); 64 } 65 66 m_isBeforeSuiteConfiguration = isBeforeSuite; 67 m_isAfterSuiteConfiguration = isAfterSuite; 68 69 m_isBeforeTestConfiguration = isBeforeTest; 70 m_isAfterTestConfiguration = isAfterTest; 71 72 m_isBeforeClassConfiguration = isBeforeClass; 73 m_isAfterClassConfiguration = isAfterClass; 74 75 m_isBeforeMethodConfiguration = isBeforeMethod; 76 m_isAfterMethodConfiguration = isAfterMethod; 77 78 m_beforeGroups = beforeGroups; 79 m_afterGroups = afterGroups; 80 81 } 82 83 public ConfigurationMethod(Method method, 84 IAnnotationFinder annotationFinder, 85 boolean isBeforeSuite, 86 boolean isAfterSuite, 87 boolean isBeforeTest, 88 boolean isAfterTest, 89 boolean isBeforeClass, 90 boolean isAfterClass, 91 boolean isBeforeMethod, 92 boolean isAfterMethod, 93 String[] beforeGroups, 94 String[] afterGroups) 95 { 96 this(method, annotationFinder, isBeforeSuite, isAfterSuite, isBeforeTest, isAfterTest, 97 isBeforeClass, isAfterClass, isBeforeMethod, isAfterMethod, beforeGroups, afterGroups, true); 98 } 99 100 private static ITestNGMethod[] createMethods(ITestNGMethod[] methods, IAnnotationFinder finder, 101 boolean isBeforeSuite, 102 boolean isAfterSuite, 103 boolean isBeforeTest, 104 boolean isAfterTest, 105 boolean isBeforeClass, 106 boolean isAfterClass, 107 boolean isBeforeMethod, 108 boolean isAfterMethod, 109 String[] beforeGroups, 110 String[] afterGroups) 111 { 112 List<ITestNGMethod> result = Lists.newArrayList(); 113 for(int i = 0; i < methods.length; i++) { 114 result.add(new ConfigurationMethod(methods[i].getMethod(), 115 finder, 116 isBeforeSuite, 117 ! isBeforeSuite, 118 isBeforeTest, 119 ! isBeforeTest, 120 isBeforeClass, 121 ! isBeforeClass, 122 isBeforeMethod, 123 ! isBeforeMethod, 124 new String[0], 125 new String[0])); 126 } 127 128 return result.toArray(new ITestNGMethod[result.size()]); 129 } 130 131 132 public static ITestNGMethod[] createSuiteConfigurationMethods(ITestNGMethod[] methods, 133 IAnnotationFinder annotationFinder, boolean isBefore) { 134 return createMethods(methods, annotationFinder, 135 isBefore, 136 !isBefore, 137 false, 138 false, 139 false, 140 false, 141 false, 142 false, 143 new String[0], 144 new String[0]); 145 } 146 147 public static ITestNGMethod[] createTestConfigurationMethods(ITestNGMethod[] methods, 148 IAnnotationFinder annotationFinder, boolean isBefore) { 149 ITestNGMethod[] result = new ITestNGMethod[methods.length]; 150 151 for(int i = 0; i < methods.length; i++) { 152 result[i] = new ConfigurationMethod(methods[i].getMethod(), 153 annotationFinder, 154 false, 155 false, 156 isBefore, 157 !isBefore, 158 false, 159 false, 160 false, 161 false, 162 new String[0], 163 new String[0]); 164 } 165 166 return result; 167 } 168 169 public static ITestNGMethod[] createClassConfigurationMethods(ITestNGMethod[] methods, 170 IAnnotationFinder annotationFinder, boolean isBefore) { 171 return createMethods(methods, annotationFinder, 172 false, 173 false, 174 false, 175 false, 176 isBefore, 177 !isBefore, 178 false, 179 false, 180 new String[0], 181 new String[0]); 182 } 183 184 public static ITestNGMethod[] createBeforeConfigurationMethods(ITestNGMethod[] methods, 185 IAnnotationFinder annotationFinder, boolean isBefore) 186 { 187 ITestNGMethod[] result = new ITestNGMethod[methods.length]; 188 for(int i = 0; i < methods.length; i++) { 189 result[i] = new ConfigurationMethod(methods[i].getMethod(), 190 annotationFinder, 191 false, 192 false, 193 false, 194 false, 195 false, 196 false, 197 false, 198 false, 199 isBefore ? methods[i].getBeforeGroups() : new String[0], 200 new String[0]); 201 } 202 203 return result; 204 } 205 206 public static ITestNGMethod[] createAfterConfigurationMethods(ITestNGMethod[] methods, 207 IAnnotationFinder annotationFinder, boolean isBefore) 208 { 209 ITestNGMethod[] result = new ITestNGMethod[methods.length]; 210 for(int i = 0; i < methods.length; i++) { 211 result[i] = new ConfigurationMethod(methods[i].getMethod(), 212 annotationFinder, 213 false, 214 false, 215 false, 216 false, 217 false, 218 false, 219 false, 220 false, 221 new String[0], 222 isBefore ? new String[0] : methods[i].getAfterGroups()); 223 } 224 225 return result; 226 } 227 228 public static ITestNGMethod[] createTestMethodConfigurationMethods(ITestNGMethod[] methods, 229 IAnnotationFinder annotationFinder, boolean isBefore) { 230 return createMethods(methods, annotationFinder, 231 false, 232 false, 233 false, 234 false, 235 false, 236 false, 237 isBefore, 238 !isBefore, 239 new String[0], 240 new String[0]); 241 } 242 243 /** 244 * @return Returns the isAfterClassConfiguration. 245 */ 246 @Override 247 public boolean isAfterClassConfiguration() { 248 return m_isAfterClassConfiguration; 249 } 250 /** 251 * @return Returns the isAfterMethodConfiguration. 252 */ 253 @Override 254 public boolean isAfterMethodConfiguration() { 255 return m_isAfterMethodConfiguration; 256 } 257 /** 258 * @return Returns the isBeforeClassConfiguration. 259 */ 260 @Override 261 public boolean isBeforeClassConfiguration() { 262 return m_isBeforeClassConfiguration; 263 } 264 /** 265 * @return Returns the isBeforeMethodConfiguration. 266 */ 267 @Override 268 public boolean isBeforeMethodConfiguration() { 269 return m_isBeforeMethodConfiguration; 270 } 271 272 273 /** 274 * @return Returns the isAfterSuiteConfiguration. 275 */ 276 @Override 277 public boolean isAfterSuiteConfiguration() { 278 return m_isAfterSuiteConfiguration; 279 } 280 281 /** 282 * @return Returns the isBeforeSuiteConfiguration. 283 */ 284 @Override 285 public boolean isBeforeSuiteConfiguration() { 286 return m_isBeforeSuiteConfiguration; 287 } 288 289 @Override 290 public boolean isBeforeTestConfiguration() { 291 return m_isBeforeTestConfiguration; 292 } 293 294 @Override 295 public boolean isAfterTestConfiguration() { 296 return m_isAfterTestConfiguration; 297 } 298 299 @Override 300 public boolean isBeforeGroupsConfiguration() { 301 return m_beforeGroups != null && m_beforeGroups.length > 0; 302 } 303 304 @Override 305 public boolean isAfterGroupsConfiguration() { 306 return m_afterGroups != null && m_afterGroups.length > 0; 307 } 308 309 private boolean inheritGroupsFromTestClass() { 310 return m_inheritGroupsFromTestClass; 311 } 312 313 private void init() { 314 IAnnotation a = AnnotationHelper.findConfiguration(m_annotationFinder, m_method.getMethod()); 315 IConfigurationAnnotation annotation = (IConfigurationAnnotation) a; 316 if (a != null) { 317 m_inheritGroupsFromTestClass = annotation.getInheritGroups(); 318 setDescription(annotation.getDescription()); 319 } 320 321 if (annotation != null && annotation.isFakeConfiguration()) { 322 if (annotation.getBeforeSuite()) { 323 initGroups(IBeforeSuite.class); 324 } 325 if (annotation.getAfterSuite()) { 326 initGroups(IAfterSuite.class); 327 } 328 if (annotation.getBeforeTest()) { 329 initGroups(IBeforeTest.class); 330 } 331 if (annotation.getAfterTest()) { 332 initGroups(IAfterTest.class); 333 } 334 if (annotation.getBeforeGroups().length != 0) { 335 initGroups(IBeforeGroups.class); 336 } 337 if (annotation.getAfterGroups().length != 0) { 338 initGroups(IAfterGroups.class); 339 } 340 if (annotation.getBeforeTestClass()) { 341 initGroups(IBeforeClass.class); 342 } 343 if (annotation.getAfterTestClass()) { 344 initGroups(IAfterClass.class); 345 } 346 if (annotation.getBeforeTestMethod()) { 347 initGroups(IBeforeMethod.class); 348 } 349 if (annotation.getAfterTestMethod()) { 350 initGroups(IAfterMethod.class); 351 } 352 } 353 else { 354 initGroups(IConfigurationAnnotation.class); 355 } 356 357 // If this configuration method has inherit-groups=true, add the groups 358 // defined in the @Test class 359 if (inheritGroupsFromTestClass()) { 360 ITestAnnotation classAnnotation = 361 (ITestAnnotation) m_annotationFinder.findAnnotation(m_methodClass, ITestAnnotation.class); 362 if (classAnnotation != null) { 363 String[] groups = classAnnotation.getGroups(); 364 Map<String, String> newGroups = Maps.newHashMap(); 365 for (String g : getGroups()) { 366 newGroups.put(g, g); 367 } 368 if (groups != null) { 369 for (String g : groups) { 370 newGroups.put(g, g); 371 } 372 setGroups(newGroups.values().toArray(new String[newGroups.size()])); 373 } 374 } 375 } 376 377 if (annotation != null) { 378 setTimeOut(annotation.getTimeOut()); 379 } 380 } 381 382 private static void ppp(String s) { 383 System.out.println("[ConfigurationMethod] " + s); 384 } 385 386 @Override 387 public ConfigurationMethod clone() { 388 ConfigurationMethod clone= new ConfigurationMethod(getMethod(), 389 getAnnotationFinder(), 390 isBeforeSuiteConfiguration(), 391 isAfterSuiteConfiguration(), 392 isBeforeTestConfiguration(), 393 isAfterTestConfiguration(), 394 isBeforeClassConfiguration(), 395 isAfterClassConfiguration(), 396 isBeforeMethodConfiguration(), 397 isAfterMethodConfiguration(), 398 getBeforeGroups(), 399 getAfterGroups(), 400 false /* do not call init() */ 401 ); 402 clone.m_testClass= getTestClass(); 403 clone.setDate(getDate()); 404 clone.setGroups(getGroups()); 405 clone.setGroupsDependedUpon(getGroupsDependedUpon()); 406 clone.setMethodsDependedUpon(getMethodsDependedUpon()); 407 clone.setAlwaysRun(isAlwaysRun()); 408 clone.setMissingGroup(getMissingGroup()); 409 clone.setDescription(getDescription()); 410 clone.setParameterInvocationCount(getParameterInvocationCount()); 411 clone.m_inheritGroupsFromTestClass= inheritGroupsFromTestClass(); 412 413 return clone; 414 } 415 416 public boolean isFirstTimeOnly() { 417 boolean result = false; 418 IAnnotation before = m_annotationFinder.findAnnotation(getMethod(), IBeforeMethod.class); 419 if (before != null) { 420 result = ((ConfigurationAnnotation) before).isFirstTimeOnly(); 421 } 422 return result; 423 } 424 425 public boolean isLastTimeOnly() { 426 boolean result = false; 427 IAnnotation before = m_annotationFinder.findAnnotation(getMethod(), IAfterMethod.class); 428 if (before != null) { 429 result = ((ConfigurationAnnotation) before).isLastTimeOnly(); 430 } 431 return result; 432 } 433 434} 435 436