1 package org.junit.tests.junit3compatibility;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.OutputStream;
5 import java.io.PrintStream;
6
7 import junit.framework.JUnit4TestAdapter;
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 import org.junit.Assert;
14 import org.junit.Test;
15
16 public class ForwardCompatibilityPrintingTest extends TestCase {
17 static class TestResultPrinter extends ResultPrinter {
18 TestResultPrinter(PrintStream writer) {
19 super(writer);
20 }
21
22
23
24
25 @Override
26 protected String elapsedTimeAsString(long runTime) {
27 return "0";
28 }
29 }
30
31 public void testError() {
32 ByteArrayOutputStream output = new ByteArrayOutputStream();
33 TestRunner runner = new TestRunner(new TestResultPrinter(
34 new PrintStream(output)));
35
36 String expected = expected(new String[]{".E", "Time: 0",
37 "Errors here", "", "FAILURES!!!",
38 "Tests run: 1, Failures: 0, Errors: 1", ""});
39 ResultPrinter printer = new TestResultPrinter(new PrintStream(output)) {
40 @Override
41 public void printErrors(TestResult result) {
42 getWriter().println("Errors here");
43 }
44 };
45 runner.setPrinter(printer);
46 TestSuite suite = new TestSuite();
47 suite.addTest(new TestCase() {
48 @Override
49 public void runTest() throws Exception {
50 throw new Exception();
51 }
52 });
53 runner.doRun(suite);
54 assertEquals(expected, output.toString());
55 }
56
57 public static class ATest {
58 @Test
59 public void error() {
60 Assert.fail();
61 }
62 }
63
64 public void testErrorAdapted() {
65 ByteArrayOutputStream output = new ByteArrayOutputStream();
66 TestRunner runner = new TestRunner(new TestResultPrinter(
67 new PrintStream(output)));
68
69 String expected = expected(new String[]{".E", "Time: 0",
70 "Errors here", "", "FAILURES!!!",
71 "Tests run: 1, Failures: 0, Errors: 1", ""});
72 ResultPrinter printer = new TestResultPrinter(new PrintStream(output)) {
73 @Override
74 public void printErrors(TestResult result) {
75 getWriter().println("Errors here");
76 }
77 };
78 runner.setPrinter(printer);
79 runner.doRun(new JUnit4TestAdapter(ATest.class));
80 assertEquals(expected, output.toString());
81 }
82
83 private String expected(String[] lines) {
84 OutputStream expected = new ByteArrayOutputStream();
85 PrintStream expectedWriter = new PrintStream(expected);
86 for (int i = 0; i < lines.length; i++) {
87 expectedWriter.println(lines[i]);
88 }
89 return expected.toString();
90 }
91 }