Java Code Examples for javax.script.ScriptEngine#getFactory()

The following examples show how to use javax.script.ScriptEngine#getFactory() . 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: NbPacScriptEvaluator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private String getEngineInfo(ScriptEngine engine) {
    StringBuilder sb = new StringBuilder();
    ScriptEngineFactory f = engine.getFactory();
    sb.append("LanguageName=");
    sb.append("\"").append(f.getLanguageName()).append("\"");
    sb.append(" ");
    sb.append("LanguageVersion=");
    sb.append("\"").append(f.getLanguageVersion()).append("\"");
    sb.append(" ");
    sb.append("EngineName=");
    sb.append("\"").append(f.getEngineName()).append("\"");
    sb.append(" ");
    sb.append("EngineNameAliases=");
    sb.append(Arrays.toString(f.getNames().toArray(new String[f.getNames().size()])));
    sb.append(" ");
    sb.append("EngineVersion=");
    sb.append("\"").append(f.getEngineVersion()).append("\"");
    return sb.toString();
}
 
Example 2
Source File: ScriptModuleTypeProvider.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * As {@link ScriptEngineFactory}s are added/removed, this method will create the {@link ParameterOption}s
 * that are available when selecting a script type in a ScriptActionType or ScriptConditionType.
 */
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
public void setScriptEngineFactory(ScriptEngineFactory engineFactory) {
    ScriptEngine scriptEngine = engineFactory.createScriptEngine(engineFactory.getScriptTypes().get(0));
    if (scriptEngine != null) {
        List<String> mimeTypes = new ArrayList<>();

        javax.script.ScriptEngineFactory factory = scriptEngine.getFactory();
        String languageName = String.format("%s (%s)",
                factory.getLanguageName().substring(0, 1).toUpperCase() + factory.getLanguageName().substring(1),
                factory.getLanguageVersion());
        mimeTypes.addAll(factory.getMimeTypes());
        String preferredMimeType = mimeTypes.get(0);
        mimeTypes.removeIf(mimeType -> !mimeType.contains("application") || mimeType.contains("x-"));
        if (!mimeTypes.isEmpty()) {
            preferredMimeType = mimeTypes.get(0);
        }
        parameterOptions.put(preferredMimeType, languageName);
        logger.trace("ParameterOptions: {}", parameterOptions);
    } else {
        logger.trace("setScriptEngineFactory: engine was null");
    }
}
 
Example 3
Source File: SaveAsAction.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
    ScriptEngine scriptEngine = getScriptManager().getEngine();
    ScriptEngineFactory engineFactory = scriptEngine.getFactory();
    List<String> extensions = engineFactory.getExtensions();
    JFileChooser fileChooser = OpenAction.createFileChooser(engineFactory);
    while (true) {
        int i = fileChooser.showSaveDialog(getScriptConsoleTopComponent());
        if (i != JFileChooser.APPROVE_OPTION || fileChooser.getSelectedFile() == null) {
            return;
        }

        File file = fileChooser.getSelectedFile();
        if (extensions.size() > 0) {
            file = ensureFileNameWithExtension(file, extensions);
        }
        if (!file.exists()) {
            getScriptConsoleTopComponent().saveScriptAs(file);
            return;
        } else {
            String msg = MessageFormat.format("File ''{0}'' already exists.\n" +
                    "Do you want to overwrite it?", file.getName());
            int ret = JOptionPane.showConfirmDialog(getScriptConsoleTopComponent(),
                                                    msg,
                                                    "Save Script",
                                                    JOptionPane.YES_NO_CANCEL_OPTION);
            if (ret == JOptionPane.YES_OPTION) {
                getScriptConsoleTopComponent().saveScriptAs(file);
                return;
            } else if (ret == JOptionPane.CANCEL_OPTION) {
                return;
            }
        }
    }
}
 
Example 4
Source File: ScriptManager.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
public void setEngine(final ScriptEngine engine) {
    if (this.engine != null && this.engine.getFactory() == engine.getFactory()) {
        return;
    }

    executorService.submit(() -> {
        ScriptManager.this.engine = engine;
        configureEngine();
    });
}
 
Example 5
Source File: ScriptConnection.java    From scriptella-etl with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new connection to JSR 223 scripting engine.
 *
 * @param parameters connection parameters.
 */
public ScriptConnection(ConnectionParameters parameters) {
    super(parameters);
    String lang = parameters.getStringProperty(LANGUAGE);
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager(ScriptConnection.class.getClassLoader());
    if (StringUtils.isEmpty(lang)) { //JavaScript is used by default
        LOG.fine("Script language was not specified. JavaScript is default.");
        lang = "js";
    }

    ScriptEngine engine = scriptEngineManager.getEngineByName(lang);
    if (engine == null) {
        throw new ConfigurationException("Specified " + LANGUAGE + "=" + lang + " not supported. Available values are: " +
                getAvailableEngines(scriptEngineManager));
    }
    engineWrapper = new ScriptEngineWrapper(engine);
    LOG.fine("Script engine selected: " + engine.getFactory().getEngineName());
    if (engineWrapper.isCompilable()) {
        cache = new IdentityHashMap<>();
    } else {
        LOG.info("Engine " + engine.getFactory().getEngineName() + " does not support compilation. Running in interpreted mode.");
    }
    if (engineWrapper.isNashornScriptEngine()) {
        LOG.warning("Nashorn JavaScript Engine is not fully supported. \n" +
                "See https://github.com/scriptella/scriptella-etl/issues/2 for status and workarounds.");
    }
    if (!StringUtils.isEmpty(parameters.getUrl())) { //if url is specified
        url = parameters.getResolvedUrl();
        //setUp reader and writer for it
        ScriptContext ctx = engine.getContext();
        ctx.setReader(new LazyReader());
        //JS engine bug - we have to wrap with PrintWriter, because otherwise print function won't work.
        ctx.setWriter(new PrintWriter(new LazyWriter()));
    }
    encoding = parameters.getCharsetProperty(ENCODING);
    ScriptEngineFactory f = engine.getFactory();
    setDialectIdentifier(new DialectIdentifier(f.getLanguageName(), f.getLanguageVersion()));
}
 
