1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 package junitx.ddtunit.optional.junit4;
40
41 import java.lang.reflect.Constructor;
42 import java.lang.reflect.InvocationTargetException;
43 import java.lang.reflect.Method;
44 import java.util.Enumeration;
45
46 import junit.framework.TestCase;
47 import junit.framework.TestFailure;
48 import junit.framework.TestResult;
49
50 import org.junit.After;
51 import org.junit.Before;
52 import org.junit.Ignore;
53 import org.junit.internal.runners.InitializationError;
54 import org.junit.internal.runners.JUnit4ClassRunner;
55 import org.junit.runner.Description;
56 import org.junit.runner.notification.Failure;
57 import org.junit.runner.notification.RunNotifier;
58
59 public class DDTUnit4ClassRunner extends JUnit4ClassRunner {
60
61 private final Class<? extends TestCase> clazz;
62
63 public DDTUnit4ClassRunner(Class<? extends TestCase> clazz)
64 throws InitializationError {
65 super(clazz);
66 this.clazz = clazz;
67 }
68
69 @Override
70 protected void invokeTestMethod(Method method, RunNotifier runNotifier) {
71
72 Description testDescription = Description.createTestDescription(clazz,
73 method.getName());
74
75 try {
76 if (method.isAnnotationPresent(Ignore.class)) {
77 runNotifier.fireTestIgnored(testDescription);
78 }
79 Constructor<? extends TestCase> constructor = clazz
80 .getConstructor(String.class);
81
82 TestCase newInstance = constructor.newInstance(method.getName());
83
84 runNotifier.fireTestStarted(testDescription);
85
86 TestResult result;
87 try {
88 setUp(newInstance);
89
90 result = newInstance.run();
91 } finally {
92
93 tearDown(newInstance);
94 }
95
96 if (!result.wasSuccessful()) {
97
98 for (Enumeration<TestFailure> en = result.errors(); en
99 .hasMoreElements();) {
100 TestFailure failure = en.nextElement();
101 runNotifier.fireTestFailure(new Failure(testDescription,
102 failure.thrownException()));
103 }
104
105 for (Enumeration<TestFailure> en = result.failures(); en
106 .hasMoreElements();) {
107 TestFailure failure = en.nextElement();
108 runNotifier.fireTestFailure(new Failure(testDescription,
109 failure.thrownException()));
110 }
111
112 }
113
114 runNotifier.fireTestFinished(testDescription);
115
116 } catch (Throwable e) {
117 runNotifier.fireTestFailure(new Failure(testDescription, e));
118 }
119 }
120
121 private void tearDown(TestCase newInstance)
122 throws IllegalArgumentException, IllegalAccessException,
123 InvocationTargetException {
124 Method[] methods = clazz.getMethods();
125 for (Method method : methods) {
126 if ((method.isAnnotationPresent(After.class) && !method
127 .isAnnotationPresent(Ignore.class))) {
128 method.invoke(newInstance, new Object[] {});
129 }
130 }
131
132 }
133
134 private void setUp(TestCase newInstance) throws IllegalArgumentException,
135 IllegalAccessException, InvocationTargetException {
136 Method[] methods = clazz.getMethods();
137 for (Method method : methods) {
138
139 if ((method.isAnnotationPresent(Before.class) && !method
140 .isAnnotationPresent(Ignore.class))) {
141 method.invoke(newInstance, new Object[] {});
142 }
143 }
144
145 }
146
147 }