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