1 /* 2 Copyright (c) 2013, 2021, Oracle and/or its affiliates. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2.0, 6 as published by the Free Software Foundation. 7 8 This program is also distributed with certain software (including 9 but not limited to OpenSSL) that is licensed under separate terms, 10 as designated in a particular file or component or in included license 11 documentation. The authors of MySQL hereby grant you an additional 12 permission to link the program and your derivative works with the 13 separately licensed software that they have included with MySQL. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License, version 2.0, for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software 22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 */ 24 25 /* 26 * This assortment of classes is a mock http://en.wikipedia.org/wiki/Mock_object 27 * implementation of junit http://en.wikipedia.org/wiki/Junit. It contains annotations, 28 * classes, and interfaces that mock junit for use with test classes 29 * that use a subset of junit functionality. 30 * <p> 31 * In clusterj, test classes can use either the real junit or this mock junit. 32 * The mock can be used stand-alone or invoked by the maven surefire junit plugin. 33 * Other test runners and harnesses might not have been tested and might not work. 34 * <p> 35 * There is no code copied from Junit itself. Only concepts and names of 36 * annotations, interfaces, classes, and methods are copied, which must exactly match 37 * the corresponding items from junit in order to be mocked. 38 */ 39 40 package junit.textui; 41 42 import java.io.PrintStream; 43 import java.io.PrintWriter; 44 import java.io.StringWriter; 45 46 import junit.framework.AssertionFailedError; 47 import junit.framework.Test; 48 import junit.framework.TestListener; 49 50 /** This class implements TestListener which monitors the execution of tests and tracks errors and failures. 51 * It is implemented as part of the test runner framework. For each error and failure, collect the relevant 52 * information in a message buffer. 53 * When the test is over, the error and failure information is printed in reportErrors(); 54 */ 55 public class ResultPrinter implements TestListener { 56 57 /** the test number */ 58 int testNumber = 0; 59 60 /** the printer */ 61 PrintStream printer = System.out; 62 63 /** the message buffer */ 64 StringBuilder messages = new StringBuilder(); 65 66 /** constructor with printer to report immediate results */ ResultPrinter(PrintStream printer)67 public ResultPrinter(PrintStream printer) { 68 this.printer = printer; 69 } 70 71 /** An error (exception) occurred during the execution of the test. 72 */ addError(Test test, Throwable t)73 public void addError(Test test, Throwable t) { 74 // report status immediately 75 printer.print("ERROR..."); 76 // remember details 77 messages.append(testNumber); 78 messages.append(": "); 79 messages.append(test.toString()); 80 messages.append(" FAILED:\n"); 81 StringWriter stringWriter = new StringWriter(); 82 PrintWriter stackPrinter = new PrintWriter(stringWriter); 83 t.printStackTrace(stackPrinter); 84 messages.append(stringWriter.toString()); 85 messages.append("\n"); 86 } 87 88 /** A failure (junit assertion) occurred during the execution of the test. 89 */ addFailure(Test test, AssertionFailedError t)90 public void addFailure(Test test, AssertionFailedError t) { 91 // report status immediately 92 printer.print("FAILURE..."); 93 // remember details 94 messages.append(testNumber); 95 messages.append(": "); 96 messages.append(test.toString()); 97 messages.append(" FAILED:\n"); 98 messages.append(t.getMessage()); 99 messages.append("\n"); 100 } 101 102 /** A test ended. Report immediately. 103 */ endTest(Test test)104 public void endTest(Test test) { 105 printer.println(); 106 } 107 108 /** A test started. Report status immediately. 109 */ startTest(Test test)110 public void startTest(Test test) { 111 testNumber++; 112 printer.print(testNumber + ": " + test.toString() + " running..."); 113 } 114 115 /** Return the results of the tests. 116 */ 117 @Override toString()118 public String toString() { 119 return messages.toString(); 120 } 121 122 } 123