use of groovy.lang.Binding in project groovy by apache.
the class InvokerHelper method runScript.
public static Object runScript(Class scriptClass, String[] args) {
Binding context = new Binding(args);
Script script = createScript(scriptClass, context);
return invokeMethod(script, "run", EMPTY_ARGS);
}
use of groovy.lang.Binding in project groovy by apache.
the class SecurityTestSupport method executeScript.
protected void executeScript(Class scriptClass, Permission missingPermission) {
try {
Script script = InvokerHelper.createScript(scriptClass, new Binding());
script.run();
// InvokerHelper.runScript(scriptClass, null);
} catch (AccessControlException ace) {
if (missingPermission != null && missingPermission.implies(ace.getPermission())) {
return;
} else {
fail(ace.toString());
}
}
if (missingPermission != null) {
fail("Should catch an AccessControlException");
}
}
use of groovy.lang.Binding in project groovy by apache.
the class PlatformLineWriterTest method testPlatformLineWriter.
public void testPlatformLineWriter() throws IOException, ClassNotFoundException {
String LS = System.lineSeparator();
Binding binding = new Binding();
binding.setVariable("first", "Tom");
binding.setVariable("last", "Adams");
StringWriter stringWriter = new StringWriter();
Writer platformWriter = new PlatformLineWriter(stringWriter);
GroovyShell shell = new GroovyShell(binding);
platformWriter.write(shell.evaluate("\"$first\\n$last\\n\"").toString());
platformWriter.flush();
assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
stringWriter = new StringWriter();
platformWriter = new PlatformLineWriter(stringWriter);
platformWriter.write(shell.evaluate("\"$first\\r\\n$last\\r\\n\"").toString());
platformWriter.flush();
assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
}
use of groovy.lang.Binding in project groovy by apache.
the class InvokerHelperTest method testCreateScriptWithScriptClass.
@Test
public void testCreateScriptWithScriptClass() throws Exception {
try (GroovyClassLoader classLoader = new GroovyClassLoader()) {
String controlProperty = "text", controlValue = "I am a script";
Class<?> scriptClass = classLoader.parseClass(new GroovyCodeSource(controlProperty + " = '" + controlValue + "'", "testscript", "/groovy/shell"), false);
Script script = InvokerHelper.createScript(scriptClass, new Binding(variables));
assertSame(variables, script.getBinding().getVariables());
script.run();
assertEquals(controlValue, script.getProperty(controlProperty));
}
}
use of groovy.lang.Binding in project groovy by apache.
the class InvokerHelperTest method testBindingVariablesSetPropertiesInSingleClassScripts.
// GROOVY-5802
@Test
public void testBindingVariablesSetPropertiesInSingleClassScripts() {
variables.put("first", "John");
variables.put("last", "Smith");
PrintStream sysout = System.out;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
String source = "class Person {\n" + " static String first, last, unused\n" + " static main(args) { print \"$first $last\" }\n" + "}\n";
new GroovyShell(new Binding(variables)).parse(source).run();
assertEquals("John Smith", baos.toString());
} finally {
System.setOut(sysout);
}
}
Aggregations