Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/drivers/MacRefEnv.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.util.Vector;
9 import java.util.Date;
10 import java.io.*;
11 import java.applet.Applet;
12
13 /**
14 * Test environment for running the JavaScript language tests on the Macintosh.
15 * On the Macintosh, we have to use JBindery, a MRJ tool, to run the tests.
16 *
17 * <p>
18 *
19 * In order to get tests to run on the Mac, the shell must be built with
20 * MAC_TEST_HACK defined. When MAC_TEST_HACK is defined, the shell expects
21 * to find file called "testargs.txt" in the directory where the shell
22 * executable is. The shell expects testargs.txt to have one argument per line.
23 * Legal arguments in testargs.txt are the same as the command line arguments
24 * to the shell (although we only use the -f argument, which tells the shell
25 * to evaluate files).
26 *
27 * <p>
28 *
29 * The shell writes any output generated by the print statement to a file
30 * called "results.txt". The test environment parses the results.txt file
31 * in the same way that RefEnv parses the output stream of the JavaScript
32 * shell process.
33 *
34 * <p>
35 */
36
37 public class MacRefEnv implements TestEnvironment {
38 TestFile file;
39 TestSuite suite;
40 RefDrv driver;
41 String directoryName;
42 TestLog testargs;
43 File results;
44 Process task;
45
46 /**
47 * Create a new MacRefEnv
48 *
49 */
50
51 public MacRefEnv ( TestFile f, TestSuite s, RefDrv d ) {
52 this.file = f;
53 this.suite = s;
54 this.driver = d;
55 }
56
57 /**
58 * This implementation does nothing, since creating the JS shell
59 * automatically runs the test on the Mac.
60 *
61 */
62 public Object executeTestFile() {
63 return null;
64 }
65
66 /**
67 * Instantiate a new JavaScript shell which has been built with
68 * MAC_TEST_HACK defined.
69 */
70 public Object createContext() {
71 try {
72 // look for a file called flagfile.flg
73 File flag = new File ( "flagfile.flg" );
74
75 if ( flag.exists() ) {
76 flag.delete();
77 }
78
79 task = Runtime.getRuntime().exec(driver.EXECUTABLE);
80
81 // wait a maximum of five minutes
82
83 int i = 0;
84
85 while ( (! flag.exists()) && i++ <= 60 ) {
86 Thread.currentThread().sleep(5000);
87 }
88
89 } catch (IOException x) {
90 System.out.println("IOException in RunJS : " + x);
91 } catch (InterruptedException x) {
92 System.out.println("InterruptedException in RunJS : " + x);
93 }
94
95 return null;
96 }
97
98 public boolean setupMacFiles() {
99
100 boolean result1 = getDirectoryName();
101 deleteResultsFile();
102 boolean result3 = createTestargsFile();
103 boolean result4 = writeTestargsFile();
104
105
106 if ( result1 && result3 && result4 ) {
107 return true;
108 } else {
109 return false;
110 }
111 }
112
113 public synchronized void runTest() {
114 try {
115 if ( setupMacFiles() ) {
116 file.startTime = driver.getCurrentTime();
117 createContext();
118 file.endTime = driver.getCurrentTime();
119 }
120 /*
121 if (task.getExitValue() != 0) {
122 if ( file.name.endsWith( "-n.js" )) {
123 file.passed = true;
124 } else {
125 suite.passed = false;
126 file.passed = false;
127 }
128 }
129 */
130 if ( ! parseResult() ) {
131 if ( file.name.endsWith( "-n.js" ) ) {
132 file.passed = true;
133 } else {
134 suite.passed = false;
135 file.passed = false;
136 }
137 // file.exception = new String(task.getError());
138 }
139
140 } catch ( Exception e ) {
141 suite.passed = false;
142 file.passed = false;
143 file.exception = "Unknown process exception.";
144 /*
145 file.exception = new String(task.getError())
146 + " exit value: " + task.getExitValue()
147 + "\nThrew Exception:" + e;
148 */
149 }
150 }
151
152 public boolean parseResult() {
153 String line;
154 int i, j;
155
156 results = new File( "results.txt");
157
158 if (! results.exists()) {
159 return false;
160 }
161
162 // spit out all the lines we find in the result file
163 try {
164 FileReader fr = new FileReader(results);
165 LineNumberReader lnr = new LineNumberReader(fr);
166 do {
167 line = lnr.readLine();
168 driver.p( line );
169 if (line == null) {
170 driver.p("\tERROR: No lines to read");
171 return false;
172 }
173 } while (!line.equals(sizeTag));
174
175 if ((line = lnr.readLine()) == null) {
176 return false;
177 }
178
179 file.totalCases = Integer.valueOf(line).intValue();
180
181 if ((line = lnr.readLine()) == null) {
182 driver.p("\tERROR: No lines after " + sizeTag);
183 return false;
184 }
185
186 for ( i = 0; i < file.totalCases; i++) {
187 String values[] = new String[tags.length];
188 try {
189 for ( j = 0; j < tags.length; j++) {
190 values[j] = null;
191
192 if (!line.startsWith(tags[j])) {
193 driver.p("line didn't start with " + tags[j] +":"+line);
194 return false;
195 }
196 while (((line = lnr.readLine()) != null) &&
197 (!(line.startsWith(startTag))))
198 {
199 values[j] = (values[j] == null) ? line : (values[j] +
200 "\n" + line);
201 }
202 if (values[j] == null) values[j] = "";
203 }
204 if ((line == null) && (i < file.totalCases - 1)) {
205 driver.p("line == null and " + i + "<" +
206 (file.totalCases - 1));
207 return false;
208 }
209 } catch ( IOException e ) {
210 driver.p( "Exception reading process output: " + e );
211 file.exception = e.toString();
212 return false;
213 }
214
215 TestCase rt = new TestCase(values[0],values[1],values[4],values[2],
216 values[3],values[5]);
217
218 file.bugnumber = values[6];
219 file.caseVector.addElement( rt );
220
221 if ( rt.passed.equals("false") ) {
222 if ( file.name.endsWith( "-n.js" ) ) {
223 this.file.passed = true;
224 } else {
225 this.file.passed = false;
226 this.suite.passed = false;
227 }
228 }
229 }
230 try {
231 lnr.close();
232 fr.close();
233 } catch (IOException x) {
234 System.out.println("IOException in RunJS : " + x);
235 return false;
236 }
237
238
239 } catch (NumberFormatException nfe) {
240 System.out.println("\tERROR: No integer after " + sizeTag);
241 return false;
242 } catch ( IOException e ) {
243 System.out.println( "Exception reading process output:" + e.toString() );
244 file.exception = e.toString();
245 return false;
246 }
247
248 return true;
249 }
250
251 /**
252 * Get the executable's directory, which is where we will write testargs.txt
253 * and from where we will get results.txt.
254 */
255
256 public boolean getDirectoryName() {
257 directoryName = ":";
258 return true;
259 }
260
261 /**
262 * Called by the driver to execute the test program.
263 *
264 */
265
266 /**
267 * Check to see whether testargs.txt exists. If so, delete it.
268 */
269 public boolean createTestargsFile() {
270 String testargsname = "testargs.txt";
271
272 File testargsfile = new File( testargsname );
273 if ( testargsfile.exists() ) {
274 testargsfile.delete();
275 }
276
277 testargs = new TestLog( "testargs.txt", "" );
278
279 return true;
280 }
281
282 public boolean deleteResultsFile() {
283 String resultsname = "results.txt";
284
285 File resultsfile = new File( resultsname );
286 if ( resultsfile.exists() ) {
287 resultsfile.delete();
288 }
289 return true;
290 }
291
292 /**
293 * Write testargs.txt to the directory where the executable is.
294 */
295 public boolean writeTestargsFile() {
296 String helper = getMacFileString( ":"+ driver.HELPER_STRING );
297 String testfile = getMacFileString( ":"+file.filePath );
298
299 testargs.writeLine( "-f" );
300 testargs.writeLine( helper );
301 testargs.writeLine( "-f" );
302 testargs.writeLine( testfile );
303 testargs.closeLog();
304
305 return true;
306 }
307
308 public void close() {
309 return;
310 }
311 /**
312 * Replace slashes in a string with colons.
313 *
314 */
315 public String getMacFileString( String path ) {
316 StringBuffer buffer = new StringBuffer();
317 int i;
318
319 for ( i = 0; i < path.length(); i++ ) {
320 if ( path.charAt(i) == '/' ) {
321 buffer.append(":");
322 } else {
323 buffer.append( path.charAt(i) );
324 }
325 }
326
327 return buffer.toString();
328 }
329
330
331 /**
332 * array of in output file to get attributes of a specific TestCase
333 */
334 public static final String tags[];
335 /**
336 * tag to specify the number of TestCases
337 */
338 public static final String sizeTag = "<#TEST CASES SIZE>";
339 /**
340 * beginning of a tag
341 */
342 public static final String startTag = "<#TEST CASE";
343 /**
344 * end of a tag
345 */
346 public static final String endTag = ">";
347
348 /**
349 * creating of String tags[]
350 */
351 static {
352 String fields[] = { "PASSED", "NAME", "EXPECTED", "ACTUAL", "DESCRIPTION", "REASON",
353 "BUGNUMBER" };
354
355 tags = new String[fields.length];
356
357 for (int i = 0; i < fields.length; ++i) {
358 tags[i] = startTag + " " + fields[i] + endTag;
359 }
360 }
361 }