636
|
1 |
package org.junit.internal.runners;
|
|
2 |
|
|
3 |
import junit.extensions.TestDecorator;
|
|
4 |
import junit.framework.AssertionFailedError;
|
|
5 |
import junit.framework.Test;
|
|
6 |
import junit.framework.TestCase;
|
|
7 |
import junit.framework.TestListener;
|
|
8 |
import junit.framework.TestResult;
|
|
9 |
import junit.framework.TestSuite;
|
|
10 |
import org.junit.runner.Describable;
|
|
11 |
import org.junit.runner.Description;
|
|
12 |
import org.junit.runner.Runner;
|
|
13 |
import org.junit.runner.manipulation.Filter;
|
|
14 |
import org.junit.runner.manipulation.Filterable;
|
|
15 |
import org.junit.runner.manipulation.NoTestsRemainException;
|
|
16 |
import org.junit.runner.manipulation.Sortable;
|
|
17 |
import org.junit.runner.manipulation.Sorter;
|
|
18 |
import org.junit.runner.notification.Failure;
|
|
19 |
import org.junit.runner.notification.RunNotifier;
|
|
20 |
|
|
21 |
public class JUnit38ClassRunner extends Runner implements Filterable, Sortable {
|
|
22 |
private static final class OldTestClassAdaptingListener implements
|
|
23 |
TestListener {
|
|
24 |
private final RunNotifier fNotifier;
|
|
25 |
|
|
26 |
private OldTestClassAdaptingListener(RunNotifier notifier) {
|
|
27 |
fNotifier= notifier;
|
|
28 |
}
|
|
29 |
|
|
30 |
public void endTest(Test test) {
|
|
31 |
fNotifier.fireTestFinished(asDescription(test));
|
|
32 |
}
|
|
33 |
|
|
34 |
public void startTest(Test test) {
|
|
35 |
fNotifier.fireTestStarted(asDescription(test));
|
|
36 |
}
|
|
37 |
|
|
38 |
// Implement junit.framework.TestListener
|
|
39 |
public void addError(Test test, Throwable t) {
|
|
40 |
Failure failure= new Failure(asDescription(test), t);
|
|
41 |
fNotifier.fireTestFailure(failure);
|
|
42 |
}
|
|
43 |
|
|
44 |
private Description asDescription(Test test) {
|
|
45 |
if (test instanceof Describable) {
|
|
46 |
Describable facade= (Describable) test;
|
|
47 |
return facade.getDescription();
|
|
48 |
}
|
|
49 |
return Description.createTestDescription(test.getClass(), getName(test));
|
|
50 |
}
|
|
51 |
|
|
52 |
private String getName(Test test) {
|
|
53 |
if (test instanceof TestCase)
|
|
54 |
return ((TestCase) test).getName();
|
|
55 |
else
|
|
56 |
return test.toString();
|
|
57 |
}
|
|
58 |
|
|
59 |
public void addFailure(Test test, AssertionFailedError t) {
|
|
60 |
addError(test, t);
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
private Test fTest;
|
|
65 |
|
|
66 |
public JUnit38ClassRunner(Class<?> klass) {
|
|
67 |
this(new TestSuite(klass.asSubclass(TestCase.class)));
|
|
68 |
}
|
|
69 |
|
|
70 |
public JUnit38ClassRunner(Test test) {
|
|
71 |
super();
|
|
72 |
fTest= test;
|
|
73 |
}
|
|
74 |
|
|
75 |
@Override
|
|
76 |
public void run(RunNotifier notifier) {
|
|
77 |
TestResult result= new TestResult();
|
|
78 |
result.addListener(createAdaptingListener(notifier));
|
|
79 |
fTest.run(result);
|
|
80 |
}
|
|
81 |
|
|
82 |
public static TestListener createAdaptingListener(final RunNotifier notifier) {
|
|
83 |
return new OldTestClassAdaptingListener(notifier);
|
|
84 |
}
|
|
85 |
|
|
86 |
@Override
|
|
87 |
public Description getDescription() {
|
|
88 |
return makeDescription(fTest);
|
|
89 |
}
|
|
90 |
|
|
91 |
private Description makeDescription(Test test) {
|
|
92 |
if (test instanceof TestCase) {
|
|
93 |
TestCase tc= (TestCase) test;
|
|
94 |
return Description.createTestDescription(tc.getClass(), tc.getName());
|
|
95 |
} else if (test instanceof TestSuite) {
|
|
96 |
TestSuite ts= (TestSuite) test;
|
|
97 |
String name= ts.getName() == null ? "" : ts.getName();
|
|
98 |
Description description= Description.createSuiteDescription(name);
|
|
99 |
int n= ts.testCount();
|
|
100 |
for (int i= 0; i < n; i++)
|
|
101 |
description.addChild(makeDescription(ts.testAt(i)));
|
|
102 |
return description;
|
|
103 |
} else if (test instanceof Describable) {
|
|
104 |
Describable adapter= (Describable) test;
|
|
105 |
return adapter.getDescription();
|
|
106 |
} else if (test instanceof TestDecorator) {
|
|
107 |
TestDecorator decorator= (TestDecorator) test;
|
|
108 |
return makeDescription(decorator.getTest());
|
|
109 |
} else {
|
|
110 |
// This is the best we can do in this case
|
|
111 |
return Description.createSuiteDescription(test.getClass());
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
public void filter(Filter filter) throws NoTestsRemainException {
|
|
116 |
if (fTest instanceof Filterable) {
|
|
117 |
Filterable adapter= (Filterable) fTest;
|
|
118 |
adapter.filter(filter);
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
public void sort(Sorter sorter) {
|
|
123 |
if (fTest instanceof Sortable) {
|
|
124 |
Sortable adapter= (Sortable) fTest;
|
|
125 |
adapter.sort(sorter);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
}
|