org.mozilla.javascript.commonjs.module.Require Java Examples

The following examples show how to use org.mozilla.javascript.commonjs.module.Require. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequireJarTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
@Override
public void testSandboxed() throws Exception
{
    final Context cx = createContext();
    final Require require = getSandboxedRequire(cx);
    require.requireMain(cx, "testSandboxed");
    // Also, test idempotent double-require of same main:
    require.requireMain(cx, "testSandboxed");
    // Also, test failed require of different main:
    try {
        require.requireMain(cx, "blah");
        fail();
    }
    catch(IllegalStateException e) {
        // Expected, success
    }
}
 
Example #2
Source File: RequireJarTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSandboxed() throws Exception
{
    final Context cx = createContext();
    final Require require = getSandboxedRequire(cx);
    require.requireMain(cx, "testSandboxed");
    // Also, test idempotent double-require of same main:
    require.requireMain(cx, "testSandboxed");
    // Also, test failed require of different main:
    try {
        require.requireMain(cx, "blah");
        fail();
    }
    catch(IllegalStateException e) {
        // Expected, success
    }
}
 
Example #3
Source File: RequireJarTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
@Override
public void testSetMainForAlreadyLoadedModule() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"), 
            "testSetMainForAlreadyLoadedModule.js", 1, null);
    try {
        require.requireMain(cx, "assert");
        fail();
    }
    catch(IllegalStateException e) {
        assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
    }
}
 
Example #4
Source File: RequireTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSandboxed() throws Exception
{
    final Context cx = createContext();
    final Require require = getSandboxedRequire(cx);
    require.requireMain(cx, "testSandboxed");
    // Also, test idempotent double-require of same main:
    require.requireMain(cx, "testSandboxed");
    // Also, test failed require of different main:
    try {
        require.requireMain(cx, "blah");
        fail();
    }
    catch(IllegalStateException e) {
        // Expected, success
    }
}
 
Example #5
Source File: ComplianceTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static Require createRequire(File dir, Context cx, Scriptable scope)
throws URISyntaxException
{
    return new Require(cx, scope, new StrongCachingModuleScriptProvider(
            new UrlModuleSourceProvider(
                    Collections.singleton(dir.getAbsoluteFile().toURI()),
                    Collections.singleton(new URI(ComplianceTest.class.getResource(".").toExternalForm() + "/")))),
                    null, null, false);
}
 
Example #6
Source File: Global.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public Require installRequire(Context cx, List<String> modulePath,
                              boolean sandboxed) {
    RequireBuilder rb = new RequireBuilder();
    rb.setSandboxed(sandboxed);
    List<URI> uris = new ArrayList<URI>();
    if (modulePath != null) {
        for (String path : modulePath) {
            try {
                URI uri = new URI(path);
                if (!uri.isAbsolute()) {
                    // call resolve("") to canonify the path
                    uri = new File(path).toURI().resolve("");
                }
                if (!uri.toString().endsWith("/")) {
                    // make sure URI always terminates with slash to
                    // avoid loading from unintended locations
                    uri = new URI(uri + "/");
                }
                uris.add(uri);
            } catch (URISyntaxException usx) {
                throw new RuntimeException(usx);
            }
        }
    }
    rb.setModuleScriptProvider(
            new SoftCachingModuleScriptProvider(
                    new UrlModuleSourceProvider(uris, null)));
    Require require = rb.createRequire(cx, this);
    require.install(this);
    return require;
}
 
Example #7
Source File: RequireJarTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Require getSandboxedRequire(Context cx, Scriptable scope, boolean sandboxed)
        throws URISyntaxException
{
    return new Require(cx, cx.initStandardObjects(),
            new StrongCachingModuleScriptProvider(
                    new UrlModuleSourceProvider(Collections.singleton(
                            getDirectory()), null)), null, null, true);
}
 
Example #8
Source File: RequireJarTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testSetMainForAlreadyLoadedModule() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"),
            "testSetMainForAlreadyLoadedModule.js", 1, null);
    try {
        require.requireMain(cx, "assert");
        fail();
    }
    catch(IllegalStateException e) {
        assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
    }
}
 
Example #9
Source File: RequireJarTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRelativeId() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testRelativeId.js"),
            "testRelativeId.js", 1, null);
}
 
Example #10
Source File: RequireJarTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testNonSandboxed() throws Exception
{
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    final String jsFile = getClass().getResource("testNonSandboxed.js").toExternalForm();
    ScriptableObject.putProperty(scope, "moduleUri", jsFile);
    require.requireMain(cx, "testNonSandboxed");
}
 
Example #11
Source File: RequireTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Require getSandboxedRequire(Context cx, Scriptable scope, boolean sandboxed)
        throws URISyntaxException
{
    return new Require(cx, cx.initStandardObjects(),
            new StrongCachingModuleScriptProvider(
                    new UrlModuleSourceProvider(Collections.singleton(
                            getDirectory()), null)), null, null, true);
}
 
