KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > framework > JUnit4TestAdapterCache


1 /**
2  *
3  */

4 package junit.framework;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Arrays JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10
11 import org.junit.runner.Description;
12 import org.junit.runner.notification.Failure;
13 import org.junit.runner.notification.RunListener;
14 import org.junit.runner.notification.RunNotifier;
15
16 public class JUnit4TestAdapterCache extends HashMap JavaDoc<Description, Test> {
17     private static final long serialVersionUID = 1L;
18     private static final JUnit4TestAdapterCache fInstance = new JUnit4TestAdapterCache();
19
20     public static JUnit4TestAdapterCache getDefault() {
21         return fInstance;
22     }
23     
24     public Test asTest(Description description) {
25         if (description.isSuite())
26             return createTest(description);
27         else {
28             if (!containsKey(description))
29                 put(description, createTest(description));
30             return get(description);
31         }
32     }
33
34     Test createTest(Description description) {
35         if (description.isTest())
36             return new JUnit4TestCaseFacade(description);
37         else {
38             TestSuite suite = new TestSuite(description.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 JavaDoc
50             public void testFailure(Failure failure) throws Exception JavaDoc {
51                 result.addError(asTest(failure.getDescription()), failure.getException());
52             }
53
54             @Override JavaDoc
55             public void testFinished(Description description)
56                     throws Exception JavaDoc {
57                 result.endTest(asTest(description));
58             }
59
60             @Override JavaDoc
61             public void testStarted(Description description)
62                     throws Exception JavaDoc {
63                 result.startTest(asTest(description));
64             }
65         });
66         return notifier;
67     }
68
69     public List JavaDoc<Test> asTestList(Description description) {
70         if (description.isTest())
71             return Arrays.asList(asTest(description));
72         else {
73             List JavaDoc<Test> returnThis = new ArrayList JavaDoc<Test>();
74             for (Description child : description.getChildren()) {
75                 returnThis.add(asTest(child));
76             }
77             return returnThis;
78         }
79     }
80
81 }
Popular Tags