01: /**
02: *
03: */package junit.framework;
04:
05: import java.util.ArrayList;
06: import java.util.Arrays;
07: import java.util.HashMap;
08: import java.util.List;
09:
10: import org.junit.runner.Description;
11: import org.junit.runner.notification.Failure;
12: import org.junit.runner.notification.RunListener;
13: import org.junit.runner.notification.RunNotifier;
14:
15: public class JUnit4TestAdapterCache extends HashMap<Description, Test> {
16: private static final long serialVersionUID = 1L;
17: private static final JUnit4TestAdapterCache fInstance = new JUnit4TestAdapterCache();
18:
19: public static JUnit4TestAdapterCache getDefault() {
20: return fInstance;
21: }
22:
23: public Test asTest(Description description) {
24: if (description.isSuite())
25: return createTest(description);
26: else {
27: if (!containsKey(description))
28: put(description, createTest(description));
29: return get(description);
30: }
31: }
32:
33: Test createTest(Description description) {
34: if (description.isTest())
35: return new JUnit4TestCaseFacade(description);
36: else {
37: TestSuite suite = new TestSuite(description
38: .getDisplayName());
39: for (Description child : description.getChildren())
40: suite.addTest(asTest(child));
41: return suite;
42: }
43: }
44:
45: public RunNotifier getNotifier(final TestResult result,
46: final JUnit4TestAdapter adapter) {
47: RunNotifier notifier = new RunNotifier();
48: notifier.addListener(new RunListener() {
49: @Override
50: public void testFailure(Failure failure) throws Exception {
51: result.addError(asTest(failure.getDescription()),
52: failure.getException());
53: }
54:
55: @Override
56: public void testFinished(Description description)
57: throws Exception {
58: result.endTest(asTest(description));
59: }
60:
61: @Override
62: public void testStarted(Description description)
63: throws Exception {
64: result.startTest(asTest(description));
65: }
66: });
67: return notifier;
68: }
69:
70: public List<Test> asTestList(Description description) {
71: if (description.isTest())
72: return Arrays.asList(asTest(description));
73: else {
74: List<Test> returnThis = new ArrayList<Test>();
75: for (Description child : description.getChildren()) {
76: returnThis.add(asTest(child));
77: }
78: return returnThis;
79: }
80: }
81:
82: }
|