| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.drivers; |
| 6 |
|
| 7 |
import java.io.BufferedReader; |
| 8 |
import java.io.ByteArrayInputStream; |
| 9 |
import java.io.ByteArrayOutputStream; |
| 10 |
import java.io.File; |
| 11 |
import java.io.FileFilter; |
| 12 |
import java.io.InputStreamReader; |
| 13 |
import java.io.PrintStream; |
| 14 |
import java.util.ArrayList; |
| 15 |
|
| 16 |
import org.mozilla.javascript.*; |
| 17 |
import org.mozilla.javascript.tools.shell.Global; |
| 18 |
import org.mozilla.javascript.tools.shell.Main; |
| 19 |
import org.mozilla.javascript.tools.shell.ShellContextFactory; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class ShellTest { |
| 25 |
public static final FileFilter DIRECTORY_FILTER = new FileFilter() { |
| 26 |
public boolean accept(File pathname) |
| 27 |
{ |
| 28 |
return pathname.isDirectory() && !pathname.getName().equals("CVS"); |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
public static final FileFilter TEST_FILTER = new FileFilter() { |
| 33 |
public boolean accept(File pathname) |
| 34 |
{ |
| 35 |
return pathname.getName().endsWith(".js") |
| 36 |
&& !pathname.getName().equals("shell.js") |
| 37 |
&& !pathname.getName().equals("browser.js") |
| 38 |
&& !pathname.getName().equals("template.js"); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
public static String getStackTrace(Throwable t) { |
| 43 |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 44 |
t.printStackTrace(new PrintStream(bytes)); |
| 45 |
return new String(bytes.toByteArray()); |
| 46 |
} |
| 47 |
|
| 48 |
private static void runFileIfExists(Context cx, Scriptable global, File f) { |
| 49 |
if(f.isFile()) { |
| 50 |
Main.processFileNoThrow(cx, global, f.getPath()); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
private static class TestState { |
| 55 |
boolean finished; |
| 56 |
ErrorReporterWrapper errors; |
| 57 |
int exitCode = 0; |
| 58 |
} |
| 59 |
|
| 60 |
public static abstract class Status { |
| 61 |
private boolean negative; |
| 62 |
|
| 63 |
public final void setNegative() { |
| 64 |
this.negative = true; |
| 65 |
} |
| 66 |
|
| 67 |
public final boolean isNegative() { |
| 68 |
return this.negative; |
| 69 |
} |
| 70 |
|
| 71 |
public final void hadErrors(JsError[] errors) { |
| 72 |
if (!negative && errors.length > 0) { |
| 73 |
failed("JavaScript errors:\n" + JsError.toString(errors)); |
| 74 |
} else if (negative && errors.length == 0) { |
| 75 |
failed("Should have produced runtime error."); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public final void hadErrors(File jsFile, JsError[] errors) { |
| 80 |
if (!negative && errors.length > 0) { |
| 81 |
failed("JavaScript errors in " + jsFile + ":\n" + JsError.toString(errors)); |
| 82 |
} else if (negative && errors.length == 0) { |
| 83 |
failed("Should have produced runtime error in " + jsFile + "."); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public abstract void running(File jsFile); |
| 88 |
|
| 89 |
public abstract void failed(String s); |
| 90 |
public abstract void threw(Throwable t); |
| 91 |
public abstract void timedOut(); |
| 92 |
public abstract void exitCodesWere(int expected, int actual); |
| 93 |
public abstract void outputWas(String s); |
| 94 |
|
| 95 |
static Status compose(final Status[] array) { |
| 96 |
return new Status() { |
| 97 |
@Override |
| 98 |
public void running(File file) { |
| 99 |
for (int i=0; i<array.length; i++) { |
| 100 |
array[i].running(file); |
| 101 |
} |
| 102 |
} |
| 103 |
@Override |
| 104 |
public void threw(Throwable t) { |
| 105 |
for (int i=0; i<array.length; i++) { |
| 106 |
array[i].threw(t); |
| 107 |
} |
| 108 |
} |
| 109 |
@Override |
| 110 |
public void failed(String s) { |
| 111 |
for (int i=0; i<array.length; i++) { |
| 112 |
array[i].failed(s); |
| 113 |
} |
| 114 |
} |
| 115 |
@Override |
| 116 |
public void exitCodesWere(int expected, int actual) { |
| 117 |
for (int i=0; i<array.length; i++) { |
| 118 |
array[i].exitCodesWere(expected, actual); |
| 119 |
} |
| 120 |
} |
| 121 |
@Override |
| 122 |
public void outputWas(String s) { |
| 123 |
for (int i=0; i<array.length; i++) { |
| 124 |
array[i].outputWas(s); |
| 125 |
} |
| 126 |
} |
| 127 |
@Override |
| 128 |
public void timedOut() { |
| 129 |
for (int i=0; i<array.length; i++) { |
| 130 |
array[i].timedOut(); |
| 131 |
} |
| 132 |
} |
| 133 |
}; |
| 134 |
} |
| 135 |
|
| 136 |
static class JsError { |
| 137 |
static String toString(JsError[] e) { |
| 138 |
String rv = ""; |
| 139 |
for (int i=0; i<e.length; i++) { |
| 140 |
rv += e[i].toString(); |
| 141 |
if (i+1 != e.length) { |
| 142 |
rv += "\n"; |
| 143 |
} |
| 144 |
} |
| 145 |
return rv; |
| 146 |
} |
| 147 |
|
| 148 |
private String message; |
| 149 |
private String sourceName; |
| 150 |
private int line; |
| 151 |
private String lineSource; |
| 152 |
private int lineOffset; |
| 153 |
|
| 154 |
JsError(String message, String sourceName, int line, String lineSource, int lineOffset) { |
| 155 |
this.message = message; |
| 156 |
this.sourceName = sourceName; |
| 157 |
this.line = line; |
| 158 |
this.lineSource = lineSource; |
| 159 |
this.lineOffset = lineOffset; |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public String toString() { |
| 164 |
String locationLine = ""; |
| 165 |
if (sourceName != null) |
| 166 |
locationLine += sourceName + ":"; |
| 167 |
if (line != 0) |
| 168 |
locationLine += line + ": "; |
| 169 |
locationLine += message; |
| 170 |
String sourceLine = this.lineSource; |
| 171 |
String errCaret = null; |
| 172 |
if (lineSource != null) { |
| 173 |
errCaret = ""; |
| 174 |
for (int i=0; i<lineSource.length(); i++) { |
| 175 |
char c = lineSource.charAt(i); |
| 176 |
if (i < lineOffset-1) { |
| 177 |
if (c == '\t') { |
| 178 |
errCaret += "\t"; |
| 179 |
} else { |
| 180 |
errCaret += " "; |
| 181 |
} |
| 182 |
} else if (i == lineOffset-1) { |
| 183 |
errCaret += "^"; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
String rv = locationLine; |
| 188 |
if (sourceLine != null) { |
| 189 |
rv += "\n" + sourceLine; |
| 190 |
} |
| 191 |
if (errCaret != null) { |
| 192 |
rv += "\n" + errCaret; |
| 193 |
} |
| 194 |
return rv; |
| 195 |
} |
| 196 |
|
| 197 |
String getMessage() { |
| 198 |
return message; |
| 199 |
} |
| 200 |
|
| 201 |
String getSourceName() { |
| 202 |
return sourceName; |
| 203 |
} |
| 204 |
|
| 205 |
int getLine() { |
| 206 |
return line; |
| 207 |
} |
| 208 |
|
| 209 |
String getLineSource() { |
| 210 |
return lineSource; |
| 211 |
} |
| 212 |
|
| 213 |
int getLineOffset() { |
| 214 |
return lineOffset; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
private static class ErrorReporterWrapper implements ErrorReporter { |
| 220 |
private ErrorReporter original; |
| 221 |
private ArrayList<Status.JsError> errors = new ArrayList<Status.JsError>(); |
| 222 |
|
| 223 |
ErrorReporterWrapper(ErrorReporter original) { |
| 224 |
this.original = original; |
| 225 |
} |
| 226 |
|
| 227 |
private void addError(String string, String string0, int i, String string1, int i0) { |
| 228 |
errors.add( new Status.JsError(string, string0, i, string1, i0) ); |
| 229 |
} |
| 230 |
|
| 231 |
public void warning(String string, String string0, int i, String string1, int i0) { |
| 232 |
original.warning(string, string0, i, string1, i0); |
| 233 |
} |
| 234 |
|
| 235 |
public EvaluatorException runtimeError(String string, String string0, int i, String string1, int i0) { |
| 236 |
return original.runtimeError(string, string0, i, string1, i0); |
| 237 |
} |
| 238 |
|
| 239 |
public void error(String string, String string0, int i, String string1, int i0) { |
| 240 |
addError(string, string0, i, string1, i0); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public static abstract class Parameters { |
| 245 |
public abstract int getTimeoutMilliseconds(); |
| 246 |
} |
| 247 |
|
| 248 |
@SuppressWarnings(value={"deprecation"}) |
| 249 |
private static void callStop(Thread t) { |
| 250 |
t.stop(); |
| 251 |
} |
| 252 |
|
| 253 |
public static void run(final ShellContextFactory shellContextFactory, |
| 254 |
final File jsFile, final Parameters parameters, |
| 255 |
final Status status) throws Exception { |
| 256 |
final Global global = new Global(); |
| 257 |
final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 258 |
final PrintStream p = new PrintStream(out); |
| 259 |
global.setOut(p); |
| 260 |
global.setErr(p); |
| 261 |
global.defineFunctionProperties( |
| 262 |
new String[] { "options" }, ShellTest.class, |
| 263 |
ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | |
| 264 |
ScriptableObject.READONLY); |
| 265 |
|
| 266 |
shellContextFactory.setAllowReservedKeywords(false); |
| 267 |
final TestState testState = new TestState(); |
| 268 |
if (jsFile.getName().endsWith("-n.js")) { |
| 269 |
status.setNegative(); |
| 270 |
} |
| 271 |
final Throwable thrown[] = {null}; |
| 272 |
|
| 273 |
Thread t = new Thread(new Runnable() |
| 274 |
{ |
| 275 |
public void run() |
| 276 |
{ |
| 277 |
try |
| 278 |
{ |
| 279 |
shellContextFactory.call(new ContextAction() |
| 280 |
{ |
| 281 |
public Object run(Context cx) |
| 282 |
{ |
| 283 |
status.running(jsFile); |
| 284 |
testState.errors = new ErrorReporterWrapper(cx.getErrorReporter()); |
| 285 |
cx.setErrorReporter( testState.errors ); |
| 286 |
global.init(cx); |
| 287 |
try { |
| 288 |
runFileIfExists(cx, global, new File(jsFile.getParentFile().getParentFile().getParentFile(), "shell.js")); |
| 289 |
runFileIfExists(cx, global, new File(jsFile.getParentFile().getParentFile(), "shell.js")); |
| 290 |
runFileIfExists(cx, global, new File(jsFile.getParentFile(), "shell.js")); |
| 291 |
runFileIfExists(cx, global, jsFile); |
| 292 |
status.hadErrors(jsFile, testState.errors.errors.toArray(new Status.JsError[0])); |
| 293 |
} catch (ThreadDeath e) { |
| 294 |
} catch (Throwable t) { |
| 295 |
status.threw(t); |
| 296 |
} |
| 297 |
return null; |
| 298 |
} |
| 299 |
}); |
| 300 |
} |
| 301 |
catch (Error t) |
| 302 |
{ |
| 303 |
thrown[0] = t; |
| 304 |
} |
| 305 |
catch (RuntimeException t) |
| 306 |
{ |
| 307 |
thrown[0] = t; |
| 308 |
} |
| 309 |
finally { |
| 310 |
synchronized(testState) |
| 311 |
{ |
| 312 |
testState.finished = true; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
}, jsFile.getPath()); |
| 317 |
t.setDaemon(true); |
| 318 |
t.start(); |
| 319 |
t.join(parameters.getTimeoutMilliseconds()); |
| 320 |
synchronized(testState) |
| 321 |
{ |
| 322 |
if(!testState.finished) |
| 323 |
{ |
| 324 |
callStop(t); |
| 325 |
status.timedOut(); |
| 326 |
} |
| 327 |
} |
| 328 |
int expectedExitCode = 0; |
| 329 |
p.flush(); |
| 330 |
status.outputWas(new String(out.toByteArray())); |
| 331 |
BufferedReader r = new BufferedReader(new InputStreamReader( |
| 332 |
new ByteArrayInputStream(out.toByteArray()))); |
| 333 |
String failures = ""; |
| 334 |
for(;;) |
| 335 |
{ |
| 336 |
String s = r.readLine(); |
| 337 |
if(s == null) |
| 338 |
{ |
| 339 |
break; |
| 340 |
} |
| 341 |
if(s.indexOf("FAILED!") != -1) |
| 342 |
{ |
| 343 |
failures += s + '\n'; |
| 344 |
} |
| 345 |
int expex = s.indexOf("EXPECT EXIT CODE "); |
| 346 |
if(expex != -1) |
| 347 |
{ |
| 348 |
expectedExitCode = s.charAt(expex + "EXPECT EXIT CODE ".length()) - '0'; |
| 349 |
} |
| 350 |
} |
| 351 |
if (thrown[0] != null) |
| 352 |
{ |
| 353 |
status.threw(thrown[0]); |
| 354 |
} |
| 355 |
status.exitCodesWere(expectedExitCode, testState.exitCode); |
| 356 |
if(failures != "") |
| 357 |
{ |
| 358 |
status.failed(failures); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public static String options() { |
| 367 |
return ""; |
| 368 |
} |
| 369 |
} |