Permalink
Browse files

2nd pass on before/after annotation

  • Loading branch information...
1 parent 355aa30 commit 586c9a8cb3cae9463cca372d839d4eacd8c8adbd unknown committed Feb 28, 2012
@@ -3,17 +3,26 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
public class AnnotationGrabber
{
- public static ArrayList<Method> grabMethodsWithAnnotations(Class klass, Class<? extends Annotation> anno)
+ public static ArrayList<Method> grabMethodsWithAnnotations(Class<?> klass, Class<? extends Annotation> anno)
{
ArrayList<Method> interestingMethods = new ArrayList<Method>();
- Method[] methods = klass.getDeclaredMethods();
+ ArrayList<Method> methods = new ArrayList<Method>(Arrays.asList(klass.getDeclaredMethods()));
+ ArrayList<Method> superMethods = new ArrayList<Method>(Arrays.asList(klass.getSuperclass().getDeclaredMethods()));
+ ArrayList<Method> allMethods = (ArrayList<Method>) methods.clone();
- for(Method method : methods)
+ //consolidate both declared and super methods
+ allMethods.addAll(superMethods);
+
+ for(Method method : allMethods)
{
+ System.err.println("");
+
if(method.isAnnotationPresent(anno))
{
interestingMethods.add(method);
@@ -1,7 +1,9 @@
package org.mearvk;
+import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
/**
* An annotation which specifies in which order test classes should be run
@@ -11,6 +13,7 @@
* @author Max Rupplin
*/
@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
public @interface ClassRunOrder
{
/**
@@ -1,7 +1,9 @@
package org.mearvk;
+import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
/**
* An annotation which specifies in which order methods within an annotated
@@ -11,6 +13,7 @@
* @author Max Rupplin
*/
@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
public @interface MethodRunOrder
{
/**
@@ -2,6 +2,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -79,21 +80,24 @@ public static void runNext(RunNotifier notifier)
{
// notify listeners that test is about to start
notifier.fireTestRunStarted(testDescription);
-
- //run the @Before annotations
- for(Method before : AnnotationGrabber.grabMethodsWithAnnotations(classToRun, Before.class))
- {
- before.invoke(classToRun, (Object[])null);
- }
- // try and run the method
- method.invoke(classToRun.newInstance(), (Object[]) null);
-
- //run the @After annotations
- for(Method after : AnnotationGrabber.grabMethodsWithAnnotations(classToRun, After.class))
+ // try and run the method, skipping static functions
+ if(!Modifier.isStatic(method.getModifiers()))
{
- after.invoke(classToRun, (Object[])null);
- }
+ //run the @Before annotations
+ for(Method before : AnnotationGrabber.grabMethodsWithAnnotations(classToRun, Before.class))
+ {
+ before.invoke(classToRun.newInstance(), (Object[])null);
+ }
+
+ method.invoke(classToRun.newInstance(), (Object[]) null);
+
+ //run the @After annotations
+ for(Method after : AnnotationGrabber.grabMethodsWithAnnotations(classToRun, After.class))
+ {
+ after.invoke(classToRun.newInstance(), (Object[])null);
+ }
+ }
// notify listeners that test run has completed
notifier.fireTestFinished(testDescription);

0 comments on commit 586c9a8

Please sign in to comment.