[testng] 136/160: Rework multiple interceptors tests
Eugene Zhukov
eugene-guest at moszumanska.debian.org
Tue Aug 18 10:22:30 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 32d6a8f068256f44bcd8a6813b0447e028feecfb
Author: Julien Herr <julien.herr at alcatel-lucent.com>
Date: Tue Jun 30 09:58:32 2015 +0200
Rework multiple interceptors tests
---
.../multipleinterceptors/FirstInterceptor.java | 21 ++----------
.../MethodNameFilterInterceptor.java | 37 ++++++++++++++++++++++
.../MultipleInterceptorsTest.java | 29 ++++++-----------
.../multipleinterceptors/SecondInterceptor.java | 20 ++----------
.../multipleinterceptors/ThirdInterceptor.java | 20 ++----------
5 files changed, 56 insertions(+), 71 deletions(-)
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/FirstInterceptor.java b/src/test/java/test/methodinterceptors/multipleinterceptors/FirstInterceptor.java
index f5a0db9..5f785ba 100644
--- a/src/test/java/test/methodinterceptors/multipleinterceptors/FirstInterceptor.java
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/FirstInterceptor.java
@@ -1,23 +1,8 @@
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 extends MethodNameFilterInterceptor {
-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;
+ public FirstInterceptor() {
+ super("a");
}
}
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/MethodNameFilterInterceptor.java b/src/test/java/test/methodinterceptors/multipleinterceptors/MethodNameFilterInterceptor.java
new file mode 100644
index 0000000..6f48258
--- /dev/null
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/MethodNameFilterInterceptor.java
@@ -0,0 +1,37 @@
+package test.methodinterceptors.multipleinterceptors;
+
+import org.testng.IMethodInstance;
+import org.testng.IMethodInterceptor;
+import org.testng.ITestContext;
+import org.testng.ITestNGMethod;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public abstract class MethodNameFilterInterceptor implements IMethodInterceptor {
+
+ private final String methodName;
+
+ protected MethodNameFilterInterceptor(String methodName) {
+ this.methodName = methodName;
+ }
+
+ @Override
+ public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
+ List<IMethodInstance> result = new ArrayList<>();
+ for (IMethodInstance methodInstance : methods) {
+ ITestNGMethod method = methodInstance.getMethod();
+ String name = method.getMethodName();
+ if (!name.equals(methodName)) {
+ result.add(methodInstance);
+ String currentDescription = method.getDescription();
+ if (currentDescription == null) {
+ method.setDescription(methodName);
+ } else {
+ method.setDescription(currentDescription + methodName);
+ }
+ }
+ }
+ return result;
+ }
+}
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/MultipleInterceptorsTest.java b/src/test/java/test/methodinterceptors/multipleinterceptors/MultipleInterceptorsTest.java
index 7211eb5..f1f3fa5 100644
--- a/src/test/java/test/methodinterceptors/multipleinterceptors/MultipleInterceptorsTest.java
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/MultipleInterceptorsTest.java
@@ -1,46 +1,37 @@
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 java.util.Collections;
+
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());
+ TestNG tng = create(FooTest.class);
tng.setMethodInterceptor(new FirstInterceptor());
tng.setMethodInterceptor(new SecondInterceptor());
+ tng.setMethodInterceptor(new ThirdInterceptor());
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
+ // FIXME With or without preserve-order, test is working
public void testMultipleInterceptorsWithPreserveOrder() {
TestNG tng = create();
- tng.setTestSuites(Arrays.asList("target/test-classes/methodinterceptors/multipleinterceptors/multiple-interceptors.xml"));
+ tng.setTestSuites(Collections.singletonList("target/test-classes/methodinterceptors/multipleinterceptors/multiple-interceptors.xml"));
+ TestListenerAdapter tla = new TestListenerAdapter();
+ tng.addListener(tla);
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();
+ Assert.assertEquals(tla.getPassedTests().get(0).getMethod().getDescription(), "abcabc"); // Interceptor are called twice => GITHUB #154
}
}
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/SecondInterceptor.java b/src/test/java/test/methodinterceptors/multipleinterceptors/SecondInterceptor.java
index 988aafd..df4a465 100644
--- a/src/test/java/test/methodinterceptors/multipleinterceptors/SecondInterceptor.java
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/SecondInterceptor.java
@@ -1,22 +1,8 @@
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 extends MethodNameFilterInterceptor {
-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;
+ public SecondInterceptor() {
+ super("b");
}
}
diff --git a/src/test/java/test/methodinterceptors/multipleinterceptors/ThirdInterceptor.java b/src/test/java/test/methodinterceptors/multipleinterceptors/ThirdInterceptor.java
index dba9111..90e3a53 100644
--- a/src/test/java/test/methodinterceptors/multipleinterceptors/ThirdInterceptor.java
+++ b/src/test/java/test/methodinterceptors/multipleinterceptors/ThirdInterceptor.java
@@ -1,22 +1,8 @@
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 extends MethodNameFilterInterceptor {
-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;
+ public ThirdInterceptor() {
+ super("c");
}
}
--
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