Example #12
Source File: RequireTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testSetMainForAlreadyLoadedModule() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"),
            "testSetMainForAlreadyLoadedModule.js", 1, null);
    try {
        require.requireMain(cx, "assert");
        fail();
    }
    catch(IllegalStateException e) {
        assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
    }
}
 
Example #13
Source File: RequireTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRelativeId() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testRelativeId.js"),
            "testRelativeId.js", 1, null);
}
 
Example #14
Source File: RequireTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testNonSandboxed() throws Exception
{
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    final String jsFile = getClass().getResource("testNonSandboxed.js").toExternalForm();
    ScriptableObject.putProperty(scope, "moduleUri", jsFile);
    require.requireMain(cx, "testNonSandboxed");
}
 
Example #15
Source File: Global.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public Require installRequire(Context cx, List<String> modulePath,
                              boolean sandboxed) {
    RequireBuilder rb = new RequireBuilder();
    rb.setSandboxed(sandboxed);
    List<URI> uris = new ArrayList<URI>();
    if (modulePath != null) {
        for (String path : modulePath) {
            try {
                URI uri = new URI(path);
                if (!uri.isAbsolute()) {
                    // call resolve("") to canonify the path
                    uri = new File(path).toURI().resolve("");
                }
                if (!uri.toString().endsWith("/")) {
                    // make sure URI always terminates with slash to
                    // avoid loading from unintended locations
                    uri = new URI(uri + "/");
                }
                uris.add(uri);
            } catch (URISyntaxException usx) {
                throw new RuntimeException(usx);
            }
        }
    }
    rb.setModuleScriptProvider(
            new SoftCachingModuleScriptProvider(
                    new UrlModuleSourceProvider(uris, null)));
    Require require = rb.createRequire(cx, this);
    require.install(this);
    return require;
}
 
Example #16
Source File: RequireJarTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
private Require getSandboxedRequire(Context cx, Scriptable scope, boolean sandboxed)
        throws URISyntaxException
{
    return new Require(cx, cx.initStandardObjects(), 
            new StrongCachingModuleScriptProvider(
                    new UrlModuleSourceProvider(Collections.singleton(
                            getDirectory()), null)), null, null, true);
}
 
Example #17
Source File: RequireJarTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Override
public void testRelativeId() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testRelativeId.js"), 
            "testRelativeId.js", 1, null);
}
 
Example #18
Source File: RequireJarTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Override
public void testNonSandboxed() throws Exception
{
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    final String jsFile = getClass().getResource("testNonSandboxed.js").toExternalForm();
    ScriptableObject.putProperty(scope, "moduleUri", jsFile);
    require.requireMain(cx, "testNonSandboxed");
}
 
Example #19
Source File: RequireTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
public void testSetMainForAlreadyLoadedModule() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateString(scope, TestUtils.readAsset(dir + File.separator + "testSetMainForAlreadyLoadedModule.js"),
            "testSetMainForAlreadyLoadedModule.js", 1, null);
    try {
        require.requireMain(cx, "assert");
        fail();
    } catch (IllegalStateException e) {
        assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
    }
}
 
Example #20
Source File: RequireTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
public void testRelativeId() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateString(scope, TestUtils.readAsset(dir + File.separator + "testRelativeId.js"),
            "testRelativeId.js", 1, null);
}
 
Example #21
Source File: RequireTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
public void testNonSandboxed() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    final String jsFile = TestUtils.readAsset(dir + File.separator + "testNonSandboxed.js");
    ScriptableObject.putProperty(scope, "moduleUri", jsFile);
    require.requireMain(cx, "testNonSandboxed");
}
 
Example #22
Source File: RequireTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
public void testSandboxed() throws Exception {
    final Context cx = createContext();
    final Require require = getSandboxedRequire(cx);
    require.requireMain(cx, "testSandboxed");
    // Also, test idempotent double-require of same main:
    require.requireMain(cx, "testSandboxed");
    // Also, test failed require of different main:
    try {
        require.requireMain(cx, "blah");
        fail();
    } catch (IllegalStateException e) {
        // Expected, success
    }
}
 
Example #23
Source File: RequireJarTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
private Require getSandboxedRequire(final Context cx)
throws URISyntaxException {
    return getSandboxedRequire(cx, cx.initStandardObjects(), true);
}
 
Example #24
Source File: RequireTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Require getSandboxedRequire(final Context cx)
throws URISyntaxException {
    return getSandboxedRequire(cx, cx.initStandardObjects(), true);
}
 
Example #25
Source File: RequireTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
private Require getSandboxedRequire(final Context cx)
        throws URISyntaxException {
    return getSandboxedRequire(cx, cx.initStandardObjects(), true);
}
 
Example #26
Source File: RequireJarTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Require getSandboxedRequire(final Context cx)
throws URISyntaxException {
    return getSandboxedRequire(cx, cx.initStandardObjects(), true);
}