Search in sources :

Example 6 with StaticScriptSource

use of org.springframework.scripting.support.StaticScriptSource in project spring-framework by spring-projects.

the class GroovyScriptEvaluatorTests method testGroovyScriptWithCompilerConfiguration.

@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"));
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) Test(org.junit.Test)

Example 7 with StaticScriptSource

use of org.springframework.scripting.support.StaticScriptSource in project spring-framework by spring-projects.

the class GroovyScriptEvaluatorTests method testGroovyScriptWithArgumentsUsingJsr223.

@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);
}
Also used : StandardScriptEvaluator(org.springframework.scripting.support.StandardScriptEvaluator) StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 8 with StaticScriptSource

use of org.springframework.scripting.support.StaticScriptSource in project spring-framework by spring-projects.

the class BshScriptEvaluatorTests method testGroovyScriptWithArguments.

@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);
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) ScriptEvaluator(org.springframework.scripting.ScriptEvaluator) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 StaticScriptSource (org.springframework.scripting.support.StaticScriptSource)8 ScriptEvaluator (org.springframework.scripting.ScriptEvaluator)4 StandardScriptEvaluator (org.springframework.scripting.support.StandardScriptEvaluator)4 HashMap (java.util.HashMap)3 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)1