KickJava   Java API By Example, From Geeks To Geeks.

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


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