1 31 package org.jruby.test; 32 33 import java.io.BufferedReader ; 34 import java.io.File ; 35 import java.io.FileInputStream ; 36 import java.io.InputStreamReader ; 37 import java.util.ArrayList ; 38 39 import junit.framework.Test; 40 import junit.framework.TestCase; 41 import junit.framework.TestSuite; 42 43 import org.jruby.Ruby; 44 import org.jruby.RubyArray; 45 import org.jruby.RubyString; 46 47 50 public class ScriptTestSuite extends TestSuite { 51 52 private static final String TEST_DIR = "test"; 53 private static final String TEST_INDEX = "test_index"; 54 55 public ScriptTestSuite(String name) { 56 super(name); 57 } 58 59 public static Test suite() throws java.io.IOException { 60 TestSuite suite = new TestSuite(); 61 62 File testDir; 63 if (System.getProperty("basedir") != null) { 64 testDir = new File (System.getProperty("basedir"), "target/test-classes/" + TEST_DIR); 65 } else { 66 testDir = new File (TEST_DIR); 67 } 68 69 File testIndex = new File (testDir, TEST_INDEX); 70 71 if (! testIndex.canRead()) { 72 suite.addTest(new FailingTest("ScriptTestSuite", 75 "Couldn't locate " + TEST_INDEX + 76 ". Make sure you run the tests from the base " + 77 "directory of the JRuby sourcecode.")); 78 return suite; 79 } 80 81 BufferedReader testFiles = 82 new BufferedReader (new InputStreamReader (new FileInputStream (testIndex))); 83 String line; 84 while ((line = testFiles.readLine()) != null) { 85 line = line.trim(); 86 if (line.startsWith("#") || line.length() == 0) { 87 continue; 88 } 89 90 Ruby runtime = setupInterpreter(); 93 94 suite.addTest(new ScriptTest(runtime, testDir, line)); 95 } 96 97 return suite; 98 } 99 100 private static Ruby setupInterpreter() { 101 Ruby runtime = Ruby.getDefaultInstance(); 102 103 runtime.getLoadService().init(new ArrayList ()); 104 105 return runtime; 106 } 107 108 private static class ScriptTest extends TestCase { 109 private final Ruby runtime; 110 private final File testDir; 111 private final String filename; 112 113 public ScriptTest(Ruby runtime, File testDir, String filename) { 114 super(filename); 115 this.runtime = runtime; 116 this.testDir = testDir; 117 this.filename = filename; 118 } 119 120 private String scriptName() { 121 return new File (testDir, filename).getPath(); 122 } 123 124 public void runTest() throws Throwable { 125 StringBuffer script = new StringBuffer (); 126 127 script.append("require 'test/minirunit'").append('\n'); 128 script.append("$silentTests = true").append('\n'); 129 script.append("test_load('").append(scriptName()).append("')").append('\n'); 130 script.append("$failed").append('\n'); 131 132 RubyArray lastFailed = (RubyArray)runtime.evalScript(script.toString()); 133 134 if (!lastFailed.isEmpty()) { 135 RubyString message = (RubyString) lastFailed.callMethod(lastFailed.getRuntime().getCurrentContext(), "to_s"); 136 fail(scriptName() + " failed, complete failure list follows:\n" + message.toString()); 137 } 138 139 System.out.flush(); } 141 } 142 } 143 | Popular Tags |