Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/commonjs/module/RequireJarTest.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 /**
17 * @author Attila Szegedi
18 * @version $Id: RequireTest.java,v 1.1 2011/04/07 22:24:37 hannes%helma.at Exp $
19 */
20 public class RequireJarTest extends RequireTest
21 {
22 @Override
23 public void testSandboxed() throws Exception
24 {
25 final Context cx = createContext();
26 final Require require = getSandboxedRequire(cx);
27 require.requireMain(cx, "testSandboxed");
28 // Also, test idempotent double-require of same main:
29 require.requireMain(cx, "testSandboxed");
30 // Also, test failed require of different main:
31 try {
32 require.requireMain(cx, "blah");
33 fail();
34 }
35 catch(IllegalStateException e) {
36 // Expected, success
37 }
38 }
39
40 private Context createContext()
41 {
42 final Context cx = Context.enter();
43 cx.setOptimizationLevel(-1);
44 return cx;
45 }
46
47 @Override
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 @Override
59 public void testVariousUsageErrors() throws Exception {
60 testWithSandboxedRequire("testNoArgsRequire");
61 }
62
63 @Override
64 public void testRelativeId() throws Exception {
65 final Context cx = createContext();
66 final Scriptable scope = cx.initStandardObjects();
67 final Require require = getSandboxedRequire(cx, scope, false);
68 require.install(scope);
69 cx.evaluateReader(scope, getReader("testRelativeId.js"),
70 "testRelativeId.js", 1, null);
71 }
72
73 @Override
74 public void testSetMainForAlreadyLoadedModule() throws Exception {
75 final Context cx = createContext();
76 final Scriptable scope = cx.initStandardObjects();
77 final Require require = getSandboxedRequire(cx, scope, false);
78 require.install(scope);
79 cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"),
80 "testSetMainForAlreadyLoadedModule.js", 1, null);
81 try {
82 require.requireMain(cx, "assert");
83 fail();
84 }
85 catch(IllegalStateException e) {
86 assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
87 }
88 }
89
90 private Reader getReader(String name) {
91 return new InputStreamReader(getClass().getResourceAsStream(name));
92 }
93
94 private void testWithSandboxedRequire(String moduleId) throws Exception {
95 final Context cx = createContext();
96 getSandboxedRequire(cx).requireMain(cx, moduleId);
97 }
98
99 private Require getSandboxedRequire(final Context cx)
100 throws URISyntaxException {
101 return getSandboxedRequire(cx, cx.initStandardObjects(), true);
102 }
103
104 private Require getSandboxedRequire(Context cx, Scriptable scope, boolean sandboxed)
105 throws URISyntaxException
106 {
107 return new Require(cx, cx.initStandardObjects(),
108 new StrongCachingModuleScriptProvider(
109 new UrlModuleSourceProvider(Collections.singleton(
110 getDirectory()), null)), null, null, true);
111 }
112
113 private URI getDirectory() throws URISyntaxException {
114 final String jarFileLoc = getClass().getResource("modules.jar").toURI().toString();
115 String jarParent = "jar:" + jarFileLoc + "!/";
116 return new URI(jarParent);
117 }
118 }