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