Java Reference
In-Depth Information
Unlike Eval ,the GroovyShell classdoesnotcontainonlystaticmethods.Itneedstobe
instantiated before invoking its evaluate method. As a simple example, consider adding
the following test to the previous set of test cases:
@Test
public void testEvaluateString() {
GroovyShell shell = new GroovyShell();
Object result = shell.evaluate("3+4");
assertEquals(7, result);
}
The evaluate method is heavily overloaded. The version I'm using here takes a string
representing the script to be evaluated. Other overloads take a java.io.File or a
java.io.Reader instance, with various additional arguments. There are overloads that
take a java.io.InputStream as well, but they're deprecated due to possible encod-
ing issues.
So far, using the GroovyShell looks a lot like using the ScriptEngine class, though
you can instantiate it directly in this case. To deal with input and output variables, however,
the GroovyShell uses the groovy.lang.Binding class to provide a map of input
and output variables.
The next listing shows the Binding and GroovyShell classes in action. It's another
test to add to the growing JUnit 4 test case.
Search WWH ::




Custom Search