1package org.junit.internal.runners;
2
3import java.lang.reflect.InvocationTargetException;
4import java.util.Arrays;
5import java.util.List;
6
7import org.junit.runner.Description;
8import org.junit.runner.Runner;
9import org.junit.runner.notification.Failure;
10import org.junit.runner.notification.RunNotifier;
11import org.junit.runners.model.InitializationError;
12
13public class ErrorReportingRunner extends Runner {
14	private final List<Throwable> fCauses;
15
16	private final Class<?> fTestClass;
17
18	public ErrorReportingRunner(Class<?> testClass, Throwable cause) {
19		fTestClass= testClass;
20		fCauses= getCauses(cause);
21	}
22
23	@Override
24	public Description getDescription() {
25		Description description= Description.createSuiteDescription(fTestClass);
26		for (Throwable each : fCauses)
27			description.addChild(describeCause(each));
28		return description;
29	}
30
31	@Override
32	public void run(RunNotifier notifier) {
33		for (Throwable each : fCauses)
34			runCause(each, notifier);
35	}
36
37	@SuppressWarnings("deprecation")
38	private List<Throwable> getCauses(Throwable cause) {
39		if (cause instanceof InvocationTargetException)
40			return getCauses(cause.getCause());
41		if (cause instanceof InitializationError)
42			return ((InitializationError) cause).getCauses();
43		if (cause instanceof org.junit.internal.runners.InitializationError)
44			return ((org.junit.internal.runners.InitializationError) cause)
45					.getCauses();
46		return Arrays.asList(cause);
47	}
48
49	private Description describeCause(Throwable child) {
50		return Description.createTestDescription(fTestClass,
51				"initializationError");
52	}
53
54	private void runCause(Throwable child, RunNotifier notifier) {
55		Description description= describeCause(child);
56		notifier.fireTestStarted(description);
57		notifier.fireTestFailure(new Failure(description, child));
58		notifier.fireTestFinished(description);
59	}
60}