KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > test > TestUnitTestSuite


1 /*
2  * TestUnitTestSuite.java
3  * JUnit based test
4  *
5  * Created on January 15, 2007, 4:06 PM
6  */

7
8 package org.jruby.test;
9
10 import java.io.BufferedReader JavaDoc;
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.FileReader JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStreamReader JavaDoc;
19 import java.io.PrintStream JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.jruby.Ruby;
31 import org.jruby.RubyArray;
32 import org.jruby.exceptions.RaiseException;
33
34 /**
35  *
36  * @author headius
37  */

38 public class TestUnitTestSuite extends TestCase {
39     private static final String JavaDoc TEST_DIR = "test";
40     private static final String JavaDoc TEST_INDEX = "test_unit_index";
41     private static final String JavaDoc REGEXP_TEST_CASE_SUBCLASS = "^\\s*class\\s+([^\\s]+)\\s*<.*TestCase\\s*$";
42     private static final Pattern JavaDoc PATTERN_TEST_CASE_SUBCLASS = Pattern.compile(REGEXP_TEST_CASE_SUBCLASS);
43
44     public TestUnitTestSuite(String JavaDoc testName) {
45         super(testName);
46     }
47
48     /**
49      * suite method automatically generated by JUnit module
50      */

51     public static Test suite() throws Exception JavaDoc {
52         TestSuite suite = new TestSuite();
53
54         File JavaDoc testDir;
55         if (System.getProperty("basedir") != null) {
56             testDir = new File JavaDoc(System.getProperty("basedir"), "target/test-classes/" + TEST_DIR);
57         } else {
58             testDir = new File JavaDoc(TEST_DIR);
59         }
60
61         File JavaDoc testIndex = new File JavaDoc(testDir, TEST_INDEX);
62
63         if (!testIndex.canRead()) {
64             // Since we don't have any other error reporting mechanism, we
65
// add the error message as an always-failing test to the test suite.
66
suite.addTest(new FailingTest("TestUnitTestSuite",
67                                           "Couldn't locate " + TEST_INDEX +
68                                           ". Make sure you run the tests from the base " +
69                                           "directory of the JRuby sourcecode."));
70             return suite;
71         }
72
73         BufferedReader JavaDoc testFiles =
74             new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(testIndex)));
75
76         String JavaDoc line;
77         while ((line = testFiles.readLine()) != null) {
78             line = line.trim();
79             if (line.startsWith("#") || line.length() == 0) {
80                 continue;
81             }
82
83             suite.addTest(new ScriptTest(line, testDir));
84         }
85
86         return suite;
87     }
88
89     private static class ScriptTest extends TestCase {
90         private ByteArrayInputStream JavaDoc in;
91         private ByteArrayOutputStream JavaDoc out;
92         private PrintStream JavaDoc printOut;
93         private ByteArrayOutputStream JavaDoc err;
94         private PrintStream JavaDoc printErr;
95         private Ruby runtime;
96         private final String JavaDoc filename;
97         private final File JavaDoc testDir;
98
99         public ScriptTest(String JavaDoc filename, File JavaDoc dir) {
100             super(filename);
101             this.filename = filename;
102             this.testDir = dir;
103         }
104
105         protected void setUp() throws Exception JavaDoc {
106             in = new ByteArrayInputStream JavaDoc(new byte[0]);
107             out = new ByteArrayOutputStream JavaDoc();
108             err = new ByteArrayOutputStream JavaDoc();
109             runtime = Ruby.newInstance(in, printOut = new PrintStream JavaDoc(out), printErr = new PrintStream JavaDoc(err));
110             setupInterpreter(runtime);
111         }
112
113         protected void tearDown() throws Exception JavaDoc {
114             in.close();
115             out.close();
116             err.close();
117             printOut.close();
118             printErr.close();
119
120             in = null;
121             out = null;
122             err = null;
123             printOut = null;
124             printErr = null;
125             runtime = null;
126         }
127
128         private void setupInterpreter(Ruby runtime) {
129             ArrayList JavaDoc loadPath = new ArrayList JavaDoc();
130             
131             loadPath.add("test/externals/bfts");
132             
133             runtime.getLoadService().init(loadPath);
134             runtime.defineGlobalConstant("ARGV", runtime.newArray());
135         }
136
137         private String JavaDoc scriptName() {
138             return new File JavaDoc(testDir, filename).getPath();
139         }
140
141         private String JavaDoc pretty(List JavaDoc list) {
142             StringBuffer JavaDoc prettyOut = new StringBuffer JavaDoc();
143
144             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
145                 prettyOut.append(iter.next().toString());
146             }
147
148             return prettyOut.toString();
149         }
150         
151         private String JavaDoc getTestClassNameFromReadingTestScript(String JavaDoc filename) {
152             File JavaDoc f = new File JavaDoc(TEST_DIR + "/" + filename + ".rb");
153             BufferedReader JavaDoc reader = null;
154             try {
155                 reader = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
156                 while (true) {
157                     String JavaDoc line = reader.readLine();
158                     if (line == null) break;
159                     Matcher JavaDoc m = PATTERN_TEST_CASE_SUBCLASS.matcher(line);
160                     if (m.find())
161                     {
162                         return m.group(1);
163                     }
164                 }
165                     
166             } catch (FileNotFoundException JavaDoc e) {
167                 throw new RuntimeException JavaDoc(e);
168             } catch (IOException JavaDoc e) {
169                 throw new RuntimeException JavaDoc(e);
170             }
171             finally {
172                 if (reader != null) {
173                     try {
174                         reader.close();
175                     } catch (IOException JavaDoc e) {
176                         throw new RuntimeException JavaDoc("Could not close reader!", e);
177                     }
178                 }
179                 
180             }
181             throw new RuntimeException JavaDoc("No *TestCase derivative found in '" + filename + ".rb'!");
182         }
183         
184
185         public void runTest() throws Throwable JavaDoc {
186             StringBuffer JavaDoc script = new StringBuffer JavaDoc();
187
188             try {
189                 script.append("require 'test/junit_testrunner.rb'\n");
190                 script.append("require '" + scriptName() + "'\n");
191                 script.append("runner = Test::Unit::UI::JUnit::TestRunner.new(" + getTestClassNameFromReadingTestScript(filename) + ")\n");
192                 script.append("runner.start\n");
193                 script.append("runner.faults\n");
194
195                 RubyArray faults = (RubyArray)runtime.evalScript(script.toString());
196
197                 if (!faults.isEmpty()) {
198                     StringBuffer JavaDoc faultString = new StringBuffer JavaDoc("Faults encountered running " + scriptName() + ", complete output follows:\n");
199                     for (Iterator JavaDoc iter = faults.iterator(); iter.hasNext();) {
200                         String JavaDoc fault = iter.next().toString();
201
202                         faultString.append(fault).append("\n");
203                     }
204
205                     fail(faultString.toString());
206                 }
207             } catch (RaiseException re) {
208                 fail("Faults encountered running " + scriptName() + ", complete output follows:\n" + re.getException().message + "\n" + pretty(((RubyArray)re.getException().backtrace()).getList()));
209             }
210         }
211     }
212 }
213
Popular Tags