1 7 8 package org.jruby.test; 9 10 import java.io.BufferedReader ; 11 import java.io.ByteArrayInputStream ; 12 import java.io.ByteArrayOutputStream ; 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileNotFoundException ; 16 import java.io.FileReader ; 17 import java.io.IOException ; 18 import java.io.InputStreamReader ; 19 import java.io.PrintStream ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 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 38 public class TestUnitTestSuite extends TestCase { 39 private static final String TEST_DIR = "test"; 40 private static final String TEST_INDEX = "test_unit_index"; 41 private static final String REGEXP_TEST_CASE_SUBCLASS = "^\\s*class\\s+([^\\s]+)\\s*<.*TestCase\\s*$"; 42 private static final Pattern PATTERN_TEST_CASE_SUBCLASS = Pattern.compile(REGEXP_TEST_CASE_SUBCLASS); 43 44 public TestUnitTestSuite(String testName) { 45 super(testName); 46 } 47 48 51 public static Test suite() throws Exception { 52 TestSuite suite = new TestSuite(); 53 54 File testDir; 55 if (System.getProperty("basedir") != null) { 56 testDir = new File (System.getProperty("basedir"), "target/test-classes/" + TEST_DIR); 57 } else { 58 testDir = new File (TEST_DIR); 59 } 60 61 File testIndex = new File (testDir, TEST_INDEX); 62 63 if (!testIndex.canRead()) { 64 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 testFiles = 74 new BufferedReader (new InputStreamReader (new FileInputStream (testIndex))); 75 76 String 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 in; 91 private ByteArrayOutputStream out; 92 private PrintStream printOut; 93 private ByteArrayOutputStream err; 94 private PrintStream printErr; 95 private Ruby runtime; 96 private final String filename; 97 private final File testDir; 98 99 public ScriptTest(String filename, File dir) { 100 super(filename); 101 this.filename = filename; 102 this.testDir = dir; 103 } 104 105 protected void setUp() throws Exception { 106 in = new ByteArrayInputStream (new byte[0]); 107 out = new ByteArrayOutputStream (); 108 err = new ByteArrayOutputStream (); 109 runtime = Ruby.newInstance(in, printOut = new PrintStream (out), printErr = new PrintStream (err)); 110 setupInterpreter(runtime); 111 } 112 113 protected void tearDown() throws Exception { 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 loadPath = new ArrayList (); 130 131 loadPath.add("test/externals/bfts"); 132 133 runtime.getLoadService().init(loadPath); 134 runtime.defineGlobalConstant("ARGV", runtime.newArray()); 135 } 136 137 private String scriptName() { 138 return new File (testDir, filename).getPath(); 139 } 140 141 private String pretty(List list) { 142 StringBuffer prettyOut = new StringBuffer (); 143 144 for (Iterator iter = list.iterator(); iter.hasNext();) { 145 prettyOut.append(iter.next().toString()); 146 } 147 148 return prettyOut.toString(); 149 } 150 151 private String getTestClassNameFromReadingTestScript(String filename) { 152 File f = new File (TEST_DIR + "/" + filename + ".rb"); 153 BufferedReader reader = null; 154 try { 155 reader = new BufferedReader (new FileReader (f)); 156 while (true) { 157 String line = reader.readLine(); 158 if (line == null) break; 159 Matcher m = PATTERN_TEST_CASE_SUBCLASS.matcher(line); 160 if (m.find()) 161 { 162 return m.group(1); 163 } 164 } 165 166 } catch (FileNotFoundException e) { 167 throw new RuntimeException (e); 168 } catch (IOException e) { 169 throw new RuntimeException (e); 170 } 171 finally { 172 if (reader != null) { 173 try { 174 reader.close(); 175 } catch (IOException e) { 176 throw new RuntimeException ("Could not close reader!", e); 177 } 178 } 179 180 } 181 throw new RuntimeException ("No *TestCase derivative found in '" + filename + ".rb'!"); 182 } 183 184 185 public void runTest() throws Throwable { 186 StringBuffer script = new StringBuffer (); 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 faultString = new StringBuffer ("Faults encountered running " + scriptName() + ", complete output follows:\n"); 199 for (Iterator iter = faults.iterator(); iter.hasNext();) { 200 String 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 |