1 17 package org.apache.tools.ant.taskdefs.optional.junit; 18 19 import java.io.*; 20 import junit.framework.*; 21 import org.apache.tools.ant.BuildException; 22 23 28 public class JUnitTestRunnerTest extends TestCase { 29 30 public JUnitTestRunnerTest(String name){ 32 super(name); 33 } 34 35 public void testNoSuite(){ 37 TestRunner runner = createRunner(NoSuiteTestCase.class); 38 runner.run(); 39 assertEquals(runner.getFormatter().getError(), JUnitTestRunner.SUCCESS, runner.getRetCode()); 40 } 41 42 public void testSuite(){ 44 TestRunner runner = createRunner(SuiteTestCase.class); 45 runner.run(); 46 assertEquals(runner.getFormatter().getError(), JUnitTestRunner.SUCCESS, runner.getRetCode()); 47 } 48 49 public void testInvalidSuite(){ 51 TestRunner runner = createRunner(InvalidSuiteTestCase.class); 52 runner.run(); 53 String error = runner.getFormatter().getError(); 54 assertEquals(error, JUnitTestRunner.ERRORS, runner.getRetCode()); 55 assertTrue(error, error.indexOf("thrown on purpose") != -1); 56 } 57 58 public void testNoTestCase(){ 61 TestRunner runner = createRunner(NoTestCase.class); 62 runner.run(); 63 assertEquals(runner.getFormatter().getError(), JUnitTestRunner.FAILURES, runner.getRetCode()); 64 } 65 66 public void testInvalidTestCase(){ 68 TestRunner runner = createRunner(InvalidTestCase.class); 69 runner.run(); 70 String error = runner.getFormatter().getError(); 71 assertEquals(error, JUnitTestRunner.FAILURES, runner.getRetCode()); 72 } 75 76 protected TestRunner createRunner(Class clazz){ 77 return new TestRunner(new JUnitTest(clazz.getName()), true, true, true); 78 } 79 80 private final static class TestRunner extends JUnitTestRunner { 82 private ResultFormatter formatter = new ResultFormatter(); 83 TestRunner(JUnitTest test, boolean haltonerror, boolean filtertrace, boolean haltonfailure){ 84 super(test, haltonerror, filtertrace, haltonfailure, TestRunner.class.getClassLoader()); 85 addFormatter(formatter); 89 } 90 ResultFormatter getFormatter(){ 91 return formatter; 92 } 93 } 94 95 private final static class ResultFormatter implements JUnitResultFormatter { 97 private Throwable error; 98 public void setSystemOutput(String output){} 99 public void setSystemError(String output){} 100 public void startTestSuite(JUnitTest suite) throws BuildException{} 101 public void endTestSuite(JUnitTest suite) throws BuildException{} 102 public void setOutput(java.io.OutputStream out){} 103 public void startTest(Test t) {} 104 public void endTest(Test test) {} 105 public void addFailure(Test test, Throwable t) { } 106 public void addFailure(Test test, AssertionFailedError t) { } 107 public void addError(Test test, Throwable t) { 108 error = t; 109 } 110 String getError(){ 111 if (error == null){ 112 return ""; 113 } 114 StringWriter sw = new StringWriter(); 115 error.printStackTrace(new PrintWriter(sw)); 116 return sw.toString(); 117 } 118 } 119 120 public static class NoTestCase { 121 } 122 123 public static class InvalidTestCase extends TestCase { 124 public InvalidTestCase(String name){ 125 super(name); 126 throw new NullPointerException ("thrown on purpose"); 127 } 128 } 129 130 public static class NoSuiteTestCase extends TestCase { 131 public NoSuiteTestCase(String name){ super(name); } 132 public void testA(){} 133 } 134 135 public static class SuiteTestCase extends NoSuiteTestCase { 136 public SuiteTestCase(String name){ super(name); } 137 public static Test suite(){ 138 return new TestSuite(SuiteTestCase.class); 139 } 140 } 141 142 public static class InvalidSuiteTestCase extends NoSuiteTestCase { 143 public InvalidSuiteTestCase(String name){ super(name); } 144 public static Test suite(){ 145 throw new NullPointerException ("thrown on purpose"); 146 } 147 } 148 public static void main(String [] args){ 149 junit.textui.TestRunner.run(JUnitTestRunnerTest.class); 150 } 151 } 152 153 | Popular Tags |