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
|
package org.testng.internal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.TestNGException;
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
*
* @author <a href="mailto:cedric@beust.com">Cedric Beust</a>
*/
public class FactoryMethod extends BaseTestMethod {
/**
*
*/
private static final long serialVersionUID = -7329918821346197099L;
private Object m_instance = null;
private XmlTest m_xmlTest = null;
private ITestContext m_testContext = null;
public FactoryMethod(ConstructorOrMethod com,
Object instance,
XmlTest xmlTest,
IAnnotationFinder annotationFinder,
ITestContext testContext)
{
super(com.getName(), com, annotationFinder, instance);
// Utils.checkInstanceOrStatic(instance, method);
Class<?> declaringClass = com.getDeclaringClass();
if (instance != null && ! declaringClass.isAssignableFrom(instance.getClass())) {
throw new TestNGException("Mismatch between instance/method classes:"
+ instance.getClass() + " " + declaringClass);
}
m_instance = instance;
m_xmlTest = xmlTest;
m_testContext = testContext;
NoOpTestClass tc = new NoOpTestClass();
tc.setTestClass(declaringClass);
m_testClass = tc;
}
private static void ppp(String s) {
System.out.println("[FactoryMethod] " + s);
}
public Object[] invoke() {
List<Object> result = Lists.newArrayList();
Map<String, String> allParameterNames = Maps.newHashMap();
Iterator<Object[]> parameterIterator =
Parameters.handleParameters(this,
allParameterNames,
m_instance,
new Parameters.MethodParameters(m_xmlTest.getAllParameters(),
findMethodParameters(m_xmlTest),
null, null, m_testContext,
null /* testResult */),
m_xmlTest.getSuite(),
m_annotationFinder,
null /* fedInstance */).parameters;
try {
while (parameterIterator.hasNext()) {
Object[] parameters = parameterIterator.next();
Object[] testInstances;
ConstructorOrMethod com = getConstructorOrMethod();
if (com.getMethod() != null) {
testInstances = (Object[]) getMethod().invoke(m_instance, parameters);
for (Object testInstance : testInstances) {
result.add(testInstance);
}
} else {
Object instance = com.getConstructor().newInstance(parameters);
result.add(instance);
}
}
}
catch (Throwable t) {
ConstructorOrMethod com = getConstructorOrMethod();
throw new TestNGException("The factory method "
+ com.getDeclaringClass() + "." + com.getName()
+ "() threw an exception", t);
}
return result.toArray(new Object[result.size()]);
}
@Override
public ITestNGMethod clone() {
throw new IllegalStateException("clone is not supported for FactoryMethod");
}
}
|