Example 6
Source File: ScriptEngineTests.java    From luaj with MIT License 5 votes vote down vote up
public void testFactoryMetadata() {
       ScriptEngine e = new ScriptEngineManager().getEngineByName("luaj");
       ScriptEngineFactory f = e.getFactory();
       assertEquals("Luaj", f.getEngineName());
       assertEquals("Luaj 0.0", f.getEngineVersion());
       assertEquals("lua", f.getLanguageName());
       assertEquals("5.2", f.getLanguageVersion());
}
 
Example 7
Source File: ScriptEngineManagerImpl.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
public void addScriptEngineFactory(ScriptEngineFactory engineFactory) {
    List<String> scriptTypes = engineFactory.getScriptTypes();

    logger.trace("{}.getScriptTypes(): {}", engineFactory.getClass().getSimpleName(), scriptTypes);
    for (String scriptType : scriptTypes) {
        if (isCustomFactory(engineFactory)) {
            this.customSupport.put(scriptType, engineFactory);
        } else {
            this.genericSupport.put(scriptType, engineFactory);
        }
    }
    if (!engineFactory.getScriptTypes().isEmpty()) {
        ScriptEngine scriptEngine = engineFactory.createScriptEngine(engineFactory.getScriptTypes().get(0));
        if (scriptEngine != null) {
            javax.script.ScriptEngineFactory factory = scriptEngine.getFactory();
            logger.debug(
                    "Initialized a {} ScriptEngineFactory for {} ({}): supports {} ({}) with file extensions {}, names {}, and mimetypes {}",
                    (isCustomFactory(engineFactory)) ? "custom" : "generic", factory.getEngineName(),
                    factory.getEngineVersion(), factory.getLanguageName(), factory.getLanguageVersion(),
                    factory.getExtensions(), factory.getNames(), factory.getMimeTypes());
        } else {
            logger.trace("addScriptEngineFactory: engine was null");
        }
    } else {
        logger.trace("addScriptEngineFactory: scriptTypes was empty");
    }
}
 
Example 8
Source File: ScriptEngineTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 9
Source File: ExtensionZest.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public void hook(ExtensionHook extensionHook) {
    super.hook(extensionHook);

    extensionHook.addOptionsParamSet(getParam());

    if (getView() != null) {
        extensionHook.addProxyListener(this);
        extensionHook.addSessionListener(new ViewSessionChangedListener());

        extensionHook.getHookView().addStatusPanel(this.getZestResultsPanel());
        extensionHook.getHookView().addOptionPanel(getOptionsPanel());

        this.dialogManager = new ZestDialogManager(this, this.getExtScript().getScriptUI());
        new ZestMenuManager(this, extensionHook);

        View.getSingleton().addMainToolbarButton(getRecordButton());
        View.getSingleton().addMainToolbarSeparator(getToolbarSeparator());

        if (getExtScript().getScriptUI() != null) {
            ZestTreeTransferHandler th = new ZestTreeTransferHandler(this);
            getExtScript()
                    .getScriptUI()
                    .addScriptTreeTransferHandler(ZestElementWrapper.class, th);
        }
    }

    List<Path> defaultTemplates = getDefaultTemplates();

    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine se = mgr.getEngineByName(ZestScriptEngineFactory.NAME);
    if (se != null) {
        // Looks like this only works if the Zest lib is in the top level
        // lib directory
        this.zestEngineFactory = (ZestScriptEngineFactory) se.getFactory();
    } else {
        // Needed for when the Zest lib is in an add-on (usual case)
        this.zestEngineFactory = new ZestScriptEngineFactory();
        se = zestEngineFactory.getScriptEngine();
    }
    zestEngineWrapper = new ZestEngineWrapper(zestEngineFactory, defaultTemplates);
    this.getExtScript().registerScriptEngineWrapper(zestEngineWrapper);

    this.getExtScript().addListener(this);

    if (this.getExtScript().getScriptUI() != null) {
        ZestTreeCellRenderer renderer = new ZestTreeCellRenderer();
        this.getExtScript().getScriptUI().addRenderer(ZestElementWrapper.class, renderer);
        this.getExtScript().getScriptUI().addRenderer(ZestScriptWrapper.class, renderer);
        this.getExtScript().getScriptUI().disableScriptDialog(ZestScriptWrapper.class);
    }
}
 
Example 10
Source File: ScriptEngineTest.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 11
Source File: ScriptEngineTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 12
Source File: ScriptEngineTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 13
Source File: ScriptEngineTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 14
Source File: ScriptEngineTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 15
Source File: ScriptEngineTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 16
Source File: ScriptEngineTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 17
Source File: ScriptEngineTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}