KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > tests > runner > TextFeedbackTest


1
2 package junit.tests.runner;
3
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.OutputStream JavaDoc;
6 import java.io.PrintStream JavaDoc;
7
8 import junit.framework.AssertionFailedError;
9 import junit.framework.TestCase;
10 import junit.framework.TestResult;
11 import junit.framework.TestSuite;
12 import junit.textui.ResultPrinter;
13 import junit.textui.TestRunner;
14
15 public class TextFeedbackTest extends TestCase {
16     OutputStream JavaDoc output;
17     TestRunner runner;
18     
19     class TestResultPrinter extends ResultPrinter {
20         TestResultPrinter(PrintStream JavaDoc writer) {
21             super(writer);
22         }
23         
24         /* Spoof printing time so the tests are deterministic
25          */

26         protected String JavaDoc elapsedTimeAsString(long runTime) {
27             return "0";
28         }
29     }
30     
31     public static void main(String JavaDoc[] args) {
32         TestRunner.run(TextFeedbackTest.class);
33     }
34     
35     public void setUp() {
36         output= new ByteArrayOutputStream JavaDoc();
37         runner= new TestRunner(new TestResultPrinter(new PrintStream JavaDoc(output)));
38     }
39     
40     public void testEmptySuite() {
41         String JavaDoc expected= expected(new String JavaDoc[]{"", "Time: 0", "", "OK (0 tests)", ""});
42         runner.doRun(new TestSuite());
43         assertEquals(expected.toString(), output.toString());
44     }
45
46     
47     public void testOneTest() {
48         String JavaDoc expected= expected(new String JavaDoc[]{".", "Time: 0", "", "OK (1 test)", ""});
49         TestSuite suite = new TestSuite();
50         suite.addTest(new TestCase() { public void runTest() {}});
51         runner.doRun(suite);
52         assertEquals(expected.toString(), output.toString());
53     }
54     
55     public void testTwoTests() {
56         String JavaDoc expected= expected(new String JavaDoc[]{"..", "Time: 0", "", "OK (2 tests)", ""});
57         TestSuite suite = new TestSuite();
58         suite.addTest(new TestCase() { public void runTest() {}});
59         suite.addTest(new TestCase() { public void runTest() {}});
60         runner.doRun(suite);
61         assertEquals(expected.toString(), output.toString());
62     }
63
64     public void testFailure() {
65         String JavaDoc expected= expected(new String JavaDoc[]{".F", "Time: 0", "Failures here", "", "FAILURES!!!", "Tests run: 1, Failures: 1, Errors: 0", ""});
66         ResultPrinter printer= new TestResultPrinter(new PrintStream JavaDoc(output)) {
67             public void printFailures(TestResult result) {
68                 getWriter().println("Failures here");
69             }
70         };
71         runner.setPrinter(printer);
72         TestSuite suite = new TestSuite();
73         suite.addTest(new TestCase() { public void runTest() {throw new AssertionFailedError();}});
74         runner.doRun(suite);
75         assertEquals(expected.toString(), output.toString());
76     }
77     
78     public void testError() {
79         String JavaDoc expected= expected(new String JavaDoc[]{".E", "Time: 0", "Errors here", "", "FAILURES!!!", "Tests run: 1, Failures: 0, Errors: 1", ""});
80         ResultPrinter printer= new TestResultPrinter(new PrintStream JavaDoc(output)) {
81             public void printErrors(TestResult result) {
82                 getWriter().println("Errors here");
83             }
84         };
85         runner.setPrinter(printer);
86         TestSuite suite = new TestSuite();
87         suite.addTest(new TestCase() { public void runTest() throws Exception JavaDoc {throw new Exception JavaDoc();}});
88         runner.doRun(suite);
89         assertEquals(expected.toString(), output.toString());
90     }
91     
92     private String JavaDoc expected(String JavaDoc[] lines) {
93         OutputStream JavaDoc expected= new ByteArrayOutputStream JavaDoc();
94         PrintStream JavaDoc expectedWriter= new PrintStream JavaDoc(expected);
95         for (int i= 0; i < lines.length; i++)
96             expectedWriter.println(lines[i]);
97         return expected.toString();
98     }
99
100 }
101
Popular Tags