[testng] 174/355: Allow Guice injection in external DataProvider
Eugene Zhukov
eugene-guest at moszumanska.debian.org
Tue Aug 18 10:20:01 UTC 2015
This is an automated email from the git hooks/post-receive script.
eugene-guest pushed a commit to annotated tag OpenBSD
in repository testng.
commit 9270c7a2b3898b4cb6048a69fc958530ab2fd825
Author: Julien Herr <julien.herr at alcatel-lucent.com>
Date: Tue Apr 7 15:59:54 2015 +0200
Allow Guice injection in external DataProvider
---
src/main/java/org/testng/ITestContext.java | 3 +-
src/main/java/org/testng/TestClass.java | 3 ++
src/main/java/org/testng/TestRunner.java | 53 +++++++++++++++++++++-
src/main/java/org/testng/internal/ClassImpl.java | 35 +-------------
src/main/java/org/testng/internal/Parameters.java | 28 ++++++++----
.../java/test/dataprovider/InjectionProvider.java | 19 ++++++++
.../dataprovider/StaticDataProviderSampleTest.java | 18 ++++++++
7 files changed, 113 insertions(+), 46 deletions(-)
diff --git a/src/main/java/org/testng/ITestContext.java b/src/main/java/org/testng/ITestContext.java
index ccb68cf..fe6f35d 100644
--- a/src/main/java/org/testng/ITestContext.java
+++ b/src/main/java/org/testng/ITestContext.java
@@ -3,6 +3,7 @@ package org.testng;
import com.google.inject.Injector;
import com.google.inject.Module;
+import org.testng.internal.ClassImpl;
import org.testng.xml.XmlTest;
import java.util.Collection;
@@ -118,8 +119,8 @@ public interface ITestContext extends IAttributes {
public XmlTest getCurrentXmlTest();
public List<Module> getGuiceModules(Class<? extends Module> cls);
- public void addGuiceModule(Class<? extends Module> cls, Module module);
public Injector getInjector(List<Module> moduleInstances);
+ Injector getInjector(IClass iClass);
public void addInjector(List<Module> moduleInstances, Injector injector);
}
diff --git a/src/main/java/org/testng/TestClass.java b/src/main/java/org/testng/TestClass.java
index 3f60a30..213ce1a 100755
--- a/src/main/java/org/testng/TestClass.java
+++ b/src/main/java/org/testng/TestClass.java
@@ -244,4 +244,7 @@ class TestClass extends NoOpTestClass implements ITestClass {
.toString();
}
+ public IClass getIClass() {
+ return m_iClass;
+ }
}
\ No newline at end of file
diff --git a/src/main/java/org/testng/TestRunner.java b/src/main/java/org/testng/TestRunner.java
index 49daa5f..e44dc29 100644
--- a/src/main/java/org/testng/TestRunner.java
+++ b/src/main/java/org/testng/TestRunner.java
@@ -1,5 +1,6 @@
package org.testng;
+import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -12,11 +13,13 @@ import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
+import org.testng.annotations.Guice;
import org.testng.collections.ListMultiMap;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.internal.Attributes;
import org.testng.internal.ClassHelper;
+import org.testng.internal.ClassImpl;
import org.testng.internal.ClassInfoMap;
import org.testng.internal.ConfigurationGroupMethods;
import org.testng.internal.Constants;
@@ -37,6 +40,7 @@ import org.testng.internal.TestNGClassFinder;
import org.testng.internal.TestNGMethodFinder;
import org.testng.internal.Utils;
import org.testng.internal.XmlMethodSelector;
+import org.testng.internal.annotations.AnnotationHelper;
import org.testng.internal.annotations.IAnnotationFinder;
import org.testng.internal.annotations.IListeners;
import org.testng.internal.annotations.Sets;
@@ -1592,8 +1596,7 @@ public class TestRunner
return result;
}
- @Override
- public void addGuiceModule(Class<? extends Module> cls, Module module) {
+ private void addGuiceModule(Class<? extends Module> cls, Module module) {
m_guiceModules.put(cls, module);
}
@@ -1605,6 +1608,52 @@ public class TestRunner
}
@Override
+ public Injector getInjector(IClass iClass) {
+ Annotation annotation = AnnotationHelper.findAnnotationSuperClasses(Guice.class, iClass.getRealClass());
+ if (annotation == null) return null;
+ if (iClass instanceof TestClass) {
+ iClass = ((TestClass)iClass).getIClass();
+ }
+ if (!(iClass instanceof ClassImpl)) return null;
+ Injector parentInjector = ((ClassImpl)iClass).getParentInjector();
+
+ Guice guice = (Guice) annotation;
+ List<Module> moduleInstances = Lists.newArrayList(getModules(guice, parentInjector, iClass.getRealClass()));
+
+ // Reuse the previous injector, if any
+ Injector injector = getInjector(moduleInstances);
+ if (injector == null) {
+ injector = parentInjector.createChildInjector(moduleInstances);
+ addInjector(moduleInstances, injector);
+ }
+ return injector;
+ }
+
+ private Module[] getModules(Guice guice, Injector parentInjector, Class<?> testClass) {
+ List<Module> result = Lists.newArrayList();
+ for (Class<? extends Module> moduleClass : guice.modules()) {
+ List<Module> modules = getGuiceModules(moduleClass);
+ if (modules != null && modules.size() > 0) {
+ result.addAll(modules);
+ } else {
+ Module instance = parentInjector.getInstance(moduleClass);
+ result.add(instance);
+ addGuiceModule(moduleClass, instance);
+ }
+ }
+ Class<? extends IModuleFactory> factory = guice.moduleFactory();
+ if (factory != IModuleFactory.class) {
+ IModuleFactory factoryInstance = parentInjector.getInstance(factory);
+ Module moduleClass = factoryInstance.createModule(this, testClass);
+ if (moduleClass != null) {
+ result.add(moduleClass);
+ }
+ }
+
+ return result.toArray(new Module[result.size()]);
+ }
+
+ @Override
public void addInjector(List<Module> moduleInstances, Injector injector) {
m_injectors.put(moduleInstances, injector);
}
diff --git a/src/main/java/org/testng/internal/ClassImpl.java b/src/main/java/org/testng/internal/ClassImpl.java
index fc19eaa..ddf05f5 100755
--- a/src/main/java/org/testng/internal/ClassImpl.java
+++ b/src/main/java/org/testng/internal/ClassImpl.java
@@ -130,17 +130,8 @@ public class ClassImpl implements IClass {
private Object getInstanceFromGuice() {
Annotation annotation = AnnotationHelper.findAnnotationSuperClasses(Guice.class, m_class);
if (annotation == null) return null;
- Injector parentInjector = getParentInjector();
- Guice guice = (Guice) annotation;
- List<Module> moduleInstances = Lists.newArrayList(getModules(guice, parentInjector, m_class));
-
- // Reuse the previous injector, if any
- Injector injector = m_testContext.getInjector(moduleInstances);
- if (injector == null) {
- injector = parentInjector.createChildInjector(moduleInstances);
- m_testContext.addInjector(moduleInstances, injector);
- }
+ Injector injector = m_testContext.getInjector(this);
return injector.getInstance(m_class);
}
@@ -173,30 +164,6 @@ public class ClassImpl implements IClass {
}
}
- private Module[] getModules(Guice guice, Injector parentInejctor, Class<?> testClass) {
- List<Module> result = Lists.newArrayList();
- for (Class<? extends Module> moduleClass : guice.modules()) {
- List<Module> modules = m_testContext.getGuiceModules(moduleClass);
- if (modules != null && modules.size() > 0) {
- result.addAll(modules);
- } else {
- Module instance = parentInejctor.getInstance(moduleClass);
- result.add(instance);
- m_testContext.addGuiceModule(moduleClass, instance);
- }
- }
- Class<? extends IModuleFactory> factory = guice.moduleFactory();
- if (factory != IModuleFactory.class) {
- IModuleFactory factoryInstance = parentInejctor.getInstance(factory);
- Module moduleClass = factoryInstance.createModule(m_testContext, testClass);
- if (moduleClass != null) {
- result.add(moduleClass);
- }
- }
-
- return result.toArray(new Module[result.size()]);
- }
-
@Override
public Object[] getInstances(boolean create) {
Object[] result = {};
diff --git a/src/main/java/org/testng/internal/Parameters.java b/src/main/java/org/testng/internal/Parameters.java
index e05fb19..16548bb 100755
--- a/src/main/java/org/testng/internal/Parameters.java
+++ b/src/main/java/org/testng/internal/Parameters.java
@@ -1,5 +1,7 @@
package org.testng.internal;
+import com.google.inject.Injector;
+
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -10,6 +12,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import org.testng.ITestClass;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
@@ -253,9 +256,9 @@ public class Parameters {
return result;
}
- private static DataProviderHolder findDataProvider(Object instance, Class clazz,
+ private static DataProviderHolder findDataProvider(Object instance, ITestClass clazz,
ConstructorOrMethod m,
- IAnnotationFinder finder) {
+ IAnnotationFinder finder, ITestContext context) {
DataProviderHolder result = null;
IDataProvidable dp = findDataProviderInfo(clazz, m, finder);
@@ -264,7 +267,7 @@ public class Parameters {
Class dataProviderClass = dp.getDataProviderClass();
if (! Utils.isStringEmpty(dataProviderName)) {
- result = findDataProvider(instance, clazz, finder, dataProviderName, dataProviderClass);
+ result = findDataProvider(instance, clazz, finder, dataProviderName, dataProviderClass, context);
if(null == result) {
throw new TestNGException("Method " + m + " requires a @DataProvider named : "
@@ -281,7 +284,7 @@ public class Parameters {
* Find the data provider info (data provider name and class) on either @Test(dataProvider),
* @Factory(dataProvider) on a method or @Factory(dataProvider) on a constructor.
*/
- private static IDataProvidable findDataProviderInfo(Class clazz, ConstructorOrMethod m,
+ private static IDataProvidable findDataProviderInfo(ITestClass clazz, ConstructorOrMethod m,
IAnnotationFinder finder) {
IDataProvidable result;
@@ -299,7 +302,7 @@ public class Parameters {
if (result == null) {
//
// @Test(dataProvider) on a class
- result = AnnotationHelper.findTest(finder, clazz);
+ result = AnnotationHelper.findTest(finder, clazz.getRealClass());
}
} else {
//
@@ -314,12 +317,16 @@ public class Parameters {
/**
* Find a method that has a @DataProvider(name=name)
*/
- private static DataProviderHolder findDataProvider(Object instance, Class cls,
+ private static DataProviderHolder findDataProvider(Object instance, ITestClass clazz,
IAnnotationFinder finder,
- String name, Class dataProviderClass)
+ String name, Class dataProviderClass,
+ ITestContext context)
{
DataProviderHolder result = null;
+ Injector injector = context.getInjector(clazz);
+
+ Class cls = clazz.getRealClass();
boolean shouldBeStatic = false;
if (dataProviderClass != null) {
cls = dataProviderClass;
@@ -331,6 +338,9 @@ public class Parameters {
if (null != dp && name.equals(getDataProviderName(dp, m))) {
if (shouldBeStatic && (m.getModifiers() & Modifier.STATIC) == 0) {
instance = ClassHelper.newInstance(dataProviderClass);
+ if (injector != null) {
+ injector.injectMembers(instance);
+ }
}
if (result != null) {
@@ -419,8 +429,8 @@ public class Parameters {
* sets of parameters for this method
*/
DataProviderHolder dataProviderHolder =
- findDataProvider(instance, testMethod.getTestClass().getRealClass(),
- testMethod.getConstructorOrMethod(), annotationFinder);
+ findDataProvider(instance, testMethod.getTestClass(),
+ testMethod.getConstructorOrMethod(), annotationFinder, methodParams.context);
if (null != dataProviderHolder) {
int parameterCount = testMethod.getConstructorOrMethod().getParameterTypes().length;
diff --git a/src/test/java/test/dataprovider/InjectionProvider.java b/src/test/java/test/dataprovider/InjectionProvider.java
new file mode 100644
index 0000000..1923c71
--- /dev/null
+++ b/src/test/java/test/dataprovider/InjectionProvider.java
@@ -0,0 +1,19 @@
+package test.dataprovider;
+
+import com.google.inject.Inject;
+import com.google.inject.name.Named;
+
+import org.testng.annotations.DataProvider;
+
+public class InjectionProvider {
+
+ @Inject @Named("test")
+ private String value;
+
+ @DataProvider(name = "injection")
+ public Object[][] create() {
+ return new Object[][] {
+ new Object[] { value },
+ };
+ }
+}
diff --git a/src/test/java/test/dataprovider/StaticDataProviderSampleTest.java b/src/test/java/test/dataprovider/StaticDataProviderSampleTest.java
index 64e5bf1..c761c34 100644
--- a/src/test/java/test/dataprovider/StaticDataProviderSampleTest.java
+++ b/src/test/java/test/dataprovider/StaticDataProviderSampleTest.java
@@ -1,8 +1,13 @@
package test.dataprovider;
+import com.google.inject.AbstractModule;
+import com.google.inject.name.Names;
+
import org.testng.Assert;
+import org.testng.annotations.Guice;
import org.testng.annotations.Test;
+ at Guice(modules = StaticDataProviderSampleTest.InjectionProviderModule.class)
public class StaticDataProviderSampleTest {
@Test(dataProvider = "static", dataProviderClass = StaticProvider.class)
@@ -14,4 +19,17 @@ public class StaticDataProviderSampleTest {
public void verifyExternal(String s) {
Assert.assertEquals(s, "Cedric");
}
+
+ @Test(dataProvider = "injection", dataProviderClass = InjectionProvider.class)
+ public void verifyInjection(String s) {
+ Assert.assertEquals(s, "Cedric");
+ }
+
+ public static class InjectionProviderModule extends AbstractModule {
+
+ @Override
+ protected void configure() {
+ bind(String.class).annotatedWith(Names.named("test")).toInstance("Cedric");
+ }
+ }
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/testng.git
More information about the pkg-java-commits
mailing list