Debian Codegraph @

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

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8 -*-
2 * Copyright (C) 1997, 1998 Netscape Communications Corporation,
3 * All Rights Reserved.
4 */
5
6 package com.netscape.javascript.qa.drivers;
7
8 import java.io.*;
9 import java.util.StringTokenizer;
10
11 /**
12 * RefEnv is used to run a JavaScript test in the JavaScript engine
13 * implemented in C. RefEnv creates a new ObservedTask and processes
14 * the standard output and standard error of the tests to determine the
15 * test result. The RefDrv expects the process output to contain the
16 * following content:
17 *
18 * <p>
19 * 1 - anything (before the formatted output can be unstructured.
20 *
21 * <p>
22 *
23 * 2 - the number of test cases output in the following way.
24 * <pre>
25 * <#TEST CASES SIZE>
26 * int
27 * </pre>
28 *
29 * 3 - the results of each test formatted in the following way
30 * (name, expected, actual, description, and reason can all be multilines)
31 * <pre>
32 * <#TEST CASE PASSED>
33 * true|false
34 * <#TEST CASE NAME>
35 * text
36 * <#TEST CASE EXPECTED>
37 * text
38 * <#TEST CASE ACTUAL>
39 * text
40 * <#TEST CASE DESCRIPTION>
41 * text
42 * <#TEST CASE REASON>
43 * text
44 * <#TEST CASE BUGNUMBER>
45 * text
46 * </pre>
47 *
48 * 4 - a marker that signifies the end of the tests
49 *
50 * <pre>
51 * <#TEST CASES DONE>
52 * </pre>
53
54 * 5 - after the end of the tests you can have more unstructed text.
55 * <p>
56 * The test case output is generated by the stopTest function, which is in
57 * the shared functions file, jsref.js.
58 *
59 * @see com.netscape.javascript.qa.drivers.TestEnvironment
60 * @see ObservedTask
61 *
62 * @author [email protected]
63 * @author Nick Lerissa
64 */
65 public class RefEnv implements TestEnvironment {
66 TestFile file;
67 TestSuite suite;
68 RefDrv driver;
69 ObservedTask task;
70
71 /**
72 * Create a new RefEnv.
73 */
74 public RefEnv( TestFile f, TestSuite s, RefDrv d) {
75 this.file = f;
76 this.suite = s;
77 this.driver = d;
78 }
79
80 /**
81 * Called by the driver to execute the test program.
82 */
83 public void runTest() {
84 driver.p( file.name );
85 try {
86 file.startTime = driver.getCurrentTime();
87 createContext();
88 executeTestFile();
89 file.endTime = driver.getCurrentTime();
90
91 if (task.getExitValue() != 0) {
92 if ( file.name.endsWith( "-n.js" )) {
93 file.passed = true;
94 } else {
95 suite.passed = false;
96 file.passed = false;
97 }
98 }
99 if ( ! parseResult() ) {
100 if ( file.name.endsWith( "-n.js" ) ) {
101 file.passed = true;
102 } else {
103 suite.passed = false;
104 file.passed = false;
105 }
106 file.exception = new String(task.getError());
107 }
108
109 } catch ( Exception e ) {
110 suite.passed = false;
111 file.passed = false;
112 file.exception = "Unknown process exception.";
113 /*
114 file.exception = new String(task.getError())
115 + " exit value: " + task.getExitValue()
116 + "\nThrew Exception:" + e;
117 */
118 }
119 }
120
121 /**
122 * Instantiate a new JavaScript shell, passing the TestFile as an
123 * argument.
124 *
125 */
126 public Object createContext() {
127 if ( driver.CODE_COVERAGE ) {
128 String command = "coverage " +
129 "/SaveMergeData /SaveMergeTextData "+
130 driver.EXECUTABLE + " -f " +
131 driver.HELPER_FUNCTIONS.getAbsolutePath() + " -f " +
132 file.filePath;
133
134 System.out.println( "command is " + command );
135
136 task = new ObservedTask( command, this );
137 } else {
138 task = new ObservedTask( driver.EXECUTABLE + " -f " +
139 driver.HELPER_FUNCTIONS.getAbsolutePath() + " -f " +
140 file.filePath, this);
141 }
142 return (Object) task;
143 }
144 /**
145 * Start the shell process.
146 *
147 */
148 public Object executeTestFile() {
149 try {
150 task.exec();
151 } catch ( IOException e ) {
152 driver.p( e.toString() );
153 file.exception = e.toString();
154 e.printStackTrace();
155 }
156 return null;
157 }
158
159 /**
160 * Parse the standard output of the process, and try to create new
161 * TestCase objects.
162 */
163 public boolean parseResult() {
164 String line;
165 int i,j;
166
167 BufferedReader br = new BufferedReader(new StringReader(
168 new String(task.getInput())));
169 try {
170 do {
171 line = br.readLine();
172 driver.p( line );
173
174 if (line == null) {
175 driver.p("\tERROR: No lines to read");
176 return false;
177 }
178 } while (!line.equals(sizeTag));
179
180 if ((line = br.readLine()) == null)
181 return false;
182
183 file.totalCases = Integer.valueOf(line).intValue();
184
185 if ((line = br.readLine()) == null) {
186 driver.p("\tERROR: No lines after " + sizeTag);
187 return false;
188 }
189
190 } catch (NumberFormatException nfe) {
191 driver.p("\tERROR: No integer after " + sizeTag);
192 return false;
193 } catch ( IOException e ) {
194 driver.p( "Exception reading process output:" + e.toString() );
195 file.exception = e.toString();
196 return false;
197 }
198
199 for ( i = 0; i < file.totalCases; i++) {
200 String values[] = new String[tags.length];
201 try {
202 for ( j = 0; j < tags.length; j++) {
203 values[j] = null;
204
205 if (!line.startsWith(tags[j])) {
206 driver.p("line didn't start with " + tags[j] +":"+line);
207 return false;
208 }
209 while (((line = br.readLine()) != null) &&
210 (!(line.startsWith(startTag))))
211 {
212 values[j] = (values[j] == null) ? line : (values[j] +
213 "\n" + line);
214 }
215 if (values[j] == null) values[j] = "";
216 }
217 if ((line == null) && (i < file.totalCases - 1)) {
218 driver.p("line == null and " + i + "<" +
219 (file.totalCases - 1));
220 return false;
221 }
222 } catch ( IOException e ) {
223 driver.p( "Exception reading process output: " + e );
224 file.exception = e.toString();
225 return false;
226 }
227
228 TestCase rt = new TestCase(values[0],values[1],values[4],values[2],
229 values[3],values[5]);
230
231 file.bugnumber = values[6];
232 file.caseVector.addElement( rt );
233
234 if ( rt.passed.equals("false") ) {
235 if ( file.name.endsWith( "-n.js" ) ) {
236 this.file.passed = true;
237 } else {
238 this.file.passed = false;
239 this.suite.passed = false;
240 }
241 }
242
243 }
244
245 if ( file.totalCases == 0 ) {
246 if ( file.name.endsWith( "-n.js" ) ) {
247 this.file.passed = true;
248 } else {
249 this.file.reason = "File contains no testcases. " + this.file.reason;
250 this.file.passed = false;
251 this.suite.passed = false;
252 }
253 }
254
255 return true;
256 }
257 /**
258 * Method required by TestEnvironment; this implemenation does nothing.
259 *
260 */
261 public void close(){
262 return;
263 }
264
265 /**
266 * array of in output file to get attributes of a specific TestCase
267 */
268 public static final String tags[];
269 /**
270 * tag to specify the number of TestCases
271 */
272 public static final String sizeTag = "<#TEST CASES SIZE>";
273 /**
274 * beginning of a tag
275 */
276 public static final String startTag = "<#TEST CASE";
277 /**
278 * end of a tag
279 */
280 public static final String endTag = ">";
281
282 /**
283 * creating of String tags[]
284 */
285 static {
286 String fields[] = { "PASSED", "NAME", "EXPECTED", "ACTUAL", "DESCRIPTION", "REASON",
287 "BUGNUMBER" };
288
289 tags = new String[fields.length];
290
291 for (int i = 0; i < fields.length; ++i) {
292 tags[i] = startTag + " " + fields[i] + endTag;
293 }
294 }
295 }