Java cvprof Coverage Report for
TextFeedbackTest.java

    1   
    2   package junit.tests.runner;
    3   
    4   import java.io.ByteArrayOutputStream;
    5   import java.io.OutputStream;
    6   import java.io.PrintStream;
    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 output;
   17   	TestRunner runner;
   18   	
   19   	class TestResultPrinter extends ResultPrinter {
   20   		TestResultPrinter(PrintStream writer) {
   21   			super(writer);
   22   		}
   23   		
   24   		/* Spoof printing time so the tests are deterministic
   25   		 */
   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   	public void setUp() {
   36   		output= new ByteArrayOutputStream();
   37   		runner= new TestRunner(new TestResultPrinter(new PrintStream(output)));
   38   	}
   39   	
   40   	public void testEmptySuite() {
   41   		String expected= expected(new String[]{"", "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 expected= expected(new String[]{".", "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 expected= expected(new String[]{"..", "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 expected= expected(new String[]{".F", "Time: 0", "Failures here", "", "FAILURES!!!", "Tests run: 1,  Failures: 1,  Errors: 0", ""});
   66   		ResultPrinter printer= new TestResultPrinter(new PrintStream(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 expected= expected(new String[]{".E", "Time: 0", "Errors here", "", "FAILURES!!!", "Tests run: 1,  Failures: 0,  Errors: 1", ""});
   80   		ResultPrinter printer= new TestResultPrinter(new PrintStream(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 {throw new Exception();}});
   88   		runner.doRun(suite);
   89   		assertEquals(expected.toString(), output.toString());
   90   	}
   91   	
   92   	private String expected(String[] lines) {
   93   		OutputStream expected= new ByteArrayOutputStream();
   94   		PrintStream expectedWriter= new PrintStream(expected);
   95   		for (int i= 0; i < lines.length; i++)
   96   			expectedWriter.println(lines[i]);
   97   		return expected.toString(); 
   98   	}
   99   
  100   }