01: /*
02: This source file is part of Smyle, a database library.
03: For up-to-date information, see http://www.drjava.de/smyle
04: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
05:
06: This library is free software; you can redistribute it and/or
07: modify it under the terms of the GNU Lesser General Public
08: License as published by the Free Software Foundation; either
09: version 2.1 of the License, or (at your option) any later version.
10:
11: This library is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public
17: License along with this library; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19:
20: For full license text, see doc/license/lgpl.txt in this distribution
21: */
22:
23: package drjava.tf;
24:
25: import java.io.*;
26: import bsh.*;
27:
28: public class TF {
29: private static void catchTargetError(TargetError e,
30: TestResult result) {
31: result.addException(e.getTarget());
32: }
33:
34: public static TestResult runTest(String scriptFile)
35: throws EvalError, IOException {
36: TestResult result = new TestResult(scriptFile);
37:
38: result.redirectSystemStreams();
39:
40: try {
41: Interpreter in = new Interpreter();
42: in.set("_result", result);
43:
44: // redirect methods - we should find a better way to do this
45: in
46: .eval("void error(String msg, String invocationText) { _result.error(msg, invocationText); } \n"
47: + "void error(String msg) { _result.error(msg, this.namespace.getInvocationText()); } \n"
48: + "void assertTrue(boolean flag) { _result.assertEquals(\"\", true, flag, this.namespace.getInvocationText()); } \n"
49: + "void assertFalse(boolean flag) { _result.assertEquals(\"\", false, flag, this.namespace.getInvocationText()); } \n"
50: + "void assertEquals(expected, actual) { \n"
51: + " _result.assertEquals(\"\", expected, actual, this.namespace.getInvocationText()); \n"
52: + "} \n"
53: + "void checkIterator(a, b) { _result.checkIterator(a, b, this.namespace.getInvocationText()); } \n"
54: + "void description(desc) { _result.description(desc); } \n");
55:
56: try {
57: in.source(scriptFile);
58: } catch (TargetError e) {
59: catchTargetError(e, result);
60: }
61:
62: // find and run test methods
63: boolean hasSetUp = (in.getNameSpace().getMethod("setUp",
64: new Class[0]) != null);
65: String[] methods = in.getNameSpace().getMethodNames();
66: for (int i = methods.length - 1; i >= 0; i--) {
67: String name = methods[i];
68: if (!(name.startsWith("test") || name
69: .startsWith("bench")))
70: continue;
71: TestResult subresult = new TestResult(name);
72: subresult.redirectSystemStreams();
73: try {
74: in.set("_result", subresult);
75: try {
76: if (hasSetUp)
77: in.eval("setUp();");
78: double time = result.timer.getMilliseconds();
79: in.eval(name + "();");
80: subresult.runtime = result.timer
81: .getMilliseconds()
82: - time;
83: /*if (name.startsWith("bench"))
84: result.oldOut.println("Runtime="+subresult.runtime+", timer="+result.timer);*/
85: } catch (TargetError e) {
86: catchTargetError(e, result);
87: }
88: result.addSubtest(subresult);
89: } finally {
90: subresult.restoreSystemStreams();
91: }
92: }
93: } finally {
94: result.restoreSystemStreams();
95: }
96:
97: return result;
98: }
99: }
|