...8import junit.framework.AssertionFailedError;9import junit.framework.TestCase;10import junit.framework.TestResult;11import junit.framework.TestSuite;12import junit.textui.ResultPrinter;13import junit.textui.TestRunner;1415public class TextFeedbackTest extends TestCase {16 OutputStream output;17 TestRunner runner;18 19 static class TestResultPrinter extends ResultPrinter {20 TestResultPrinter(PrintStream writer) {21 super(writer);22 }23 24 /* Spoof printing time so the tests are deterministic25 */26 @Override27 protected String elapsedTimeAsString(long runTime) {28 return "0";29 }30 }31 32 public static void main(String[] args) {33 TestRunner.run(TextFeedbackTest.class);34 }35 36 @Override37 public void setUp() {38 output= new ByteArrayOutputStream();39 runner= new TestRunner(new TestResultPrinter(new PrintStream(output)));40 }41 42 public void testEmptySuite() {43 String expected= expected(new String[]{"", "Time: 0", "", "OK (0 tests)", ""});44 runner.doRun(new TestSuite());45 assertEquals(expected, output.toString());46 }4748 49 public void testOneTest() {50 String expected= expected(new String[]{".", "Time: 0", "", "OK (1 test)", ""});51 TestSuite suite = new TestSuite();52 suite.addTest(new TestCase() { @Override53 public void runTest() {}});54 runner.doRun(suite);55 assertEquals(expected, output.toString());56 }57 58 public void testTwoTests() {59 String expected= expected(new String[]{"..", "Time: 0", "", "OK (2 tests)", ""});60 TestSuite suite = new TestSuite();61 suite.addTest(new TestCase() { @Override62 public void runTest() {}});63 suite.addTest(new TestCase() { @Override64 public void runTest() {}});65 runner.doRun(suite);66 assertEquals(expected, output.toString());67 }6869 public void testFailure() {70 String expected= expected(new String[]{".F", "Time: 0", "Failures here", "", "FAILURES!!!", "Tests run: 1, Failures: 1, Errors: 0", ""});71 ResultPrinter printer= new TestResultPrinter(new PrintStream(output)) {72 @Override73 public void printFailures(TestResult result) {74 getWriter().println("Failures here");75 }76 };77 runner.setPrinter(printer);78 TestSuite suite = new TestSuite();79 suite.addTest(new TestCase() { @Override80 public void runTest() {throw new AssertionFailedError();}});81 runner.doRun(suite);82 assertEquals(expected, output.toString());83 }84 85 public void testError() {86 String expected= expected(new String[]{".E", "Time: 0", "Errors here", "", "FAILURES!!!", "Tests run: 1, Failures: 0, Errors: 1", ""});87 ResultPrinter printer= new TestResultPrinter(new PrintStream(output)) {88 @Override89 public void printErrors(TestResult result) {90 getWriter().println("Errors here");91 }92 };93 runner.setPrinter(printer);94 TestSuite suite = new TestSuite();95 suite.addTest(new TestCase() { @Override96 public void runTest() throws Exception {throw new Exception();}});97 runner.doRun(suite);98 assertEquals(expected, output.toString());99 }100 101 private String expected(String[] lines) {
...