1 package org.junit.internal.runners; 2 3 import org.junit.AfterClass; 4 import org.junit.BeforeClass; 5 import org.junit.runner.Description; 6 import org.junit.runner.Runner; 7 import org.junit.runner.manipulation.Filter; 8 import org.junit.runner.manipulation.Filterable; 9 import org.junit.runner.manipulation.NoTestsRemainException; 10 import org.junit.runner.manipulation.Sortable; 11 import org.junit.runner.manipulation.Sorter; 12 import org.junit.runner.notification.Failure; 13 import org.junit.runner.notification.RunNotifier; 14 15 public class TestClassRunner extends Runner implements Filterable, Sortable { 16 protected final Runner fEnclosedRunner; 17 private final Class <?> fTestClass; 18 19 public TestClassRunner(Class <?> klass) throws InitializationError { 20 this(klass, new TestClassMethodsRunner(klass)); 21 } 22 23 public TestClassRunner(Class <?> klass, Runner runner) throws InitializationError { 24 fTestClass= klass; 25 fEnclosedRunner= runner; 26 MethodValidator methodValidator= new MethodValidator(klass); 27 validate(methodValidator); 28 methodValidator.assertValid(); 29 } 30 31 protected void validate(MethodValidator methodValidator) { 33 methodValidator.validateMethodsForDefaultRunner(); 34 } 35 36 @Override 37 public void run(final RunNotifier notifier) { 38 BeforeAndAfterRunner runner = new BeforeAndAfterRunner(getTestClass(), 39 BeforeClass.class, AfterClass.class, null) { 40 @Override 41 protected void runUnprotected() { 42 fEnclosedRunner.run(notifier); 43 } 44 45 @Override 47 protected void addFailure(Throwable targetException) { 48 notifier.fireTestFailure(new Failure(getDescription(), targetException)); 49 } 50 }; 51 52 runner.runProtected(); 53 } 54 55 @Override 56 public Description getDescription() { 57 return fEnclosedRunner.getDescription(); 58 } 59 60 62 public void filter(Filter filter) throws NoTestsRemainException { 64 filter.apply(fEnclosedRunner); 65 } 66 67 public void sort(Sorter sorter) { 68 sorter.apply(fEnclosedRunner); 69 } 70 71 protected Class <?> getTestClass() { 72 return fTestClass; 73 } 74 } 75 | Popular Tags |