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  		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 oldOut= System.out;
46  		System.setOut(new PrintStream (
47  			new OutputStream() {
48  				public void write(int arg0) throws IOException {
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  }