org.testng.internal.FactoryMethod Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testng Show documentation
Show all versions of testng Show documentation
A testing framework for the JVM
package org.testng.internal;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.testng.IDataProviderListener;
import org.testng.IInstanceInfo;
import org.testng.IObjectFactory;
import org.testng.IObjectFactory2;
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 final ITestObjectFactory objectFactory;
private final Map, IDataProviderListener> m_dataProviderListeners;
/**
* @deprecated - This constructor is un-used within TestNG and hence stands deprecated as of TestNG v6.13
*/
@Deprecated
@SuppressWarnings("unused")
public FactoryMethod(ConstructorOrMethod com, Object instance, XmlTest xmlTest, IAnnotationFinder annotationFinder,
ITestContext testContext, ITestObjectFactory objectFactory) {
this(com, instance, annotationFinder, testContext, objectFactory,
Collections., IDataProviderListener>emptyMap());
}
@SuppressWarnings("unchecked")
private void init(Object instance, IAnnotationFinder annotationFinder, ConstructorOrMethod com) {
IListenersAnnotation annotation = annotationFinder.findAnnotation(com.getDeclaringClass(), IListenersAnnotation.class);
if (annotation == null) {
return;
}
Class[] listeners = annotation.getValue();
for (Class listener : listeners) {
if (! IDataProviderListener.class.isAssignableFrom(listener)) {
continue;
}
Class key = (Class) listener;
if (m_dataProviderListeners.containsKey(key)){
continue;
}
if (instance != null && IDataProviderListener.class.isAssignableFrom(instance.getClass())) {
m_dataProviderListeners.put(key, (IDataProviderListener) instance);
continue;
}
Object object = ClassHelper.newInstanceOrNull(listener);
if (object != null) {
m_dataProviderListeners.put(key, (IDataProviderListener) object);
}
}
}
//This constructor is intentionally created with package visibility because we dont have any callers of this
//constructor outside of this package.
FactoryMethod(ConstructorOrMethod com, Object instance, IAnnotationFinder annotationFinder,
ITestContext testContext, ITestObjectFactory objectFactory,
Map, IDataProviderListener> dataProviderListeners) {
super(com.getName(), com, annotationFinder, instance);
m_dataProviderListeners = dataProviderListeners;
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())) {
throw new TestNGException("Mismatch between instance/method classes:"
+ instance.getClass() + " " + declaringClass);
}
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 = instance;
m_testContext = testContext;
NoOpTestClass tc = new NoOpTestClass();
tc.setTestClass(declaringClass);
m_testClass = tc;
this.objectFactory = objectFactory;
m_groups = getAllGroups(declaringClass, testContext.getCurrentXmlTest(), annotationFinder);
}
/**
* @deprecated - This constructor is un-used within TestNG and hence stands deprecated as of TestNG v6.13
*/
@Deprecated
@SuppressWarnings("unused")
public FactoryMethod(ConstructorOrMethod com, Object instance, XmlTest xmlTest, IAnnotationFinder annotationFinder,
ITestContext testContext, ITestObjectFactory objectFactory,
Map, IDataProviderListener> dataProviderListeners) {
this(com, instance, annotationFinder, testContext, objectFactory, dataProviderListeners);
}
private static String[] getAllGroups(Class declaringClass, XmlTest xmlTest,
IAnnotationFinder annotationFinder) {
// Find the groups of the factory => all groups of all test methods
ITestMethodFinder testMethodFinder = new TestNGMethodFinder(new RunInfo(), annotationFinder);
ITestNGMethod[] testMethods = testMethodFinder.getTestMethods(declaringClass, xmlTest);
Set groups = new HashSet<>();
for (ITestNGMethod method : testMethods) {
groups.addAll(Arrays.asList(method.getGroups()));
}
return groups.toArray(new String[groups.size()]);
}
public Object[] invoke() {
List