Support ScriptEngine#eval(String, Bindings) in ScriptTemplateView

Supporting ScriptEngine#eval(String, Bindings) when no render function
is specified allows to support use cases where script templates are
simply evaluating a script expression with an even more simplified
configuration.

This improvement also makes it possible to use script engines that
do not implement Invocable.

Issue: SPR-15115
This commit is contained in:
Sebastien Deleuze
2017-06-16 10:26:26 +02:00
parent 7e251274ee
commit d5f9ad03a7
12 changed files with 139 additions and 34 deletions

View File

@@ -74,6 +74,19 @@ public class KotlinScriptTemplateTests {
response.getContentAsString());
}
@Test
public void renderTemplateWithoutRenderFunction() throws Exception {
Map<String, Object> model = new HashMap<>();
model.put("header", "<html><body>");
model.put("hello", "Hello");
model.put("foo", "Foo");
model.put("footer", "</body></html>");
MockHttpServletResponse response = renderViewWithModel("org/springframework/web/servlet/view/script/kotlin/eval.kts",
model, Locale.ENGLISH, ScriptTemplatingConfigurationWithoutRenderFunction.class);
assertEquals("<html><body>\n<p>Hello Foo</p>\n</body></html>",
response.getContentAsString());
}
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model, Locale locale, Class<?> configuration) throws Exception {
ScriptTemplateView view = createViewWithUrl(viewUrl, configuration);
view.setLocale(locale);
@@ -116,4 +129,13 @@ public class KotlinScriptTemplateTests {
}
}
@Configuration
static class ScriptTemplatingConfigurationWithoutRenderFunction {
@Bean
public ScriptTemplateConfigurer kotlinScriptConfigurer() {
return new ScriptTemplateConfigurer("kotlin");
}
}
}

View File

@@ -166,17 +166,16 @@ public class ScriptTemplateViewTests {
@Test
public void nonInvocableScriptEngine() throws Exception {
this.expectedException.expect(IllegalArgumentException.class);
this.view.setEngine(mock(ScriptEngine.class));
this.expectedException.expectMessage(contains("instance"));
this.view.setApplicationContext(this.wac);
}
@Test
public void noRenderFunctionDefined() {
this.view.setEngine(mock(InvocableScriptEngine.class));
public void nonInvocableScriptEngineWithRenderFunction() throws Exception {
this.view.setEngine(mock(ScriptEngine.class));
this.view.setRenderFunction("render");
this.expectedException.expect(IllegalArgumentException.class);
this.view.setApplicationContext(this.wac);
this.expectedException.expectMessage(contains("renderFunction"));
}
@Test