Implementing @Priority with method interceptors
diff --git a/examples/build.xml b/examples/build.xml
index af37ab6..49293e4 100644
--- a/examples/build.xml
+++ b/examples/build.xml
@@ -57,7 +57,7 @@
<echo message=" testng-run "/>
<java fork="yes"
classpathref="run.cp"
- classname="com.beust.testng.TestNG">
+ classname="org.testng.TestNG">
<arg value="-d"/>
<arg value="${test.output}"/>
<arg value="${example.dir}/testng.xml"/>
diff --git a/examples/src/priority/Priority.java b/examples/src/priority/Priority.java
new file mode 100644
index 0000000..7a2224e
--- /dev/null
+++ b/examples/src/priority/Priority.java
@@ -0,0 +1,12 @@
+package priority;
+
+import static java.lang.annotation.ElementType.METHOD;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
+@Target({METHOD})
+public @interface Priority {
+ int value() default 0;
+}
diff --git a/examples/src/priority/PriorityInterceptor.java b/examples/src/priority/PriorityInterceptor.java
new file mode 100644
index 0000000..1b80847
--- /dev/null
+++ b/examples/src/priority/PriorityInterceptor.java
@@ -0,0 +1,38 @@
+package priority;
+
+import org.testng.IMethodInstance;
+import org.testng.IMethodInterceptor;
+import org.testng.ITestContext;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+
+public class PriorityInterceptor implements IMethodInterceptor {
+
+ public List<IMethodInstance> intercept(List<IMethodInstance> methods,
+ ITestContext context)
+ {
+ Comparator<IMethodInstance> comparator = new Comparator<IMethodInstance>() {
+
+ private int getPriority(IMethodInstance mi) {
+ int result = 0;
+ Priority a1 = mi.getMethod().getMethod().getAnnotation(Priority.class);
+ if (a1 != null) {
+ result = a1.value();
+ }
+ return result;
+ }
+
+ public int compare(IMethodInstance m1, IMethodInstance m2) {
+ return getPriority(m1) - getPriority(m2);
+ }
+
+ };
+ IMethodInstance[] array = methods.toArray(new IMethodInstance[methods.size()]);
+ Arrays.sort(array, comparator);
+
+ return Arrays.asList(array);
+ }
+
+}
diff --git a/examples/src/priority/PriorityTest.java b/examples/src/priority/PriorityTest.java
new file mode 100644
index 0000000..b85996a
--- /dev/null
+++ b/examples/src/priority/PriorityTest.java
@@ -0,0 +1,31 @@
+package priority;
+
+import org.testng.annotations.Test;
+
+import priority.Priority;
+
+public class PriorityTest {
+
+ @Test
+ public void b1() { System.out.println("Priority 0");}
+
+ @Priority(-1)
+ @Test
+ public void a1() { System.out.println("Priority -1");}
+
+ @Priority(-1)
+ @Test
+ public void a2() { System.out.println("Priority -1");}
+
+ @Priority(3)
+ @Test
+ public void c1() { System.out.println("Priority 3");}
+
+ @Priority(3)
+ @Test
+ public void c2() { System.out.println("Priority 3");}
+
+ @Priority(4)
+ @Test
+ public void d1() { System.out.println("Priority 4");}
+}
diff --git a/examples/testng.xml b/examples/testng.xml
index fa49c3f..6765fc1 100644
--- a/examples/testng.xml
+++ b/examples/testng.xml
@@ -1,6 +1,10 @@
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
-<suite name="Example" >
+<suite name="Example" verbose="10">
+
+ <listeners>
+ <listener class-name="priority.PriorityInterceptor" />
+ </listeners>
<test name="Simple example" >
<!--
@@ -18,5 +22,11 @@
<class name="example1.Test1" />
</classes>
</test>
+
+ <test name="Method interceptor example" >
+ <classes>
+ <class name="priority.PriorityTest" />
+ </classes>
+ </test>
</suite>