KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > internal > runners > TestClassRunner


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 JavaDoc<?> fTestClass;
18
19     public TestClassRunner(Class JavaDoc<?> klass) throws InitializationError {
20         this(klass, new TestClassMethodsRunner(klass));
21     }
22     
23     public TestClassRunner(Class JavaDoc<?> 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     // TODO: this is parallel to passed-in runner
32
protected void validate(MethodValidator methodValidator) {
33         methodValidator.validateMethodsForDefaultRunner();
34     }
35
36     @Override JavaDoc
37     public void run(final RunNotifier notifier) {
38         BeforeAndAfterRunner runner = new BeforeAndAfterRunner(getTestClass(),
39                 BeforeClass.class, AfterClass.class, null) {
40             @Override JavaDoc
41             protected void runUnprotected() {
42                 fEnclosedRunner.run(notifier);
43             }
44         
45             // TODO: looks very similar to other method of BeforeAfter, now
46
@Override JavaDoc
47             protected void addFailure(Throwable JavaDoc targetException) {
48                 notifier.fireTestFailure(new Failure(getDescription(), targetException));
49             }
50         };
51
52         runner.runProtected();
53     }
54
55     @Override JavaDoc
56     public Description getDescription() {
57         return fEnclosedRunner.getDescription();
58     }
59     
60     // TODO: good behavior when createTest fails
61

62     // TODO: dup?
63
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 JavaDoc<?> getTestClass() {
72         return fTestClass;
73     }
74 }
75
Popular Tags