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