How to use printFooter method of junit.textui.ResultPrinter class

Best junit code snippet using junit.textui.ResultPrinter.printFooter

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package utfx.printers;
2
3import java.io.OutputStream;
4import java.io.PrintStream;
5
6import junit.framework.Test;
7import junit.framework.TestCase;
8import junit.framework.TestListener;
9import junit.framework.TestResult;
10import junit.framework.TestSuite;
11
12import org.apache.log4j.Logger;
13
14import utfx.framework.ConfigurationManager;
15
16/**
17 * Abstract result printer. Result printers are used for printing test run
18 * results. General idea based on <code>junit.textui.ResultPrinter</code>. In
19 * this implementation we allow for different result printers to be attached to
20 * the TestResult object and print the results of a test run in various ways.
21 * 
22 * <p>
23 * Copyright &copy; 2004 - <a href="http://www.usq.edu.au"> University of
24 * Southern Queensland. </a>
25 * </p>
26 * 
27 * <p>
28 * This program is free software; you can redistribute it and/or modify it under
29 * the terms of the <a href="http://www.gnu.org/licenses/gpl.txt">GNU General
30 * Public License v2 </a> as published by the Free Software Foundation.
31 * </p>
32 * 
33 * <p>
34 * This program is distributed in the hope that it will be useful, but WITHOUT
35 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
36 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
37 * details.
38 * </p>
39 * 
40 * <code>
41 * $Source: /cvs/utf-x/framework/src/java/utfx/printers/ResultPrinter.java,v $
42 * </code>
43 * 
44 * @author Jacek Radajewski
45 * @version $Revision$ $Date$ $Name:  $
46 */
47public abstract class ResultPrinter extends PrintStream implements TestListener {
48
49    /** the depth of the current suite; root test suite = 0 */
50    protected int suiteDepth = -1;
51
52    /** total test case conter; first test = 1 */
53    protected int testCaseCount = 0;
54
55    /** indent string */
56    protected String indentString = "    ";
57
58    /** start time in milliseconds */
59    private long startTime;
60
61    /** loggin facility */
62    protected Logger log;
63
64    /**
65     * Construnct a new Result Printer from an OutputStream.
66     */
67    public ResultPrinter(OutputStream os) {
68        super(os);
69        log = Logger.getLogger("utfx.framework");        
70    }
71
72    /**
73     * @return indent string
74     */
75    public String getIndent(int depth) {
76        StringBuffer indent = new StringBuffer(indentString.length() * depth);
77
78        for (int i = 0; i < depth; i++) {
79            indent.append(indentString);
80        }
81        return indent.toString();
82    }
83
84    /**
85     * Print the result summary. This method should be called at the end of a
86     * test run to produce a regression test summary.
87     * 
88     * based on <code>junit.textui.ResultPrinter</code>
89     * 
90     * @param result test result
91     */
92    public void printSummary(TestResult result) {
93        log.debug("calling printHeader() ...");
94        printHeader(result);
95        log.debug("calling printErrors() ...");
96        printErrors(result);
97        log.debug("calling printFailures() ...");
98        printFailures(result);
99        log.debug("calling printFooter() ...");
100        printFooter(result);
101    }
102
103    /**
104     * Print report header. Based on <code>junit.textui.ResultPrinter</code>
105     */
106    public abstract void printHeader(TestResult result);
107
108    /**
109     * Print report header. Based on <code>junit.textui.ResultPrinter</code>
110     */
111    public abstract void printFooter(TestResult result);
112
113    /**
114     * Print errors. Based on <code>junit.textui.ResultPrinter</code>
115     * 
116     * @param result test result
117     */
118    public abstract void printErrors(TestResult result);
119
120    /**
121     * Print failures. Based on <code>junit.textui.ResultPrinter</code>
122     * 
123     * @param result test result
124     */
125    public abstract void printFailures(TestResult result);
126
127    /**
128     * Start of a test case.
129     * 
130     * @param testCase the TestSuite we are entering.
131     */
132    public abstract void startTestCase(TestCase testCase);
133
134    /**
135     * End of a test case.
136     * 
137     * @param testCase
138     */
139    public abstract void endTestCase(TestCase testCase);
140
141    /**
142     * Start of a test suite.
143     * 
144     * @param suite the TestSuite we are entering.
145     */
146    public abstract void startTestSuite(TestSuite suite);
147
148    /**
149     * End of a test suite.
150     * 
151     * @param suite the test suite we are leaving
152     */
153    public abstract void endTestSuite(TestSuite suite);
154
155    /**
156     * Start a test
157     */
158    public final void startTest(Test test) {
159        if (test instanceof TestSuite) {
160            suiteDepth++;
161            startTestSuite((TestSuite) test);
162        } else if (test instanceof TestCase) {
163            testCaseCount++;
164            startTestCase((TestCase) test);
165        }
166    }
167
168    /**
169     * end a test
170     */
171    public final void endTest(Test test) {
172        if (test instanceof TestSuite) {
173            endTestSuite((TestSuite) test);
174            suiteDepth--;
175        } else if (test instanceof TestCase) {
176            endTestCase((TestCase) test);
177        }
178    }
179
180    /**
181     * This method is called before any test runs.
182     */
183    public void start() {
184        startTime = System.currentTimeMillis();
185    }
186
187    /**
188     * Print the number of milli seconds that elapsed since the start() method
189     * was called.
190     */
191    public void printElapsedTimeMillis() {
192        print(System.currentTimeMillis() - startTime);
193    }
194
195//    public void print
196    
197    /**
198     * Specific result printers can se the indent string to what ever they like.
199     * 
200     * @param indentString new indent string. This string will be used as a
201     *        single unit of indentation. Indentation = indentString *
202     *        suiteDepth.
203     */
204    protected void setIndentString(String indentString) {
205        this.indentString = indentString;
206    }
207
208    /**
209     *  
210     */
211    public void printIndent() {
212        StringBuffer indent = new StringBuffer(indentString.length()
213                * suiteDepth);
214
215        for (int i = 0; i < suiteDepth; i++) {
216            indent.append(indentString);
217        }
218        print(indent);
219    }
220}
Full Screen
copy
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 * 
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 * 
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package org.apache.uima.test.junit_extension;
21
22import java.io.PrintStream;
23import java.text.NumberFormat;
24import java.util.Enumeration;
25
26import junit.framework.AssertionFailedError;
27import junit.framework.Test;
28import junit.framework.TestFailure;
29import junit.framework.TestListener;
30import junit.framework.TestResult;
31import junit.runner.BaseTestRunner;
32import junit.textui.ResultPrinter;
33
34/**
35 * UIMAResultPrinter is a ResultPrinter extension for the JUnit framework.
36 */
37public class UIMAResultPrinter extends ResultPrinter implements TestListener {
38  // print stream for the output
39  private PrintStream fWriter;
40
41  // current column in line
42  private int fColumn = 0;
43
44  // test case counter
45  private int testCounter;
46
47  // name of the current test class
48  private String currentTestClass;
49
50  // success status of the current test method
51  private boolean currentTestSuccess;
52
53  // abort execution if an error occurs
54  private boolean abortOnFail;
55
56  // dublicated output stream, change output layout
57  private boolean teeOutputStream;
58
59  public UIMAResultPrinter(PrintStream writer, boolean abortOnFail, boolean teeOutputStream) {
60    // init class members
61    super(writer);
62    this.fWriter = writer;
63    this.testCounter = 0;
64    this.currentTestClass = null;
65    this.currentTestSuccess = true;
66    this.abortOnFail = abortOnFail;
67    this.teeOutputStream = teeOutputStream;
68  }
69
70  /**
71   * @see junit.textui.ResultPrinter#printHeader(long)
72   */
73  @Override
74  protected void printHeader(long runTime) {
75    getWriter().println();
76    getWriter().println();
77    getWriter().println("Time: " + elapsedTimeAsString(runTime));
78  }
79
80  /**
81   * @see junit.textui.ResultPrinter#printErrors(junit.framework.TestResult)
82   */
83  @Override
84  protected void printErrors(TestResult result) {
85    printDefects(result.errors(), result.errorCount(), "error");
86  }
87
88  /**
89   * @see junit.textui.ResultPrinter#printFailures(junit.framework.TestResult)
90   */
91  @Override
92  protected void printFailures(TestResult result) {
93    printDefects(result.failures(), result.failureCount(), "failure");
94  }
95
96  /**
97   * @see junit.textui.ResultPrinter#printDefects(java.util.Enumeration, int, java.lang.String)
98   */
99  @Override
100  protected void printDefects(Enumeration booBoos, int count, String type) {
101    if (count == 0)
102      return;
103    if (count == 1)
104      getWriter().println("There was " + count + " " + type + ":");
105    else
106      getWriter().println("There were " + count + " " + type + "s:");
107    for (int i = 1; booBoos.hasMoreElements(); i++) {
108      printDefect((TestFailure) booBoos.nextElement(), i);
109    }
110  }
111
112  /**
113   * @see junit.textui.ResultPrinter#printDefect(junit.framework.TestFailure, int)
114   */
115  @Override
116  public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes
117    printDefectHeader(booBoo, count);
118    printDefectTrace(booBoo);
119  }
120
121  /**
122   * @see junit.textui.ResultPrinter#printDefectHeader(junit.framework.TestFailure, int)
123   */
124  @Override
125  protected void printDefectHeader(TestFailure booBoo, int count) {
126    // I feel like making this a println, then adding a line giving the throwable a chance to print
127    // something
128    // before we get to the stack trace.
129    getWriter().print(count + ") " + booBoo.failedTest());
130  }
131
132  /**
133   * @see junit.textui.ResultPrinter#printDefectTrace(junit.framework.TestFailure)
134   */
135  @Override
136  protected void printDefectTrace(TestFailure booBoo) {
137    getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace()));
138  }
139
140  /**
141   * @see junit.textui.ResultPrinter#printFooter(junit.framework.TestResult)
142   */
143  @Override
144  protected void printFooter(TestResult result) {
145    if (result.wasSuccessful()) {
146      getWriter().println();
147      getWriter().print("OK");
148      getWriter().println(
149              " (" + result.runCount() + " test" + (result.runCount() == 1 ? "" : "s") + ")");
150
151    } else {
152      getWriter().println();
153      getWriter().println("FAILURES!!!");
154      getWriter().println("Tests run: " + result.runCount() + ",  Failures: "
155              + result.failureCount() + ",  Errors: " + result.errorCount());
156    }
157    getWriter().println();
158  }
159
160  /**
161   * Returns the formatted string of the elapsed time. Duplicated from BaseTestRunner. Fix it.
162   */
163  @Override
164  protected String elapsedTimeAsString(long runTime) {
165    return NumberFormat.getInstance().format((double) runTime / 1000);
166  }
167
168  /**
169   * @see junit.textui.ResultPrinter#getWriter()
170   */
171  @Override
172  public PrintStream getWriter() {
173    return this.fWriter;
174  }
175
176  /**
177   * @see junit.framework.TestListener#addError(Test, Throwable)
178   */
179  @Override
180  public void addError(Test test, Throwable t) {
181    getWriter().print("error");
182    this.currentTestSuccess = false;
183
184    if (this.abortOnFail) {
185      getWriter().println();
186      getWriter().println();
187      getWriter().println("Stop executing testcases...");
188      getWriter().println("Print Stacktrace: ");
189      getWriter().println();
190      StackTraceElement[] stackTrace = t.getStackTrace();
191      for (int i = 0; i < stackTrace.length; i++) {
192        getWriter().println(stackTrace[i].toString());
193      }
194
195      throw new RuntimeException("Abort on error");
196    }
197  }
198
199  /**
200   * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError)
201   */
202  @Override
203  public void addFailure(Test test, AssertionFailedError t) {
204    getWriter().print("failure");
205    this.currentTestSuccess = false;
206    if (this.abortOnFail) {
207      getWriter().println();
208      getWriter().println();
209      getWriter().println("Stop executing testcases...");
210      getWriter().println("Print Stacktrace: ");
211      getWriter().println();
212      StackTraceElement[] stackTrace = t.getStackTrace();
213      for (int i = 0; i < stackTrace.length; i++) {
214        getWriter().println(stackTrace[i].toString());
215      }
216
217      throw new RuntimeException("Abort on failure");
218    }
219  }
220
221  /**
222   * @see junit.framework.TestListener#endTest(Test)
223   */
224  @Override
225  public void endTest(Test test) {
226    if (this.currentTestSuccess == false)
227      this.currentTestSuccess = true;
228    else
229      getWriter().print("ok");
230  }
231
232  /**
233   * @see junit.framework.TestListener#startTest(Test)
234   */
235  @Override
236  public void startTest(Test test) {
237    this.testCounter++;
238    String name = test.toString();
239    String tempCurrentTestClass = name.substring(name.indexOf('(') + 1, name.lastIndexOf(')'));
240    String testName = name.substring(0, name.indexOf('('));
241
242    if (!tempCurrentTestClass.equals(this.currentTestClass)) {
243      this.currentTestClass = tempCurrentTestClass;
244      getWriter().println();
245      getWriter().println();
246      getWriter().print(this.currentTestClass);
247      getWriter().println();
248      for (int i = 0; i < this.currentTestClass.length(); i++)
249        getWriter().print("=");
250    }
251
252    getWriter().println();
253    getWriter().print(this.testCounter + ":  " + testName + ": ");
254    if (this.fColumn++ >= 40 || this.teeOutputStream) {
255      getWriter().println();
256      this.fColumn = 0;
257    }
258  }
259}
260
Full Screen
copy
1package utfx.printers;
2
3import java.io.OutputStream;
4import java.util.Enumeration;
5
6import junit.framework.*;
7import junit.runner.BaseTestRunner;
8
9/**
10 * Simple result printer which produces output similar to that of
11 * <code>junit.textui.ResultPrinter</code>. This result printer is based on
12 * <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich Gamma.
13 * 
14 * <p>
15 * Copyright &copy; 2004 - <a href="http://www.usq.edu.au"> University of
16 * Southern Queensland. </a>
17 * </p>
18 * 
19 * <p>
20 * This program is free software; you can redistribute it and/or modify it under
21 * the terms of the <a href="http://www.gnu.org/licenses/gpl.txt">GNU General
22 * Public License v2 </a> as published by the Free Software Foundation.
23 * </p>
24 * 
25 * <p>
26 * This program is distributed in the hope that it will be useful, but WITHOUT
27 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
28 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
29 * details.
30 * </p>
31 * 
32 * <code>
33 * $Source: /cvs/utf-x/framework/src/java/utfx/printers/JunitResultPrinter.java,v $
34 * </code>
35 * 
36 * @author Jacek Radajewski
37 * @author Kent Beck and Erich Gamma (authors of
38 *         <code>junit.textui.ResultPrinter</code>)
39 * @version $Revision$ $Date$ $Name:  $
40 */
41public class JunitResultPrinter extends ResultPrinter {
42
43    /** column number */
44    private int colNum = 0;
45
46    public JunitResultPrinter(OutputStream os) {
47        super(os);
48    }
49
50    /**
51     * Print summary header.
52     * 
53     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
54     * Gamma.
55     * 
56     * @see junit.textui.ResultPrinter
57     */
58    public void printHeader(TestResult result) {
59        println();
60        print("Time: ");
61        printElapsedTimeMillis();
62        println();
63    }
64
65    /**
66     * Print errors.
67     * 
68     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
69     * Gamma.
70     * 
71     * @see junit.textui.ResultPrinter
72     */
73    public void printErrors(TestResult result) {
74        printDefects(result.errors(), result.errorCount(), "error");
75    }
76
77    /**
78     * Print failures
79     * 
80     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
81     * Gamma.
82     * 
83     * @see junit.textui.ResultPrinter
84     */
85    public void printFailures(TestResult result) {
86        printDefects(result.failures(), result.failureCount(), "failure");
87    }
88
89    /**
90     * Print defects.
91     * 
92     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
93     * Gamma.
94     * 
95     * @see junit.textui.ResultPrinter
96     */
97    public void printDefects(Enumeration booBoos, int count, String type) {
98        if (count == 0) {
99            return;
100        }
101        if (count == 1) {
102            println("There was " + count + " " + type + ":");
103        } else {
104            println("There were " + count + " " + type + "s:");
105        }
106        for (int i = 1; booBoos.hasMoreElements(); i++) {
107            printDefect((TestFailure) booBoos.nextElement(), i);
108        }
109    }
110
111    /**
112     * Print a defect.
113     * 
114     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
115     * Gamma.
116     * 
117     * @see junit.textui.ResultPrinter
118     */
119    public void printDefect(TestFailure booBoo, int count) {
120        printDefectHeader(booBoo, count);
121        printDefectTrace(booBoo);
122    }
123
124    /**
125     * Print defect header.
126     * 
127     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
128     * Gamma.
129     * 
130     * @see junit.textui.ResultPrinter
131     */
132    public void printDefectHeader(TestFailure booBoo, int count) {
133        print("[" + count + "] " + booBoo.failedTest());
134    }
135
136    /**
137     * Print defect trace.
138     * 
139     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
140     * Gamma.
141     * 
142     * @see junit.textui.ResultPrinter
143     */
144    public void printDefectTrace(TestFailure booBoo) {
145        print(BaseTestRunner.getFilteredTrace(booBoo.trace()));
146    }
147
148    /**
149     * Print footer.
150     * 
151     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
152     * Gamma.
153     * 
154     * @see junit.textui.ResultPrinter
155     */
156    public void printFooter(TestResult result) {
157        if (result.wasSuccessful()) {
158            println();
159            print("OK");
160            println(" (" + result.runCount() + " test"
161                    + (result.runCount() == 1 ? "" : "s") + ")");
162        } else {
163            println();
164            println("FAILURES!!!");
165            println("Tests run: " + result.runCount() + ",  Failures: "
166                    + result.failureCount() + ",  Errors: "
167                    + result.errorCount());
168        }
169        println();
170    }
171
172    /**
173     * Print error symbol.
174     * 
175     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
176     * Gamma.
177     * 
178     * @see junit.textui.ResultPrinter
179     * @see junit.framework.TestListener#addError(Test, Throwable)
180     */
181    public void addError(Test test, Throwable t) {
182        print('E');
183    }
184
185    /**
186     * Print failure symbol.
187     * 
188     * Based on <code>junit.textui.ResultPrinter</code> by Kent Beck and Erich
189     * Gamma.
190     * 
191     * @see junit.textui.ResultPrinter
192     * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError)
193     */
194    public void addFailure(Test test, AssertionFailedError t) {
195        print('F');
196    }
197
198    /**
199     * Start of a test case.
200     * 
201     * @param testCase the TestSuite we are entering.
202     */
203    public void startTestCase(TestCase testCase) {
204        print('.');
205
206        // just like the original Junit printer we only print up to 40
207        // columns wide
208        if (colNum++ >= 40) {
209            println();
210            colNum = 0;
211        }
212    }
213
214    /**
215     * End of a test case.
216     * 
217     * @param testCase
218     */
219    public void endTestCase(TestCase testCase) {
220        // nothing to do here
221    }
222
223    /**
224     * Start of a test suite.
225     * 
226     * @param suite the TestSuite we are entering.
227     */
228    public void startTestSuite(TestSuite suite) {
229        // nothing to do here
230    }
231
232    /**
233     * End of a test suite.
234     * 
235     * @param suite the TestSuite we are leaving
236     */
237    public void endTestSuite(TestSuite suite) {
238        // nothing to do here
239    }
240
241}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger printFooter code on LambdaTest Cloud Grid

Execute automation tests with printFooter on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)