Code with Finding: |
class MethodInheritance {
/**
* Fix the methodsDependedUpon to make sure that @Configuration methods
* respect inheritance (before methods are invoked in the order Base first
* and after methods are invoked in the order Child first)
*
* @param methods the list of methods
* @param before true if we are handling a before method (meaning, the methods
* need to be sorted base class first and subclass last). false otherwise (subclass
* methods first, base classes last).
*/
public static void fixMethodInheritance(ITestNGMethod[] methods, boolean before) {
// Map of classes -> List of methods that belong to this class or same hierarchy
Map<Class, List<ITestNGMethod>> map = Maps.newHashMap();
//
// Put the list of methods in their hierarchy buckets
//
for (ITestNGMethod method : methods) {
Class< ? extends ITestNGMethod> methodClass = method.getRealClass();
List<ITestNGMethod> l = findMethodListSuperClass(map, methodClass);
if (null != l) {
l.add(method);
}
else {
Class subClass = findSubClass(map, methodClass);
if (null != subClass) {
l = map.get(subClass);
l.add(method);
map.remove(subClass);
map.put(methodClass, l);
}
else {
l = Lists.newArrayList();
l.add(method);
map.put(methodClass, l);
}
}
}
//
// Each bucket that has a list bigger than one element gets sorted
//
for (List<ITestNGMethod> l : map.values()) {
if (l.size() > 1) {
// Sort them
sortMethodsByInheritance(l, before);
/*
* Set methodDependedUpon accordingly
* E.g. Base class can have multiple @BeforeClass methods. Need to ensure
* that @BeforeClass methods in derived class depend on all @BeforeClass methods
* of base class. Vice versa for @AfterXXX methods
*/
for (int i = 0; i < l.size() - 1; i++) {
ITestNGMethod m1 = l.get(i);
for (int j = i + 1; j < l.size(); j++) {
ITestNGMethod m2 = l.get(j);
if (!equalsEffectiveClass(m1, m2) && !dependencyExists(m1, m2, methods)) {
Utils.log("MethodInheritance", 4, m2 + " DEPENDS ON " + m1);
m2.addMethodDependedUpon(MethodHelper.calculateMethodCanonicalName(m1));
}
}
}
}
}
}
}
|