|
1 package org.junit.internal.runners; |
|
2 |
|
3 import java.lang.annotation.Annotation; |
|
4 import java.lang.reflect.Method; |
|
5 import java.util.ArrayList; |
|
6 import java.util.Collections; |
|
7 import java.util.List; |
|
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< ?> fTestClass; |
|
18 |
|
19 public TestIntrospector(Class<?> testClass) { |
|
20 fTestClass= testClass; |
|
21 } |
|
22 |
|
23 public List<Method> getTestMethods(Class<? extends Annotation> annotationClass) { |
|
24 List<Method> results= new ArrayList<Method>(); |
|
25 for (Class<?> eachClass : getSuperClasses(fTestClass)) { |
|
26 Method[] methods= eachClass.getDeclaredMethods(); |
|
27 for (Method eachMethod : methods) { |
|
28 Annotation 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 eachMethod) { |
|
39 return eachMethod.getAnnotation(Ignore.class) != null; |
|
40 } |
|
41 |
|
42 private boolean runsTopToBottom(Class< ? extends Annotation> annotation) { |
|
43 return annotation.equals(Before.class) || annotation.equals(BeforeClass.class); |
|
44 } |
|
45 |
|
46 private boolean isShadowed(Method method, List<Method> results) { |
|
47 for (Method each : results) { |
|
48 if (isShadowed(method, each)) |
|
49 return true; |
|
50 } |
|
51 return false; |
|
52 } |
|
53 |
|
54 private boolean isShadowed(Method current, Method 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<Class<?>> getSuperClasses(Class< ?> testClass) { |
|
67 ArrayList<Class<?>> results= new ArrayList<Class<?>>(); |
|
68 Class<?> current= testClass; |
|
69 while (current != null) { |
|
70 results.add(current); |
|
71 current= current.getSuperclass(); |
|
72 } |
|
73 return results; |
|
74 } |
|
75 |
|
76 long getTimeout(Method method) { |
|
77 Test annotation= method.getAnnotation(Test.class); |
|
78 long timeout= annotation.timeout(); |
|
79 return timeout; |
|
80 } |
|
81 |
|
82 Class<? extends Throwable> expectedException(Method 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 |