KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > internal > runners > TestIntrospector


1 package org.junit.internal.runners;
2
3 import java.lang.annotation.Annotation JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.junit.Before;
10 import org.junit.BeforeClass;
11 import org.junit.Ignore;
12 import org.junit.Test;
13 import org.junit.Test.None;
14
15
16 public class TestIntrospector {
17     private final Class JavaDoc< ?> fTestClass;
18     
19     public TestIntrospector(Class JavaDoc<?> testClass) {
20         fTestClass= testClass;
21     }
22
23     public List JavaDoc<Method JavaDoc> getTestMethods(Class JavaDoc<? extends Annotation JavaDoc> annotationClass) {
24         List JavaDoc<Method JavaDoc> results= new ArrayList JavaDoc<Method JavaDoc>();
25         for (Class JavaDoc<?> eachClass : getSuperClasses(fTestClass)) {
26             Method JavaDoc[] methods= eachClass.getDeclaredMethods();
27             for (Method JavaDoc eachMethod : methods) {
28                 Annotation JavaDoc annotation= eachMethod.getAnnotation(annotationClass);
29                 if (annotation != null && ! isShadowed(eachMethod, results))
30                     results.add(eachMethod);
31             }
32         }
33         if (runsTopToBottom(annotationClass))
34             Collections.reverse(results);
35         return results;
36     }
37
38     public boolean isIgnored(Method JavaDoc eachMethod) {
39         return eachMethod.getAnnotation(Ignore.class) != null;
40     }
41
42     private boolean runsTopToBottom(Class JavaDoc< ? extends Annotation JavaDoc> annotation) {
43         return annotation.equals(Before.class) || annotation.equals(BeforeClass.class);
44     }
45     
46     private boolean isShadowed(Method JavaDoc method, List JavaDoc<Method JavaDoc> results) {
47         for (Method JavaDoc each : results) {
48             if (isShadowed(method, each))
49                 return true;
50         }
51         return false;
52     }
53
54     private boolean isShadowed(Method JavaDoc current, Method JavaDoc previous) {
55         if (! previous.getName().equals(current.getName()))
56             return false;
57         if (previous.getParameterTypes().length != current.getParameterTypes().length)
58             return false;
59         for (int i= 0; i < previous.getParameterTypes().length; i++) {
60             if (! previous.getParameterTypes()[i].equals(current.getParameterTypes()[i]))
61                 return false;
62         }
63         return true;
64     }
65
66     private List JavaDoc<Class JavaDoc<?>> getSuperClasses(Class JavaDoc< ?> testClass) {
67         ArrayList JavaDoc<Class JavaDoc<?>> results= new ArrayList JavaDoc<Class JavaDoc<?>>();
68         Class JavaDoc<?> current= testClass;
69         while (current != null) {
70             results.add(current);
71             current= current.getSuperclass();
72         }
73         return results;
74     }
75
76     long getTimeout(Method JavaDoc method) {
77         Test annotation= method.getAnnotation(Test.class);
78         long timeout= annotation.timeout();
79         return timeout;
80     }
81
82     Class JavaDoc<? extends Throwable JavaDoc> expectedException(Method JavaDoc method) {
83         Test annotation= method.getAnnotation(Test.class);
84         if (annotation.expected() == None.class)
85             return null;
86         else
87             return annotation.expected();
88     }
89
90 }
91
92
Popular Tags