Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/commonjs/module/RequireTest.java

raw symbols SCIP
1 package org.mozilla.javascript.tests.commonjs.module;
2
3 import java.io.InputStreamReader;
4 import java.io.Reader;
5 import java.net.URI;
6 import java.net.URISyntaxException;
7 import java.util.Collections;
8
9 import org.mozilla.javascript.Context;
10 import org.mozilla.javascript.Scriptable;
11 import org.mozilla.javascript.ScriptableObject;
12 import org.mozilla.javascript.commonjs.module.Require;
13 import org.mozilla.javascript.commonjs.module.provider.StrongCachingModuleScriptProvider;
14 import org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider;
15
16 import junit.framework.TestCase;
17
18 /**
19 * @author Attila Szegedi
20 * @version $Id: RequireTest.java,v 1.1 2011/04/07 22:24:37 hannes%helma.at Exp $
21 */
22 public class RequireTest extends TestCase
23 {
24 public void testSandboxed() throws Exception
25 {
26 final Context cx = createContext();
27 final Require require = getSandboxedRequire(cx);
28 require.requireMain(cx, "testSandboxed");
29 // Also, test idempotent double-require of same main:
30 require.requireMain(cx, "testSandboxed");
31 // Also, test failed require of different main:
32 try {
33 require.requireMain(cx, "blah");
34 fail();
35 }
36 catch(IllegalStateException e) {
37 // Expected, success
38 }
39 }
40
41 private Context createContext()
42 {
43 final Context cx = Context.enter();
44 cx.setOptimizationLevel(-1);
45 return cx;
46 }
47
48 public void testNonSandboxed() throws Exception
49 {
50 final Context cx = createContext();
51 final Scriptable scope = cx.initStandardObjects();
52 final Require require = getSandboxedRequire(cx, scope, false);
53 final String jsFile = getClass().getResource("testNonSandboxed.js").toExternalForm();
54 ScriptableObject.putProperty(scope, "moduleUri", jsFile);
55 require.requireMain(cx, "testNonSandboxed");
56 }
57
58 public void testVariousUsageErrors() throws Exception {
59 testWithSandboxedRequire("testNoArgsRequire");
60 }
61
62 public void testRelativeId() throws Exception {
63 final Context cx = createContext();
64 final Scriptable scope = cx.initStandardObjects();
65 final Require require = getSandboxedRequire(cx, scope, false);
66 require.install(scope);
67 cx.evaluateReader(scope, getReader("testRelativeId.js"),
68 "testRelativeId.js", 1, null);
69 }
70
71 public void testSetMainForAlreadyLoadedModule() throws Exception {
72 final Context cx = createContext();
73 final Scriptable scope = cx.initStandardObjects();
74 final Require require = getSandboxedRequire(cx, scope, false);
75 require.install(scope);
76 cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"),
77 "testSetMainForAlreadyLoadedModule.js", 1, null);
78 try {
79 require.requireMain(cx, "assert");
80 fail();
81 }
82 catch(IllegalStateException e) {
83 assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
84 }
85 }
86
87 private Reader getReader(String name) {
88 return new InputStreamReader(getClass().getResourceAsStream(name));
89 }
90
91 private void testWithSandboxedRequire(String moduleId) throws Exception {
92 final Context cx = createContext();
93 getSandboxedRequire(cx).requireMain(cx, moduleId);
94 }
95
96 private Require getSandboxedRequire(final Context cx)
97 throws URISyntaxException {
98 return getSandboxedRequire(cx, cx.initStandardObjects(), true);
99 }
100
101 private Require getSandboxedRequire(Context cx, Scriptable scope, boolean sandboxed)
102 throws URISyntaxException
103 {
104 return new Require(cx, cx.initStandardObjects(),
105 new StrongCachingModuleScriptProvider(
106 new UrlModuleSourceProvider(Collections.singleton(
107 getDirectory()), null)), null, null, true);
108 }
109
110 private URI getDirectory() throws URISyntaxException {
111 final String jsFile = getClass().getResource("testSandboxed.js").toExternalForm();
112 return new URI(jsFile.substring(0, jsFile.lastIndexOf('/') + 1));
113 }
114 }