// Map for unicity, Linked for guaranteed order
Map<Method, ITestNGMethod> beforeSuiteMethods= new LinkedHashMap<Method, ITestNGMethod>();
Map<Method, ITestNGMethod> afterSuiteMethods = new LinkedHashMap<Method, ITestNGMethod>();
IInvoker invoker = null;
//
// First, we create all the test runners so we can discover all the ITestClasses
//
for (XmlTest test : m_suite.getTests()) {
TestRunner tr = m_runnerFactory.newTestRunner(this, test, m_invokedMethodListeners);
//
// Install the method interceptor, if any was passed
//
if (m_methodInterceptor != null) {
tr.setMethodInterceptor(m_methodInterceptor);
}
// Reuse the same text reporter so we can accumulate all the results
// (this is used to display the final suite report at the end)
tr.addListener(m_textReporter);
m_testRunners.add(tr);
// TODO: Code smell. Invoker should belong to SuiteRunner, not TestRunner
// -- cbeust
invoker = tr.getInvoker();
for (ITestNGMethod m : tr.getBeforeSuiteMethods()) {
beforeSuiteMethods.put(m.getMethod(), m);
}
for (ITestNGMethod m : tr.getAfterSuiteMethods()) {
afterSuiteMethods.put(m.getMethod(), m);
}
//
// Find all the listener factories and collect all the listeners requested in a
// @Listeners annotation.
//
List<Class<? extends ITestNGListener>> listenerClasses = Lists.newArrayList();
Class<? extends ITestNGListenerFactory> listenerFactoryClass = null;
for (IClass cls : tr.getTestClasses()) {
IAnnotationFinder finder = m_configuration.getAnnotationFinder();
Class<? extends ITestNGListenerFactory> realClass = cls.getRealClass();
IListeners l = (IListeners) finder.findAnnotation(realClass, IListeners.class);
if (ITestNGListenerFactory.class.isAssignableFrom(realClass)) {
if (listenerFactoryClass == null) {
listenerFactoryClass = realClass;
}
else {
throw new TestNGException("Found more than one class implementing" +
"ITestNGListenerFactory:" + realClass + " and " + listenerFactoryClass);
}
}
if (l != null) {
listenerClasses.addAll(Arrays.asList(l.getValue()));
}
}
//
// Now we have all the listeners collected from @Listeners and at most one
// listener factory collected from a class implementing ITestNGListenerFactory.
// Instantiate all the requested listeners.
//
ITestNGListenerFactory listenerFactory = null;
// If we found a test listener factory, instantiate it.
try {
listenerFactory = listenerFactoryClass != null ? listenerFactoryClass.newInstance() : null;
}
catch(Exception ex) {
throw new TestNGException("Couldn't instantiate the ITestNGListenerFactory: "
+ ex.getMessage());
}
// Instantiate all the listeners
for (Class<? extends ITestNGListener> c : listenerClasses) {
Object listener = listenerFactory != null ? listenerFactory.createListener(c) : null;
if (listener == null) listener = ClassHelper.newInstance(c);
if (listener instanceof IMethodInterceptor) {
tr.setMethodInterceptor((IMethodInterceptor) listener);
} else if (listener instanceof ISuiteListener) {
addListener((ISuiteListener) listener);
} else if (listener instanceof IInvokedMethodListener) {
if (m_invokedMethodListeners == null) m_invokedMethodListeners = Lists.newArrayList();
m_invokedMethodListeners.add((IInvokedMethodListener) listener);
} else if (listener instanceof ITestListener) {
// At this point, the field m_testListeners has already been used in the creation
// of the TestRunner factory, so there is no point in adding the listener
// to it. Instead, just add the listener to the TestRunner directly.
tr.addTestListener((ITestListener) listener);
} else if (listener instanceof IReporter) {
addReporter((IReporter) listener);
}
}
}
//
// Invoke beforeSuite methods (the invoker can be null
// if the suite we are currently running only contains
// a <file-suite> tag and no real tests)
//
if (invoker != null) {
if(beforeSuiteMethods.values().size() > 0) {
invoker.invokeConfigurations(null,
beforeSuiteMethods.values().toArray(new ITestNGMethod[beforeSuiteMethods.size()]),
m_suite, m_suite.getParameters(), null, /* no parameter values */
null /* instance */
);
}
Utils.log("SuiteRunner", 3, "Created " + m_testRunners.size() + " TestRunners");
//
// Run all the test runners
//
boolean testsInParallel = XmlSuite.PARALLEL_TESTS.equals(m_suite.getParallel());
if (!testsInParallel) {
runSequentially();
}
else {
runConcurrently();
}
// SuitePlan sp = new SuitePlan();
// for (TestRunner tr : m_testRunners) {
// sp.addTestPlan(tr.getTestPlan());
// }
// sp.dump();
//
// Invoke afterSuite methods
//
if (afterSuiteMethods.values().size() > 0) {
invoker.invokeConfigurations(null,
afterSuiteMethods.values().toArray(new ITestNGMethod[afterSuiteMethods.size()]),
m_suite, m_suite.getAllParameters(), null, /* no parameter values */
null /* instance */);
}