01: package org.junit.internal.runners;
02:
03: import java.lang.reflect.InvocationTargetException;
04: import java.lang.reflect.Method;
05: import java.util.List;
06:
07: import org.junit.Assume.AssumptionViolatedException;
08: import org.junit.runner.Description;
09: import org.junit.runner.notification.Failure;
10: import org.junit.runner.notification.RunNotifier;
11:
12: public class ClassRoadie {
13: private RunNotifier fNotifier;
14: private TestClass fTestClass;
15: private Description fDescription;
16: private final Runnable fRunnable;
17:
18: public ClassRoadie(RunNotifier notifier, TestClass testClass,
19: Description description, Runnable runnable) {
20: fNotifier = notifier;
21: fTestClass = testClass;
22: fDescription = description;
23: fRunnable = runnable;
24: }
25:
26: protected void runUnprotected() {
27: fRunnable.run();
28: };
29:
30: protected void addFailure(Throwable targetException) {
31: fNotifier.fireTestFailure(new Failure(fDescription,
32: targetException));
33: }
34:
35: public void runProtected() {
36: try {
37: runBefores();
38: runUnprotected();
39: } catch (FailedBefore e) {
40: } finally {
41: runAfters();
42: }
43: }
44:
45: private void runBefores() throws FailedBefore {
46: try {
47: try {
48: List<Method> befores = fTestClass.getBefores();
49: for (Method before : befores)
50: before.invoke(null);
51: } catch (InvocationTargetException e) {
52: throw e.getTargetException();
53: }
54: } catch (AssumptionViolatedException e) {
55: throw new FailedBefore();
56: } catch (Throwable e) {
57: addFailure(e);
58: throw new FailedBefore();
59: }
60: }
61:
62: private void runAfters() {
63: List<Method> afters = fTestClass.getAfters();
64: for (Method after : afters)
65: try {
66: after.invoke(null);
67: } catch (InvocationTargetException e) {
68: addFailure(e.getTargetException());
69: } catch (Throwable e) {
70: addFailure(e); // Untested, but seems impossible
71: }
72: }
73: }
|