Skip to content

Commit

Permalink
BATCH-2270: Allow ScriptEvaluator to be injectable in the ScriptItemP…
Browse files Browse the repository at this point in the history
…rocessor
  • Loading branch information
chrisjs authored and fmbenhassine committed Mar 13, 2020
1 parent afb24ac commit 40fd8bd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,34 @@ public void setItemBindingVariableName(String itemBindingVariableName) {
this.itemBindingVariableName = itemBindingVariableName;
}

/**
* <p>
* Provides the ability to set a custom {@link org.springframework.scripting.ScriptEvaluator}
* implementation. If not set, a {@link org.springframework.scripting.support.StandardScriptEvaluator}
* will be used by default.
* </p>
*
* @param scriptEvaluator the {@link org.springframework.scripting.ScriptEvaluator} to use
*/
public void setScriptEvaluator(ScriptEvaluator scriptEvaluator) {
this.scriptEvaluator = scriptEvaluator;
}

@Override
public void afterPropertiesSet() throws Exception {
scriptEvaluator = new StandardScriptEvaluator();
if(scriptEvaluator == null) {
scriptEvaluator = new StandardScriptEvaluator();
}

Assert.state(scriptSource != null || script != null,
"Either the script source or script file must be provided");

Assert.state(scriptSource == null || script == null,
"Either a script source or script file must be provided, not both");

if (scriptSource != null) {
if (scriptSource != null && scriptEvaluator instanceof StandardScriptEvaluator) {
Assert.isTrue(!StringUtils.isEmpty(language),
"Language must be provided when using script source");
"Language must be provided when using the default ScriptEvaluator and raw source code");

((StandardScriptEvaluator) scriptEvaluator).setLanguage(language);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.scripting.bsh.BshScriptEvaluator;
import org.springframework.scripting.groovy.GroovyScriptEvaluator;

import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
Expand Down Expand Up @@ -200,6 +202,30 @@ public void testItemBindingNameChange() throws Exception {
assertEquals("Incorrect transformed value", "SS", scriptItemProcessor.process("ss"));
}

@Test
public void testBshScriptEvaluator() throws Exception {
assumeTrue(languageExists("bsh"));

ScriptItemProcessor<String, Object> scriptItemProcessor = new ScriptItemProcessor<String, Object>();
scriptItemProcessor.setScriptEvaluator(new BshScriptEvaluator());
scriptItemProcessor.setScriptSource("String process(String item) { return item.toUpperCase(); } process(item);", "bsh");
scriptItemProcessor.afterPropertiesSet();

assertEquals("Incorrect transformed value", "SS", scriptItemProcessor.process("ss"));
}

@Test
public void testGroovyScriptEvaluator() throws Exception {
assumeTrue(languageExists("groovy"));

ScriptItemProcessor<String, Object> scriptItemProcessor = new ScriptItemProcessor<String, Object>();
scriptItemProcessor.setScriptEvaluator(new GroovyScriptEvaluator());
scriptItemProcessor.setScriptSource("def process(item) { return item.toUpperCase() } \n process(item)", "groovy");
scriptItemProcessor.afterPropertiesSet();

assertEquals("Incorrect transformed value", "SS", scriptItemProcessor.process("ss"));
}

private boolean languageExists(String engineName) {
return availableLanguages.contains(engineName);
}
Expand Down

0 comments on commit 40fd8bd

Please sign in to comment.