Skip to content

Commit 8d2ffaf

Browse files
committed
Verify that ScriptEngines have access to the ScriptModule instance
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 30e493c commit 8d2ffaf

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/test/java/org/scijava/script/ScriptEngineTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@
3333

3434
import static org.junit.Assert.assertEquals;
3535
import static org.junit.Assert.assertNotNull;
36+
import static org.junit.Assert.assertTrue;
3637

3738
import java.io.IOException;
3839
import java.io.Reader;
3940
import java.io.StringReader;
4041
import java.util.Arrays;
42+
import java.util.HashMap;
4143
import java.util.List;
44+
import java.util.Map;
4245

46+
import javax.script.Bindings;
4347
import javax.script.ScriptEngine;
4448
import javax.script.ScriptException;
4549

@@ -65,6 +69,20 @@ public void testRot13() throws Exception {
6569
assertEquals("Svool", rot13.getScriptEngine().eval("Hello"));
6670
}
6771

72+
@Test
73+
public void testScriptModuleValue() throws Exception {
74+
final Context context = new Context(ScriptService.class);
75+
final ScriptService scriptService = context.getService(ScriptService.class);
76+
final ScriptModule module =
77+
scriptService.run("test.rot13", ScriptModule.class.getName(), false,
78+
(Map<String, Object>) null).get();
79+
final ScriptModule scriptModule = Rot13Engine.latestModule;
80+
assertEquals(module, scriptModule);
81+
assertTrue(scriptModule instanceof ScriptModule);
82+
final ScriptInfo info = ((ScriptModule) scriptModule).getInfo();
83+
assertEquals(context, info.context());
84+
}
85+
6886
@Plugin(type = ScriptLanguage.class)
6987
public static class Rot13 extends AbstractScriptLanguage {
7088

@@ -77,9 +95,20 @@ public ScriptEngine getScriptEngine() {
7795
public List<String> getNames() {
7896
return Arrays.asList("Hello", "World", "Rot13");
7997
}
98+
99+
@Override
100+
public List<String> getExtensions() {
101+
return Arrays.asList("rot13");
102+
}
80103
}
81104

82-
public static class Rot13Engine extends AbstractScriptEngine {
105+
private static class Rot13Engine extends AbstractScriptEngine {
106+
107+
{
108+
engineScopeBindings = new Rot13Bindings();
109+
}
110+
111+
private static ScriptModule latestModule;
83112

84113
@Override
85114
public Object eval(String script) throws ScriptException {
@@ -88,6 +117,7 @@ public Object eval(String script) throws ScriptException {
88117

89118
@Override
90119
public Object eval(Reader reader) throws ScriptException {
120+
latestModule = (ScriptModule) get(ScriptModule.class.getName());
91121
final StringBuilder builder = new StringBuilder();
92122
try {
93123
for (;;) {
@@ -108,4 +138,6 @@ public Object eval(Reader reader) throws ScriptException {
108138
return builder.toString();
109139
}
110140
}
141+
142+
private static class Rot13Bindings extends HashMap<String, Object> implements Bindings { }
111143
}

0 commit comments

Comments
 (0)