Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/liveconnect/LiveConnectTest.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8 -*-
2 * Copyright (C) 1997, 1998 Netscape Communications Corporation, All Rights Reserved.
3 */
4
5 package com.netscape.javascript.qa.liveconnect;
6
7 import java.applet.Applet;
8 import java.io.File;
9 import java.util.Vector;
10 import netscape.javascript.*;
11 //import netscape.security.*;
12
13 import com.netscape.javascript.qa.drivers.*;
14
15 /**
16 * LiveConnectTest is the parent class for all LiveConnect test applets.
17 *
18 * <p> Subclasses must override the executeTest() and main() methods, and
19 * provide a constructor. To add test cases, subclasses should use the
20 * addTestCaseMethod.
21 *
22 * <p> Each test class creates JavaScript TestCase objects through LiveConnect,
23 * and prints results to standard output, just like the JavaScript language
24 * tests do.
25 *
26 * <p> To run a test individually, add the test package to your classpath
27 * and start the LiveConnected JavaScript shell. Instantiate the test class,
28 * and call the test's run method. Test results will be written to standard
29 * out.
30 *
31 * <p> To run the test in Navigator, create an HTML page for each test applet.
32 * Load the page in Navigator. Test results will be written to the Java
33 * console.
34 *
35 * <p> To run the test in the LiveConnect test driver, use the class
36 * LiveConnectDrv. LiveConnectDrv starts an out of process Liveconnect
37 * enabled jsshell.exe, and passes the shell the arguments necessary to
38 * start the test application.
39 *
40 * <p> LiveConnectTests write results to the log files, using the static
41 * methods in com.netscape.javascript.qa.drivers.TestDriver.
42 *
43 * After each test file is completed, each applet writes test case failures
44 * to the test case log, and if the test fails, also writes to the file log.
45 *
46 * <p> The applet writes a summary of that file's information to a temporary
47 * log that is parsed by the driver. The temporary log contains name and
48 * value pairs of the TestFile properties, which the driver uses to generate
49 * suite and summary logs.
50 *
51 * <p> Running the test suite is very dependent on LiveConnect working.ft
52 * Not sure whether this is a good thing, or not.
53 *
54 * @see LiveConnectTest#addTestCase
55 * @see com.netscape.javascript.qa.drivers.LiveConnectDrv
56 * @see com.netscape.javascript.qa.drivers.LiveConnectEnv
57 * @see com.netscape.javascript.qa.drivers.TestDriver
58 *
59 * @author [email protected]
60 *
61 */
62
63 public class LiveConnectTest extends Applet implements Runnable {
64 /**
65 * Create a new LiveConnectTest.
66 */
67 public LiveConnectTest() {
68 }
69
70 /**
71 * Main method. Used when running as an application.
72 */
73 public static void main( String[] args ) {
74 LiveConnectTest test = new LiveConnectTest();
75 }
76
77 public void start() {
78 run();
79 }
80
81 public void stop() {
82 }
83
84 /**
85 * Main method when run as an Applet.
86 *
87 */
88
89 public void run() {
90 System.out.println("Running the test." );
91 setupTestEnvironment();
92 file.startTime = TestDriver.getCurrentTime();
93 executeTest();
94 file.endTime= TestDriver.getCurrentTime();
95 getResults();
96 cleanupTestEnvironment();
97 stop();
98 }
99
100 /**
101 * Initialize variables, open log files.
102 */
103 public void setupTestEnvironment() {
104 global = JSObject.getWindow( this );
105
106 getEnvironment();
107
108 if ( ENVIRONMENT == BROWSER ) {
109 // PrivilegeManager.enablePrivilege( "UniversalFileAccess" );
110 // PrivilegeManager.enablePrivilege( "UniversalPropertyRead" );
111 }
112 output = getOutputDirectory();
113 if (LOGGING) {
114 TestDriver.openLogFiles( output );
115 templog = getTempLog( output );
116 }
117 testdir = getTestDirectory();
118 file = new TestFile( this.getClass().getName(), testdir.toString() +
119 this.getClass().toString() );
120
121 file.bugnumber = this.BUGNUMBER;
122 file.description = ( this.getClass().toString() );
123 }
124
125 public void getEnvironment() {
126 this.ENVIRONMENT = ( global.getMember("version").equals( "undefined"))
127 ? BROWSER
128 : SHELL;
129 return;
130 }
131
132 /**
133 * Create a TestLog object which will be used to write the testclass
134 * results to a temporary log file. If there is an existing log,
135 * delete it.
136 */
137 public TestLog getTempLog( File output ) {
138 String templog = "";
139
140 try {
141 TEMP_LOG_NAME = ((String) global.getMember( "OUTPUT_FILE" )).equals
142 ("undefined")
143 ? TEMP_LOG_NAME
144 : (String) global.getMember("OUTPUT_FILE");
145
146 templog = output.getAbsolutePath() + TEMP_LOG_NAME;
147 } catch ( Exception e ) {
148 this.exception = e.toString();
149 p("Exception deleting existing templog: " + e.toString() );
150 }
151
152 return new TestLog( templog, "" );
153 }
154
155 /**
156 * Get the OUTPUT_FILE if defined, which is where the temp log is written.
157 * If the file is not defined, assume we are running in the shell, and do
158 * not write any output.
159 *
160 * XXX change this to match description above.
161 *
162 * @see com.netscape.javascript.drivers.LiveConnectDrv
163 */
164 public File getOutputDirectory() {
165 String o = "";
166
167 if ( this.ENVIRONMENT == BROWSER ) {
168 String outputParam = this.getParameter( "output" );
169
170 return new File( getParameter( "output" ) );
171 } else {
172 try {
173 o = (String) global.getMember( "OUTPUT_DIRECTORY" );
174
175 if ( ! o.equals( "undefined") ) {
176 LOGGING = true;
177 }
178 } catch ( Exception e ) {
179 System.err.println( "OUTPUT_DIRECTORY threw: " + e );
180 e.printStackTrace();
181 System.exit(1);
182 }
183
184 System.out.println( "Output file is " + o.toString() );
185
186 return new File( o.toString() );
187 }
188 }
189
190 /**
191 * Get the TEST_DIRECTORY variable, which must be defined in the
192 * JavaScript helper file.
193 *
194 * @see com.netscape.javascript.drivers.LiveConnectDrv
195 */
196 public File getTestDirectory() {
197 try {
198 String o = (String) global.getMember( "TEST_DIRECTORY" );
199 o = o.endsWith( File.separator ) ? o : o + File.separator;
200 return new File( o.toString() );
201 } catch ( Exception e ) {
202 System.err.println( "TEST_DIRECTORY is not defined: " + e );
203 return new File( "." );
204 }
205 }
206
207 /**
208 * Execute the test. Subclasses must implement this method. The
209 * default implemenation does nothing. This method should instantiate
210 * TestCases, and add them to the testcase Vector.
211 *
212 */
213 public void executeTest() {
214 return;
215 }
216
217 /**
218 * Add a TestCase to the testcase Vector.
219 *
220 * @param description a description of the test case
221 * @param expect a string representation of the expected result
222 * @param actual a string representation of the actual result
223 * @param exception the message in any JavaScript runtime error
224 * or Java exception that was thrown.
225 *
226 */
227
228 public void addTestCase( String description, String expect, String actual,
229 String exception )
230 {
231 String result = ( expect == actual ) ? "true" : "false";
232 TestCase tc = new TestCase( result, this.getClass().getName().toString(),
233 description, expect, actual, exception );
234 file.caseVector.addElement( tc );
235 file.totalCases++;
236 if ( result == "false" ) {
237 file.passed = false;
238 file.casesFailed++;
239 } else {
240 file.casesPassed++;
241 }
242 return;
243 }
244
245 /**
246 * Close all logs.
247 *
248 */
249 public void closeLogs() {
250 TestDriver.getLog( output, TestDriver.SUMMARY_LOG_NAME ).closeLog();
251 TestDriver.getLog( output, TestDriver.SUITE_LOG_NAME ).closeLog();
252 TestDriver.getLog( output, TestDriver.FILE_LOG_NAME ).closeLog();
253 TestDriver.getLog( output, TestDriver.CASE_LOG_NAME ).closeLog();
254 templog.closeLog();
255 templog = null;
256 }
257
258 /**
259 * Iterate through the testcase Vector. Populate the properties of the
260 * TestFile object.
261 */
262 public void getResults() {
263 displayResults();
264 if (LOGGING) {
265 writeResultsToCaseLog();
266 writeResultsToFileLog();
267 writeResultsToTempLog();
268 }
269 }
270
271 /**
272 * Write a summary of the TestCase to standard out.
273 *
274 */
275 public void displayResults() {
276 for ( int i = 0; i < file.caseVector.size(); i++ ) {
277 TestCase tc = (TestCase) file.caseVector.elementAt(i);
278 p( tc.description +" = "+ tc.actual+
279 ( tc.expect != tc.actual
280 ? " FAILED! expected: " + tc.expect
281 : " PASSED!" ) );
282 }
283 getFailedCases();
284 }
285
286 /**
287 * If any test cases did not pass, write a summary of the failed cases
288 * to the CASE_LOG using static TestDriver methods.
289 *
290 * @see com.netscape.javascript.qa.drivers.TestDriver#writeCaseResults
291 */
292 public void writeResultsToCaseLog() {
293 if ( !file.passed ) {
294 TestDriver.writeCaseResults( file, file.description, output );
295 }
296 }
297
298 /**
299 * If the test failed, write a summary of this test to the FILE_LOG. Use
300 * static TestDriver methods to write to the FILE_LOG.
301 *
302 * @see com.netscape.javascript.qa.drivers.TestDriver#writeFileResult
303 */
304 public void writeResultsToFileLog() {
305 if ( !file.passed ) {
306 TestDriver.writeFileResult(file, null, output);
307 }
308 }
309
310 /**
311 * Write to a file containing the results of this TestFile. The content
312 * of this file is parsed by the parseResult() method of LiveConnectEnv,
313 * which maintains information about all files and suites executed. The
314 * format of the temp log is a list of name value pairs, one per line.
315 * The temp log is overwritten each time the LiveConnectDrv executes a
316 * test.
317 *
318 * Changes to the format of the templog file will require changes to
319 * LiveConnectDrv.parseResult.
320 *
321 * <pre>
322 * CLASSNAME LiveConnectTest
323 * PASSED [true, false]
324 * LENGTH [number of testcases in this test]
325 * NO_PASSED [number of testcases that passed]
326 * NO_FAILED [number of testcases that failed]
327 * </pre>
328 *
329 * XXX may also want to write bugnumber, time completed, etc?. probably
330 * what this should do is enumerate all the properties of a TestFile object,
331 * that the driver can parse and treat as though it had a normal TestFile
332 * object.
333 *
334 * @see com.netscape.javascript.qa.drivers.LiveConnectDrv#parseResult
335 */
336 public void writeResultsToTempLog(){
337 System.out.println( "Writing results to " + templog.toString() );
338
339 templog.writeLine( file.description );
340 templog.writeLine( file.passed + "" );
341 templog.writeLine( file.caseVector.size() +"" );
342 templog.writeLine( file.casesPassed + "");
343 templog.writeLine( file.casesFailed + "");
344 templog.writeLine( file.bugnumber );
345
346 p( file.name );
347 p( "passed: " + passed );
348 p( "total cases: " + file.caseVector.size() );
349 p( "cases passed: " + file.casesPassed );
350 p( "cases failed: " + file.casesFailed );
351 p( "bugnumber: " + file.bugnumber );
352 return;
353 }
354
355 /**
356 * Close logs, and set the value of the "completed" variable in the
357 * JavaScript environment so that the driver knows the test has
358 * finished.
359 */
360 public void cleanupTestEnvironment() {
361 try {
362 if ( LOGGING ) {
363 // templog.closeLog();
364 }
365 // global.eval("var completed = true;");
366 } catch ( Exception e ) {
367 p( "exception in cleanupTestEnvironment: " + e.toString() );
368 e.printStackTrace();
369 }
370 }
371
372 public void p (String s ) {
373 System.out.println( s );
374 }
375
376 /**
377 * This dandy little hack from Nix that is used to test things that
378 * should fail. It runs a method on a JavaScript object, catching any
379 * exceptions. It returns the detail message from the exception, or
380 * NO_EXCEPTION if it succeeds. This is from nix.
381 *
382 */
383 public static String catchException(JSObject self, String method,
384 Object args[]) {
385 Object rval;
386 String msg;
387 try {
388 rval = self.call(method, args);
389 msg = NO_EXCEPTION;
390 } catch (Throwable e) {
391 msg = e.getMessage();
392 }
393 return msg;
394 }
395
396 public void getFailedCases() {
397 if ( file.passed ) {
398 return;
399 }
400
401 p( "********** Failed Test Cases **********" );
402
403 for ( int i = 0; i < file.caseVector.size(); i++ ) {
404 TestCase tc = (TestCase) file.caseVector.elementAt(i);
405
406 if ( tc.passed != "true" ) {
407
408 p( tc.description +" = "+ tc.actual+
409 ( tc.expect != tc.actual
410 ? " FAILED! expected: " + tc.expect
411 : " PASSED!"
412 ) +
413 ( tc.reason.length() > 0
414 ? ": " + tc.reason
415 : ""
416 )
417 );
418 }
419 }
420 }
421
422 public File output;
423 public File testdir;
424
425 public TestFile file;
426
427 public Vector testcase;
428 public JSObject global;
429
430 public boolean passed = true;
431 public Vector failedVector = new Vector();
432 public TestLog templog;
433 public String exception = "";
434
435 public String BUGNUMBER = "";
436
437 boolean LOGGING = false;
438 public static String TEMP_LOG_NAME = "result.tmp";
439
440 public static final int BROWSER = 1;
441 public static final int SHELL = 0;
442
443 public int ENVIRONMENT;
444
445 public static final String NO_EXCEPTION = "Expected exception not thrown.";
446
447 public static final String DATA_CLASS = "com.netscape.javascript.qa.liveconnect.DataTypeClass";
448 }