forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FactoryMethod.java
244 lines (228 loc) · 8.87 KB
/
FactoryMethod.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package org.testng.internal;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.testng.DataProviderHolder;
import org.testng.IDataProviderInterceptor;
import org.testng.IDataProviderListener;
import org.testng.IInstanceInfo;
import org.testng.ITestContext;
import org.testng.ITestMethodFinder;
import org.testng.ITestNGListener;
import org.testng.ITestNGMethod;
import org.testng.ITestObjectFactory;
import org.testng.TestNGException;
import org.testng.annotations.IFactoryAnnotation;
import org.testng.annotations.IListenersAnnotation;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.internal.annotations.IAnnotationFinder;
import org.testng.xml.XmlTest;
/** This class represents a method annotated with @Factory */
public class FactoryMethod extends BaseTestMethod {
private final IFactoryAnnotation factoryAnnotation;
private final Object m_instance;
private final ITestContext m_testContext;
private String m_factoryCreationFailedMessage = null;
private final DataProviderHolder holder;
public String getFactoryCreationFailedMessage() {
return m_factoryCreationFailedMessage;
}
private void init(Object instance, IAnnotationFinder annotationFinder, ConstructorOrMethod com) {
IListenersAnnotation annotation =
annotationFinder.findAnnotation(com.getDeclaringClass(), IListenersAnnotation.class);
if (annotation == null) {
return;
}
Class<? extends ITestNGListener>[] listeners = annotation.getValue();
for (Class<? extends ITestNGListener> listener : listeners) {
Object obj = instance;
if (obj == null) {
try {
obj = m_objectFactory.newInstance(listener);
} catch (TestNGException e) {
// TODO log
}
}
if (obj != null) {
if (IDataProviderListener.class.isAssignableFrom(obj.getClass())) {
holder.addListener((IDataProviderListener) obj);
}
if (IDataProviderInterceptor.class.isAssignableFrom(obj.getClass())) {
holder.addInterceptor((IDataProviderInterceptor) obj);
}
}
}
}
// This constructor is intentionally created with package visibility because we dont have any
// callers of this
// constructor outside of this package.
FactoryMethod(
ConstructorOrMethod com,
IObject.IdentifiableObject identifiable,
IAnnotationFinder annotationFinder,
ITestContext testContext,
ITestObjectFactory objectFactory,
DataProviderHolder holder) {
super(objectFactory, com.getName(), com, annotationFinder, identifiable);
this.holder = holder;
Object instance = IObject.IdentifiableObject.unwrap(identifiable);
init(instance, annotationFinder, com);
Utils.checkInstanceOrStatic(instance, com.getMethod());
Utils.checkReturnType(com.getMethod(), Object[].class, IInstanceInfo[].class);
Class<?> declaringClass = com.getDeclaringClass();
if (instance != null && !declaringClass.isAssignableFrom(instance.getClass())) {
if (instance instanceof IParameterInfo) {
instance = ((IParameterInfo) instance).getInstance();
}
Class<?> cls = instance.getClass();
String msg =
"Found a default constructor and also a Factory method when working with "
+ declaringClass.getName()
+ ". Root cause: Mismatch between instance/method classes:["
+ cls.getName()
+ "] ["
+ declaringClass.getName()
+ "]";
throw new TestNGException(msg);
}
if (instance == null
&& com.getMethod() != null
&& !Modifier.isStatic(com.getMethod().getModifiers())) {
throw new TestNGException(
"An inner factory method MUST be static. But '"
+ com.getMethod().getName()
+ "' from '"
+ declaringClass.getName()
+ "' is not.");
}
if (com.getMethod() != null && !Modifier.isPublic(com.getMethod().getModifiers())) {
try {
com.getMethod().setAccessible(true);
} catch (SecurityException e) {
throw new TestNGException(com.getMethod().getName() + " must be public", e);
}
}
factoryAnnotation = annotationFinder.findAnnotation(com, IFactoryAnnotation.class);
m_instance = getInstance();
m_testContext = testContext;
NoOpTestClass tc = new NoOpTestClass();
tc.setTestClass(declaringClass);
m_testClass = tc;
m_groups =
getAllGroups(
objectFactory, declaringClass, testContext.getCurrentXmlTest(), annotationFinder);
}
private static String[] getAllGroups(
ITestObjectFactory objectFactory,
Class<?> declaringClass,
XmlTest xmlTest,
IAnnotationFinder annotationFinder) {
// Find the groups of the factory => all groups of all test methods
ITestMethodFinder testMethodFinder =
new TestNGMethodFinder(objectFactory, new RunInfo(() -> xmlTest), annotationFinder);
ITestNGMethod[] testMethods = testMethodFinder.getTestMethods(declaringClass, xmlTest);
Set<String> groups = new HashSet<>();
for (ITestNGMethod method : testMethods) {
groups.addAll(Arrays.asList(method.getGroups()));
}
return groups.toArray(new String[0]);
}
public org.testng.IParameterInfo[] invoke() {
List<org.testng.IParameterInfo> result = Lists.newArrayList();
Map<String, String> allParameterNames = Maps.newHashMap();
Parameters.MethodParameters methodParameters =
new Parameters.MethodParameters(
m_testContext.getCurrentXmlTest().getAllParameters(),
findMethodParameters(m_testContext.getCurrentXmlTest()),
null,
null,
m_testContext,
null /* testResult */);
Iterator<Object[]> parameterIterator =
Parameters.handleParameters(
m_objectFactory,
this,
allParameterNames,
m_instance,
methodParameters,
m_testContext.getCurrentXmlTest().getSuite(),
m_annotationFinder,
null /* fedInstance */,
this.holder,
"@Factory")
.parameters;
try {
List<Integer> indices = factoryAnnotation.getIndices();
int position = 0;
AtomicInteger counter = new AtomicInteger(0);
while (parameterIterator.hasNext()) {
Object[] parameters = parameterIterator.next();
if (parameters == null) {
// skipped value
continue;
}
ConstructorOrMethod com = getConstructorOrMethod();
if (com.getMethod() != null) {
Object[] testInstances = (Object[]) com.getMethod().invoke(m_instance, parameters);
if (testInstances == null) {
testInstances = new Object[] {};
}
if (testInstances.length == 0) {
this.m_factoryCreationFailedMessage =
String.format(
"The Factory method %s.%s() should have produced at-least one instance.",
com.getDeclaringClass().getName(), com.getName());
}
if (indices == null || indices.isEmpty()) {
final int instancePosition = position;
result.addAll(
Arrays.stream(testInstances)
.map(
instance ->
new ParameterInfo(
instance, instancePosition, parameters, counter.getAndIncrement()))
.collect(Collectors.toList()));
} else {
for (Integer index : indices) {
int i = index - position;
if (i >= 0 && i < testInstances.length) {
result.add(
new ParameterInfo(
testInstances[i], position, parameters, counter.getAndIncrement()));
}
}
}
position += testInstances.length;
} else {
if (indices == null || indices.isEmpty() || indices.contains(position)) {
Object instance = m_objectFactory.newInstance(com.getConstructor(), parameters);
result.add(
new ParameterInfo(instance, position, parameters, counter.getAndIncrement()));
}
position++;
}
}
} catch (Throwable t) {
ConstructorOrMethod com = getConstructorOrMethod();
throw new TestNGException(
"The factory method "
+ com.getDeclaringClass()
+ "."
+ com.getName()
+ "() threw an exception",
t);
}
return result.toArray(new IParameterInfo[0]);
}
@Override
public ITestNGMethod clone() {
throw new IllegalStateException("clone is not supported for FactoryMethod");
}
}