Message5417
Here the test:
import org.junit.Test;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;
import java.io.FileReader;
import java.io.Reader;
public class TestJSR223EvalWithReader {
@Test public void should_reuse_reader_in_eval() throws Exception {
final ScriptEngineManager manager = new ScriptEngineManager();
final String engineType = "jython";
final ScriptEngine engine = manager.getEngineByName(engineType);
final Bindings bindings = new SimpleBindings();
bindings.put("firstLevelNodes", 10);
bindings.put("secondLevelNodes", 5);
engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
final Reader dfsScript = new FileReader(getDepthFirstSearchPath(engineType));
for (int i = 1; i <= 10; i++)
engine.eval(dfsScript);
}
private String getDepthFirstSearchPath(final String engineType) {
if ("jython".equalsIgnoreCase(engineType))
return "src/test/scripts/py/DepthFirstSearch.py";
else if ("jruby".equalsIgnoreCase(engineType))
return "src/test/scripts/rb/DepthFirstSearch.rb";
else if ("rhino".equalsIgnoreCase(engineType))
return "src/test/scripts/js/DepthFirstSearch.js";
else if ("groovy".equalsIgnoreCase(engineType))
return "src/test/scripts/groovy/DepthFirstSearch.groovy";
throw new IllegalArgumentException("Invalid engine: " + engineType);
}
}
= = =
Here the script:
class DFS:
def __init__(self):
self.visited_node_counter = 0
def visitor(self):
self.visited_node_counter += 1
def visit(self, node):
node.accept_visitor(self.visitor)
for child in node.children: self.visit(child)
class Node:
def __init__(self):
self.children = []
def add_child(self, node):
self.children.append(node)
def accept_visitor(self, visitor):
visitor()
root = Node()
for i in xrange(0, firstLevelNodes):
root.add_child(Node())
for child in root.children:
for i in xrange(0, secondLevelNodes): child.add_child(Node())
dfs = DFS()
dfs.visit(root)
result = dfs.visited_node_counter
= = =
Just for the records, the same evaluation works well with other scripting engines, e.g. JRuby, Groovy, Rhino. |
|
Date |
User |
Action |
Args |
2010-01-07 14:09:13 | tiago182 | set | recipients:
+ tiago182 |
2010-01-07 14:09:12 | tiago182 | set | messageid: <1262873352.9.0.00221238394014.issue1536@psf.upfronthosting.co.za> |
2010-01-07 14:09:12 | tiago182 | link | issue1536 messages |
2010-01-07 14:09:11 | tiago182 | create | |
|