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