Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/StackTraceTest.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 /**
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 * @author Marc Guillemot
20 */
21 public class StackTraceTest extends TestCase {
22 final static String LS = System.getProperty("line.separator");
23
24 /**
25 * As of CVS head on May, 11. 2009, stacktrace information is lost when a call to some
26 * native function has been made.
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 }