Home | History | Annotate | Download | only in coretests
      1 /*
      2  * Copyright (C) 2008 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  */
     16 
     17 package com.google.coretests;
     18 
     19 import java.io.PrintStream;
     20 // The following line was removed for compatibility with Android libraries.
     21 //import java.text.NumberFormat;
     22 import java.util.Enumeration;
     23 
     24 import junit.framework.AssertionFailedError;
     25 import junit.framework.Test;
     26 import junit.framework.TestFailure;
     27 import junit.framework.TestListener;
     28 import junit.framework.TestResult;
     29 import junit.runner.BaseTestRunner;
     30 
     31 public class ResultPrinter implements TestListener {
     32     PrintStream fWriter;
     33     int fColumn= 0;
     34 
     35     public ResultPrinter(PrintStream writer) {
     36         fWriter= writer;
     37     }
     38 
     39     /* API for use by textui.TestRunner
     40      */
     41 
     42     synchronized void print(TestResult result, long runTime) {
     43         printHeader(runTime);
     44         printErrors(result);
     45         printFailures(result);
     46         printFooter(result);
     47     }
     48 
     49     void printWaitPrompt() {
     50         getWriter().println();
     51         getWriter().println("<RETURN> to continue");
     52     }
     53 
     54     /* Internal methods
     55      */
     56 
     57     protected void printHeader(long runTime) {
     58         getWriter().println();
     59         getWriter().println("Time: "+elapsedTimeAsString(runTime));
     60     }
     61 
     62     protected void printErrors(TestResult result) {
     63         printDefects(result.errors(), result.errorCount(), "error");
     64     }
     65 
     66     protected void printFailures(TestResult result) {
     67         printDefects(result.failures(), result.failureCount(), "failure");
     68     }
     69 
     70     protected void printDefects(Enumeration booBoos, int count, String type) {
     71         if (count == 0) return;
     72         if (count == 1)
     73             getWriter().println("There was " + count + " " + type + ":");
     74         else
     75             getWriter().println("There were " + count + " " + type + "s:");
     76         for (int i= 1; booBoos.hasMoreElements(); i++) {
     77             printDefect((TestFailure) booBoos.nextElement(), i);
     78         }
     79     }
     80 
     81     public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes
     82         printDefectHeader(booBoo, count);
     83         printDefectTrace(booBoo);
     84     }
     85 
     86     protected void printDefectHeader(TestFailure booBoo, int count) {
     87         // I feel like making this a println, then adding a line giving the throwable a chance to print something
     88         // before we get to the stack trace.
     89         getWriter().print(count + ") " + booBoo.failedTest());
     90     }
     91 
     92     protected void printDefectTrace(TestFailure booBoo) {
     93         getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace()));
     94     }
     95 
     96     protected void printFooter(TestResult result) {
     97         if (result.wasSuccessful()) {
     98             getWriter().println();
     99             getWriter().print("OK");
    100             getWriter().println (" (" + result.runCount() + " test" + (result.runCount() == 1 ? "": "s") + ")");
    101 
    102         } else {
    103             getWriter().println();
    104             getWriter().println("FAILURES!!!");
    105             getWriter().println("Tests run: "+result.runCount()+
    106                          ",  Failures: "+result.failureCount()+
    107                          ",  Errors: "+result.errorCount());
    108         }
    109         getWriter().println();
    110     }
    111 
    112 
    113     /**
    114      * Returns the formatted string of the elapsed time.
    115      * Duplicated from BaseTestRunner. Fix it.
    116      */
    117     protected String elapsedTimeAsString(long runTime) {
    118             // The following line was altered for compatibility with
    119             // Android libraries.
    120         return Double.toString((double)runTime/1000);
    121     }
    122 
    123     public PrintStream getWriter() {
    124         return fWriter;
    125     }
    126     /**
    127      * @see junit.framework.TestListener#addError(Test, Throwable)
    128      */
    129     public void addError(Test test, Throwable t) {
    130         getWriter().print("E");
    131     }
    132 
    133     /**
    134      * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError)
    135      */
    136     public void addFailure(Test test, AssertionFailedError t) {
    137         getWriter().print("F");
    138     }
    139 
    140     /**
    141      * @see junit.framework.TestListener#endTest(Test)
    142      */
    143     public void endTest(Test test) {
    144     }
    145 
    146     /**
    147      * @see junit.framework.TestListener#startTest(Test)
    148      */
    149     public void startTest(Test test) {
    150         getWriter().print(".");
    151         if (fColumn++ >= 40) {
    152             getWriter().println();
    153             fColumn= 0;
    154         }
    155     }
    156 
    157 }
    158