[testng] 04/160: Introduce availability of multiple custom interceptors
Eugene Zhukov
eugene-guest at moszumanska.debian.org
Tue Aug 18 10:22:13 UTC 2015
This is an automated email from the git hooks/post-receive script.
eugene-guest pushed a commit to annotated tag testng-6.9.5
in repository testng.
commit f09e2f96ea603c7711d77775abc3539f9a2954fb
Author: Tomas Remes <tremes at redhat.com>
Date: Mon Apr 27 08:32:44 2015 +0200
Introduce availability of multiple custom interceptors
Conflicts:
src/main/java/org/testng/TestNG.java
src/main/java/org/testng/TestRunner.java
Conflicts:
pom-test.xml
src/main/java/org/testng/TestRunner.java
---
src/main/java/org/testng/SuiteRunner.java | 18 ++++----
src/main/java/org/testng/TestNG.java | 14 +++---
src/main/java/org/testng/TestRunner.java | 50 ++++++++++++++--------
.../java/test/factory/FactoryInterleavingTest.java | 1 -
.../multipleinterceptors/FirstInterceptor.java | 23 ++++++++++
.../multipleinterceptors/FooTest.java | 22 ++++++++++
.../MultipleInterceptorsTest.java | 46 ++++++++++++++++++++
.../multipleinterceptors/SecondInterceptor.java | 22 ++++++++++
.../multipleinterceptors/ThirdInterceptor.java | 22 ++++++++++
.../multipleinterceptors/multiple-interceptors.xml | 15 +++++++
src/test/resources/testng.xml | 1 +
11 files changed, 197 insertions(+), 37 deletions(-)
diff --git a/src/main/java/org/testng/SuiteRunner.java b/src/main/java/org/testng/SuiteRunner.java
index 94d78d4..20b0600 100644
--- a/src/main/java/org/testng/SuiteRunner.java
+++ b/src/main/java/org/testng/SuiteRunner.java
@@ -19,6 +19,7 @@ import org.testng.xml.XmlTest;
import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Method;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -66,7 +67,7 @@ public class SuiteRunner implements ISuite, Serializable, IInvokedMethodListener
transient private ITestObjectFactory m_objectFactory;
transient private Boolean m_skipFailedInvocationCounts = Boolean.FALSE;
- transient private IMethodInterceptor m_methodInterceptor;
+ transient private List<IMethodInterceptor> m_methodInterceptors;
private List<IInvokedMethodListener> m_invokedMethodListeners;
/** The list of all the methods invoked during this run */
@@ -96,7 +97,7 @@ public class SuiteRunner implements ISuite, Serializable, IInvokedMethodListener
boolean useDefaultListeners)
{
this(configuration, suite, outputDir, runnerFactory, useDefaultListeners,
- null /* method interceptor */,
+ new ArrayList<IMethodInterceptor>() /* method interceptor */,
null /* invoked method listeners */,
null /* test listeners */);
}
@@ -106,12 +107,11 @@ public class SuiteRunner implements ISuite, Serializable, IInvokedMethodListener
String outputDir,
ITestRunnerFactory runnerFactory,
boolean useDefaultListeners,
- IMethodInterceptor methodInterceptor,
+ List<IMethodInterceptor> methodInterceptors,
List<IInvokedMethodListener> invokedMethodListeners,
List<ITestListener> testListeners)
{
- init(configuration, suite, outputDir, runnerFactory, useDefaultListeners,
- methodInterceptor, invokedMethodListeners, testListeners);
+ init(configuration, suite, outputDir, runnerFactory, useDefaultListeners, methodInterceptors, invokedMethodListeners, testListeners);
}
private void init(IConfiguration configuration,
@@ -119,7 +119,7 @@ public class SuiteRunner implements ISuite, Serializable, IInvokedMethodListener
String outputDir,
ITestRunnerFactory runnerFactory,
boolean useDefaultListeners,
- IMethodInterceptor methodInterceptor,
+ List<IMethodInterceptor> methodInterceptors,
List<IInvokedMethodListener> invokedMethodListener,
List<ITestListener> testListeners)
{
@@ -127,7 +127,7 @@ public class SuiteRunner implements ISuite, Serializable, IInvokedMethodListener
m_suite = suite;
m_useDefaultListeners = useDefaultListeners;
m_tmpRunnerFactory= runnerFactory;
- m_methodInterceptor = methodInterceptor;
+ m_methodInterceptors = methodInterceptors != null ? methodInterceptors : new ArrayList<IMethodInterceptor>();
setOutputDir(outputDir);
m_objectFactory = m_configuration.getObjectFactory();
if(m_objectFactory == null) {
@@ -161,8 +161,8 @@ public class SuiteRunner implements ISuite, Serializable, IInvokedMethodListener
//
// Install the method interceptor, if any was passed
//
- if (m_methodInterceptor != null) {
- tr.setMethodInterceptor(m_methodInterceptor);
+ for (IMethodInterceptor methodInterceptor : methodInterceptors) {
+ tr.addMethodInterceptor(methodInterceptor);
}
// Reuse the same text reporter so we can accumulate all the results
diff --git a/src/main/java/org/testng/TestNG.java b/src/main/java/org/testng/TestNG.java
index b84d81a..c0e77ff 100644
--- a/src/main/java/org/testng/TestNG.java
+++ b/src/main/java/org/testng/TestNG.java
@@ -709,7 +709,7 @@ public class TestNG {
setAnnotationTransformer((IAnnotationTransformer) listener);
}
if (listener instanceof IMethodInterceptor) {
- setMethodInterceptor((IMethodInterceptor) listener);
+ m_methodInterceptors.add((IMethodInterceptor) listener);
}
if (listener instanceof IInvokedMethodListener) {
addInvokedMethodListener((IInvokedMethodListener) listener);
@@ -775,7 +775,7 @@ public class TestNG {
private Boolean m_skipFailedInvocationCounts = false;
- private IMethodInterceptor m_methodInterceptor = null;
+ private List<IMethodInterceptor> m_methodInterceptors = new ArrayList<IMethodInterceptor>();
/** The list of test names to run from the given suite */
private List<String> m_testNames;
@@ -1300,7 +1300,7 @@ public class TestNG {
m_outputDir,
m_testRunnerFactory,
m_useDefaultListeners,
- m_methodInterceptor,
+ m_methodInterceptors,
m_invokedMethodListeners,
m_testListeners);
@@ -1953,12 +1953,8 @@ public class TestNG {
m_hookable = h;
}
- public void setMethodInterceptor(IMethodInterceptor i) {
- // compare by reference!
- if (m_methodInterceptor != null && m_methodInterceptor != i) {
- LOGGER.warn("MethodInterceptor already set");
- }
- m_methodInterceptor = i;
+ public void setMethodInterceptor(IMethodInterceptor methodInterceptor) {
+ m_methodInterceptors.add(methodInterceptor);
}
public void setDataProviderThreadCount(int count) {
diff --git a/src/main/java/org/testng/TestRunner.java b/src/main/java/org/testng/TestRunner.java
index 2772445..de83c2d 100644
--- a/src/main/java/org/testng/TestRunner.java
+++ b/src/main/java/org/testng/TestRunner.java
@@ -1,6 +1,7 @@
package org.testng;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -140,11 +141,12 @@ public class TestRunner
private String m_host;
// Defined dynamically depending on <test preserve-order="true/false">
- private transient IMethodInterceptor m_methodInterceptor;
+ transient private List<IMethodInterceptor> m_methodInterceptors;
private transient ClassMethodMap m_classMethodMap;
private transient TestNGClassFinder m_testClassFinder;
private transient IConfiguration m_configuration;
+ private IMethodInterceptor builtinInterceptor;
protected TestRunner(IConfiguration configuration,
ISuite suite,
@@ -183,9 +185,10 @@ public class TestRunner
m_skipFailedInvocationCounts = skipFailedInvocationCounts;
setVerbose(test.getVerbose());
+
boolean preserveOrder = "true".equalsIgnoreCase(test.getPreserveOrder());
- m_methodInterceptor = preserveOrder ? new PreserveOrderMethodInterceptor()
- : new InstanceOrderingMethodInterceptor();
+ m_methodInterceptors = new ArrayList<IMethodInterceptor>();
+ builtinInterceptor = preserveOrder ? new PreserveOrderMethodInterceptor() : new InstanceOrderingMethodInterceptor();
m_packageNamesFromXml= test.getXmlPackages();
if(null != m_packageNamesFromXml) {
@@ -329,7 +332,7 @@ public class TestRunner
}
if (listener instanceof IMethodInterceptor) {
- setMethodInterceptor((IMethodInterceptor) listener);
+ m_methodInterceptors.add((IMethodInterceptor) listener);
}
if (listener instanceof ISuiteListener) {
m_suite.addListener((ISuiteListener) listener);
@@ -784,20 +787,24 @@ public class TestRunner
* Apply the method interceptor (if applicable) to the list of methods.
*/
private ITestNGMethod[] intercept(ITestNGMethod[] methods) {
- if (m_methodInterceptor == null) return methods;
+ List<IMethodInstance> methodInstances = methodsToMethodInstances(Arrays.asList(methods));
- IMethodInstance[] instances = methodsToMethodInstances(Arrays.asList(methods));
+ // add built-in interceptor (PreserveOrderMethodInterceptor or InstanceOrderingMethodInterceptor at the end of the list
+ m_methodInterceptors.add(builtinInterceptor);
+ for (IMethodInterceptor m_methodInterceptor : m_methodInterceptors) {
+ methodInstances = m_methodInterceptor.intercept(methodInstances, this);
+ }
- List<IMethodInstance> resultInstances =
- m_methodInterceptor.intercept(Arrays.asList(instances), this);
List<ITestNGMethod> result = Lists.newArrayList();
- for (IMethodInstance imi : resultInstances) {
+ for (IMethodInstance imi : methodInstances) {
result.add(imi.getMethod());
}
+
//Since an interceptor is involved, we would need to ensure that the ClassMethodMap object is in sync with the
//output of the interceptor, else @AfterClass doesn't get executed at all when interceptors are involved.
//so let's update the current classMethodMap object with the list of methods obtained from the interceptor.
this.m_classMethodMap = new ClassMethodMap(result, null);
+
return result.toArray(new ITestNGMethod[result.size()]);
}
@@ -848,7 +855,10 @@ public class TestRunner
//
// Finally, sort the parallel methods by classes
//
- methodInstances = m_methodInterceptor.intercept(methodInstances, this);
+ for (IMethodInterceptor m_methodInterceptor : m_methodInterceptors) {
+ methodInstances = m_methodInterceptor.intercept(methodInstances, this);
+ }
+
Map<String, String> params = m_xmlTest.getAllParameters();
Set<Class<?>> processedClasses = Sets.newHashSet();
@@ -968,12 +978,11 @@ public class TestRunner
return vResult;
}
- private MethodInstance[] methodsToMethodInstances(List<ITestNGMethod> sl) {
- MethodInstance[] result = new MethodInstance[sl.size()];
- for (int i = 0; i < result.length; i++) {
- result[i] = new MethodInstance(sl.get(i));
- }
-
+ private List<IMethodInstance> methodsToMethodInstances(List<ITestNGMethod> sl) {
+ List<IMethodInstance> result = new ArrayList<>();
+ for (ITestNGMethod iTestNGMethod : sl) {
+ result.add(new MethodInstance(iTestNGMethod));
+ }
return result;
}
@@ -1557,8 +1566,13 @@ public class TestRunner
}
}
- public void setMethodInterceptor(IMethodInterceptor methodInterceptor) {
- m_methodInterceptor = methodInterceptor;
+ @Deprecated
+ public void setMethodInterceptor(IMethodInterceptor methodInterceptor){
+ m_methodInterceptors.add(methodInterceptor);
+ }
+
+ public void addMethodInterceptor(IMethodInterceptor methodInterceptor){
+ m_methodInterceptors.add(methodInterceptor);
}
@Override
diff --git a/src/test/java/test/factory/FactoryInterleavingTest.java b/src/test/java/test/factory/FactoryInterleavingTest.java
index 89e8d66..d3a1aaf 100644
--- a/src/test/java/test/factory/FactoryInterleavingTest.java
+++ b/src/test/java/test/factory/FactoryInterleavingTest.java
@@ -7,7 +7,6 @@ import org.testng.collections.Lists;
import test.SimpleBaseTest;
-import java.util.Arrays;
import java.util.List;
public class FactoryInterleavingTest extends SimpleBaseTest {
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/FirstInterceptor.java b/src/test/java/test/methodinterceptors/multipleinterceptors/FirstInterceptor.java
new file mode 100644
index 0000000..f5a0db9
--- /dev/null
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/FirstInterceptor.java
@@ -0,0 +1,23 @@
+package test.methodinterceptors.multipleinterceptors;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.testng.IMethodInstance;
+import org.testng.IMethodInterceptor;
+import org.testng.ITestContext;
+
+public class FirstInterceptor implements IMethodInterceptor{
+ @Override
+ public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
+ MultipleInterceptorsTest.interceptors.add(this.getClass());
+ List<IMethodInstance> result = new ArrayList<IMethodInstance>();
+
+ for (IMethodInstance method : methods) {
+ String name = method.getMethod().getMethodName();
+ if (!name.equals("a")) {
+ result.add(method);
+ }
+ }
+ return result;
+ }
+}
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/FooTest.java b/src/test/java/test/methodinterceptors/multipleinterceptors/FooTest.java
new file mode 100644
index 0000000..9e05fd2
--- /dev/null
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/FooTest.java
@@ -0,0 +1,22 @@
+package test.methodinterceptors.multipleinterceptors;
+
+import org.testng.annotations.Test;
+
+public class FooTest {
+
+ @Test
+ public void a() {
+ }
+
+ @Test
+ public void b() {
+ }
+
+ @Test
+ public void c() {
+ }
+
+ @Test
+ public void d() {
+ }
+}
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/MultipleInterceptorsTest.java b/src/test/java/test/methodinterceptors/multipleinterceptors/MultipleInterceptorsTest.java
new file mode 100644
index 0000000..7211eb5
--- /dev/null
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/MultipleInterceptorsTest.java
@@ -0,0 +1,46 @@
+package test.methodinterceptors.multipleinterceptors;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.testng.Assert;
+import org.testng.TestListenerAdapter;
+import org.testng.TestNG;
+import org.testng.annotations.Test;
+import test.SimpleBaseTest;
+
+public class MultipleInterceptorsTest extends SimpleBaseTest {
+
+ public static List<Class> interceptors = new ArrayList<>();
+
+ @Test
+ public void testMultipleInterceptors(){
+ TestNG tng = create();
+ tng.setTestClasses(new Class[] { FooTest.class });
+ tng.setMethodInterceptor(new ThirdInterceptor());
+ tng.setMethodInterceptor(new FirstInterceptor());
+ tng.setMethodInterceptor(new SecondInterceptor());
+ TestListenerAdapter tla = new TestListenerAdapter();
+ tng.addListener(tla);
+ tng.run();
+ Assert.assertEquals(tla.getPassedTests().size(), 1);
+ Assert.assertEquals(tla.getPassedTests().get(0).getName(), "d");
+ reset();
+ }
+
+ @Test
+ public void testMultipleInterceptorsWithPreserveOrder() {
+ TestNG tng = create();
+ tng.setTestSuites(Arrays.asList("target/test-classes/methodinterceptors/multipleinterceptors/multiple-interceptors.xml"));
+ tng.run();
+ //Assert.assertEquals(interceptors.size(), 3);
+ Assert.assertTrue(interceptors.get(0).equals(FirstInterceptor.class));
+ Assert.assertTrue(interceptors.get(1).equals(SecondInterceptor.class));
+ Assert.assertTrue(interceptors.get(2).equals(ThirdInterceptor.class));
+ reset();
+ }
+
+ private void reset(){
+ interceptors.clear();
+ }
+}
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/SecondInterceptor.java b/src/test/java/test/methodinterceptors/multipleinterceptors/SecondInterceptor.java
new file mode 100644
index 0000000..988aafd
--- /dev/null
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/SecondInterceptor.java
@@ -0,0 +1,22 @@
+package test.methodinterceptors.multipleinterceptors;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.testng.IMethodInstance;
+import org.testng.IMethodInterceptor;
+import org.testng.ITestContext;
+
+public class SecondInterceptor implements IMethodInterceptor{
+ @Override
+ public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
+ List<IMethodInstance> result = new ArrayList<IMethodInstance>();
+ MultipleInterceptorsTest.interceptors.add(this.getClass());
+ for (IMethodInstance method : methods) {
+ String name = method.getMethod().getMethodName();
+ if (!name.equals("b")) {
+ result.add(method);
+ }
+ }
+ return result;
+ }
+}
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/ThirdInterceptor.java b/src/test/java/test/methodinterceptors/multipleinterceptors/ThirdInterceptor.java
new file mode 100644
index 0000000..dba9111
--- /dev/null
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/ThirdInterceptor.java
@@ -0,0 +1,22 @@
+package test.methodinterceptors.multipleinterceptors;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.testng.IMethodInstance;
+import org.testng.IMethodInterceptor;
+import org.testng.ITestContext;
+
+public class ThirdInterceptor implements IMethodInterceptor{
+ @Override
+ public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
+ List<IMethodInstance> result = new ArrayList<IMethodInstance>();
+ MultipleInterceptorsTest.interceptors.add(this.getClass());
+ for (IMethodInstance method : methods) {
+ String name = method.getMethod().getMethodName();
+ if (!name.equals("c")) {
+ result.add(method);
+ }
+ }
+ return result;
+ }
+}
diff --git a/src/test/resources/methodinterceptors/multipleinterceptors/multiple-interceptors.xml b/src/test/resources/methodinterceptors/multipleinterceptors/multiple-interceptors.xml
new file mode 100644
index 0000000..1d13348
--- /dev/null
+++ b/src/test/resources/methodinterceptors/multipleinterceptors/multiple-interceptors.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<suite name="Multiple interceptors">
+
+ <listeners>
+ <listener class-name="test.methodinterceptors.multipleinterceptors.FirstInterceptor"/>
+ <listener class-name="test.methodinterceptors.multipleinterceptors.SecondInterceptor"/>
+ <listener class-name="test.methodinterceptors.multipleinterceptors.ThirdInterceptor"/>
+ </listeners>
+
+ <test name="MultipleInterceptors:Tests" preserve-order="true">
+ <classes>
+ <class name="test.methodinterceptors.multipleinterceptors.FooTest"/>
+ </classes>
+ </test>
+</suite>
diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml
index 2ac1621..60e9301 100644
--- a/src/test/resources/testng.xml
+++ b/src/test/resources/testng.xml
@@ -630,6 +630,7 @@
<test name="MethodInterceptor">
<classes>
<class name="test.methodinterceptors.MethodInterceptorTest" />
+ <class name="test.methodinterceptors.multipleinterceptors.MultipleInterceptorsTest" />
</classes>
</test>
--
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