1b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpackage org.junit.internal;
2b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
3b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.io.PrintStream;
4b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.text.NumberFormat;
5b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.util.List;
6b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
7b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.Description;
8b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.Result;
9b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.notification.Failure;
10b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.notification.RunListener;
11b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
12b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpublic class TextListener extends RunListener {
13b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
14aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    private final PrintStream writer;
15aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
16aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    public TextListener(JUnitSystem system) {
17aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        this(system.out());
18aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
19aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
20aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    public TextListener(PrintStream writer) {
21aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        this.writer = writer;
22aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
23aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
24aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    @Override
25aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    public void testRunFinished(Result result) {
26aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        printHeader(result.getRunTime());
27aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        printFailures(result);
28aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        printFooter(result);
29aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
30aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
31aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    @Override
32aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    public void testStarted(Description description) {
33aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        writer.append('.');
34aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
35aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
36aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    @Override
37aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    public void testFailure(Failure failure) {
38aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        writer.append('E');
39aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
40aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
41aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    @Override
42aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    public void testIgnored(Description description) {
43aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        writer.append('I');
44aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
45aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
46aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    /*
47aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin      * Internal methods
48aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin      */
49aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
50aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    private PrintStream getWriter() {
51aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        return writer;
52aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
53aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
54aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    protected void printHeader(long runTime) {
55aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        getWriter().println();
56aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        getWriter().println("Time: " + elapsedTimeAsString(runTime));
57aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
58aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
59aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    protected void printFailures(Result result) {
60aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        List<Failure> failures = result.getFailures();
61aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        if (failures.size() == 0) {
62aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            return;
63aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        }
64aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        if (failures.size() == 1) {
65aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            getWriter().println("There was " + failures.size() + " failure:");
66aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        } else {
67aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            getWriter().println("There were " + failures.size() + " failures:");
68aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        }
69aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        int i = 1;
70aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        for (Failure each : failures) {
71aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            printFailure(each, "" + i++);
72aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        }
73aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
74aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
75aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    protected void printFailure(Failure each, String prefix) {
76aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        getWriter().println(prefix + ") " + each.getTestHeader());
77aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        getWriter().print(each.getTrace());
78aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
79aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
80aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    protected void printFooter(Result result) {
81aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        if (result.wasSuccessful()) {
82aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            getWriter().println();
83aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            getWriter().print("OK");
84aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            getWriter().println(" (" + result.getRunCount() + " test" + (result.getRunCount() == 1 ? "" : "s") + ")");
85aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
86aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        } else {
87aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            getWriter().println();
88aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            getWriter().println("FAILURES!!!");
89aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            getWriter().println("Tests run: " + result.getRunCount() + ",  Failures: " + result.getFailureCount());
90aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        }
91aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        getWriter().println();
92aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
93aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
94aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    /**
95aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * Returns the formatted string of the elapsed time. Duplicated from
96aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * BaseTestRunner. Fix it.
97aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     */
98aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    protected String elapsedTimeAsString(long runTime) {
99aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        return NumberFormat.getInstance().format((double) runTime / 1000);
100aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
101b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot}
102