1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.google.coretests;
17
18import java.io.PrintStream;
19
20import junit.framework.Test;
21import junit.framework.TestCase;
22import junit.framework.TestResult;
23import junit.textui.ResultPrinter;
24
25/**
26 * A special ResultPrinter implementation that displays additional statistics
27 * about the test that have been executed.
28 */
29public class CoreTestPrinter extends ResultPrinter {
30
31    /**
32     * The last test class we executed.
33     */
34    private Class<?> fLastClass;
35
36    /**
37     * The current output column for dots.
38     */
39    private int fColumn;
40
41    /**
42     * The time it took to execute the tests.
43     */
44    private int fRunTime;
45
46    /**
47     * The flags the user specified.
48     */
49    private int fFlags;
50
51    /**
52     * Creates a new CoreTestPrinter for the given parameters.
53     */
54    public CoreTestPrinter(PrintStream writer, int flags) {
55        super(writer);
56        fFlags = flags;
57    }
58
59    @Override
60    protected void printHeader(long runTime) {
61        fRunTime = (int)(runTime / 1000);
62
63        if (fColumn != 0) {
64            getWriter().println();
65        }
66
67        getWriter().println();
68    }
69
70    @Override
71    protected void printFooter(TestResult result) {
72        CoreTestResult coreResult = (CoreTestResult)result;
73
74        PrintStream printer = getWriter();
75
76        if (fColumn != 0) {
77            printer.println();
78        }
79
80        printer.println();
81        printer.println("Total tests   : " + coreResult.fTotalTestCount);
82        printer.println("Tests run     : " + coreResult.runCount());
83        printer.println("Tests ignored : " + coreResult.fIgnoredCount);
84
85        printer.println();
86        printer.println("Normal tests  : " + coreResult.fNormalTestCount);
87        printer.println("Android-only  : " + coreResult.fAndroidOnlyCount);
88        printer.println("Broken tests  : " + coreResult.fBrokenTestCount);
89        printer.println("Known failures: " + coreResult.fKnownFailureCount);
90        printer.println("Side-effects  : " + coreResult.fSideEffectCount);
91
92        printMemory();
93
94        int seconds = fRunTime;
95
96        int hours = seconds / 3600;
97        seconds = seconds % 3600;
98
99        int minutes = seconds / 60;
100        seconds = seconds % 60;
101
102        String text = String.format("%02d:%02d:%02d", hours, minutes, seconds);
103
104        printer.println();
105        printer.println("Time taken    : " + text);
106
107        super.printFooter(result);
108    }
109
110    /**
111     * Dumps some memory info.
112     */
113    private void printMemory() {
114        PrintStream printer = getWriter();
115        Runtime runtime = Runtime.getRuntime();
116
117        long total = runtime.totalMemory();
118        long free = runtime.freeMemory();
119        long used = total - free;
120
121        printer.println();
122        printer.println("Total memory  : " + total);
123        printer.println("Used memory   : " + used);
124        printer.println("Free memory   : " + free);
125    }
126
127    @Override
128    public void startTest(Test test) {
129        TestCase caze = (TestCase)test;
130
131        if (fLastClass == null ||
132                caze.getClass().getPackage() != fLastClass.getPackage()) {
133
134            if (fColumn != 0) {
135                getWriter().println();
136                fColumn = 0;
137            }
138
139            getWriter().println();
140            Package pack = caze.getClass().getPackage();
141            getWriter().println(pack == null ? "Default package" :
142                pack.getName());
143            getWriter().println();
144
145        }
146
147        if ((fFlags & CoreTestSuite.VERBOSE) != 0) {
148            if (caze.getClass() != fLastClass) {
149                if (fColumn != 0) {
150                    getWriter().println();
151                    fColumn = 0;
152                }
153
154                String name = caze.getClass().getSimpleName().toString();
155
156                printMemory();
157                getWriter().println("Now executing : " + name);
158                getWriter().println();
159            }
160        }
161
162        getWriter().print(".");
163        if (fColumn++ >= 40) {
164            getWriter().println();
165            fColumn= 0;
166        }
167
168        fLastClass = caze.getClass();
169    }
170
171    @Override
172    public void addError(Test test, Throwable t) {
173        if (t instanceof CoreTestTimeout) {
174            getWriter().print("T");
175        } else {
176            super.addError(test, t);
177        }
178    }
179
180}
181