001: package org.testng.internal;
002:
003: import org.testng.ITestNGMethod;
004: import org.testng.internal.annotations.AnnotationHelper;
005: import org.testng.internal.annotations.ConfigurationAnnotation;
006: import org.testng.internal.annotations.IAfterClass;
007: import org.testng.internal.annotations.IAfterGroups;
008: import org.testng.internal.annotations.IAfterMethod;
009: import org.testng.internal.annotations.IAfterSuite;
010: import org.testng.internal.annotations.IAfterTest;
011: import org.testng.internal.annotations.IAnnotation;
012: import org.testng.internal.annotations.IAnnotationFinder;
013: import org.testng.internal.annotations.IBeforeClass;
014: import org.testng.internal.annotations.IBeforeGroups;
015: import org.testng.internal.annotations.IBeforeMethod;
016: import org.testng.internal.annotations.IBeforeSuite;
017: import org.testng.internal.annotations.IBeforeTest;
018: import org.testng.internal.annotations.IConfiguration;
019: import org.testng.internal.annotations.ITest;
020:
021: import java.lang.reflect.Method;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: public class ConfigurationMethod extends BaseTestMethod {
026: private final boolean m_isBeforeSuiteConfiguration;
027: private final boolean m_isAfterSuiteConfiguration;
028:
029: private final boolean m_isBeforeTestConfiguration;
030: private final boolean m_isAfterTestConfiguration;
031:
032: private final boolean m_isBeforeClassConfiguration;
033: private final boolean m_isAfterClassConfiguration;
034:
035: private final boolean m_isBeforeMethodConfiguration;
036: private final boolean m_isAfterMethodConfiguration;
037:
038: private boolean m_inheritGroupsFromTestClass = false;
039:
040: private ConfigurationMethod(Method method,
041: IAnnotationFinder annotationFinder, boolean isBeforeSuite,
042: boolean isAfterSuite, boolean isBeforeTest,
043: boolean isAfterTest, boolean isBeforeClass,
044: boolean isAfterClass, boolean isBeforeMethod,
045: boolean isAfterMethod, String[] beforeGroups,
046: String[] afterGroups, boolean initialize) {
047: super (method, annotationFinder);
048: if (initialize) {
049: init();
050: }
051:
052: m_isBeforeSuiteConfiguration = isBeforeSuite;
053: m_isAfterSuiteConfiguration = isAfterSuite;
054:
055: m_isBeforeTestConfiguration = isBeforeTest;
056: m_isAfterTestConfiguration = isAfterTest;
057:
058: m_isBeforeClassConfiguration = isBeforeClass;
059: m_isAfterClassConfiguration = isAfterClass;
060:
061: m_isBeforeMethodConfiguration = isBeforeMethod;
062: m_isAfterMethodConfiguration = isAfterMethod;
063:
064: m_beforeGroups = beforeGroups;
065: m_afterGroups = afterGroups;
066:
067: }
068:
069: public ConfigurationMethod(Method method,
070: IAnnotationFinder annotationFinder, boolean isBeforeSuite,
071: boolean isAfterSuite, boolean isBeforeTest,
072: boolean isAfterTest, boolean isBeforeClass,
073: boolean isAfterClass, boolean isBeforeMethod,
074: boolean isAfterMethod, String[] beforeGroups,
075: String[] afterGroups) {
076: this (method, annotationFinder, isBeforeSuite, isAfterSuite,
077: isBeforeTest, isAfterTest, isBeforeClass, isAfterClass,
078: isBeforeMethod, isAfterMethod, beforeGroups,
079: afterGroups, true);
080: }
081:
082: public static ITestNGMethod[] createSuiteConfigurationMethods(
083: ITestNGMethod[] methods,
084: IAnnotationFinder annotationFinder, boolean isBefore) {
085: ITestNGMethod[] result = new ITestNGMethod[methods.length];
086:
087: for (int i = 0; i < methods.length; i++) {
088: result[i] = new ConfigurationMethod(methods[i].getMethod(),
089: annotationFinder, isBefore, !isBefore, false,
090: false, false, false, false, false, new String[0],
091: new String[0]);
092: }
093:
094: return result;
095: }
096:
097: public static ITestNGMethod[] createTestConfigurationMethods(
098: ITestNGMethod[] methods,
099: IAnnotationFinder annotationFinder, boolean isBefore) {
100: ITestNGMethod[] result = new ITestNGMethod[methods.length];
101:
102: for (int i = 0; i < methods.length; i++) {
103: result[i] = new ConfigurationMethod(methods[i].getMethod(),
104: annotationFinder, false, false, isBefore,
105: !isBefore, false, false, false, false,
106: new String[0], new String[0]);
107: }
108:
109: return result;
110: }
111:
112: public static ITestNGMethod[] createClassConfigurationMethods(
113: ITestNGMethod[] methods,
114: IAnnotationFinder annotationFinder, boolean isBefore) {
115: ITestNGMethod[] result = new ITestNGMethod[methods.length];
116:
117: for (int i = 0; i < methods.length; i++) {
118: result[i] = new ConfigurationMethod(methods[i].getMethod(),
119: annotationFinder, false, false, false, false,
120: isBefore, !isBefore, false, false, new String[0],
121: new String[0]);
122: }
123:
124: return result;
125: }
126:
127: public static ITestNGMethod[] createBeforeConfigurationMethods(
128: ITestNGMethod[] methods,
129: IAnnotationFinder annotationFinder, boolean isBefore) {
130: ITestNGMethod[] result = new ITestNGMethod[methods.length];
131: for (int i = 0; i < methods.length; i++) {
132: result[i] = new ConfigurationMethod(methods[i].getMethod(),
133: annotationFinder, false, false, false, false,
134: false, false, false, false, isBefore ? methods[i]
135: .getBeforeGroups() : new String[0],
136: new String[0]);
137: }
138:
139: return result;
140: }
141:
142: public static ITestNGMethod[] createAfterConfigurationMethods(
143: ITestNGMethod[] methods,
144: IAnnotationFinder annotationFinder, boolean isBefore) {
145: ITestNGMethod[] result = new ITestNGMethod[methods.length];
146: for (int i = 0; i < methods.length; i++) {
147: result[i] = new ConfigurationMethod(methods[i].getMethod(),
148: annotationFinder, false, false, false, false,
149: false, false, false, false, new String[0],
150: isBefore ? new String[0] : methods[i]
151: .getAfterGroups());
152: }
153:
154: return result;
155: }
156:
157: public static ITestNGMethod[] createTestMethodConfigurationMethods(
158: ITestNGMethod[] methods,
159: IAnnotationFinder annotationFinder, boolean isBefore) {
160: ITestNGMethod[] result = new ITestNGMethod[methods.length];
161:
162: for (int i = 0; i < methods.length; i++) {
163: result[i] = new ConfigurationMethod(methods[i].getMethod(),
164: annotationFinder, false, false, false, false,
165: false, false, isBefore, !isBefore, new String[0],
166: new String[0]);
167: }
168:
169: return result;
170: }
171:
172: /**
173: * @return Returns the isAfterClassConfiguration.
174: */
175: @Override
176: public boolean isAfterClassConfiguration() {
177: return m_isAfterClassConfiguration;
178: }
179:
180: /**
181: * @return Returns the isAfterMethodConfiguration.
182: */
183: @Override
184: public boolean isAfterMethodConfiguration() {
185: return m_isAfterMethodConfiguration;
186: }
187:
188: /**
189: * @return Returns the isBeforeClassConfiguration.
190: */
191: @Override
192: public boolean isBeforeClassConfiguration() {
193: return m_isBeforeClassConfiguration;
194: }
195:
196: /**
197: * @return Returns the isBeforeMethodConfiguration.
198: */
199: @Override
200: public boolean isBeforeMethodConfiguration() {
201: return m_isBeforeMethodConfiguration;
202: }
203:
204: /**
205: * @return Returns the isAfterSuiteConfiguration.
206: */
207: @Override
208: public boolean isAfterSuiteConfiguration() {
209: return m_isAfterSuiteConfiguration;
210: }
211:
212: /**
213: * @return Returns the isBeforeSuiteConfiguration.
214: */
215: @Override
216: public boolean isBeforeSuiteConfiguration() {
217: return m_isBeforeSuiteConfiguration;
218: }
219:
220: @Override
221: public boolean isBeforeTestConfiguration() {
222: return m_isBeforeTestConfiguration;
223: }
224:
225: @Override
226: public boolean isAfterTestConfiguration() {
227: return m_isAfterTestConfiguration;
228: }
229:
230: public boolean isBeforeGroupsConfiguration() {
231: return m_beforeGroups != null && m_beforeGroups.length > 0;
232: }
233:
234: public boolean isAfterGroupsConfiguration() {
235: return m_afterGroups != null && m_afterGroups.length > 0;
236: }
237:
238: private boolean inheritGroupsFromTestClass() {
239: return m_inheritGroupsFromTestClass;
240: }
241:
242: private void init() {
243: IAnnotation a = AnnotationHelper.findConfiguration(
244: m_annotationFinder, m_method);
245: IConfiguration annotation = (IConfiguration) a;
246: if (a != null) {
247: m_inheritGroupsFromTestClass = annotation
248: .getInheritGroups();
249: setDescription(annotation.getDescription());
250: }
251:
252: if (annotation != null && annotation.isFakeConfiguration()) {
253: if (annotation.getBeforeSuite())
254: initGroups(IBeforeSuite.class);
255: if (annotation.getAfterSuite())
256: initGroups(IAfterSuite.class);
257: if (annotation.getBeforeTest())
258: initGroups(IBeforeTest.class);
259: if (annotation.getAfterTest())
260: initGroups(IAfterTest.class);
261: if (annotation.getBeforeGroups().length != 0)
262: initGroups(IBeforeGroups.class);
263: if (annotation.getAfterGroups().length != 0)
264: initGroups(IAfterGroups.class);
265: if (annotation.getBeforeTestClass())
266: initGroups(IBeforeClass.class);
267: if (annotation.getAfterTestClass())
268: initGroups(IAfterClass.class);
269: if (annotation.getBeforeTestMethod())
270: initGroups(IBeforeMethod.class);
271: if (annotation.getAfterTestMethod())
272: initGroups(IAfterMethod.class);
273: } else {
274: initGroups(IConfiguration.class);
275: }
276:
277: // If this configuration method has inherit-groups=true, add the groups
278: // defined in the @Test class
279: if (inheritGroupsFromTestClass()) {
280: ITest classAnnotation = (ITest) m_annotationFinder
281: .findAnnotation(m_methodClass, ITest.class);
282: if (classAnnotation != null) {
283: String[] groups = classAnnotation.getGroups();
284: Map<String, String> newGroups = new HashMap<String, String>();
285: for (String g : getGroups()) {
286: newGroups.put(g, g);
287: }
288: if (groups != null) {
289: for (String g : groups) {
290: newGroups.put(g, g);
291: }
292: setGroups(newGroups.values().toArray(
293: new String[newGroups.size()]));
294: }
295: }
296: }
297: }
298:
299: private static void ppp(String s) {
300: System.out.println("[ConfigurationMethod] " + s);
301: }
302:
303: public ConfigurationMethod clone() {
304: ConfigurationMethod clone = new ConfigurationMethod(
305: getMethod(), getAnnotationFinder(),
306: isBeforeSuiteConfiguration(),
307: isAfterSuiteConfiguration(),
308: isBeforeTestConfiguration(),
309: isAfterTestConfiguration(),
310: isBeforeClassConfiguration(),
311: isAfterClassConfiguration(),
312: isBeforeMethodConfiguration(),
313: isAfterMethodConfiguration(), getBeforeGroups(),
314: getAfterGroups(), false /* do not call init() */
315: );
316: clone.m_testClass = getTestClass();
317: clone.setDate(getDate());
318: clone.setGroups(getGroups());
319: clone.setGroupsDependedUpon(getGroupsDependedUpon());
320: clone.setMethodsDependedUpon(getMethodsDependedUpon());
321: clone.setAlwaysRun(isAlwaysRun());
322: clone.setMissingGroup(getMissingGroup());
323: clone.setDescription(getDescription());
324: clone
325: .setParameterInvocationCount(getParameterInvocationCount());
326: clone.m_inheritGroupsFromTestClass = inheritGroupsFromTestClass();
327:
328: return clone;
329: }
330:
331: public boolean isFirstTimeOnly() {
332: boolean result = false;
333: IAnnotation before = m_annotationFinder.findAnnotation(
334: getMethod(), IBeforeMethod.class);
335: if (before != null) {
336: result = ((ConfigurationAnnotation) before)
337: .isFirstTimeOnly();
338: }
339: return result;
340: }
341:
342: public boolean isLastTimeOnly() {
343: boolean result = false;
344: IAnnotation before = m_annotationFinder.findAnnotation(
345: getMethod(), IAfterMethod.class);
346: if (before != null) {
347: result = ((ConfigurationAnnotation) before)
348: .isLastTimeOnly();
349: }
350: return result;
351: }
352:
353: }
|