Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/ClassShutterExceptionTest.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.Scriptable;
14 import org.mozilla.javascript.ClassShutter;
15 import org.mozilla.javascript.RhinoException;
16 import org.mozilla.javascript.EvaluatorException;
17
18 /**
19 * @author Norris Boyd
20 */
21 public class ClassShutterExceptionTest extends TestCase {
22 private static Context.ClassShutterSetter classShutterSetter;
23
24 /**
25 * Define a ClassShutter that prevents access to all Java classes.
26 */
27 static class OpaqueShutter implements ClassShutter {
28 public boolean visibleToScripts(String name) {
29 return false;
30 }
31 }
32
33 public void helper(String source) {
34 Context cx = Context.enter();
35 Context.ClassShutterSetter setter = cx.getClassShutterSetter();
36 try {
37 Scriptable globalScope = cx.initStandardObjects();
38 if (setter == null) {
39 setter = classShutterSetter;
40 } else {
41 classShutterSetter = setter;
42 }
43 setter.setClassShutter(new OpaqueShutter());
44 cx.evaluateString(globalScope, source, "test source", 1, null);
45 } finally {
46 setter.setClassShutter(null);
47 Context.exit();
48 }
49 }
50
51 public void testClassShutterException() {
52 try {
53 helper("java.lang.System.out.println('hi');");
54 fail();
55 } catch (RhinoException e) {
56 // OpaqueShutter should prevent access to java.lang...
57 return;
58 }
59 }
60
61 public void testThrowingException() {
62 // JavaScript exceptions with no reference to Java
63 // should not be affected by the ClassShutter
64 helper("try { throw 3; } catch (e) { }");
65 }
66
67 public void testThrowingEcmaError() {
68 try {
69 // JavaScript exceptions with no reference to Java
70 // should not be affected by the ClassShutter
71 helper("friggin' syntax error!");
72 fail("Should have thrown an exception");
73 } catch (EvaluatorException e) {
74 // should have thrown an exception for syntax error
75 }
76 }
77
78 public void testThrowingEvaluatorException() {
79 // JavaScript exceptions with no reference to Java
80 // should not be affected by the ClassShutter
81 helper("try { eval('for;if;else'); } catch (e) { }");
82 }
83 }