KickJava   Java API By Example, From Geeks To Geeks.

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


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002 uid41545 <uid41545@users.sourceforge.net>
15  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
16  * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
17  * Copyright (C) 2003 Joey Gibson <joey@joeygibson.com>
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

31 package org.jruby.test;
32
33 import java.io.BufferedReader JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.InputStreamReader JavaDoc;
37 import java.util.ArrayList JavaDoc;
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 /**
48  * @author Anders
49  */

50 public class ScriptTestSuite extends TestSuite {
51
52     private static final String JavaDoc TEST_DIR = "test";
53     private static final String JavaDoc TEST_INDEX = "test_index";
54
55     public ScriptTestSuite(String JavaDoc name) {
56         super(name);
57     }
58
59     public static Test suite() throws java.io.IOException JavaDoc {
60         TestSuite suite = new TestSuite();
61
62         File JavaDoc testDir;
63         if (System.getProperty("basedir") != null) {
64             testDir = new File JavaDoc(System.getProperty("basedir"), "target/test-classes/" + TEST_DIR);
65         } else {
66             testDir = new File JavaDoc(TEST_DIR);
67         }
68
69         File JavaDoc testIndex = new File JavaDoc(testDir, TEST_INDEX);
70
71         if (! testIndex.canRead()) {
72             // Since we don't have any other error reporting mechanism, we
73
// add the error message as an always-failing test to the test suite.
74
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 JavaDoc testFiles =
82             new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(testIndex)));
83         String JavaDoc line;
84         while ((line = testFiles.readLine()) != null) {
85             line = line.trim();
86             if (line.startsWith("#") || line.length() == 0) {
87                 continue;
88             }
89             
90             // Ensure we have a new interpreter for each test. Previous we were using the
91
// same interpreter which caused problems as soon as one test failed.
92
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 JavaDoc());
104         
105         return runtime;
106     }
107
108     private static class ScriptTest extends TestCase {
109         private final Ruby runtime;
110         private final File JavaDoc testDir;
111         private final String JavaDoc filename;
112
113         public ScriptTest(Ruby runtime, File JavaDoc testDir, String JavaDoc filename) {
114             super(filename);
115             this.runtime = runtime;
116             this.testDir = testDir;
117             this.filename = filename;
118         }
119
120         private String JavaDoc scriptName() {
121             return new File JavaDoc(testDir, filename).getPath();
122         }
123
124         public void runTest() throws Throwable JavaDoc {
125             StringBuffer JavaDoc script = new StringBuffer JavaDoc();
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(); // Without a flush Ant will miss some of our output
140
}
141     }
142 }
143
Popular Tags