/** * Create a new StaticScriptSource for the given script. * @param script the script String */ public StaticScriptSource(String script) { setScript(script); }
@Override public boolean isModified() { synchronized (this.lastModifiedMonitor) { return (this.lastModified < 0 || retrieveLastModifiedTime() > this.lastModified); } }
@Override @Nullable public Object evaluate(ScriptSource script) { return evaluate(script, null); }
/** * Convert the given script source locator to a ScriptSource instance. * <p>By default, supported locators are Spring resource locations * (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh") * and inline scripts ("inline:myScriptText..."). * @param beanName the name of the scripted bean * @param scriptSourceLocator the script source locator * @param resourceLoader the ResourceLoader to use (if necessary) * @return the ScriptSource instance */ protected ScriptSource convertToScriptSource(String beanName, String scriptSourceLocator, ResourceLoader resourceLoader) { if (scriptSourceLocator.startsWith(INLINE_SCRIPT_PREFIX)) { return new StaticScriptSource(scriptSourceLocator.substring(INLINE_SCRIPT_PREFIX.length()), beanName); } else { return new ResourceScriptSource(resourceLoader.getResource(scriptSourceLocator)); } }
@Test public void testGroovyScriptFromStringUsingJsr223() { StandardScriptEvaluator evaluator = new StandardScriptEvaluator(); evaluator.setLanguage("Groovy"); Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2")); assertEquals(6, result); }
@Test public void testGroovyScriptFromFileUsingJsr223() { ScriptEvaluator evaluator = new StandardScriptEvaluator(); Object result = evaluator.evaluate(new ResourceScriptSource(new ClassPathResource("simple.groovy", getClass()))); assertEquals(6, result); }
@Test(expected = IllegalArgumentException.class) public void createWithWhitespaceOnlyScript() throws Exception { new StaticScriptSource(" \n\n\t \t\n"); }
@Test public void isModifiedToggledWhenDifferentScriptIsSet() throws Exception { source.setScript("use warnings;"); assertTrue("Script must be flagged as 'modified' when different script is passed in.", source.isModified()); }
/** * Set the globally scoped bindings on the underlying script engine manager, * shared by all scripts, as an alternative to script argument bindings. * @since 4.2.2 * @see #evaluate(ScriptSource, Map) * @see javax.script.ScriptEngineManager#setBindings(Bindings) * @see javax.script.SimpleBindings */ public void setGlobalBindings(Map<String, Object> globalBindings) { Bindings bindings = StandardScriptUtils.getBindings(globalBindings); this.globalBindings = bindings; ScriptEngineManager scriptEngineManager = this.scriptEngineManager; if (scriptEngineManager != null) { scriptEngineManager.setBindings(bindings); } }
@Test public void gettingScriptViaToStringDoesNotToggleIsModified() throws Exception { boolean isModifiedState = source.isModified(); source.toString(); assertEquals("Script's 'modified' flag must not change after script is read via toString().", isModifiedState, source.isModified()); }
@Test public void gettingScriptTogglesIsModified() throws Exception { source.getScriptAsString(); assertFalse("Script must be flagged as 'not modified' after script is read.", source.isModified()); }
@Test public void isModifiedIsTrueByDefault() throws Exception { assertTrue("Script must be flagged as 'modified' when first created.", source.isModified()); }
@Test public void testGroovyScriptWithArgumentsUsingJsr223() { StandardScriptEvaluator evaluator = new StandardScriptEvaluator(); evaluator.setLanguage("Groovy"); Map<String, Object> arguments = new HashMap<>(); arguments.put("a", 3); arguments.put("b", 2); Object result = evaluator.evaluate(new StaticScriptSource("return a * b"), arguments); assertEquals(6, result); }
@Test(expected = IllegalArgumentException.class) public void createWithEmptyScript() throws Exception { new StaticScriptSource(""); }
@Test public void isModifiedNotToggledWhenSameScriptIsSet() throws Exception { source.setScript(SCRIPT_TEXT); assertFalse("Script must not be flagged as 'modified' when same script is passed in.", source.isModified()); }
/** * Create a new StaticScriptSource for the given script. * @param script the script String */ public StaticScriptSource(String script) { setScript(script); }
@Override public boolean isModified() { synchronized (this.lastModifiedMonitor) { return (this.lastModified < 0 || retrieveLastModifiedTime() > this.lastModified); } }
@Test(expected = IllegalArgumentException.class) public void createWithNullScript() throws Exception { new StaticScriptSource(null); }
/** * Create a new StaticScriptSource for the given script. * @param script the script String * @param className the suggested class name for the script * (may be {@code null}) */ public StaticScriptSource(String script, @Nullable String className) { setScript(script); this.className = className; }
@Override public String getScriptAsString() throws IOException { synchronized (this.lastModifiedMonitor) { this.lastModified = retrieveLastModifiedTime(); } Reader reader = this.resource.getReader(); return FileCopyUtils.copyToString(reader); }