[testng] 159/355: parent-module could receive ITestContext
Eugene Zhukov
eugene-guest at moszumanska.debian.org
Tue Aug 18 10:20:00 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 4ed682a9019c3063c60dea4ff9cba5fcf6482f08
Author: Julien Herr <julien.herr at alcatel-lucent.com>
Date: Wed Mar 11 17:24:57 2015 +0100
parent-module could receive ITestContext
---
src/main/java/org/testng/internal/ClassHelper.java | 13 +++++++++++++
src/main/java/org/testng/internal/ClassImpl.java | 14 ++++++++++++--
src/test/java/test/guice/GuiceParentModule.java | 10 ++++++++++
src/test/java/test/guice/GuiceParentModuleTest.java | 6 ++++++
4 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/testng/internal/ClassHelper.java b/src/main/java/org/testng/internal/ClassHelper.java
index 42b4eb4..f83a195 100644
--- a/src/main/java/org/testng/internal/ClassHelper.java
+++ b/src/main/java/org/testng/internal/ClassHelper.java
@@ -16,6 +16,7 @@ import org.testng.junit.IJUnitTestRunner;
import org.testng.xml.XmlTest;
import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
@@ -66,6 +67,18 @@ public final class ClassHelper {
}
}
+ public static <T> T newInstance(Constructor<T> constructor, Object... parameters) {
+ try {
+ return constructor.newInstance(parameters);
+ } catch (InstantiationException e) {
+ throw new TestNGException("Cannot instantiate class " + constructor.getDeclaringClass().getName(), e);
+ } catch (IllegalAccessException e) {
+ throw new TestNGException("Cannot instantiate class " + constructor.getDeclaringClass().getName(), e);
+ } catch (InvocationTargetException e) {
+ throw new TestNGException("Cannot instantiate class " + constructor.getDeclaringClass().getName(), e);
+ }
+ }
+
/**
* Tries to load the specified class using the context ClassLoader or if none,
* than from the default ClassLoader. This method differs from the standard
diff --git a/src/main/java/org/testng/internal/ClassImpl.java b/src/main/java/org/testng/internal/ClassImpl.java
index a687dce..ca2b923 100755
--- a/src/main/java/org/testng/internal/ClassImpl.java
+++ b/src/main/java/org/testng/internal/ClassImpl.java
@@ -21,6 +21,7 @@ import org.testng.xml.XmlClass;
import org.testng.xml.XmlTest;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
import java.util.List;
import java.util.Map;
@@ -149,11 +150,11 @@ public class ClassImpl implements IClass {
Injector injector = suite.getParentInjector();
if (injector == null) {
if (m_hasParentModule) {
- Class<?> parentModule = ClassHelper.forName(suite.getParentModule());
+ Class<Module> parentModule = (Class<Module>) ClassHelper.forName(suite.getParentModule());
if (parentModule == null) {
throw new TestNGException("Cannot load parent Guice module class: " + parentModule);
}
- Module module = (Module) ClassHelper.newInstance(parentModule);
+ Module module = getModuleConstructor(parentModule);
injector = com.google.inject.Guice.createInjector(module);
} else {
injector = com.google.inject.Guice.createInjector();
@@ -163,6 +164,15 @@ public class ClassImpl implements IClass {
return injector;
}
+ private Module getModuleConstructor(Class<Module> module) {
+ try {
+ Constructor<Module> moduleConstructor = module.getDeclaredConstructor(ITestContext.class);
+ return ClassHelper.newInstance(moduleConstructor, m_testContext);
+ } catch (NoSuchMethodException e) {
+ return ClassHelper.newInstance(module);
+ }
+ }
+
private Module[] getModules(Guice guice, Injector parentInejctor, Class<?> testClass) {
List<Module> result = Lists.newArrayList();
for (Class<? extends Module> moduleClass : guice.modules()) {
diff --git a/src/test/java/test/guice/GuiceParentModule.java b/src/test/java/test/guice/GuiceParentModule.java
index 989438f..96da859 100644
--- a/src/test/java/test/guice/GuiceParentModule.java
+++ b/src/test/java/test/guice/GuiceParentModule.java
@@ -1,13 +1,23 @@
package test.guice;
import com.google.inject.AbstractModule;
+import com.google.inject.Inject;
import com.google.inject.Singleton;
+import org.testng.ITestContext;
+
public class GuiceParentModule extends AbstractModule {
+ private final ITestContext context;
+
+ public GuiceParentModule(ITestContext context) {
+ this.context = context;
+ }
+
@Override
protected void configure() {
bind(MyService.class).toProvider(MyServiceProvider.class);
bind(MyContext.class).to(MyContextImpl.class).in(Singleton.class);
+ bind(ITestContext.class).toInstance(context);
}
}
diff --git a/src/test/java/test/guice/GuiceParentModuleTest.java b/src/test/java/test/guice/GuiceParentModuleTest.java
index 9279961..6b1c0d3 100644
--- a/src/test/java/test/guice/GuiceParentModuleTest.java
+++ b/src/test/java/test/guice/GuiceParentModuleTest.java
@@ -1,6 +1,7 @@
package test.guice;
import org.testng.Assert;
+import org.testng.ITestContext;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
@@ -13,10 +14,15 @@ public class GuiceParentModuleTest {
MySession mySession;
@Inject
MyService myService;
+ @Inject
+ ITestContext context;
public void testService() {
Assert.assertNotNull(myService);
Assert.assertNotNull(mySession);
myService.serve(mySession);
+ Assert.assertNotNull(context);
+ Assert.assertEquals(context.getName(), "Guice");
+ Assert.assertEquals(context.getSuite().getName(), "parent-module-suite");
}
}
--
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