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