package org.testng.internal;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;
import org.testng.collections.ListMultiMap;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.internal.reflect.ReflectionHelper;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
public class XmlMethodSelector implements IMethodSelector {
private static final String QUOTED_DOLLAR = Matcher.quoteReplacement("\\$");
private final ListMultiMap<String, XmlInclude> m_includedMethods = Maps.newListMultiMap();
private final IBsh m_bsh = Dynamic.hasBsh() ? new Bsh() : new BshMock();
private final Map<String, String> m_logged = Maps.newHashMap();
private Map<String, String> m_includedGroups = Maps.newHashMap();
private Map<String, String> m_excludedGroups = Maps.newHashMap();
private List<XmlClass> m_classes = Collections.emptyList();
private String m_expression = null;
private boolean m_isInitialized = false;
private List<ITestNGMethod> m_testMethods = Collections.emptyList();
@Override
public boolean includeMethod(IMethodSelectorContext context,
ITestNGMethod tm, boolean isTestMethod) {
if (!m_isInitialized) {
m_isInitialized = true;
init(context);
}
if (m_expression != null) {
return m_bsh.includeMethodFromExpression(m_expression, tm);
}
return includeMethodFromIncludeExclude(tm, isTestMethod);
}
private boolean includeMethodFromIncludeExclude(ITestNGMethod tm, boolean isTestMethod) {
boolean result = false;
ConstructorOrMethod method = tm.getConstructorOrMethod();
Map<String, String> includedGroups = m_includedGroups;
Map<String, String> excludedGroups = m_excludedGroups;
String key;
boolean hasTestClass = tm.getTestClass() != null;
if (hasTestClass) {
key = makeMethodName(tm.getTestClass().getRealClass().getName(), method.getName());
} else {
key = MethodHelper.calculateMethodCanonicalName(tm);
}
List<XmlInclude> includeList = m_includedMethods.get(key);
if (includedGroups.isEmpty() && excludedGroups.isEmpty()
&& !hasIncludedMethods() && !hasExcludedMethods()) {
result = true;
} else if (includedGroups.isEmpty() && excludedGroups.isEmpty() && !isTestMethod) {
result = true;
} else if (!includeList.isEmpty()) {
result = true;
} else {
boolean noGroupsSpecified = false;
String[] groups = tm.getGroups();
boolean isIncludedInGroups = isIncluded(m_includedGroups.values(), noGroupsSpecified, groups);
boolean isExcludedInGroups = isExcluded(m_excludedGroups.values(), groups);
if (isIncludedInGroups && !isExcludedInGroups) {
result = true;
} else if (isExcludedInGroups) {
result = false;
}
if(isTestMethod) {
Class<?> methodClass = method.getDeclaringClass();
String fullMethodName = makeMethodName(methodClass.getName(), method.getName());
noGroupsSpecified = (m_includedGroups.isEmpty() && m_excludedGroups.isEmpty());
for (XmlClass xmlClass : m_classes) {
Class<?> cls = xmlClass.getSupportClass();
if (!assignable(methodClass, cls)) {
continue;
}
List<String> includedMethods = createQualifiedMethodNames(xmlClass, toStringList(xmlClass.getIncludedMethods()));
boolean isIncludedInMethods = isIncluded(includedMethods, noGroupsSpecified, fullMethodName);
List<String> excludedMethods = createQualifiedMethodNames(xmlClass, xmlClass.getExcludedMethods());
boolean isExcludedInMethods = isExcluded(excludedMethods, fullMethodName);
if (result) {
if (!xmlClass.getIncludedMethods().isEmpty()) {
result = isIncludedInMethods;
}
if (!xmlClass.getExcludedMethods().isEmpty()) {
result = result && !isExcludedInMethods;
}
}
}
}
}
Package pkg = method.getDeclaringClass().getPackage();
String methodName = pkg != null ? pkg.getName() + "." + method.getName() : method.getName();
logInclusion(result ? "Including" : "Excluding", "method", methodName + "()");
return result;
}
private static boolean assignable(Class<?> sourceClass, Class<?> targetClass) {
return sourceClass.isAssignableFrom(targetClass) || targetClass.isAssignableFrom(sourceClass);
}
private void logInclusion(String including, String type, String name) {
if (! m_logged.containsKey(name)) {
log(including + " " + type + " " + name);
m_logged.put(name, name);
}
}
private boolean hasIncludedMethods() {
for (XmlClass xmlClass : m_classes) {
if (!xmlClass.getIncludedMethods().isEmpty()) {
return true;
}
}
return false;
}
private boolean hasExcludedMethods() {
for (XmlClass xmlClass : m_classes) {
if (!xmlClass.getExcludedMethods().isEmpty()) {
return true;
}
}
return false;
}
private static List<String> toStringList(List<XmlInclude> methods) {
List<String> result = Lists.newArrayList();
for (XmlInclude m : methods) {
result.add(m.getName());
}
return result;
}
private static List<String> createQualifiedMethodNames(XmlClass xmlClass, List<String> methods) {
List<String> vResult = Lists.newArrayList();
Class<?> cls = xmlClass.getSupportClass();
while (cls != null) {
for (String im : methods) {
Pattern pattern = Pattern.compile(methodName(im));
Method[] allMethods = ReflectionHelper.getLocalMethods(cls);
for (Method m : allMethods) {
if (pattern.matcher(m.getName()).matches()) {
vResult.add(makeMethodName(m.getDeclaringClass().getName(), m.getName()));
}
}
}
cls = cls.getSuperclass();
}
return vResult;
}
private static String methodName(String methodName) {
if (methodName.contains("\\$")) {
return methodName;
}
return methodName.replaceAll("\\Q$\\E", QUOTED_DOLLAR);
}
private static String makeMethodName(String className, String methodName) {
return className + "." + methodName;
}
private static void checkMethod(Class<?> c, String methodName) {
Pattern p = Pattern.compile(methodName);
for (Method m : c.getMethods()) {
if (p.matcher(m.getName()).matches()) {
return;
}
}
Utils.log("Warning", 2, "The regular expression \"" + methodName + "\" didn't match any" +
" method in class " + c.getName());
}
public void setXmlClasses(List<XmlClass> classes) {
m_classes = classes;
for (XmlClass c : classes) {
for (XmlInclude m : c.getIncludedMethods()) {
checkMethod(c.getSupportClass(), m.getName());
String methodName = makeMethodName(c.getName(), m.getName());
m_includedMethods.put(methodName, m);
}
}
}
public Map<String, String> getExcludedGroups() {
return m_excludedGroups;
}
public Map<String, String> getIncludedGroups() {
return m_includedGroups;
}
public void setExcludedGroups(Map<String, String> excludedGroups) {
m_excludedGroups = excludedGroups;
}
public void setIncludedGroups(Map<String, String> includedGroups) {
m_includedGroups = includedGroups;
}
private static boolean isIncluded(Collection<String> includedGroups, boolean noGroupsSpecified, String... groups) {
if (noGroupsSpecified) {
return isMemberOf(includedGroups, groups);
}
return includedGroups.isEmpty() || isMemberOf(includedGroups, groups);
}
private static boolean isExcluded(Collection<String> excludedGroups, String... groups) {
return isMemberOf(excludedGroups, groups);
}
private static boolean isMemberOf(Collection<String> list, String... groups) {
for (String group : groups) {
for (String o : list) {
String regexpStr = methodName(o);
if (Pattern.matches(regexpStr, group)) {
return true;
}
}
}
return false;
}
private static void log(String s) {
Utils.log("XmlMethodSelector", 4, s);
}
public void setExpression(String expression) {
m_expression = expression;
}
@Override
public void setTestMethods(List<ITestNGMethod> testMethods) {
m_testMethods = testMethods;
}
private void init(IMethodSelectorContext context) {
String[] groups = m_includedGroups.keySet().toArray(new String[m_includedGroups.size()]);
Set<String> groupClosure = new HashSet<>();
Set<ITestNGMethod> methodClosure = new HashSet<>();
List<ITestNGMethod> includedMethods = Lists.newArrayList();
for (ITestNGMethod m : m_testMethods) {
if (includeMethod(context, m, true)) {
includedMethods.add(m);
}
}
MethodGroupsHelper.findGroupTransitiveClosure(this, includedMethods, m_testMethods,
groups, groupClosure, methodClosure);
if (!m_includedGroups.isEmpty()) {
for (String g : groupClosure) {
log("Including group "
+ (m_includedGroups.containsKey(g) ?
": " : "(implicitly): ") + g);
m_includedGroups.put(g, g);
}
for (ITestNGMethod m : methodClosure) {
String methodName = m.getQualifiedName();
XmlInclude xi = new XmlInclude(methodName);
m_includedMethods.put(methodName, xi);
logInclusion("Including", "method ", methodName);
}
}
}
}