| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package org.mozilla.javascript.tests; |
| 9 |
|
| 10 |
import junit.framework.TestCase; |
| 11 |
|
| 12 |
import org.mozilla.javascript.Context; |
| 13 |
import org.mozilla.javascript.ContextAction; |
| 14 |
import org.mozilla.javascript.JavaScriptException; |
| 15 |
import org.mozilla.javascript.RhinoException; |
| 16 |
import org.mozilla.javascript.Scriptable; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public class StackTraceTest extends TestCase { |
| 22 |
final static String LS = System.getProperty("line.separator"); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public void testFailureStackTrace() { |
| 29 |
RhinoException.useMozillaStackStyle(false); |
| 30 |
final String source1 = "function f2() { throw 'hello'; }; f2();"; |
| 31 |
final String source2 = "function f2() { 'H'.toLowerCase(); throw 'hello'; }; f2();"; |
| 32 |
final String source3 = "function f2() { new java.lang.String('H').toLowerCase(); throw 'hello'; }; f2();"; |
| 33 |
final String result = "\tat test.js (f2)" + LS + "\tat test.js" + LS; |
| 34 |
|
| 35 |
runWithExpectedStackTrace(source1, result); |
| 36 |
runWithExpectedStackTrace(source2, result); |
| 37 |
runWithExpectedStackTrace(source3, result); |
| 38 |
} |
| 39 |
|
| 40 |
private void runWithExpectedStackTrace(final String _source, final String _expectedStackTrace) |
| 41 |
{ |
| 42 |
final ContextAction action = new ContextAction() { |
| 43 |
public Object run(Context cx) { |
| 44 |
final Scriptable scope = cx.initStandardObjects(); |
| 45 |
try { |
| 46 |
cx.evaluateString(scope, _source, "test.js", 0, null); |
| 47 |
} |
| 48 |
catch (final JavaScriptException e) |
| 49 |
{ |
| 50 |
assertEquals(_expectedStackTrace, e.getScriptStackTrace()); |
| 51 |
return null; |
| 52 |
} |
| 53 |
throw new RuntimeException("Exception expected!"); |
| 54 |
} |
| 55 |
}; |
| 56 |
Utils.runWithOptimizationLevel(action, -1); |
| 57 |
} |
| 58 |
} |