/** * @param scriptText The script text */ public void setScriptText(String scriptText) { this.scriptSource = new StaticScriptSource(scriptText); }
/** * 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)); } }
/** * <p> * Sets the provided {@link String} as the script source code to use. * </p> * * @param scriptSource the {@link String} form of the script source code to use. * @param language the language of the script. */ public void setScriptSource(String scriptSource, String language) { Assert.hasText(language, "Language must contain the script language"); Assert.hasText(scriptSource, "Script source must contain the script source to evaluate"); this.language = language; this.scriptSource = new StaticScriptSource(scriptSource); }
/** * 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(expected = IllegalArgumentException.class) public void createWithWhitespaceOnlyScript() throws Exception { new StaticScriptSource(" \n\n\t \t\n"); }
@Test(expected = IllegalArgumentException.class) public void createWithNullScript() throws Exception { new StaticScriptSource(null); }
@Test(expected = IllegalArgumentException.class) public void createWithEmptyScript() throws Exception { new StaticScriptSource(""); }
@Test public void testGroovyScriptWithArguments() { ScriptEvaluator evaluator = new GroovyScriptEvaluator(); 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 public void testGroovyScriptWithArguments() { ScriptEvaluator evaluator = new BshScriptEvaluator(); 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 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 public void testGroovyScriptFromString() { ScriptEvaluator evaluator = new GroovyScriptEvaluator(); Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2")); assertEquals(6, result); }
@Test public void testBshScriptFromString() { ScriptEvaluator evaluator = new BshScriptEvaluator(); Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2;")); assertEquals(6, result); }
@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 testGroovyScriptWithCompilerConfiguration() { GroovyScriptEvaluator evaluator = new GroovyScriptEvaluator(); MyBytecodeProcessor processor = new MyBytecodeProcessor(); evaluator.getCompilerConfiguration().setBytecodePostprocessor(processor); Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2")); assertEquals(6, result); assertTrue(processor.processed.contains("Script1")); }
@Test public void testGroovyScriptWithImportCustomizer() { GroovyScriptEvaluator evaluator = new GroovyScriptEvaluator(); ImportCustomizer importCustomizer = new ImportCustomizer(); importCustomizer.addStarImports("org.springframework.util"); evaluator.setCompilationCustomizers(importCustomizer); Object result = evaluator.evaluate(new StaticScriptSource("return ResourceUtils.CLASSPATH_URL_PREFIX")); assertEquals("classpath:", result); }
@Test public void testEmbeddedVariable() { Map<String, Object> variables = new HashMap<String, Object>(); variables.put("scope", "world"); Object obj = executor.executeScript(new StaticScriptSource("\"hello, %s\"% scope"), variables); assertEquals("hello, world", obj); } }
@Test public void testLiteral() { Object obj = executor.executeScript(new StaticScriptSource("3+4")); assertEquals(7, obj); obj = executor.executeScript(new StaticScriptSource("'hello,world'")); assertEquals("hello,world", obj); }
@Test public void test1() { Object obj = executor.executeScript(new StaticScriptSource("x=2")); assertEquals(2, obj); }
@Test public void testJs() { ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("js"); Object obj = executor.executeScript(new StaticScriptSource("function js(){ return 'js';} js();")); assertEquals("js", obj.toString()); }
@Test public void testPython() { ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("python"); Object obj = executor.executeScript(new StaticScriptSource("x=2")); assertEquals(2, obj); obj = executor.executeScript(new StaticScriptSource("def foo(y):\n\tx=y\n\treturn y\nz=foo(2)")); assertEquals(2, obj); }