Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/drivers/LiveConnectDrv.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 netscape.javascript.*;
8 //import org.mozilla.javascript.*;
9 import java.io.*;
10
11 /**
12 * LiveConnectDrv is an application that drives the testing of the LiveConnect
13 * API test suite. This driver runs the stand-alone JavaScript engine in C.
14 * To run the LiveConnectTest API suite in Navigator, use XXX an as yet unwritten
15 * driver, which will depend on a generated HTML file to create the test applet.
16 *
17 * <p>
18 * The application requires the following arguments:
19 *
20 * <table>
21 * <tr>
22 * <td> -d
23 * <td> directory in which LiveConnect test applications are located
24 * </tr>
25 * <tr>
26 * <td> -s
27 * <td> list of suites to execute (optional)
28 * </tr>
29 * <tr>
30 * <td> -o
31 * <td> directory in which log files will be written
32 * </tr>
33 * <tr>
34 * <td> -e
35 * <td> path to the LiveConnect shell executable
36 * </table>
37 *
38 * LiveConnectDrv checks the directory, finds the suites in that directory,
39 * and in each suite a list of all the class files in that suite. For each
40 * class file found, LiveConnectDrv creates a new LiveConnectEnv object,
41 * which runs the test.
42 *
43 * <p>
44 *
45 * LiveConnectDrv also generates a helper file that is passed to the JavaScript
46 * shell. The helper file contains statements that allow the shell to run the
47 * test class.
48 *
49 *
50 * The contents of the helper file look something like this:
51 *
52 * <pre>
53 * var OUTPUT_DIRECTORY = <i>value of OUTPUT_DIRECTORY</i>
54 * TestClassName = arguments[0];
55 * TestClass = eval( TestClassName );
56 * testclass = new TestClass();
57 * quit();
58 * </pre>
59 *
60 * <p> Each test class creates JavaScript TestCase objects through LiveConnect,
61 * and prints results to standard output, just like the JavaScript language
62 * tests do. Additionally, test logs are created.
63 *
64 * <p> The parent test class, LiveConnectTest, contains methods from
65 * com.netscape.javascript.qa.drivers.TestDriver that allow it to write
66 * test results directly to the log files.
67 *
68 * @see com.netscape.javascript.qa.liveconnect.LiveConnectTest
69 * @see LiveConnectEnv
70 *
71 * @author [email protected]
72 *
73 */
74
75 public class LiveConnectDrv extends TestDriver {
76 public LiveConnectDrv( String[] args ) {
77 super( args );
78 setSuffix( ".class");
79 }
80 public static void main ( String[] args ) {
81 LiveConnectDrv d = new LiveConnectDrv( args );
82 d.start();
83 }
84 public boolean processOptions() {
85 int length = ARGS.length;
86
87 if (ARGS[0].startsWith("-")) {
88 //XXX need to verify that we at least get valid d and -h options
89
90 for (int i=0; i < ARGS.length; i++ ) {
91 if ( ARGS[i].equals("-d") ) {
92 this.TEST_DIRECTORY =
93 ARGS[i].endsWith(File.separator)
94 ? new File( ARGS[++i] )
95 : new File( ARGS[++i] + File.separator );
96
97 if ( ! ( this.TEST_DIRECTORY ).isDirectory() ) {
98 p( "error: " +
99 this.TEST_DIRECTORY.getAbsolutePath() +
100 " is not a directory." );
101 return false;
102 } else {
103 continue;
104 }
105 }
106 if ( ARGS[i].equals("-s") ) {
107 FILES = new String[20] ;
108 for ( int j = ++i, k=0; j < ARGS.length; j++ ) {
109 if ( ARGS[j].startsWith("-") ){
110 break;
111 }
112 FILES[k++] = ARGS[j];
113 }
114 }
115 if ( ARGS[i].equals("-h") ) {
116 this.HELPER_FUNCTIONS = new File( ARGS[++i] );
117 if ( ! (this.HELPER_FUNCTIONS ).isFile() ) {
118 p( "error: "+
119 this.HELPER_FUNCTIONS.getAbsolutePath()+
120 " file not found." );
121 return false;
122 }
123 }
124 if ( ARGS[i].equals("-o") ) {
125 String odir = ARGS[++i];
126
127 OUTPUT_DIRECTORY = new File(
128 (odir.endsWith(File.separator) ? odir :
129 odir+File.separator));
130
131 OUTPUT_DIRECTORY.mkdirs();
132
133 if ( !OUTPUT_DIRECTORY.exists() ||
134 !OUTPUT_DIRECTORY.isDirectory() )
135 {
136 p( "error: "+
137 OUTPUT_DIRECTORY.getAbsolutePath()+
138 " could not create directory.");
139 return false;
140 }
141
142 }
143
144 if ( ARGS[i].equals("-p") ) {
145 OPT_LEVEL = Integer.parseInt( ARGS[++i] );
146 }
147
148 if ( ARGS[i].equals("-db" )) {
149 DEBUG_LEVEL = Integer.parseInt( ARGS[++i] );
150 OPT_LEVEL = 0;
151 }
152
153 if ( ARGS[i].equals("-e")) {
154 EXECUTABLE = ARGS[++i];
155 }
156 }
157
158 return true;
159
160 } else {
161 switch ( ARGS.length ) {
162 case 0:
163 p( "error: specify location of JavaScript "+
164 "tests" );
165 return false;
166 case 1:
167 p( "error: specify location of JavaScript "+
168 "HELPER_FUNCTIONS file" );
169 return false;
170 case 2:
171 this.TEST_DIRECTORY = ARGS[0].endsWith(File.separator)
172 ? new File( ARGS[0] )
173 : new File( ARGS[0] + File.separator );
174 this.HELPER_FUNCTIONS = new File( ARGS[1] );
175 if ( ! ( this.TEST_DIRECTORY ).isDirectory() ) {
176 p( "error: " +
177 this.TEST_DIRECTORY.getAbsolutePath() +
178 " is not a directory." );
179 return false;
180 }
181 if ( ! (this.HELPER_FUNCTIONS ).isFile() ) {
182 p( "error: "+
183 this.HELPER_FUNCTIONS.getAbsolutePath()+
184 " file not found." );
185 return false;
186 }
187 return true;
188 default:
189 p( "could not understand arguments." );
190 return false;
191 }
192 }
193
194 }
195
196 public synchronized void executeSuite( TestSuite suite ) {
197 p( "LiveConnectDrv.executeSuite " + suite.name );
198 TestEnvironment context;
199 TestFile file;
200
201 if ( EXECUTABLE != null ) {
202 generateHelperFile();
203 }
204
205 for ( int i = 0; i < suite.size(); i++ ) {
206 synchronized ( suite ) {
207 file = (TestFile) suite.elementAt( i );
208
209 p( file.name );
210
211 // if ( EXECUTABLE != null ) {
212 context = new LiveConnectEnv( file, suite, this );
213 // } else {
214 // context = new LiveRhinoEnv( file, suite, this );
215 // }
216
217 synchronized( context ) {
218 context.runTest();
219
220 /*
221 * The following two lines are used by the other test drivers,
222 * but are not used by LiveConnectDrv, since each
223 * LiveConnecTest writes its results to these log files.
224 */
225
226 // writeFileResult( file, suite, OUTPUT_DIRECTORY );
227 // writeCaseResults(file, suite, OUTPUT_DIRECTORY );
228
229 context.close();
230 context = null;
231
232 if ( ! file.passed ) {
233 suite.passed = false;
234 }
235 }
236 }
237 }
238 writeSuiteResult( suite, OUTPUT_DIRECTORY );
239 writeSuiteSummary( suite, OUTPUT_DIRECTORY );
240 }
241
242 public void generateHelperFile() {
243 try {
244 this.HELPER_FUNCTIONS = new File( OUTPUT_DIRECTORY, "helper.js" );
245
246 p( "HELPER FUNCTIONS FILE IS " + HELPER_FUNCTIONS );
247 FileOutputStream fos = new FileOutputStream( HELPER_FUNCTIONS );
248
249 fos.write( ("var OUTPUT_DIRECTORY = \"" + OUTPUT_DIRECTORY + "\";").getBytes());
250 fos.write( ("var OUTPUT_FILE = arguments[1];").getBytes() );
251 fos.write( ("var TestClassName = arguments[0];" ).getBytes());
252 fos.write( ("var TestClass = eval( TestClassName );" ).getBytes());
253 fos.write( ("var testclass = new TestClass();" ).getBytes());
254 fos.write( ("testclass.run();").getBytes() );
255 fos.write( ("quit();" ).getBytes());
256 fos.close();
257
258 } catch ( Exception e ) {
259 p( "generateHelperFile threw " + e.toString() );
260 e.printStackTrace();
261 }
262 }
263
264 /**
265 * For each class file in the suite directory, create a TestFile object.
266 * For LiveConnect, the test class name needs to be the name of the class
267 * and the file path needs to be the full package name, prepended with the
268 * JavaScript keyword "Packages", and without the ".class" suffix.
269 */
270
271 public void getCases( TestSuite suite ) {
272 enablePrivileges();
273
274 File dir = new File ( suite.filePath );
275 String[] files = dir.list();
276
277 // XXX hardcoded package name. need to fix this.
278
279 String filename = "Packages.com.netscape.javascript.qa.liveconnect." +
280 suite.name +".";
281
282 for ( int i = 0; i < files.length; i++ ) {
283 if ( files[i].endsWith( getSuffix() )) {
284 TestFile item = new TestFile( files[i],
285 filename + (files[i].substring(0,files[i].length() -
286 getSuffix().length())));
287
288 p( item.filePath );
289 suite.addElement(item);
290 }
291 }
292 }
293 }