/
MethodInheritance.java
executable file
·178 lines (157 loc) · 5.21 KB
/
MethodInheritance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package org.testng.internal;
import org.testng.ITestNGMethod;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class MethodInheritance {
/**
* Look in map for a class that is a superclass of methodClass
* @param map
* @param methodClass
* @return
*/
private static List<ITestNGMethod> findMethodListSuperClass(Map<Class, List<ITestNGMethod>> map,
Class< ? extends ITestNGMethod> methodClass)
{
for (Class cls : map.keySet()) {
if (cls.isAssignableFrom(methodClass)) {
return map.get(cls);
}
}
return null;
}
/**
* Look in map for a class that is a subclass of methodClass
* @param map
* @param methodClass
* @return
*/
private static Class findSubClass(Map<Class, List<ITestNGMethod>> map,
Class< ? extends ITestNGMethod> methodClass)
{
for (Class cls : map.keySet()) {
if (methodClass.isAssignableFrom(cls)) {
return cls;
}
}
return null;
}
/**
* 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
* @param baseClassToChild
*/
public static void fixMethodInheritance(ITestNGMethod[] methods, boolean baseClassToChild) {
// 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, baseClassToChild);
// Set methodDependedUpon accordingly
if (baseClassToChild) {
for (int i = 1; i < l.size(); i++) {
ITestNGMethod m1 = l.get(i - 1);
ITestNGMethod m2 = l.get(i);
if (!equalsEffectiveClass(m1, m2) && !dependencyExists(m1, m2, methods)) {
Utils.log("MethodInheritance", 4, m2 + " DEPENDS ON " + m1);
m2.addMethodDependedUpon(MethodHelper.calculateMethodCanonicalName(m1));
}
}
}
else {
for (int i = 0; i < l.size() - 1; i++) {
ITestNGMethod m1 = l.get(i);
ITestNGMethod m2 = l.get(i + 1);
if (!equalsEffectiveClass(m1, m2) && !dependencyExists(m1, m2, methods)) {
m2.addMethodDependedUpon(MethodHelper.calculateMethodCanonicalName(m1));
Utils.log("MethodInheritance", 4, m2 + " DEPENDS ON " + m1);
}
}
}
}
}
}
private static boolean dependencyExists(ITestNGMethod m1, ITestNGMethod m2, ITestNGMethod[] methods) {
return true == internalDependencyExists(m1, m2, methods)
? true : internalDependencyExists(m2, m1, methods);
}
private static boolean internalDependencyExists(ITestNGMethod m1, ITestNGMethod m2, ITestNGMethod[] methods) {
ITestNGMethod[] methodsNamed =
MethodHelper.findMethodsNamed(m1, methods, m1.getMethodsDependedUpon());
for (ITestNGMethod method : methodsNamed) {
if (method.equals(m2)) {
return true;
}
}
for (String group : m1.getGroupsDependedUpon()) {
ITestNGMethod[] methodsThatBelongToGroup =
MethodHelper.findMethodsThatBelongToGroup(m1, methods, group);
for (ITestNGMethod method : methodsThatBelongToGroup) {
if (method.equals(m2)) {
return true;
}
}
}
return false;
}
private static boolean equalsEffectiveClass(ITestNGMethod m1, ITestNGMethod m2) {
try {
Class c1 = m1.getRealClass();
Class c2 = m2.getRealClass();
boolean isEqual = c1 == null ? c2 == null : c1.equals(c2);
return isEqual; // && m1.getMethod().equals(m2.getMethod());
}
catch(Exception ex) {
return false;
}
}
/**
* Given a list of methods belonging to the same class hierarchy, orders them
* from the base class to the child (if true) or from child to base class (if false)
* @param methods
*/
private static void sortMethodsByInheritance(List<ITestNGMethod> methods,
boolean baseClassToChild)
{
Collections.sort(methods);
if (! baseClassToChild) {
Collections.reverse(methods);
}
}
private static void ppp(String s) {
System.out.println("[MethodInheritance] " + s);
}
}