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