Add engineSupplier property to ScriptTemplateConfigurer
This commit adds an engineSupplier property to ScriptTemplateConfigurer and ScriptTemplateView in order to be able to customize the ScriptEngine when sharedEngine is set to false. This can be useful with Graal.js for example. Closes gh-23258
This commit is contained in:
@@ -86,7 +86,7 @@ public class ScriptTemplateViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingScriptTemplateConfig() throws Exception {
|
||||
public void missingScriptTemplateConfig() {
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() ->
|
||||
this.view.setApplicationContext(new StaticApplicationContext()))
|
||||
.withMessageContaining("ScriptTemplateConfig");
|
||||
@@ -129,7 +129,7 @@ public class ScriptTemplateViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customEngineAndRenderFunction() throws Exception {
|
||||
public void customEngineAndRenderFunction() {
|
||||
ScriptEngine engine = mock(InvocableScriptEngine.class);
|
||||
given(engine.get("key")).willReturn("value");
|
||||
this.view.setEngine(engine);
|
||||
@@ -164,13 +164,13 @@ public class ScriptTemplateViewTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonInvocableScriptEngine() throws Exception {
|
||||
public void nonInvocableScriptEngine() {
|
||||
this.view.setEngine(mock(ScriptEngine.class));
|
||||
this.view.setApplicationContext(this.wac);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonInvocableScriptEngineWithRenderFunction() throws Exception {
|
||||
public void nonInvocableScriptEngineWithRenderFunction() {
|
||||
this.view.setEngine(mock(ScriptEngine.class));
|
||||
this.view.setRenderFunction("render");
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
@@ -184,7 +184,28 @@ public class ScriptTemplateViewTests {
|
||||
this.view.setRenderFunction("render");
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
this.view.setApplicationContext(this.wac))
|
||||
.withMessageContaining("'engine' or 'engineName'");
|
||||
.withMessageContaining("You should define either 'engine', 'engineSupplier' or 'engineName'.");
|
||||
}
|
||||
|
||||
@Test // gh-23258
|
||||
public void engineAndEngineSupplierBothDefined() {
|
||||
ScriptEngine engine = mock(InvocableScriptEngine.class);
|
||||
this.view.setEngineSupplier(() -> engine);
|
||||
this.view.setEngine(engine);
|
||||
this.view.setRenderFunction("render");
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
this.view.setApplicationContext(this.wac))
|
||||
.withMessageContaining("You should define either 'engine', 'engineSupplier' or 'engineName'.");
|
||||
}
|
||||
|
||||
@Test // gh-23258
|
||||
public void engineNameAndEngineSupplierBothDefined() {
|
||||
this.view.setEngineSupplier(() -> mock(InvocableScriptEngine.class));
|
||||
this.view.setEngineName("test");
|
||||
this.view.setRenderFunction("render");
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
this.view.setApplicationContext(this.wac))
|
||||
.withMessageContaining("You should define either 'engine', 'engineSupplier' or 'engineName'.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -261,6 +282,42 @@ public class ScriptTemplateViewTests {
|
||||
|
||||
}
|
||||
|
||||
@Test // gh-23258
|
||||
public void engineSupplierWithSharedEngine() {
|
||||
this.configurer.setEngineSupplier(() -> mock(InvocableScriptEngine.class));
|
||||
this.configurer.setRenderObject("Template");
|
||||
this.configurer.setRenderFunction("render");
|
||||
this.configurer.setSharedEngine(true);
|
||||
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(this.view);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
ScriptEngine engine1 = this.view.getEngine();
|
||||
ScriptEngine engine2 = this.view.getEngine();
|
||||
assertThat(engine1).isNotNull();
|
||||
assertThat(engine2).isNotNull();
|
||||
assertThat(accessor.getPropertyValue("renderObject")).isEqualTo("Template");
|
||||
assertThat(accessor.getPropertyValue("renderFunction")).isEqualTo("render");
|
||||
assertThat(accessor.getPropertyValue("sharedEngine")).isEqualTo(true);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test // gh-23258
|
||||
public void engineSupplierWithNonSharedEngine() {
|
||||
this.configurer.setEngineSupplier(() -> mock(InvocableScriptEngine.class));
|
||||
this.configurer.setRenderObject("Template");
|
||||
this.configurer.setRenderFunction("render");
|
||||
this.configurer.setSharedEngine(false);
|
||||
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(this.view);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
ScriptEngine engine1 = this.view.getEngine();
|
||||
ScriptEngine engine2 = this.view.getEngine();
|
||||
assertThat(engine1).isNotNull();
|
||||
assertThat(engine2).isNotNull();
|
||||
assertThat(accessor.getPropertyValue("renderObject")).isEqualTo("Template");
|
||||
assertThat(accessor.getPropertyValue("renderFunction")).isEqualTo("render");
|
||||
assertThat(accessor.getPropertyValue("sharedEngine")).isEqualTo(false);
|
||||
}
|
||||
|
||||
private interface InvocableScriptEngine extends ScriptEngine, Invocable {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user