Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/drivers/RhinoEnv.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package com.netscape.javascript.qa.drivers;
6
7 import java.io.*;
8 import com.netscape.javascript.*;
9 import org.mozilla.javascript.*;
10 import org.mozilla.javascript.tools.shell.*;
11
12 /**
13 This class creates a javax.javascript.Context, which evaluates
14 the helper and RhinoFile and returns a result.
15
16 <p>
17
18 If the test throws a Java exception or JavaScript runtime or
19 compilation error, the RhinoFile fails, and the exception is stored
20 in that RhinoFile's exception variable.
21
22 <p>
23
24 If the test succeeds, the result is parsed and the RhinoFile's test
25 result variables are populated.
26
27 @author [email protected]
28
29 */
30 public class RhinoEnv implements TestEnvironment {
31 public Global global;
32 Object cx;
33 Object result;
34 TestDriver driver;
35 TestSuite suite;
36 TestFile file;
37
38 /**
39
40 @param f RhinoFile that will be executed in this RhinoEnv
41 @param s the RhinoFile's test suite
42 @param d the RhinoDrv applet that created this RhinoEnv
43
44 */
45 public RhinoEnv( TestFile f, TestSuite s, TestDriver d) {
46 this.file = f;
47 this.suite = s;
48 this.driver = d;
49 }
50 /**
51 Creates the JavaScript Context, which evaluates the contents of a
52 RhinoFile and returns a result. The RhinoEnv parses the test
53 result, and sets values of the RhinoFile test result properties.
54
55 @see com.netscape.javascript.Context#setOptimizationLevel
56 @see com.netscape.javascript.Context#setDebugLevel
57
58 */
59 public synchronized void runTest() {
60 this.driver.p( file.name );
61 try {
62 cx = createContext();
63 ((Context) cx).setOptimizationLevel( driver.OPT_LEVEL );
64 ((Context) cx).setDebugLevel( driver.DEBUG_LEVEL );
65 Object loadFn = executeTestFile(driver.HELPER_FUNCTIONS.getAbsolutePath());
66
67 file.startTime = driver.getCurrentTime();
68 result = executeTestFile( file.filePath );
69 file.endTime = driver.getCurrentTime();
70 parseResult();
71
72 } catch ( Exception e ) {
73 suite.passed = false;
74 file.passed = false;
75 file.exception += "file failed with exception: " + e ;
76 }
77 }
78 /**
79 * Create a new com.netscape.javascript.Context.
80 *
81 * @return the newly instantiated Context
82 *
83 */
84 public Object createContext() {
85 // this is stolen from Main.java
86 cx = new Context();
87 ((Context) cx).enter();
88
89 global = new Global();
90 ((Context) cx).initStandardObjects(global);
91
92 String[] names = { "print", "quit", "version", "load", "help",
93 "loadClass" };
94 try {
95 global.defineFunctionProperties(names, Main.class,
96 ScriptableObject.DONTENUM);
97 } catch (PropertyException e) {
98
99 throw new Error(e.getMessage());
100 }
101
102 return cx;
103 }
104 public Object executeTestFile() {
105 return null;
106 }
107
108 /**
109 Given a filename, evaluate the file's contents as a JavaScript
110 program. Return the value of the program. If the test throws
111 a Java exception or JavaScript runtime or compilation error,
112 return the string value of the error message.
113
114 @param s full path to the file that will be exectued.
115 @return test result object. If the test is positive, result
116 should be an instance of Scriptable. if the test is negative,
117 the result should be a String, whose value is the message in the
118 JavaScript error or Java exception.
119 */
120 public Object executeTestFile( String s ) {
121 // this bit is stolen from Main.java
122 FileReader in = null;
123 try {
124 in = new FileReader( s );
125 } catch (FileNotFoundException ex) {
126 driver.p("couldn't open file " + s);
127 }
128
129 Object result = null;
130
131 try {
132 // Here we evalute the entire contents of the file as
133 // as script. Text is printed only if the print() function
134 // is called.
135 // cx.evaluateReader((Scriptable) global, in, args[i], 1, null);
136
137 result = ((Scriptable) (((Context) cx).evaluateReader(
138 (Scriptable) global, (Reader) in, s, 1, null)));
139
140 } catch (WrappedException we) {
141 driver.p("Wrapped Exception: "+
142 we.getWrappedException().toString());
143 result = we.getWrappedException().toString();
144 } catch (Exception jse) {
145 driver.p("JavaScriptException: " + jse.getMessage());
146 result = jse.getMessage();
147 }
148
149 return ( result );
150 }
151 /**
152 Evaluates the RhinoFile result. If the result is an instance of
153 javax.javascript.Scriptable, assume it is a JavaScript Array of
154 TestCase objects, as described in RhinoDrv.java. For each test case in
155 the array, add an element to the RhinoFile's test case vector. If all
156 test cases passed, set the RhinoFile's passed value to true; else set
157 its passed value to false.
158
159 <p>
160
161 If the result is not a Scriptable object, the test failed. Set the the
162 RhinoFile's exception property to the string value of the result.
163 However, negative tests, which should have a "-n.js" extension, are
164 expected to fail.
165
166 */
167 public boolean parseResult() {
168 FlattenedObject fo = null;
169
170 if ( result instanceof Scriptable ) {
171 fo = new FlattenedObject( (Scriptable) result );
172
173 try {
174 file.totalCases = ((Number) fo.getProperty("length")).intValue();
175 for ( int i = 0; i < file.totalCases; i++ ) {
176 Scriptable tc = (Scriptable) ((Scriptable) result).get( i,
177 (Scriptable) result);
178
179 TestCase rt = new TestCase(
180 getString(tc.get( "passed", tc )),
181 getString(tc.get( "name", tc )),
182 getString(tc.get( "description", tc )),
183 getString(tc.get( "expect", tc )),
184 getString(tc.get( "actual", tc )),
185 getString(tc.get( "reason", tc ))
186 );
187
188 file.bugnumber=
189 (getString(tc.get("bugnumber", tc))).startsWith
190 ("com.netscape.javascript")
191 ? file.bugnumber
192 : getString(tc.get("bugnumber", tc));
193
194 file.caseVector.addElement( rt );
195 if ( rt.passed.equals("false") ) {
196 this.file.passed = false;
197 this.suite.passed = false;
198 }
199 }
200
201 if ( file.totalCases == 0 ) {
202 if ( file.name.endsWith( "-n.js" ) ) {
203 this.file.passed = true;
204 } else {
205 this.file.reason = "File contains no testcases. " + this.file.reason;
206 this.file.passed = false;
207 this.suite.passed = false;
208 }
209 }
210
211 } catch ( Exception e ) {
212 this.file.exception = "Got a Scriptable result, but failed "+
213 "parsing its arguments. Exception: " + e.toString() +
214 " Flattened Object is: " + fo.toString();
215
216 this.file.passed = false;
217 this.suite.passed = false;
218
219 return false;
220 }
221 } else {
222 // if it's not a scriptable object, test failed. set the file's
223 // exception to the string value of whatever result we did get.
224 this.file.exception = result.toString();
225
226 // if the file's name ends in "-n", the test expected an error.
227
228 if ( file.name.endsWith( "-n.js" ) ) {
229 this.file.passed = true;
230 } else {
231 this.file.passed = false;
232 this.suite.passed = false;
233 return false;
234 }
235 }
236
237 return true;
238 }
239 /**
240 Close the context.
241 */
242 public void close() {
243 try {
244 ((Context) cx).exit();
245 } catch ( Exception e ) {
246 suite.passed = false;
247 file.passed = false;
248 file.exception = "file failed with exception: " + e ;
249 }
250
251 }
252 /**
253 * Get the JavaScript string associated with a JavaScript object.
254 *
255 * @param object a Java identifier for a JavaScript object
256 * @return the JavaScript string representation of the object
257 */
258 public String getString( Object object ) {
259 return (((Context) cx).toString( object ));
260 }
261 }