View Javadoc
1   package junit.tests.runner;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.io.OutputStream;
7   import java.io.PrintStream;
8   
9   import junit.framework.TestCase;
10  import junit.framework.TestResult;
11  import junit.framework.TestSuite;
12  
13  public class TextRunnerTest extends TestCase {
14  
15      public void testFailure() throws Exception {
16          execTest("junit.tests.framework.Failure", false);
17      }
18  
19      public void testSuccess() throws Exception {
20          execTest("junit.tests.framework.Success", true);
21      }
22  
23      public void testError() throws Exception {
24          execTest("junit.tests.BogusDude", false);
25      }
26  
27      void execTest(String testClass, boolean success) throws Exception {
28          String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
29          String cp = System.getProperty("java.class.path");
30          //use -classpath for JDK 1.1.7 compatibility
31          String[] cmd = {java, "-classpath", cp, "junit.textui.TestRunner", testClass};
32          Process p = Runtime.getRuntime().exec(cmd);
33          InputStream i = p.getInputStream();
34          while ((i.read()) != -1)
35              ; //System.out.write(b);
36          assertTrue((p.waitFor() == 0) == success);
37          if (success) {
38              assertTrue(p.exitValue() == 0);
39          } else {
40              assertFalse(p.exitValue() == 0);
41          }
42      }
43  
44      public void testRunReturnsResult() {
45          PrintStream oldOut = System.out;
46          System.setOut(new PrintStream(
47                  new OutputStream() {
48                      @Override
49                      public void write(int arg0) throws IOException {
50                      }
51                  }
52          ));
53          try {
54              TestResult result = junit.textui.TestRunner.run(new TestSuite());
55              assertTrue(result.wasSuccessful());
56          } finally {
57              System.setOut(oldOut);
58          }
59      }
60  
61  
62  }