package org.testng;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.testng.collections.ListMultiMap;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.collections.Sets;
import org.testng.internal.AbstractParallelWorker;
import org.testng.internal.Attributes;
import org.testng.internal.ClassInfoMap;
import org.testng.internal.ConfigMethodArguments;
import org.testng.internal.ConfigMethodArguments.Builder;
import org.testng.internal.ConfigurationGroupMethods;
import org.testng.internal.DefaultListenerFactory;
import org.testng.internal.DynamicGraphHelper;
import org.testng.internal.GroupsHelper;
import org.testng.internal.IConfiguration;
import org.testng.internal.IInvoker;
import org.testng.internal.ITestResultNotifier;
import org.testng.internal.InstanceCreator;
import org.testng.internal.InvokedMethod;
import org.testng.internal.Invoker;
import org.testng.internal.TestMethodComparator;
import org.testng.internal.MethodGroupsHelper;
import org.testng.internal.MethodHelper;
import org.testng.internal.ResultMap;
import org.testng.internal.RunInfo;
import org.testng.internal.Systematiser;
import org.testng.internal.TestListenerHelper;
import org.testng.internal.TestMethodWorker;
import org.testng.internal.TestNGClassFinder;
import org.testng.internal.TestNGMethodFinder;
import org.testng.internal.Utils;
import org.testng.internal.XmlMethodSelector;
import org.testng.internal.annotations.IAnnotationFinder;
import org.testng.thread.ITestNGThreadPoolExecutor;
import org.testng.thread.IThreadWorkerFactory;
import org.testng.thread.IWorker;
import org.testng.junit.IJUnitTestRunner;
import org.testng.log4testng.Logger;
import org.testng.util.Strings;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
import org.testng.xml.XmlPackage;
import org.testng.xml.XmlTest;
import com.google.inject.Injector;
import com.google.inject.Module;
import javax.annotation.Nonnull;
import static org.testng.internal.MethodHelper.fixMethodsWithClass;
public class TestRunner
implements ITestContext, ITestResultNotifier, IThreadWorkerFactory<ITestNGMethod> {
private static final String DEFAULT_PROP_OUTPUT_DIR = "test-output";
private static final Logger LOGGER = Logger.getLogger(TestRunner.class);
private final Comparator<ITestNGMethod> comparator;
private ISuite m_suite;
private XmlTest m_xmlTest;
private String m_testName;
private IInjectorFactory m_injectorFactory;
private final GuiceHelper guiceHelper = new GuiceHelper(this);
private List<XmlClass> m_testClassesFromXml = null;
private IInvoker m_invoker = null;
private IAnnotationFinder m_annotationFinder = null;
private List<ITestListener> m_testListeners = Lists.newArrayList();
private Set<IConfigurationListener> m_configurationListeners = Sets.newHashSet();
private final Set<IExecutionVisualiser> visualisers = Sets.newHashSet();
private IConfigurationListener m_confListener = new ConfigurationListener();
private Collection<IInvokedMethodListener> m_invokedMethodListeners = Lists.newArrayList();
private final Map<Class<? extends IClassListener>, IClassListener> m_classListeners =
Maps.newHashMap();
private final DataProviderHolder holder = new DataProviderHolder();
private ITestNGMethod[] m_allTestMethods = new ITestNGMethod[0];
private Date m_startDate = new Date();
private Date m_endDate = null;
private Map<Class<?>, ITestClass> m_classMap = Maps.newLinkedHashMap();
private String m_outputDirectory = DEFAULT_PROP_OUTPUT_DIR;
private XmlMethodSelector m_xmlMethodSelector = new XmlMethodSelector();
private static int m_verbose = 1;
private ITestNGMethod[] m_beforeSuiteMethods = {};
private ITestNGMethod[] m_afterSuiteMethods = {};
private ITestNGMethod[] m_beforeXmlTestMethods = {};
private ITestNGMethod[] m_afterXmlTestMethods = {};
private List<ITestNGMethod> m_excludedMethods = Lists.newArrayList();
private ConfigurationGroupMethods m_groupMethods = null;
private Map<String, List<String>> m_metaGroups = Maps.newHashMap();
private IResultMap m_passedTests = new ResultMap();
private IResultMap m_failedTests = new ResultMap();
private IResultMap m_failedButWithinSuccessPercentageTests = new ResultMap();
private IResultMap m_skippedTests = new ResultMap();
private RunInfo m_runInfo = new RunInfo();
private String m_host;
private List<IMethodInterceptor> m_methodInterceptors;
private ClassMethodMap m_classMethodMap;
private TestNGClassFinder m_testClassFinder;
private IConfiguration m_configuration;
public enum PriorityWeight {
groupByInstance,
preserveOrder,
priority,
dependsOnGroups,
dependsOnMethods
}
protected TestRunner(
IConfiguration configuration,
ISuite suite,
XmlTest test,
String outputDirectory,
IAnnotationFinder finder,
boolean skipFailedInvocationCounts,
Collection<IInvokedMethodListener> invokedMethodListeners,
List<IClassListener> classListeners,
Comparator<ITestNGMethod> comparator, DataProviderHolder otherHolder) {
this.comparator = comparator;
this.holder.merge(otherHolder);
init(
configuration,
suite,
test,
outputDirectory,
finder,
skipFailedInvocationCounts,
invokedMethodListeners,
classListeners);
}
public TestRunner(
IConfiguration configuration,
ISuite suite,
XmlTest test,
boolean skipFailedInvocationCounts,
Collection<IInvokedMethodListener> invokedMethodListeners,
List<IClassListener> classListeners,
Comparator<ITestNGMethod> comparator) {
this.comparator = comparator;
init(
configuration,
suite,
test,
suite.getOutputDirectory(),
suite.getAnnotationFinder(),
skipFailedInvocationCounts,
invokedMethodListeners,
classListeners);
}
public TestRunner(
IConfiguration configuration,
ISuite suite,
XmlTest test,
boolean skipFailedInvocationCounts,
Collection<IInvokedMethodListener> invokedMethodListeners,
List<IClassListener> classListeners) {
this.comparator = Systematiser.getComparator();
init(
configuration,
suite,
test,
suite.getOutputDirectory(),
suite.getAnnotationFinder(),
skipFailedInvocationCounts,
invokedMethodListeners,
classListeners);
}
private void init(
IConfiguration configuration,
ISuite suite,
XmlTest test,
String outputDirectory,
IAnnotationFinder annotationFinder,
boolean skipFailedInvocationCounts,
Collection<IInvokedMethodListener> invokedMethodListeners,
List<IClassListener> classListeners) {
m_configuration = configuration;
m_xmlTest = test;
m_suite = suite;
m_testName = test.getName();
m_host = suite.getHost();
m_testClassesFromXml = test.getXmlClasses();
m_injectorFactory = m_configuration.getInjectorFactory();
setVerbose(test.getVerbose());
boolean preserveOrder = test.getPreserveOrder();
IMethodInterceptor builtinInterceptor =
preserveOrder
? new PreserveOrderMethodInterceptor()
: new InstanceOrderingMethodInterceptor();
m_methodInterceptors = new ArrayList<>();
m_methodInterceptors.add(builtinInterceptor);
List<XmlPackage> m_packageNamesFromXml = getAllPackages();
for (XmlPackage xp : m_packageNamesFromXml) {
m_testClassesFromXml.addAll(xp.getXmlClasses());
}
m_annotationFinder = annotationFinder;
m_invokedMethodListeners = invokedMethodListeners;
m_classListeners.clear();
for (IClassListener classListener : classListeners) {
m_classListeners.put(classListener.getClass(), classListener);
}
m_invoker =
new Invoker(
m_configuration,
this,
this,
m_suite.getSuiteState(),
skipFailedInvocationCounts,
invokedMethodListeners,
classListeners, holder);
if (test.getParallel() != null) {
log(
"Running the tests in '" + test.getName() + "' with parallel mode:" + test.getParallel());
}
setOutputDirectory(outputDirectory);
init();
}
private List<XmlPackage> getAllPackages() {
final List<XmlPackage> allPackages = Lists.newArrayList();
final List<XmlPackage> suitePackages = this.m_xmlTest.getSuite().getPackages();
if (suitePackages != null) {
allPackages.addAll(suitePackages);
}
final List<XmlPackage> testPackages = this.m_xmlTest.getPackages();
if (testPackages != null) {
allPackages.addAll(testPackages);
}
return allPackages;
}
public IInvoker getInvoker() {
return m_invoker;
}
public ITestNGMethod[] getBeforeSuiteMethods() {
return m_beforeSuiteMethods;
}
public ITestNGMethod[] getAfterSuiteMethods() {
return m_afterSuiteMethods;
}
public ITestNGMethod[] getBeforeTestConfigurationMethods() {
return m_beforeXmlTestMethods;
}
public ITestNGMethod[] getAfterTestConfigurationMethods() {
return m_afterXmlTestMethods;
}
private void init() {
initMetaGroups(m_xmlTest);
initRunInfo(m_xmlTest);
if (!m_xmlTest.isJUnit()) {
initMethods();
}
initListeners();
addConfigurationListener(m_confListener);
for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {
addConfigurationListener(cl);
}
}
private void initListeners() {
Set<Class<? extends ITestNGListener>> listenerClasses = Sets.newHashSet();
Class<? extends ITestNGListenerFactory> listenerFactoryClass = null;
for (IClass cls : getTestClasses()) {
Class<?> realClass = cls.getRealClass();
TestListenerHelper.ListenerHolder listenerHolder =
TestListenerHelper.findAllListeners(realClass, m_annotationFinder);
if (listenerFactoryClass == null) {
listenerFactoryClass = listenerHolder.getListenerFactoryClass();
}
listenerClasses.addAll(listenerHolder.getListenerClasses());
}
if (listenerFactoryClass == null) {
listenerFactoryClass = DefaultListenerFactory.class;
}
ITestNGListenerFactory factory =
TestListenerHelper.createListenerFactory(m_testClassFinder, listenerFactoryClass);
for (Class<? extends ITestNGListener> c : listenerClasses) {
if (IClassListener.class.isAssignableFrom(c) && m_classListeners.containsKey(c)) {
continue;
}
ITestNGListener listener = factory.createListener(c);
addListener(listener);
}
}
private void initMetaGroups(XmlTest xmlTest) {
Map<String, List<String>> metaGroups = xmlTest.getMetaGroups();
for (Map.Entry<String, List<String>> entry : metaGroups.entrySet()) {
addMetaGroup(entry.getKey(), entry.getValue());
}
}
private void initRunInfo(final XmlTest xmlTest) {
m_xmlMethodSelector.setIncludedGroups(createGroups(m_xmlTest.getIncludedGroups()));
m_xmlMethodSelector.setExcludedGroups(createGroups(m_xmlTest.getExcludedGroups()));
m_xmlMethodSelector.setScript(m_xmlTest.getScript());
m_xmlMethodSelector.setXmlClasses(m_xmlTest.getXmlClasses());
m_runInfo.addMethodSelector(m_xmlMethodSelector, 10);
if (null != xmlTest.getMethodSelectors()) {
for (org.testng.xml.XmlMethodSelector selector : xmlTest.getMethodSelectors()) {
if (selector.getClassName() != null) {
IMethodSelector s = InstanceCreator.createSelector(selector);
m_runInfo.addMethodSelector(s, selector.getPriority());
}
}
}
}
private void initMethods() {
List<ITestNGMethod> beforeClassMethods = Lists.newArrayList();
List<ITestNGMethod> testMethods = Lists.newArrayList();
List<ITestNGMethod> afterClassMethods = Lists.newArrayList();
List<ITestNGMethod> beforeSuiteMethods = Lists.newArrayList();
List<ITestNGMethod> afterSuiteMethods = Lists.newArrayList();
List<ITestNGMethod> beforeXmlTestMethods = Lists.newArrayList();
List<ITestNGMethod> afterXmlTestMethods = Lists.newArrayList();
ClassInfoMap classMap = new ClassInfoMap(m_testClassesFromXml);
m_testClassFinder =
new TestNGClassFinder(
classMap, Maps.newHashMap(), m_configuration, this, holder);
ITestMethodFinder testMethodFinder =
new TestNGMethodFinder(m_runInfo, m_annotationFinder, comparator);
m_runInfo.setTestMethods(testMethods);
IClass[] classes = m_testClassFinder.findTestClasses();
for (IClass ic : classes) {
ITestClass tc =
new TestClass(
ic,
testMethodFinder,
m_annotationFinder,
m_xmlTest,
classMap.getXmlClass(ic.getRealClass()), m_testClassFinder.getFactoryCreationFailedMessage());
m_classMap.put(ic.getRealClass(), tc);
}
Map<String, List<ITestNGMethod>> beforeGroupMethods =
MethodGroupsHelper.findGroupsMethods(m_classMap.values(), true);
Map<String, List<ITestNGMethod>> afterGroupMethods =
MethodGroupsHelper.findGroupsMethods(m_classMap.values(), false);
for (ITestClass tc : m_classMap.values()) {
fixMethodsWithClass(tc.getTestMethods(), tc, testMethods);
fixMethodsWithClass(tc.getBeforeClassMethods(), tc, beforeClassMethods);
fixMethodsWithClass(tc.getBeforeTestMethods(), tc, null);
fixMethodsWithClass(tc.getAfterTestMethods(), tc, null);
fixMethodsWithClass(tc.getAfterClassMethods(), tc, afterClassMethods);
fixMethodsWithClass(tc.getBeforeSuiteMethods(), tc, beforeSuiteMethods);
fixMethodsWithClass(tc.getAfterSuiteMethods(), tc, afterSuiteMethods);
fixMethodsWithClass(tc.getBeforeTestConfigurationMethods(), tc, beforeXmlTestMethods);
fixMethodsWithClass(tc.getAfterTestConfigurationMethods(), tc, afterXmlTestMethods);
fixMethodsWithClass(
tc.getBeforeGroupsMethods(),
tc,
MethodHelper.uniqueMethodList(beforeGroupMethods.values()));
fixMethodsWithClass(
tc.getAfterGroupsMethods(),
tc,
MethodHelper.uniqueMethodList(afterGroupMethods.values()));
}
m_beforeSuiteMethods =
MethodHelper.collectAndOrderMethods(
beforeSuiteMethods,
false ,
m_runInfo,
m_annotationFinder,
true ,
m_excludedMethods,
comparator);
m_beforeXmlTestMethods =
MethodHelper.collectAndOrderMethods(
beforeXmlTestMethods,
false ,
m_runInfo,
m_annotationFinder,
true ,
m_excludedMethods,
comparator);
m_allTestMethods =
MethodHelper.collectAndOrderMethods(
testMethods,
true ,
m_runInfo,
m_annotationFinder,
false ,
m_excludedMethods,
comparator);
m_classMethodMap = new ClassMethodMap(Arrays.asList(m_allTestMethods), m_xmlMethodSelector);
m_afterXmlTestMethods =
MethodHelper.collectAndOrderMethods(
afterXmlTestMethods,
false ,
m_runInfo,
m_annotationFinder,
true ,
m_excludedMethods,
comparator);
m_afterSuiteMethods =
MethodHelper.collectAndOrderMethods(
afterSuiteMethods,
false ,
m_runInfo,
m_annotationFinder,
true ,
m_excludedMethods,
comparator);
m_groupMethods =
new ConfigurationGroupMethods(m_allTestMethods, beforeGroupMethods, afterGroupMethods);
}
public Collection<ITestClass> getTestClasses() {
return m_classMap.values();
}
public void setTestName(String name) {
m_testName = name;
}
public void setOutputDirectory(String od) {
m_outputDirectory = od;
}
private void addMetaGroup(String name, List<String> groupNames) {
m_metaGroups.put(name, groupNames);
}
private Map<String, String> createGroups(List<String> groups) {
return GroupsHelper.createGroups(m_metaGroups, groups);
}
public void run() {
beforeRun();
try {
XmlTest test = getTest();
if (test.isJUnit()) {
privateRunJUnit();
} else {
privateRun(test);
}
} finally {
afterRun();
}
}
private void beforeRun() {
m_startDate = new Date(System.currentTimeMillis());
logStart();
fireEvent(true );
ITestNGMethod[] testConfigurationMethods = getBeforeTestConfigurationMethods();
invokeTestConfigurations(testConfigurationMethods);
}
private void invokeTestConfigurations(ITestNGMethod[] testConfigurationMethods) {
if (null != testConfigurationMethods && testConfigurationMethods.length > 0) {
ConfigMethodArguments arguments = new Builder()
.usingConfigMethodsAs(testConfigurationMethods)
.forSuite(m_xmlTest.getSuite())
.usingParameters(m_xmlTest.getAllParameters())
.build();
m_invoker.getConfigInvoker().invokeConfigurations(arguments);
}
}
private void privateRunJUnit() {
final ClassInfoMap cim = new ClassInfoMap(m_testClassesFromXml, false);
final Set<Class<?>> classes = cim.getClasses();
final List<ITestNGMethod> runMethods = Lists.newArrayList();
List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();
workers.add(
new IWorker<ITestNGMethod>() {
@Override
public long getTimeOut() {
return 0;
}
@Override
public void run() {
for (Class<?> tc : classes) {
List<XmlInclude> includedMethods = cim.getXmlClass(tc).getIncludedMethods();
List<String> methods = Lists.newArrayList();
for (XmlInclude inc : includedMethods) {
methods.add(inc.getName());
}
IJUnitTestRunner tr = IJUnitTestRunner.createTestRunner(TestRunner.this);
tr.setInvokedMethodListeners(m_invokedMethodListeners);
try {
tr.run(tc, methods.toArray(new String[0]));
} catch (Exception ex) {
LOGGER.error(ex.getMessage(), ex);
} finally {
runMethods.addAll(tr.getTestMethods());
}
}
}
@Override
public List<ITestNGMethod> getTasks() {
throw new TestNGException("JUnit not supported");
}
@Override
public int getPriority() {
if (m_allTestMethods.length == 1) {
return m_allTestMethods[0].getPriority();
} else {
return 0;
}
}
@Override
public int compareTo(@Nonnull IWorker<ITestNGMethod> other) {
return getPriority() - other.getPriority();
}
});
runJUnitWorkers(workers);
m_allTestMethods = runMethods.toArray(new ITestNGMethod[0]);
}
private static Comparator<ITestNGMethod> newComparator(boolean needPrioritySort) {
return needPrioritySort ? new TestMethodComparator() : null;
}
private boolean sortOnPriority(ITestNGMethod[] interceptedOrder) {
return m_methodInterceptors.size() > 1 ||
Arrays.stream(interceptedOrder).anyMatch(m -> m.getPriority() != 0);
}
private static BlockingQueue<Runnable> newQueue(boolean needPrioritySort) {
return needPrioritySort ? new PriorityBlockingQueue<>() : new LinkedBlockingQueue<>();
}
private void privateRun(XmlTest xmlTest) {
boolean parallel = xmlTest.getParallel().isParallel();
int threadCount = parallel ? xmlTest.getThreadCount() : 1;
ITestNGMethod[] interceptedOrder = intercept(m_allTestMethods);
IDynamicGraph<ITestNGMethod> graph =
DynamicGraphHelper.createDynamicGraph(interceptedOrder, getCurrentXmlTest());
graph.setVisualisers(this.visualisers);
boolean needPrioritySort = sortOnPriority(interceptedOrder);
Comparator<ITestNGMethod> methodComparator = newComparator(needPrioritySort);
if (parallel) {
if (graph.getNodeCount() <= 0) {
return;
}
ITestNGThreadPoolExecutor executor =
this.m_configuration.getExecutorFactory().newTestMethodExecutor(
"test=" + xmlTest.getName(),
graph,
this,
threadCount,
threadCount,
0,
TimeUnit.MILLISECONDS,
newQueue(needPrioritySort),
methodComparator);
executor.run();
try {
long timeOut = m_xmlTest.getTimeOut(XmlTest.DEFAULT_TIMEOUT_MS);
Utils.log(
"TestRunner",
2,
"Starting executor for test "
+ m_xmlTest.getName()
+ " with time out:"
+ timeOut
+ " milliseconds.");
executor.awaitTermination(timeOut, TimeUnit.MILLISECONDS);
executor.shutdownNow();
} catch (InterruptedException handled) {
LOGGER.error(handled.getMessage(), handled);
Thread.currentThread().interrupt();
}
return;
}
List<ITestNGMethod> freeNodes = graph.getFreeNodes();
if (graph.getNodeCount() > 0 && freeNodes.isEmpty()) {
throw new TestNGException("No free nodes found in:" + graph);
}
while (!freeNodes.isEmpty()) {
if (needPrioritySort) {
freeNodes.sort(methodComparator);
freeNodes = freeNodes.subList(0, 1);
}
createWorkers(freeNodes).forEach(Runnable::run);
graph.setStatus(freeNodes, IDynamicGraph.Status.FINISHED);
freeNodes = graph.getFreeNodes();
}
}
private ITestNGMethod[] intercept(ITestNGMethod[] methods) {
List<IMethodInstance> methodInstances =
MethodHelper.methodsToMethodInstances(Arrays.asList(methods));
for (IMethodInterceptor m_methodInterceptor : m_methodInterceptors) {
methodInstances = m_methodInterceptor.intercept(methodInstances, this);
}
List<ITestNGMethod> result = MethodHelper.methodInstancesToMethods(methodInstances);
this.m_classMethodMap = new ClassMethodMap(result, null);
ITestNGMethod[] resultArray = result.toArray(new ITestNGMethod[0]);
if (resultArray.length != m_groupMethods.getAllTestMethods().length) {
m_groupMethods =
new ConfigurationGroupMethods(
resultArray,
m_groupMethods.getBeforeGroupsMethods(),
m_groupMethods.getAfterGroupsMethods());
}
if (m_methodInterceptors.size() > 1) {
for (int i = 0; i < resultArray.length; ++i) {
resultArray[i].setInterceptedPriority(i);
}
}
return resultArray;
}
@Override
public List<IWorker<ITestNGMethod>> createWorkers(List<ITestNGMethod> methods) {
AbstractParallelWorker.Arguments args =
new AbstractParallelWorker.Arguments.Builder()
.classMethodMap(this.m_classMethodMap)
.configMethods(this.m_groupMethods)
.finder(this.m_annotationFinder)
.invoker(this.m_invoker)
.methods(methods)
.testContext(this)
.listeners(this.m_classListeners.values())
.build();
return AbstractParallelWorker.newWorker(m_xmlTest.getParallel(), m_xmlTest.getGroupByInstances()).createWorkers(args);
}
private void runJUnitWorkers(List<? extends IWorker<ITestNGMethod>> workers) {
workers.forEach(Runnable::run);
}
private void afterRun() {
ITestNGMethod[] testConfigurationMethods = getAfterTestConfigurationMethods();
invokeTestConfigurations(testConfigurationMethods);
m_endDate = new Date(System.currentTimeMillis());
dumpInvokedMethods();
fireEvent(false );
}
private void logStart() {
log(
"Running test "
+ m_testName
+ " on "
+ m_classMap.size()
+ " "
+ " classes, "
+ " included groups:["
+ Strings.valueOf(m_xmlMethodSelector.getIncludedGroups())
+ "] excluded groups:["
+ Strings.valueOf(m_xmlMethodSelector.getExcludedGroups())
+ "]");
if (getVerbose() >= 3) {
for (ITestClass tc : m_classMap.values()) {
((TestClass) tc).dump();
}
}
}
private void fireEvent(boolean isStart) {
for (ITestListener itl : m_testListeners) {
if (isStart) {
itl.onStart(this);
} else {
itl.onFinish(this);
}
}
}
@Override
public String getName() {
return m_testName;
}
@Override
public Date getStartDate() {
return m_startDate;
}
@Override
public Date getEndDate() {
return m_endDate;
}
@Override
public IResultMap getPassedTests() {
return m_passedTests;
}
@Override
public IResultMap getSkippedTests() {
return m_skippedTests;
}
@Override
public IResultMap getFailedTests() {
return m_failedTests;
}
@Override
public IResultMap getFailedButWithinSuccessPercentageTests() {
return m_failedButWithinSuccessPercentageTests;
}
@Override
public String[] getIncludedGroups() {
Map<String, String> ig = m_xmlMethodSelector.getIncludedGroups();
return ig.values().toArray(new String[0]);
}
@Override
public String[] getExcludedGroups() {
Map<String, String> eg = m_xmlMethodSelector.getExcludedGroups();
return eg.values().toArray(new String[0]);
}
@Override
public String getOutputDirectory() {
return m_outputDirectory;
}
@Override
public ISuite getSuite() {
return m_suite;
}
@Override
public ITestNGMethod[] getAllTestMethods() {
return m_allTestMethods;
}
@Override
public String getHost() {
return m_host;
}
@Override
public Collection<ITestNGMethod> getExcludedMethods() {
Map<ITestNGMethod, ITestNGMethod> vResult = Maps.newHashMap();
for (ITestNGMethod m : m_excludedMethods) {
vResult.put(m, m);
}
return vResult.keySet();
}
@Override
public IResultMap getFailedConfigurations() {
return m_failedConfigurations;
}
@Override
public IResultMap getPassedConfigurations() {
return m_passedConfigurations;
}
@Override
public IResultMap getSkippedConfigurations() {
return m_skippedConfigurations;
}
@Override
public void addPassedTest(ITestNGMethod tm, ITestResult tr) {
m_passedTests.addResult(tr, tm);
}
@Override
public Set<ITestResult> getPassedTests(ITestNGMethod tm) {
return m_passedTests.getResults(tm);
}
@Override
public Set<ITestResult> getFailedTests(ITestNGMethod tm) {
return m_failedTests.getResults(tm);
}
@Override
public Set<ITestResult> getSkippedTests(ITestNGMethod tm) {
return m_skippedTests.getResults(tm);
}
@Override
public void addSkippedTest(ITestNGMethod tm, ITestResult tr) {
m_skippedTests.addResult(tr, tm);
}
@Override
public void addInvokedMethod(InvokedMethod im) {
m_invokedMethods.add(im);
}
@Override
public void addFailedTest(ITestNGMethod testMethod, ITestResult result) {
logFailedTest(testMethod, result, false );
}
@Override
public void addFailedButWithinSuccessPercentageTest(
ITestNGMethod testMethod, ITestResult result) {
logFailedTest(testMethod, result, true );
}
@Override
public XmlTest getTest() {
return m_xmlTest;
}
@Override
public List<ITestListener> getTestListeners() {
return m_testListeners;
}
@Override
public List<IConfigurationListener> getConfigurationListeners() {
List<IConfigurationListener> listeners = Lists.newArrayList(m_configurationListeners);
for (IConfigurationListener each : this.m_configuration.getConfigurationListeners()) {
boolean duplicate = false;
for (IConfigurationListener listener : listeners) {
if (each.getClass().equals(listener.getClass())) {
duplicate = true;
break;
}
}
if (!duplicate) {
listeners.add(each);
}
}
return Lists.newArrayList(listeners);
}
private void logFailedTest(
ITestNGMethod method, ITestResult tr, boolean withinSuccessPercentage) {
if (withinSuccessPercentage) {
m_failedButWithinSuccessPercentageTests.addResult(tr, method);
} else {
m_failedTests.addResult(tr, method);
}
}
private static void log(String s) {
Utils.log("TestRunner", 3, s);
}
public static int getVerbose() {
return m_verbose;
}
public void setVerbose(int n) {
m_verbose = n;
}
void addTestListener(ITestListener listener) {
Optional<ITestListener> found = m_testListeners.stream()
.filter(iTestListener -> iTestListener.getClass().equals(listener.getClass()))
.findAny();
if (found.isPresent()) {
return;
}
m_testListeners.add(listener);
}
public void addListener(ITestNGListener listener) {
if (listener instanceof IMethodInterceptor) {
m_methodInterceptors.add((IMethodInterceptor) listener);
}
if (listener instanceof ITestListener) {
addTestListener((ITestListener) listener);
}
if (listener instanceof IClassListener) {
IClassListener classListener = (IClassListener) listener;
if (!m_classListeners.containsKey(classListener.getClass())) {
m_classListeners.put(classListener.getClass(), classListener);
}
}
if (listener instanceof IConfigurationListener) {
addConfigurationListener((IConfigurationListener) listener);
}
if (listener instanceof IConfigurable) {
m_configuration.setConfigurable((IConfigurable) listener);
}
if (listener instanceof IHookable) {
m_configuration.setHookable((IHookable) listener);
}
if (listener instanceof IExecutionListener) {
IExecutionListener iel = (IExecutionListener) listener;
if (m_configuration.addExecutionListenerIfAbsent(iel)) {
iel.onExecutionStart();
}
}
if (listener instanceof IDataProviderListener) {
IDataProviderListener dataProviderListener = (IDataProviderListener) listener;
holder.addListener(dataProviderListener);
}
if (listener instanceof IDataProviderInterceptor) {
IDataProviderInterceptor interceptor = (IDataProviderInterceptor) listener;
holder.addInterceptor(interceptor);
}
if (listener instanceof IExecutionVisualiser) {
IExecutionVisualiser l = (IExecutionVisualiser) listener;
visualisers.add(l);
}
m_suite.addListener(listener);
}
void addConfigurationListener(IConfigurationListener icl) {
m_configurationListeners.add(icl);
}
private final Collection<IInvokedMethod> m_invokedMethods = new ConcurrentLinkedQueue<>();
private void dumpInvokedMethods() {
MethodHelper.dumpInvokedMethodsInfoToConsole(m_invokedMethods, getVerbose());
}
public List<ITestNGMethod> getInvokedMethods() {
return MethodHelper.invokedMethodsToMethods(m_invokedMethods);
}
private IResultMap m_passedConfigurations = new ResultMap();
private IResultMap m_skippedConfigurations = new ResultMap();
private IResultMap m_failedConfigurations = new ResultMap();
private class ConfigurationListener implements IConfigurationListener {
@Override
public void beforeConfiguration(ITestResult tr) {}
@Override
public void onConfigurationFailure(ITestResult itr) {
m_failedConfigurations.addResult(itr, itr.getMethod());
}
@Override
public void onConfigurationSkip(ITestResult itr) {
m_skippedConfigurations.addResult(itr, itr.getMethod());
}
@Override
public void onConfigurationSuccess(ITestResult itr) {
m_passedConfigurations.addResult(itr, itr.getMethod());
}
}
void addMethodInterceptor(IMethodInterceptor methodInterceptor) {
if (!m_methodInterceptors.contains(methodInterceptor)) {
m_methodInterceptors.add(methodInterceptor);
}
}
@Override
public XmlTest getCurrentXmlTest() {
return m_xmlTest;
}
private IAttributes m_attributes = new Attributes();
@Override
public Object getAttribute(String name) {
return m_attributes.getAttribute(name);
}
@Override
public void setAttribute(String name, Object value) {
m_attributes.setAttribute(name, value);
}
@Override
public Set<String> getAttributeNames() {
return m_attributes.getAttributeNames();
}
@Override
public Object removeAttribute(String name) {
return m_attributes.removeAttribute(name);
}
private ListMultiMap<Class<? extends Module>, Module> m_guiceModules = Maps.newListMultiMap();
@Override
public List<Module> getGuiceModules(Class<? extends Module> cls) {
return m_guiceModules.get(cls);
}
private Map<List<Module>, Injector> m_injectors = Maps.newHashMap();
@Override
public Injector getInjector(List<Module> moduleInstances) {
return m_injectors.get(moduleInstances);
}
@Override
public Injector getInjector(IClass iClass) {
return guiceHelper.getInjector(iClass, this.m_injectorFactory);
}
@Override
public void addInjector(List<Module> moduleInstances, Injector injector) {
m_injectors.put(moduleInstances, injector);
}
}