1 package junit.tests.runner;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.OutputStream;
5 import java.io.PrintStream;
6
7 import junit.framework.AssertionFailedError;
8 import junit.framework.TestCase;
9 import junit.framework.TestResult;
10 import junit.framework.TestSuite;
11 import junit.textui.ResultPrinter;
12 import junit.textui.TestRunner;
13
14 public class TextFeedbackTest extends TestCase {
15 OutputStream output;
16 TestRunner runner;
17
18 static class TestResultPrinter extends ResultPrinter {
19 TestResultPrinter(PrintStream writer) {
20 super(writer);
21 }
22
23
24
25 @Override
26 protected String elapsedTimeAsString(long runTime) {
27 return "0";
28 }
29 }
30
31 public static void main(String[] args) {
32 TestRunner.run(TextFeedbackTest.class);
33 }
34
35 @Override
36 public void setUp() {
37 output = new ByteArrayOutputStream();
38 runner = new TestRunner(new TestResultPrinter(new PrintStream(output)));
39 }
40
41 public void testEmptySuite() {
42 String expected = expected(new String[]{"", "Time: 0", "", "OK (0 tests)", ""});
43 runner.doRun(new TestSuite());
44 assertEquals(expected, output.toString());
45 }
46
47
48 public void testOneTest() {
49 String expected = expected(new String[]{".", "Time: 0", "", "OK (1 test)", ""});
50 TestSuite suite = new TestSuite();
51 suite.addTest(new TestCase() {
52 @Override
53 public void runTest() {
54 }
55 });
56 runner.doRun(suite);
57 assertEquals(expected, output.toString());
58 }
59
60 public void testTwoTests() {
61 String expected = expected(new String[]{"..", "Time: 0", "", "OK (2 tests)", ""});
62 TestSuite suite = new TestSuite();
63 suite.addTest(new TestCase() {
64 @Override
65 public void runTest() {
66 }
67 });
68 suite.addTest(new TestCase() {
69 @Override
70 public void runTest() {
71 }
72 });
73 runner.doRun(suite);
74 assertEquals(expected, output.toString());
75 }
76
77 public void testFailure() {
78 String expected = expected(new String[]{".F", "Time: 0", "Failures here", "", "FAILURES!!!", "Tests run: 1, Failures: 1, Errors: 0", ""});
79 ResultPrinter printer = new TestResultPrinter(new PrintStream(output)) {
80 @Override
81 public void printFailures(TestResult result) {
82 getWriter().println("Failures here");
83 }
84 };
85 runner.setPrinter(printer);
86 TestSuite suite = new TestSuite();
87 suite.addTest(new TestCase() {
88 @Override
89 public void runTest() {
90 throw new AssertionFailedError();
91 }
92 });
93 runner.doRun(suite);
94 assertEquals(expected, output.toString());
95 }
96
97 public void testError() {
98 String expected = expected(new String[]{".E", "Time: 0", "Errors here", "", "FAILURES!!!", "Tests run: 1, Failures: 0, Errors: 1", ""});
99 ResultPrinter printer = new TestResultPrinter(new PrintStream(output)) {
100 @Override
101 public void printErrors(TestResult result) {
102 getWriter().println("Errors here");
103 }
104 };
105 runner.setPrinter(printer);
106 TestSuite suite = new TestSuite();
107 suite.addTest(new TestCase() {
108 @Override
109 public void runTest() throws Exception {
110 throw new Exception();
111 }
112 });
113 runner.doRun(suite);
114 assertEquals(expected, output.toString());
115 }
116
117 private String expected(String[] lines) {
118 OutputStream expected = new ByteArrayOutputStream();
119 PrintStream expectedWriter = new PrintStream(expected);
120 for (int i = 0; i < lines.length; i++) {
121 expectedWriter.println(lines[i]);
122 }
123 return expected.toString();
124 }
125
126 }