|
| 1 | +package org.junit.internal.runners; |
| 2 | + |
| 3 | +import java.lang.reflect.InvocationTargetException; |
| 4 | +import java.lang.reflect.Method; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.junit.internal.AssumptionViolatedException; |
| 8 | +import org.junit.runner.Description; |
| 9 | +import org.junit.runner.notification.Failure; |
| 10 | +import org.junit.runner.notification.RunNotifier; |
| 11 | +import org.junit.runners.BlockJUnit4ClassRunner; |
| 12 | + |
| 13 | +/** |
| 14 | + * @deprecated Included for backwards compatibility with JUnit 4.4. Will be |
| 15 | + * removed in the next major release. Please use |
| 16 | + * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. |
| 17 | + */ |
| 18 | +@Deprecated |
| 19 | +public class ClassRoadie { |
| 20 | + private RunNotifier notifier; |
| 21 | + private TestClass testClass; |
| 22 | + private Description description; |
| 23 | + private final Runnable runnable; |
| 24 | + |
| 25 | + public ClassRoadie(RunNotifier notifier, TestClass testClass, |
| 26 | + Description description, Runnable runnable) { |
| 27 | + this.notifier = notifier; |
| 28 | + this.testClass = testClass; |
| 29 | + this.description = description; |
| 30 | + this.runnable = runnable; |
| 31 | + } |
| 32 | + |
| 33 | + protected void runUnprotected() { |
| 34 | + runnable.run(); |
| 35 | + } |
| 36 | + |
| 37 | + protected void addFailure(Throwable targetException) { |
| 38 | + notifier.fireTestFailure(new Failure(description, targetException)); |
| 39 | + } |
| 40 | + |
| 41 | + public void runProtected() { |
| 42 | + try { |
| 43 | + runBefores(); |
| 44 | + runUnprotected(); |
| 45 | + } catch (FailedBefore e) { |
| 46 | + } finally { |
| 47 | + runAfters(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private void runBefores() throws FailedBefore { |
| 52 | + try { |
| 53 | + try { |
| 54 | + List<Method> befores = testClass.getBefores(); |
| 55 | + for (Method before : befores) { |
| 56 | + before.invoke(null); |
| 57 | + } |
| 58 | + } catch (InvocationTargetException e) { |
| 59 | + throw e.getTargetException(); |
| 60 | + } |
| 61 | + } catch (AssumptionViolatedException e) { |
| 62 | + throw new FailedBefore(); |
| 63 | + } catch (Throwable e) { |
| 64 | + addFailure(e); |
| 65 | + throw new FailedBefore(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private void runAfters() { |
| 70 | + List<Method> afters = testClass.getAfters(); |
| 71 | + for (Method after : afters) { |
| 72 | + try { |
| 73 | + after.invoke(null); |
| 74 | + } catch (InvocationTargetException e) { |
| 75 | + addFailure(e.getTargetException()); |
| 76 | + } catch (Throwable e) { |
| 77 | + addFailure(e); // Untested, but seems impossible |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments